使用以下这两个头文件
#include <openssl/aes.h>
#include <openssl/evp.h>
sudo apt-get install openssl
sudo apt-get install libssl-dev
sudo apt-get install libssl-doc
1、sudo apt-get install openssl
2、 sudo apt-get install libssl-dev
3、sudo apt-get install libssl-doc
4、解密和解密的接口
//编译时,注意加入 -lssl -lcrypto
// gcc .... -lssl -lcrypto
#include <stdio.h>
#include <openssl/aes.h>
#include <openssl/evp.h>
#include <string.h>
#include "securityCode.h"
//编译时,注意加入 -lssl -lcrypto
// gcc .... -lssl -lcrypto
// 加密函数
void encrypt(const unsigned char *plaintext, int plaintext_len, const unsigned char *key,
const unsigned char *iv, unsigned char *ciphertext) {
EVP_CIPHER_CTX *ctx;
int len;
int ciphertext_len;
ctx = EVP_CIPHER_CTX_new();
EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv);
EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len);
ciphertext_len = len;
EVP_EncryptFinal_ex(ctx, ciphertext + len, &len);
ciphertext_len += len;
EVP_CIPHER_CTX_free(ctx);
}
// 解密函数
void decrypt(const unsigned char *ciphertext, int ciphertext_len, const unsigned char *key,
const unsigned char *iv, unsigned char *plaintext) {
EVP_CIPHER_CTX *ctx;
int len;
int plaintext_len;
ctx = EVP_CIPHER_CTX_new();
EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv);
EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len);
plaintext_len = len;
EVP_DecryptFinal_ex(ctx, plaintext + len, &len);
plaintext_len += len;
EVP_CIPHER_CTX_free(ctx);
}