openssl aes
时间: 2023-12-15 14:32:19 浏览: 86
OpenSSL是一个开源的软件库,提供了各种加密算法的实现,其中包括AES加密算法。下面是使用OpenSSL库进行AES加密和解密的示例代码:
1. AES加密
```c
#include <openssl/aes.h>
int main()
{
AES_KEY aesKey;
unsigned char userKey[] = "0123456789abcdef";
unsigned char plainText[] = "Hello, world!";
unsigned char cipherText[16];
int bits = 128;
AES_set_encrypt_key(userKey, bits, &aesKey);
AES_encrypt(plainText, cipherText, &aesKey);
return 0;
}
```
2. AES解密
```c
#include <openssl/aes.h>
int main()
{
AES_KEY aesKey;
unsigned char userKey[] = "0123456789abcdef";
unsigned char cipherText[] = {0x3a, 0xd7, 0x7b, 0xb4, 0x0d, 0x33, 0xd0, 0x5a, 0x61, 0x9e, 0x1f, 0x5c, 0x2a, 0x18, 0xa5, 0x8f};
unsigned char plainText[16];
int bits = 128;
AES_set_decrypt_key(userKey, bits, &aesKey);
AES_decrypt(cipherText, plainText, &aesKey);
return 0;
}
```
阅读全文