c++实现 openssl/aes aes加解密函数
时间: 2023-12-12 16:33:12 浏览: 181
以下是c++实现openssl/aes aes加解密函数的例子:
```c++
#include <openssl/aes.h>
#include <string.h>
// AES加密函数
void aes_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key) {
AES_encrypt(in, out, key);
}
// AES解密函数
void aes_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key) {
AES_decrypt(in, out, key);
}
int main() {
// 初始化AES_KEY
AES_KEY key;
const unsigned char *key_data = (const unsigned char *)"0123456789abcdef";
AES_set_encrypt_key(key_data, 128, &key);
// 待加密的数据
const unsigned char *in_data = (const unsigned char *)"hello world";
int in_data_len = strlen((const char *)in_data);
// 加密后的数据
unsigned char out_data[1024] = {0};
aes_encrypt(in_data, out_data, &key);
// 解密后的数据
unsigned char decrypt_data[1024] = {0};
aes_decrypt(out_data, decrypt_data, &key);
return 0;
}
```
阅读全文