#include "ads_cbc256.h"
#include <openssl/aes.h>
int base64_encode(char *_pInput, int _InLen, char *_pOutput, int *_pOutLen)
{
BIO *bio = NULL;
BIO *bio_mem = NULL;
char *base64text;
long base64textlen;
int len = 0;
if (_pInput == NULL || _pOutput == NULL || _pOutLen == NULL) {
return -1;
}
bio = BIO_new(BIO_f_base64());
bio_mem = BIO_new(BIO_s_mem());
bio = BIO_push(bio, bio_mem);
len = BIO_write(bio, _pInput, _InLen);
BIO_flush(bio);
BIO_get_mem_data(bio, &base64text);
base64textlen = BIO_get_mem_data(bio, NULL);
// 输出 base64 编码的密文
printf("Base64 Encoded Ciphertext: %s\n", base64text);
*_pOutLen = strlen(base64text);
memcpy(_pOutput, base64text, *_pOutLen);
// 清理资源
BIO_free_all(bio);
return 0; /* SUCCESS */
}
/*
*****************************************************************************************
* 函 数 名: base64_encode
* 功能说明: 将Base64编码的字符串data转换回原始字符串
* 形 参: _pInput : 输入数据
* _InLen : 输入数据长度
* _pOutput: 输出base64编码结果
* _OutLen: 输出数据长度
* 返 回 值: 0:成功, -1:失败
*****************************************************************************************
*/
int base64_decode(char *_pInput, int _InLen, char *_pOutput, int _OutLen)
{
BIO *bmem, *b64;
int outlen = 0;
if (_pInput == NULL || _pOutput == NULL) {
printf("param failed\n");
return -1;
}
b64 = BIO_new(BIO_f_base64()); // 创建Base64解码的BIO
if (b64 == NULL) {
printf("BIO_f_base64 failed\n");
return -1;
}
bmem = BIO_new_mem_buf(_pInput, _InLen); // 创建内存BIO
if (bmem == NULL) {
printf("BIO_new_mem_buf failed\n");
BIO_free_all(b64);
return -1;
}
bmem = BIO_push(b64, bmem); // 将解码BIO链接到内存BIO
outlen = BIO_read(bmem, _pOutput, _OutLen); // 读取解码后的数据
if (outlen < 0) {
printf("BIO_read failed\n");
BIO_free_all(b64);
return -1;
}
// 输出解码后的数据
printf("Decoded data: %s\n", _pOutput);
BIO_free_all(bmem);
openssl aes cbc256加密解密
于 2024-08-14 10:45:36 首次发布