- 基于ede3的加密和解密
- 当前提供模式有cfb和cbc
- 数据长度非向量整数倍
- 特别注意当数据长度是非向量证书倍的时候该如何处理数据
- openssl 版本
OpenSSL 1.1.1 11 Sep 2018 - 验证结果:
明文 100:
00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 60 61 62 63
please select :
1: EVP_des_ede3_ofb
2: EVP_des_ede3_cbc
1
加密结果长度:100
58 ec 26 8c 73 f3 b7 99 4f d0 bd 41 43 57 e8 62 41 3c a6 9d 5c 94 e2 82 54 50 29 71 8f b5 bd 64 b2 21 b2 71 30 30 25 01 8b ef 46 df 23 e5 4a 95 0a d7 0f 45 2e 1a 03 16 7f 2e 70 64 80 e7 c6 97 85 14 37 59 7f a9 5c 53 f5 59 9a 15 24 89 dc b9 fe c6 57 5a d3 e0 e1 25 34 96 2b 43 74 43 47 7e c8 5f c5 ef
解密结果长度:100
00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 60 61 62 63
test ok!
// 编译:gcc -g test_1.c -o test_1 -L/usr/lib -lssl -lcrypto
#include <string.h>
#include "openssl/err.h"
#include "openssl/evp.h"
int main()
{
int ret, which = 1;
EVP_CIPHER_CTX *ctx;
const EVP_CIPHER *cipher;
unsigned char key[24], iv[8], in[100], out[108], de[100];
int i, len, inl, outl, total = 0;
for (i = 0; i < 24; i++)
{
memset(&key[i], i, 1);
}
for (i = 0; i < 8; i++)
{
memset(&iv[i], i, 1);
}
printf("明文 100:\n");
for (i = 0; i < 100; i++)
{
memset(&in[i], i, 1);
printf("%02x ",in[i]);
}
printf("\n");
// EVP_CIPHER_CTX_init(ctx);
ctx = EVP_CIPHER_CTX_new();
printf("please select :\n");
printf("1: EVP_des_ede3_ofb\n");
printf("2: EVP_des_ede3_cbc\n");
scanf("%d", &which);
if (which == 1)
cipher = EVP_des_ede3_ofb();
else
cipher = EVP_des_ede3_cbc();
ret = EVP_EncryptInit_ex(ctx, cipher, NULL, key, iv);
if (ret != 1)
{
printf("EVP_EncryptInit_ex err1!\n");
return -1;
}
inl = 50;
len = 0;
EVP_EncryptUpdate(ctx, out + len, &outl, in, inl);
len += outl;
EVP_EncryptUpdate(ctx, out + len, &outl, in + 50, inl);
len += outl;
EVP_EncryptFinal_ex(ctx, out + len, &outl);
len += outl;
printf("加密结果长度:%d\n", len);
for (i = 0; i < len; i++)
{
printf("%02x ",*(out + i));
}
printf("\n");
/* 解密 */
EVP_CIPHER_CTX_cleanup(ctx);
EVP_CIPHER_CTX_init(ctx);
ret = EVP_DecryptInit_ex(ctx, cipher, NULL, key, iv);
if (ret != 1)
{
printf("EVP_DecryptInit_ex err1!\n");
return -1;
}
total = 0;
EVP_DecryptUpdate(ctx, de + total, &outl, out, 44);
total += outl;
EVP_DecryptUpdate(ctx, de + total, &outl, out + 44, len - 44);
total += outl;
ret = EVP_DecryptFinal_ex(ctx, de + total, &outl);
total += outl;
printf("解密结果长度:%d\n", total);
for (i = 0; i < total; i++)
{
printf("%02x ",*(de + i));
}
printf("\n");
if (ret != 1)
{
EVP_CIPHER_CTX_cleanup(ctx);
printf("EVP_DecryptFinal_ex err\n");
return -1;
}
if ((total != 100) || (memcmp(de, in, 100)))
{
printf("err!\n");
return -1;
}
EVP_CIPHER_CTX_cleanup(ctx);
printf("test ok!\n");
return 0;
}