RSA加解密 私钥加密公钥解密 私加公解 && C++ 调用openssl库 的代码实例
前提:秘钥长度=1024
==============================================
对一片(117字节)明文加密 私加
==============================================
// 私钥加密
std::string rsa_pri_encrypt(const std::string &clearText, std::string &pubKey)
{
std::string strRet;
BIO *keybio = BIO_new_mem_buf((unsigned char *)pubKey.c_str(), -);
// 此处有三种方法
// 1, 读取内存里生成的密钥对,再从内存生成rsa
// 2, 读取磁盘里生成的密钥对文本文件,在从内存生成rsa
// 3,直接从读取文件指针生成rsa
//RSA* pRSAPublicKey = RSA_new();
RSA* rsa = RSA_new();
rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa, NULL, NULL);
if (!rsa)
{
BIO_free_all(keybio);
return std::string("");
} int len = RSA_size(rsa);
//int len = 1028;
char *encryptedText = (char *)malloc(len + );
memset(encryptedText, , len + ); // 加密
int ret = RSA_private_encrypt(clearText.length(), (const unsigned char*)clearText.c_str(), (unsigned char*)encryptedText, rsa, RSA_PKCS1_PADDING);
if (ret >= )
strRet = std::string(encryptedText, ret); // 释放内存
free(encryptedText);
BIO_free_all(keybio);
RSA_free(rsa); return strRet;
}
==============================================
对一片(128字节)密文解密 公解
==============================================
// 公钥解密
std::string rsa_pub_decrypt(const std::string &clearText, std::string &pubKey)
{
std::string strRet;
BIO *keybio = BIO_new_mem_buf((unsigned char *)pubKey.c_str(), -);
//keybio = BIO_new_mem_buf((unsigned char *)strPublicKey.c_str(), -1);
// 此处有三种方法
// 1, 读取内存里生成的密钥对,再从内存生成rsa
// 2, 读取磁盘里生成的密钥对文本文件,在从内存生成rsa
// 3,直接从读取文件指针生成rsa
//RSA* pRSAPublicKey = RSA_new();
RSA* rsa = RSA_new();
rsa = PEM_read_bio_RSAPublicKey(keybio, &rsa, NULL, NULL);
if (!rsa)
{
BIO_free_all(keybio);
return std::string("");
} int len = RSA_size(rsa);
//int len = 1028;
char *encryptedText = (char *)malloc(len + );
memset(encryptedText, , len + ); //解密
int ret = RSA_public_decrypt(clearText.length(), (const unsigned char*)clearText.c_str(), (unsigned char*)encryptedText, rsa, RSA_PKCS1_PADDING);
if (ret >= )
strRet = std::string(encryptedText, ret); // 释放内存
free(encryptedText);
BIO_free_all(keybio);
RSA_free(rsa); return strRet;
}
==============================================
对整体 明文加密 私加
==============================================
//私钥加密 + 分片
std::string rsa_pri_split117_encrypt(const std::string &clearText, std::string &pubKey)
{
std::string result;
std::string input;
result.clear();
for(int i = ; i < clearText.length()/; i++)
{
input.clear();
input.assign(clearText.begin() + i*, clearText.begin() + i* + );
result = result + rsa_pri_encrypt(input, pubKey);
}
if( clearText.length()% != )
{
int tem1 = clearText.length()/*;
int tem2 = clearText.length() - tem1;
input.clear();
input.assign(clearText.begin()+ tem1, clearText.end());
result = result + rsa_pri_encrypt(input, pubKey);
}
return result;
}
==============================================
对整体 密文解密 公解
==============================================
//公钥解密 + 分片
std::string rsa_pub_split128_decrypt(const std::string &clearText, std::string &pubKey)
{
//Base64 *base = new Base64();
std::string result;
std::string input;
result.clear();
for(int i = ; i< clearText.length()/; i++)
{
input.clear();
input.assign(clearText.begin() + i*, clearText.begin() + i* + ); result = result + rsa_pub_decrypt(input, pubKey);
}
if(clearText.length()% != )
{
int tem1 = clearText.length()/ * ;
int tem2 = clearText.length() - tem1;
input.clear();
input.assign(clearText.begin()+ tem1, clearText.end());
result = result + rsa_pri_encrypt(input, pubKey);
}
return result;
}
附1:rsa 公加私解
附2:C++ 使用openssl库实现 DES 加密——CBC模式 && RSA加密——公加私解——私加公解
RSA加解密 私钥加密公钥解密 私加公解 && C++ 调用openssl库 的代码实例的更多相关文章
- RSA加解密 公钥加密私钥解密 公加私解 && C++ 调用openssl库 的代码实例
前提:秘钥长度=1024 ============================================== 对一片(117字节)明文加密 ========================= ...
- RSA公钥加密-私钥解密/私钥加密-公钥解密
package com.tebon.ams.util;import org.apache.commons.codec.binary.Base64;import org.apache.log4j.Log ...
- RSA不对称加密,公钥加密私钥解密,私钥加密公钥解密
RSA算法是第一个能同时用于加密和数字签名的算法,也易于理解和操作. RSA是被研究得最广泛的公钥算法,从提出到现在已近二十年,经历了各种攻击的考验,逐渐为人们接受,普遍认为是目前最优秀的公钥方案之一 ...
- 银联手机支付(.Net Csharp),3DES加密解密,RSA加密解密,RSA私钥加密公钥解密,.Net RSA 3DES C#
前段时间做的银联支付,折腾了好久,拼凑的一些代码,有需要的朋友可以参考,本人.Net新手,不保证准确性! 这个银联手机支付没有SDK提供,技术支持也没有.Net的,真心不好搞! RSA加解密,这里有个 ...
- RSA 加密算法 Java 公钥加密私钥解密 和 私钥加密公钥解密 的特点
package com.smt.cipher.unsymmetry; import org.apache.commons.codec.binary.Base64; import org.apache. ...
- 基于私钥加密公钥解密的RSA算法C#实现
RSA算法是第一个能同时用于加密和数字签名的算法,也易于理解和操作. RSA是被研究得最广泛的公钥算法,从提出到现在已近二十年,经历了各种攻击的考验,逐渐为人们接受,普遍认为是目前最优秀的公钥方案之一 ...
- 求求你们不要再用 RSA 私钥加密公钥解密了,这非常不安全!
最近经常在网上看到有人说巨硬的 CNG(Cryptography Next Generation 即下一代加密技术) 只提供 RSA 公钥加密私钥解密,没有提供 RSA 私钥加密公钥解密,他们要自己封 ...
- golang 私钥"加密"公钥"解密"
---恢复内容开始--- 之前工作主要使用C/C++与银行/第三方支付对接,但C/C++无法满足客户"当天给协议明天实盘上载"的开发速度以及现公司一些特殊情况,所以决定用go来 ...
- C# 基于大整数类的RSA算法实现(公钥加密私钥解密,私钥加密公钥解密)
但是C#自带的RSA算法类RSACryptoServiceProvider只支持公钥加密私钥解密,即数字证书的使用. 所以参考了一些网上的资料写了一个RSA的算法实现.算法实现是基于网上提供的一个大整 ...
随机推荐
- [No0000169]Potplayer倍速播放快捷键修改速率步长
右键-播放-播放设置-速度调整单位改成0.5,即可一次加速到1.5
- linux:基本指令ls、cd
cd 指令 使用 cd 指令, 我们能在 Terminal 中轻松切换到不同的文件夹. 想从 Home 去 Documents 这个文件夹? 输入下面的命令就可以了. ~$ cd Documents ...
- 查询大数据表的效率对比:Linq to SQL、Entity Framework、企业库存储过程、ADO.Net
最近因为要开发大数据量网站,特作比较. Linq to SQL 查询 记录数:399997Linq to SQL 查询 Milliseconds:1910视图查询 记录数:399997视图查询 Mil ...
- piano class 13
1,手放在琴键上,不妨碍另外一只手弹奏即可 2,识别五线谱可以加上几条线,减去几条线,一下子就记住了所有的 3,弹得还是有点快,要慢弹奏,四四拍的理论上比四三拍的还要慢,也要看风格 4,四二拍,强弱, ...
- winform 科学计数法转为小数
先强制转换为decimal. 例如: double xyTolerance = 0.000000008983001; txtXYTolerance.Text = ((decimal)xyToleran ...
- CSP 通信顺序进程
communicating sequential processes CSP 通信顺序进程 C.A.R.Hoare 1979 CSP是一种用来描述并行系统交互模式的形式语言,最早由C.A.R.Hoar ...
- iOS-方法之+ initialize 与 +load
Objective-C 有两个神奇的方法:+load 和 +initialize,这两个方法在类被使用时会自动调用.但是两个方法的不同点会导致应用层面上性能的显著差异. 一.+ initialize ...
- [cloud][sdn] openstack openflow opendaylight openvswitch
https://www.quora.com/What-is-the-relation-between-OpenStack-OpenDaylight-OpenFlow-and-Open-vSwitch- ...
- HBase 笔记
参考资料:HBase权威指南 一行由若干列组成,若干列又构成一个列族一个列族的所有列存储在同一个底层的存储文件里,这个文件叫HFile列族的数量有限制:一个列族里列的数量没限制谓词删除:例如允许用户只 ...
- winform版的dota改键器
从网上找的源码很不齐全,自己补充了一下,有兴趣的可以看看. 首先是建立一个hook类WFChangeKey using System; using System.Reflection; using S ...