1.openssl源码
ftp://ftp.openssl.org/source/
2.openssl命令
openssl genrsa -out prikey.pem 1024
openssl rsa -in privkey.pem -pubout -out pubkey.pem
3.openssl c++应用实例
#include <iostream>#include <string>
using namespace std;
#include "OpenSSLRSA.h"
int _tmain(int argc, _TCHAR* argv[])
{
COpenSSLRSA osrsa;
osrsa.m_sText = "123abcd我没啥地方法规ldjf464";
osrsa.m_sPubKeyFile = "public.pem";
osrsa.m_sPriKeyFile = "private.pem";
//strcpy(osrsa.m_sPriKeyPwd, "1234");
int nState = 1;
switch(nState)
{
case 1:
{
// 1.从内存中读取秘钥
osrsa.BuildKey();
if(!osrsa.ReadPubRSAByMem())
return -1;
if(!osrsa.ReadPriRSAByMem())
return -1;
}
break;
case 2:
{
// 2.直接产生秘钥对
osrsa.BuildKey();
}
break;
case 3:
{
// 3.从文件中读取秘钥
osrsa.BuildKey();
if(!osrsa.ReadPubRSAByFile())
return -1;
if(!osrsa.ReadPriRSAByFile())
return -1;
}
break;
default:
break;
}
cout << "-----原文: " << osrsa.m_sText << endl << endl;
//密文(二进制数据)
//string two = osrsa.EncodeRSAPubKeyData();
string two = osrsa.EncodeRSAPriKeyData();
int len1 = osrsa.m_sPubKey.length();
int len2 = osrsa.m_sPriKey.length();
int len3 = osrsa.m_sText.length();
int len4 = osrsa.m_sCryptText.length();
if(two.length() <= 0)
return 0;
cout << "-----密文: " << two << endl << endl;
//顺利的话,解密后的文字和原文是一致的
//string three = osrsa.DecodeRSAPriKeyData();
string three = osrsa.DecodeRSAPubKeyData();
if(three.length() <= 0)
return 0;
cout << "-----译文: " << three << endl << endl;
cout<<"公钥长度 = "<<len1<<endl;
cout<<"私钥长度 = "<<len2<<endl;
cout<<"原文长度 = "<<len3<<endl;
cout<<"密文长度 = "<<len4<<endl;
cout<<endl;
return 0;
}