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的算法实现.算法实现是基于网上提供的一个大整 ...
随机推荐
- [No0000DD]C# StringEx 扩展字符串类 类封装
using System; using System.Text.RegularExpressions; namespace Helpers { /// <summary> /// 包含常用 ...
- Flannel - 原理
Flannel GitHub 地址 Flannel 是 Kubernetes 中常用的网络配置工具,用于配置第三层(网络层)网络结构. 如何工作Flannel 需要在集群中的每台主机上运行一个名为 f ...
- ASP.NET MVC 计划任务(不使用外接程序,.net内部机制实现)
在asp.net中要不使用其他插件的情况下只能使用定时器来检查, 并执行任务. 以下讲解步骤: 1. 在Global.asax 文件中作如下修改 1 2 3 4 5 6 7 8 9 10 11 voi ...
- coreseek搜索
参考文档地址:http://github.tiankonguse.com/doc/sphinx/
- 这就是使用ReportBuilder最简单的例子
用这组控件最简单的例子:在窗体上放上组件名为ppBDEPipeline1,ppReport1,ppDesigner1,ppViewer1,DataSource1的控件,设置ppreport1的data ...
- Beanstalkd 基本概念和使用
1:什么是 Beanstalkd ? Beanstalkd 一个高性能.轻量级的分布式内存队列系统 简单来说,就是一个队列,相比于 数据库/redis 队列相比. 更专业.能完成的功能更多.就这么理解 ...
- linux基本介绍和使用
基本介绍 Linux入门教程 快捷键 linux 快捷键 用户及用户组 linux之用户和用户组
- linux 查看文件目录大小
du [-abcDhHklmsSx] [-L <符号连接>][-X <文件>][--block-size][--exclude=<目录或文件>] [--max-de ...
- 1-2-编译U-boot
1-2-编译U-boot 1.su+enter进入超级用户模式. 2.cd /mnt/+两次Tab去到根目录,ls显示共享文件夹里的文件. 3.解压tar xvfj uboot_TQ210_1.3.4 ...
- dispatch_queue_set_specific可重入的gcd
有时候我们很希望知道当前执行的queue是谁,比如UI操作需要放在main queue中执行.如果可以知道当前工作的queue是谁,就可以很方便的指定一段代码操作在特定的queue中执行.这种做法让G ...