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

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;} 结果为:

8aGVsbG8=6hello

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

接受失败等于回归真实的自我,

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

相关文章:

你感兴趣的文章:

标签云: