openssl aes cbc256加密解密


#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);          
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值