用c++实现aes128加密
时间: 2023-05-21 11:00:55 浏览: 117
可以使用 OpenSSL 库来实现 AES128 加密,以下是一个简单的示例代码:
```c
#include <openssl/aes.h>
void aes128_encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *iv, unsigned char *ciphertext) {
AES_KEY aes_key;
AES_set_encrypt_key(key, 128, &aes_key);
AES_cbc_encrypt(plaintext, ciphertext, plaintext_len, &aes_key, iv, AES_ENCRYPT);
}
void aes128_decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *iv, unsigned char *plaintext) {
AES_KEY aes_key;
AES_set_decrypt_key(key, 128, &aes_key);
AES_cbc_encrypt(ciphertext, plaintext, ciphertext_len, &aes_key, iv, AES_DECRYPT);
}
```
其中,`plaintext` 是明文,`plaintext_len` 是明文长度,`key` 是密钥,`iv` 是初始化向量,`ciphertext` 是密文。`aes128_encrypt` 函数用于加密,`aes128_decrypt` 函数用于解密。
阅读全文