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的算法实现.算法实现是基于网上提供的一个大整 ...
随机推荐
- rsync定时同步文件
rsync服务器 ip:192.168.1.198 操作系统:centos7.2 rsync客户端 ip:192.168.1.16 操作系统:centos7.2 服务器配置 1.yum -y inst ...
- Page9:结构分解以及系统内部稳定和BIBO稳定概念及其性质[Linear System Theory]
内容包含系统能控性结构分解.系统能观测性结构分解以及系统结构规范分解原理,线性系统的内部稳定.BIBO稳定概念及其性质
- Ollydbg
1.用来查看dll文件的信息,取代现在使用的exescope;
- 转:JVM 内存初学 (堆(heap)、栈(stack)和方法区(method) )
原文地址:JVM 内存初学 (堆(heap).栈(stack)和方法区(method) ) 博主推荐 深入浅出JVM 这本书 先了解具体的概念:JAVA的JVM的内存可分为3个区:堆(heap).栈( ...
- 20165317 学习基础和C语言基础调查
学习基础和C语言基础调查 关于优势技能 说来惭愧,读书多年,爱好不少,但是真的能拿的出手的.能被叫做特长的不多.至今,能在同龄人中处于较领先位置的也只有从四年级开始练起的乒乓球.记得开始练习乒乓球是从 ...
- vue关于html页面id设置问题
vue是一个前端框架,类似于angularJS等,vue在编写的时候需要在html页面指定id,但是不是都可以实现的,一般放在id需在div设置里才可以实现. (一) 在html里设置id: < ...
- C++ Reflection Library
C++ Reflection Library https://www.rttr.orghttps://github.com/rttrorg/rttr
- python摸爬滚打之day01----初识Python
1.编程语言分类 编译型语言:程序被一次性全部翻译成机器语言,计算机直接以机器语⾔言来运⾏行行此程序. 优点:运行效率高,可脱离语言环境独立运行. 缺点:开发效率低,可移植性差. 解释型语言:将程序逐 ...
- Word Embedding理解
一直以来感觉好多地方都吧Word Embedding和word2vec混起来一起说,所以导致对这俩的区别不是很清楚. 其实简单说来就是word embedding包含了word2vec,word2ve ...
- webpack浅析~
1.webpack打包原理: 把所有依赖打包成一个 bundle.js 文件,通过代码分割成单元片段并按需加载. 2.webpack的优势: ①.webpack 是以 commonJS 的形式来书写脚 ...