如何利用openssl来进行base64编解码?

972 篇文章 329 订阅
7 篇文章 2 订阅

      openssl的用法, 请见之前博文, 下面仅仅给出base64编解码的代码:

 

#include <iostream>
#include <openssl/evp.h>
#pragma comment(lib, "libeay32.lib") 
#pragma comment(lib, "ssleay32.lib")  // 可以注释掉
using namespace std;

// base64编码
int Base64Encode(const char *encoded, int encodedLength, char *decoded)
{    
	return EVP_EncodeBlock((unsigned char*)decoded, (const unsigned char*)encoded, encodedLength);
}

// base解码
int Base64Decode(const char *encoded, int encodedLength, char *decoded) 
{    
	return EVP_DecodeBlock((unsigned char*)decoded, (const unsigned char*)encoded, encodedLength);
}

int main()
{
	char test[] = "hello";
	char result[1000] = {0}; // 编码的结果
	cout << Base64Encode(test, strlen(test), result) << endl;
	cout << result << endl;

	char org[1000] = {0};    // 解码的结果
	cout << Base64Decode(result, strlen(result), org) << endl;
	cout << org << endl;

	return 0;
}

      结果为:

 

8
aGVsbG8=
6
hello
 

       经对比, 与其他工具产生的值是相同的, 小有成就感。

 

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值