#include <stdio.h>
#include <string.h>
#include "zuc256.h"

// 打印字节数组为十六进制
void print_hex(const char *label, const uint8_t *data, size_t len) {
    printf("%s: ", label);
    for (size_t i = 0; i < len; i++) {
        printf("%02x ", data[i]);
    }
    printf("\n");
}

int main() {
// 1. 明文
    uint8_t plaintext[] = "ZUC256对称加解密测试:1234567890";
    size_t plaintext_len = strlen((char*)plaintext);
    printf("明文: %s\n", plaintext);
    print_hex("明文(十六进制)", plaintext, plaintext_len);

    // 2. 密钥(32字节ASCII)
    uint8_t key[32] = "0123456789abcdef0123456789abcdef";
    print_hex("密钥", key, 32);

    // 3. 初始向量(25字节ASCII)
    //注:初始向量长度为184bit分布到25个字节中，前面17个初始向量为8bit字节，后面8个初始向量为6bit字节(占据一个字节的低6位)
    uint8_t input_iv_25byte[25] = "0123456789abcdefg01234567"; 
    uint8_t iv[23];
    extract_iv(input_iv_25byte, iv);
    print_hex("提取后的IV", iv, 23);

    // 4. 分配加密/解密缓冲区
    uint8_t *ciphertext = (uint8_t*)malloc(plaintext_len);
    uint8_t *decryptedtext = (uint8_t*)malloc(plaintext_len);
    if (!ciphertext || !decryptedtext) {
        printf("内存分配失败\n");
        return 1;
    }

    // 5. 加密
    ZUC_STATE state;
    zuc256_init(&state, key, iv);
    zuc256_crypt(&state, plaintext, plaintext_len, ciphertext);
    print_hex("密文", ciphertext, plaintext_len);

    // 6. 解密(重新初始化状态)
    zuc256_init(&state, key, iv);
    zuc256_crypt(&state, ciphertext, plaintext_len, decryptedtext);
    print_hex("解密后", decryptedtext, plaintext_len);
    printf("解密文本: %s\n", decryptedtext);

    // 7. 验证结果
    if (memcmp(plaintext, decryptedtext, plaintext_len) == 0) {
        printf("=== 测试成功: 解密结果与明文一致 ===\n");
    } else {
        printf("=== 测试失败: 解密结果与明文不一致 ===\n");
    }

    // 8. 释放内存
    free(ciphertext);
    free(decryptedtext);

    return 0;
}
