场景:
使用aes结合base64对数据进行加解密。
源码:
/*aesEncDec.h*/
#ifndef _AESENCDEC_H_
#define _AESENCDEC_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <openssl/aes.h>
#define MINLEN 128
#define MIDLEN 1024
#define MAXLEN 4096
/*aes加解密key*/
const char AES_KEY_VALUE[MINLEN] = "123456";
int Base64Encode(const char* encoded, int encodedLength, char* decoded);
int Base64Decode(const char* encoded, int encodedLength, char* decoded);
void AesEncrypt(const char* original_data, char* base64_encode);
void AesDecrypt(const char* original_data, char* decrypt_data);
#endif // !_AESENCDEC_H_
//aesEncDec.c
#include "aesEncDec.h"
/****************************************************************
* function name : Base64Encode
* functional description : base64编码
* input parameter : encoded:原始数据;encodedLength:数据长度;decoded:编码后的数据
* output parameter : None
* return value: None
* history :
*****************************************************************/
int Base64Encode(const char* encoded, int encodedLength, char* decoded)
{
return EVP_EncodeBlock((unsigned char*)decoded, (const unsigned char*)encoded, encodedLength);
}
/****************************************************************
* function name : Base64Decode
* functional description : base64解码
* input parameter : encoded:原数据;encodedLength:数据长度;decoded:解码后的数据
* output parameter : None
* return value: None
* history :
*****************************************************************/
int Base64Decode(const char* encoded, int encodedLength, char* decoded)
{
return EVP_DecodeBlock((unsigned char*)decoded, (const unsigned char*)encoded, encodedLength);
}
/****************************************************************
* function name : AesEncrypt
* functional description : aes加密
* input parameter : original_data:初始数据;base64_encode:加密并进行编码后的数据
* output parameter : None
* return value: None
* history :
*****************************************************************/
void AesEncrypt(const char* original_data, char* base64_encode)
{
AES_KEY key;
/*设置加密key及密钥长度*/
AES_set_encrypt_key(AES_KEY_VALUE, AES_BLOCK_SIZE * 8, &key);
char encrypt_data[MIDLEN] = "";
int len = 0;
/*循环加密,每次只能加密AES_BLOCK_SIZE长度的数据*/
while (len < strlen(original_data))
{
AES_encrypt(original_data + len, encrypt_data + len, &key);
len += AES_BLOCK_SIZE;
}
//printf("encrypt_data:%s\n", encrypt_data);
Base64Encode(encrypt_data, strlen(encrypt_data), base64_encode);
//printf("base64_encode:%s\n", base64_encode);
}
/****************************************************************
* function name : AesDecrypt
* functional description : aes解密
* input parameter : original_data:初始数据;decrypt_data:解密、解码后的数据
* output parameter : None
* return value: None
* history :
*****************************************************************/
void AesDecrypt(const char* original_data, char* decrypt_data)
{
/*先解码*/
char base64_decode[MIDLEN] = "";
Base64Decode(original_data, strlen(original_data), base64_decode);
//printf("base64_decode:%s\n", base64_decode);
AES_KEY key;
/*设置加密key及密钥长度*/
AES_set_decrypt_key(AES_KEY_VALUE, AES_BLOCK_SIZE * 8, &key);
int len = 0;
/*循环解密*/
while (len < MIDLEN)
{
AES_decrypt(base64_decode + len, decrypt_data + len, &key);
len += AES_BLOCK_SIZE;
}
//printf("decrypt:%s\n", decrypt_data);
}
int main()
{
char buf[MIDLEN] = "helloworld";
/*加密*/
char base64_encode[MIDLEN] = "";
AesEncrypt(buf, base64_encode);
printf("base64_encode:%s\n", base64_encode);
/*解密*/
char decrypt_data[MIDLEN] = "";
AesDecrypt(base64_encode, decrypt_data);
printf("decrypt_data:%s\n", decrypt_data);
return 0;
}
编译:
gcc -o aesEncDec aesEncDec.c -lcrypto -lssl -I /root/openssl/openssl/include/ -L /root/openssl/openssl/
以上代码需要openssl的头文件及动态库。
openssl官网:https://www.openssl.org/
使用过程中遇到的问题:
1、加解密长度过大时,会异常。
2、有些字符串无法正常加解密,还需后续分析。
参考:
openssl提供的AES加解密算法的api:
https://blog.csdn.net/whatday/article/details/97277567
openssl aes加解密算法演示: