用openssl进行rsa的加密与解密(linux,C++版)
来源:互联网 发布:热云数据融资 编辑:程序博客网 时间:2024/05/29 13:42
转自 : http://blog.csdn.net/small_qch/article/details/19330211
初学openssl库,写了一例子,记录一下。
PS:openssl和openssl库的安装就不说了,网上一大把
1:输入命令,生成公钥和私钥(1024位)
openssl genrsa -out prikey.pem 1024openssl rsa -in privkey.pem -pubout -out pubkey.pem2:C++小程序
-
-
- #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;
- }
参考文章:http://www.cnblogs.com/cswuyg/p/3187462.html
阅读全文
0 0
- 用openssl进行rsa的加密与解密(linux,C++版)
- 用openssl进行rsa的加密与解密(linux,C++版)
- 用openssl进行rsa的加密与解密(linux,C++版)
- C/C++使用openssl进行摘要和加密解密(md5, sha256, des, rsa)
- C/C++使用openssl进行摘要和加密解密(md5, sha256, des, rsa)
- C/C++使用openssl进行摘要和加密解密(md5, sha256, des, rsa)
- 利用openssl进行RSA加密解密
- 利用openssl进行RSA加密解密
- openssl命令行进行RSA加密解密
- iOS -- OpenSSL进行RSA加密解密概念
- 利用openssl进行RSA加密解密
- 利用openssl进行RSA加密解密
- 使用openssl进行RSA加密解密
- 利用openssl进行RSA加密解密实例
- linux下用openssl的RSA算法对文件加密解密
- OpenSSL生成证书进行iOS加密,java解密的RSA非对称加密 详解
- 用javascript与java进行RSA加密与解密
- 用javascript与java进行RSA加密与解密
- java执行jar出现编码问题的解决方案,Could not decode a text frame as UTF-8
- mysql索引
- Android数据库(SQLite)框架(7)——使用LitePal聚合函数
- 计算两段文本的相识度
- 25 web.xml配置详解及示例
- 用openssl进行rsa的加密与解密(linux,C++版)
- 多重背包问题
- 虚函数的作用
- NOIP2016 T5 蚯蚓
- 2、Hyperledger Fabric 交易流程
- struts.xml配置说明
- 机器学习常用算法总结
- Keepalived+LVS 安装部署文档
- mysql中有重复数据建唯一索引