Windows10下配置OpenSSL,并进行RSA加密

OpenSSL安装流程:


1、下载资源:

https://www.openssl.org/source/

(试过很多版本,1.1.0,1.0.2等,最后还是只有0.9.8a成功了,以下也是基于0.9.8a实现的)



2、安装activestate软件:
http://www.activestate.com/activeperl/downloads/


3、打开命令行,进入openssl源码目录。执行perl Configure VC-WIN32


4、在openssl源码目录运行ms\do_ms


5、转到C:\Program Files\Microsoft Visual Studio \VC98\bin目录(就是VC或VS对应的目录,你电脑可能有差异)执行vcvars32.bat


6、记事本打开ms\ntdll.mak,删除\WX;


7、跳到openssl目录下,编译动态链接库。执行 >nmake -f ms\ntdll.mak


如果编译成功,输出在out32dll目录下:包括可执行文件、两个dll和两个lib文件: libeay32.dll, libeay32.lib, ssleay32.dll,ssleay32.lib


8、VS2013配置


1.项目->属性->配置属性->VC++目录->包含目录,将openssl文件夹的inc32添加进去;
2.项目->属性->配置属性->VC++目录->库目录,添加openssl文件夹下的out32dll;
3.项目->属性->配置属性->链接器->输入->附加依赖项,增加libeay32.lib和ssleay32.lib;
4.将out32dll中的libeay32.dll和ssleay32.dll拷贝至工程目录下。



错误 1 error C4996: 'fopen': 

点击项目--》属性--》c/c++--》预处理器--》预处理定义--》,添加_CRT_SECURE_NO_WARNINGS


#include <openssl/rsa.h>
#include <openssl/err.h>
#include <openssl/pem.h>

#include <iostream>
#include <string>
#include <cstring>
#include <cassert>
using namespace std;

//加密
std::string EncodeRSAKeyFile(const std::string& strPemFileName, const std::string& strData)
{
	if (strPemFileName.empty() || strData.empty())
	{
		assert(false);
		return "";
	}
	FILE* hPubKeyFile = fopen(strPemFileName.c_str(), "rb");
	if (hPubKeyFile == NULL)
	{
		assert(false);
		return "";
	}
	std::string strRet;
	RSA* pRSAPublicKey = RSA_new();
	if (PEM_read_RSA_PUBKEY(hPubKeyFile, &pRSAPublicKey, 0, 0) == NULL)
	{
		assert(false);
		return "";
	}

	int nLen = RSA_size(pRSAPublicKey);
	char* pEncode = new char[nLen + 1];
	int ret = RSA_public_encrypt(strData.length(), (const unsigned char*)strData.c_str(), (unsigned char*)pEncode, pRSAPublicKey, RSA_PKCS1_PADDING);
	if (ret >= 0)
	{
		strRet = std::string(pEncode, ret);
	}
	delete[] pEncode;
	RSA_free(pRSAPublicKey);
	fclose(hPubKeyFile);
	CRYPTO_cleanup_all_ex_data();
	return strRet;
}

//解密
std::string DecodeRSAKeyFile(const std::string& strPemFileName, const std::string& strData)
{
	if (strPemFileName.empty() || strData.empty())
	{
		assert(false);
		return "";
	}
	FILE* hPriKeyFile = fopen(strPemFileName.c_str(), "rb");
	if (hPriKeyFile == NULL)
	{
		assert(false);
		return "";
	}
	std::string strRet;
	RSA* pRSAPriKey = RSA_new();
	if (PEM_read_RSAPrivateKey(hPriKeyFile, &pRSAPriKey, 0, 0) == NULL)
	{
		assert(false);
		return "";
	}
	int nLen = RSA_size(pRSAPriKey);
	char* pDecode = new char[nLen + 1];

	int ret = RSA_private_decrypt(strData.length(), (const unsigned char*)strData.c_str(), (unsigned char*)pDecode, pRSAPriKey, RSA_PKCS1_PADDING);
	if (ret >= 0)
	{
		strRet = std::string((char*)pDecode, ret);
	}
	delete[] pDecode;
	RSA_free(pRSAPriKey);
	fclose(hPriKeyFile);
	CRYPTO_cleanup_all_ex_data();
	return strRet;
}

int main()
{
	//原文
	const string one = "skl;dfhas;lkdfhslk;dfhsidfhoiehrfoishfsidf";
	cout << "one: " << one << endl;

	//密文(二进制数据)
	string two = EncodeRSAKeyFile("pubkey.pem", one);
	cout << "two: " << two << endl;

	//顺利的话,解密后的文字和原文是一致的
	string three = DecodeRSAKeyFile("prikey.pem", two);
	cout << "three: " << three << endl;
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值