#include "openssl/aes.h"
#pragma comment(lib, "libeay32.lib")
int main(int argc, char* argv[])
{
//1.定义加解密密钥
//加密秘钥 测试用所以简单,如果暴露了该秘钥及加密算法 则可破解!
const unsigned char* userkey = (const unsigned char*)"0123456789012345";
//2.生成加密/解密AES_KEY
AES_KEY encKey;//加密KEY
AES_KEY decKey;//解密KEY
//生成加密KEY
AES_set_encrypt_key(userkey,128,&encKey);
//生成解密KEY
AES_set_decrypt_key(userkey,128,&decKey);
const unsigned char* in = (const unsigned char*)"*";
unsigned char out[256] = {0};
unsigned char out2[256] = {0};
//3.加密 每16字节为一组 输出长度为16字节整数倍
int len = strlen((const char*)in);
int n = len/16;;
if (len%16>0)
n++;
for (int i=0;i<n;i++)
{
//等价于AES_encrypt(in+i*16,out+i*16,&key);
AES_ecb_encrypt(in+i*16,out+i*16,&encKey,AES_ENCRYPT);
}
//4.解密 每16字节为一组
//把加密的输出当做解密的输入
len = strlen((const char*)out);
n = len/16;;
if (len%16>0)
n++;
for (int i=0;i<n;i++)
{
//等价于AES_decrypt(out+i*16,out2+i*16,&key2);
AES_ecb_encrypt(out+i*16,out2+i*16,&decKey,AES_DECRYPT);
}
//5.输出结果
printf("输入内容=%s len=%d\n",in,strlen((const char*)in));
printf("加密内容=%s len=%d\n",out,strlen((const char*)out));
printf("解密内容=%s len=%d\n",out2,strlen((const char*)out2));
/*
输入内容=* len=14
加密内容="64 be e0 1b bd fd 65 e4 71 6d 45 80 12 ab 70 04" len=16
解密内容=* len=14
*/
return 0;
}