openssl aes
介绍
先生成key,然后用这个key去加解密
AES_set_encrypt_key
AES_set_decrypt_key
这两个的第二个参数必须是128 192 256,因为是按bit算的,所以16个字节的key乘8就行了,刚好128
Demo
#include <iostream>
#include <cstring>
#include <openssl/aes.h>
int main()
{
char* key = "1234567890123456";
AES_KEY enkey, dekey;
if(AES_set_encrypt_key((const unsigned char*)key, strlen((char*)key) * 8, &enkey) != 0)
{
std::cout << "AES_set_encrypt_key failed!" << std::endl;
return -1;
}
if(AES_set_decrypt_key((const unsigned char*)key, strlen((char*)key) * 8, &dekey) != 0)
{
std::cout << "AES_set_decrypt_key failed!" << std::endl;
return -1;
}
char* m = "abcdefghijklmn";
char enbuf[1024] = { 0 };
char debuf[1024] = { 0 };
AES_encrypt((const unsigned char*)m, (unsigned char*)enbuf, &enkey);
AES_decrypt((const unsigned char*)enbuf, (unsigned char*)debuf, &dekey);
std::cout << enbuf << std::endl;
std::cout << debuf << std::endl;
return 0;
}