• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

~纯净~

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

在windows上安装编译OpenSSL

一、下载Openssl

链接:https://oomake.com/download/openssl

二、链接库与头文件

1、为Openssl链接静态库

地址:Project->Build Options->Debug->Linker settings->Add 自己openssl安装目录下/lib下所有.lib文件
image

2、为Openssl链接动态库

地址:Project->Build Options->Debug->Search directories->Linker->Add 安装目录bin文件
image

3、为Openssl链接头文件

地址:Project->Build Options->Debug->Search directories->Compiler->Add 安装目录/include
image

三、编译运行

1、测试运行文件

image

2、编译代码测试

代码如下:

点击查看代码
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <string.h>
#include <iostream>

using namespace std;

char * base64Encode(const char *buffer, int length, bool newLine);
char * base64Decode(char *input, int length, bool newLine);

int main(int argc, char* argv[])
{
	bool newLine = false;
	string input = "Hello World!";

	char * encode = base64Encode(input.c_str(), input.length(), newLine);
	char * decode = base64Decode(encode, strlen(encode), newLine);

	cout << "Base64 Encoded : " << encode << endl;
	cout << "Base64 Decoded : " << decode << endl;

	cin.get();
	return 0;
}

// base64 编码
char * base64Encode(const char *buffer, int length, bool newLine)
{
	BIO *bmem = NULL;
	BIO *b64 = NULL;
	BUF_MEM *bptr;

	b64 = BIO_new(BIO_f_base64());
	if (!newLine) {
		BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
	}
	bmem = BIO_new(BIO_s_mem());
	b64 = BIO_push(b64, bmem);
	BIO_write(b64, buffer, length);
	BIO_flush(b64);
	BIO_get_mem_ptr(b64, &bptr);
	BIO_set_close(b64, BIO_NOCLOSE);

	char *buff = (char *)malloc(bptr->length + 1);
	memcpy(buff, bptr->data, bptr->length);
	buff[bptr->length] = 0;
	BIO_free_all(b64);

	return buff;
}

// base64 解码
char * base64Decode(char *input, int length, bool newLine)
{
	BIO *b64 = NULL;
	BIO *bmem = NULL;
	char *buffer = (char *)malloc(length);
	memset(buffer, 0, length);
	b64 = BIO_new(BIO_f_base64());
	if (!newLine) {
		BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
	}
	bmem = BIO_new_mem_buf(input, length);
	bmem = BIO_push(b64, bmem);
	BIO_read(bmem, buffer, length);
	BIO_free_all(bmem);

	return buffer;
}

![image](https://img2023.cnblogs.com/blog/2166633/202303/2166633-20230321110942607-2146371348.png)

posted on 2023-03-21 11:09  ~纯净~  阅读(810)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3