用openssl进行rsa的加密与解密(linux,C++版)

转自 : http://blog.csdn.net/small_qch/article/details/19330211


初学openssl库,写了一例子,记录一下。
PS:openssl和openssl库的安装就不说了,网上一大把

1:输入命令,生成公钥和私钥(1024位)
openssl genrsa -out prikey.pem 1024
openssl rsa -in privkey.pem -pubout -out pubkey.pem

2:C++小程序
[cpp]  view plain  copy
 print ?
  1. //demo.cpp  
  2. // g++ demo.cpp -o demo -lcrypto  
  3. #include <openssl/rsa.h>  
  4. #include <openssl/err.h>  
  5. #include <openssl/pem.h>  
  6.   
  7. #include <iostream>  
  8. #include <string>  
  9. #include <cstring>  
  10. #include <cassert>  
  11. using namespace std;  
  12.   
  13. //加密  
  14. std::string EncodeRSAKeyFile( const std::string& strPemFileName, const std::string& strData )  
  15. {  
  16.     if (strPemFileName.empty() || strData.empty())  
  17.     {  
  18.         assert(false);  
  19.         return "";  
  20.     }  
  21.     FILE* hPubKeyFile = fopen(strPemFileName.c_str(), "rb");  
  22.     if( hPubKeyFile == NULL )  
  23.     {  
  24.         assert(false);  
  25.         return "";   
  26.     }  
  27.     std::string strRet;  
  28.     RSA* pRSAPublicKey = RSA_new();  
  29.     if(PEM_read_RSA_PUBKEY(hPubKeyFile, &pRSAPublicKey, 0, 0) == NULL)  
  30.     {  
  31.         assert(false);  
  32.         return "";  
  33.     }  
  34.   
  35.     int nLen = RSA_size(pRSAPublicKey);  
  36.     char* pEncode = new char[nLen + 1];  
  37.     int ret = RSA_public_encrypt(strData.length(), (const unsigned char*)strData.c_str(), (unsigned char*)pEncode, pRSAPublicKey, RSA_PKCS1_PADDING);  
  38.     if (ret >= 0)  
  39.     {  
  40.         strRet = std::string(pEncode, ret);  
  41.     }  
  42.     delete[] pEncode;  
  43.     RSA_free(pRSAPublicKey);  
  44.     fclose(hPubKeyFile);  
  45.     CRYPTO_cleanup_all_ex_data();   
  46.     return strRet;  
  47. }  
  48.   
  49. //解密  
  50. std::string DecodeRSAKeyFile( const std::string& strPemFileName, const std::string& strData )  
  51. {  
  52.     if (strPemFileName.empty() || strData.empty())  
  53.     {  
  54.         assert(false);  
  55.         return "";  
  56.     }  
  57.     FILE* hPriKeyFile = fopen(strPemFileName.c_str(),"rb");  
  58.     if( hPriKeyFile == NULL )  
  59.     {  
  60.         assert(false);  
  61.         return "";  
  62.     }  
  63.     std::string strRet;  
  64.     RSA* pRSAPriKey = RSA_new();  
  65.     if(PEM_read_RSAPrivateKey(hPriKeyFile, &pRSAPriKey, 0, 0) == NULL)  
  66.     {  
  67.         assert(false);  
  68.         return "";  
  69.     }  
  70.     int nLen = RSA_size(pRSAPriKey);  
  71.     char* pDecode = new char[nLen+1];  
  72.   
  73.     int ret = RSA_private_decrypt(strData.length(), (const unsigned char*)strData.c_str(), (unsigned char*)pDecode, pRSAPriKey, RSA_PKCS1_PADDING);  
  74.     if(ret >= 0)  
  75.     {  
  76.         strRet = std::string((char*)pDecode, ret);  
  77.     }  
  78.     delete [] pDecode;  
  79.     RSA_free(pRSAPriKey);  
  80.     fclose(hPriKeyFile);  
  81.     CRYPTO_cleanup_all_ex_data();   
  82.     return strRet;  
  83. }  
  84.   
  85. int main()  
  86. {  
  87.     //原文  
  88.     const string one = "skl;dfhas;lkdfhslk;dfhsidfhoiehrfoishfsidf";  
  89.     cout << "one: " << one << endl;  
  90.   
  91.     //密文(二进制数据)  
  92.     string two = EncodeRSAKeyFile("pubkey.pem", one);  
  93.     cout << "two: " << two << endl;  
  94.   
  95.     //顺利的话,解密后的文字和原文是一致的  
  96.     string three = DecodeRSAKeyFile("prikey.pem", two);  
  97.     cout << "three: " << three << endl;  
  98.     return 0;  
  99. }  

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值