aes加密算法c++语言实现
时间: 2023-10-13 19:06:09 浏览: 210
AES加密算法是一种对称加密算法,可以用于保护数据的机密性。以下是C++语言实现AES加密算法的示例代码:
```c++
#include <iostream>
#include <cstring>
#include <openssl/aes.h>
using namespace std;
int main()
{
// 明文
unsigned char plaintext[] = "Hello, world!";
// 密钥
unsigned char key[] = "0123456789abcdef";
// 初始化向量
unsigned char iv[] = "fedcba9876543210";
// 加密后的密文
unsigned char ciphertext[sizeof(plaintext)];
// 创建AES加密上下文
AES_KEY aes_key;
AES_set_encrypt_key(key, 128, &aes_key);
// 加密数据
AES_cbc_encrypt(plaintext, ciphertext, sizeof(plaintext), &aes_key, iv, AES_ENCRYPT);
// 输出加密结果
cout << "Plaintext: " << plaintext << endl;
cout << "Ciphertext: ";
for (int i = 0; i < sizeof(ciphertext); i++) {
printf("%02x", ciphertext[i]);
}
cout << endl;
return 0;
}
```
阅读全文