更新代码方案
This commit is contained in:
107
src/main.c
107
src/main.c
@@ -1,39 +1,74 @@
|
||||
#include "zuc256.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "zuc256.h"
|
||||
|
||||
// 测试ZUC256加密解密功能
|
||||
int main() {
|
||||
// 1. 测试参数(256位密钥=32字节,128位IV=16字节)
|
||||
uint8_t key[32] = "0123456789abcdef0123456789abcdef"; // 32字节密钥
|
||||
uint8_t iv[16] = "0123456789abcdef"; // 16字节IV
|
||||
uint8_t plaintext[] = "Hello ZUC256! This is a test of the encryption algorithm.";
|
||||
size_t plaintext_len = strlen((const char *)plaintext);
|
||||
uint8_t ciphertext[1024] = {0}; // 密文缓冲区
|
||||
uint8_t decrypted[1024] = {0}; // 解密后缓冲区
|
||||
|
||||
// 2. 初始化加密上下文并加密
|
||||
ZUC256_CTX enc_ctx;
|
||||
int ret = ZUC256_Init(&enc_ctx, key, iv);
|
||||
if (ret != ZUC256_SUCCESS) {
|
||||
printf("加密初始化失败!错误码: %d\n", ret);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = ZUC256_Update(&enc_ctx, ciphertext, plaintext, plaintext_len);
|
||||
if (ret != ZUC256_SUCCESS) {
|
||||
printf("加密处理失败!错误码: %d\n", ret);
|
||||
ZUC256_Cleanup(&enc_ctx);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = ZUC256_Final(&enc_ctx, NULL);
|
||||
if (ret != ZUC256_SUCCESS) {
|
||||
printf("加密结束失败!错误码: %d\n", ret);
|
||||
}
|
||||
ZUC256_Cleanup(&enc_ctx); // 清理加密上下文(敏感数据)
|
||||
|
||||
// 3. 初始化解密上下文并解密(流密码加密解密使用相同接口)
|
||||
ZUC256_CTX dec_ctx;
|
||||
ret = ZUC256_Init(&dec_ctx, key, iv);
|
||||
if (ret != ZUC256_SUCCESS) {
|
||||
printf("解密初始化失败!错误码: %d\n", ret);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = ZUC256_Update(&dec_ctx, decrypted, ciphertext, plaintext_len);
|
||||
if (ret != ZUC256_SUCCESS) {
|
||||
printf("解密处理失败!错误码: %d\n", ret);
|
||||
ZUC256_Cleanup(&dec_ctx);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = ZUC256_Final(&dec_ctx, NULL);
|
||||
if (ret != ZUC256_SUCCESS) {
|
||||
printf("解密结束失败!错误码: %d\n", ret);
|
||||
}
|
||||
ZUC256_Cleanup(&dec_ctx); // 清理解密上下文
|
||||
|
||||
// 4. 输出结果并验证
|
||||
printf("原文: %s\n", plaintext);
|
||||
printf("密文: ");
|
||||
for (size_t i = 0; i < plaintext_len; i++) {
|
||||
printf("%02x", ciphertext[i]); // 密文以十六进制显示
|
||||
}
|
||||
printf("\n解密后: %s\n", decrypted);
|
||||
|
||||
// 验证解密结果是否与原文一致
|
||||
if (memcmp(plaintext, decrypted, plaintext_len) == 0) {
|
||||
printf("\n测试通过:加密解密一致!\n");
|
||||
} else {
|
||||
printf("\n测试失败:解密结果与原文不一致!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
// 256位密钥(32字节)
|
||||
uint8_t key[32] = {
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
||||
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
|
||||
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
|
||||
};
|
||||
|
||||
// 128位初始向量(16字节)
|
||||
uint8_t iv[16] = {
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
|
||||
};
|
||||
|
||||
// 待加密数据
|
||||
uint8_t plaintext[] = "Hello, ZUC256 algorithm!";
|
||||
uint8_t ciphertext[sizeof(plaintext)];
|
||||
uint8_t decrypted[sizeof(plaintext)];
|
||||
|
||||
zuc256_context ctx;
|
||||
|
||||
// 加密过程
|
||||
zuc256_init(&ctx, key, iv);
|
||||
zuc256_crypt(&ctx, plaintext, ciphertext, sizeof(plaintext));
|
||||
|
||||
// 解密过程(使用相同的密钥和IV重新初始化)
|
||||
zuc256_init(&ctx, key, iv);
|
||||
zuc256_crypt(&ctx, ciphertext, decrypted, sizeof(plaintext));
|
||||
|
||||
// 输出结果
|
||||
printf("Original: %s\n", plaintext);
|
||||
printf("Decrypted: %s\n", decrypted);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user