前提:秘钥长度=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库 的代码实例的更多相关文章

  1. RSA加解密 公钥加密私钥解密 公加私解 && C++ 调用openssl库 的代码实例

    前提:秘钥长度=1024 ============================================== 对一片(117字节)明文加密 ========================= ...

  2. RSA公钥加密-私钥解密/私钥加密-公钥解密

    package com.tebon.ams.util;import org.apache.commons.codec.binary.Base64;import org.apache.log4j.Log ...

  3. RSA不对称加密,公钥加密私钥解密,私钥加密公钥解密

    RSA算法是第一个能同时用于加密和数字签名的算法,也易于理解和操作. RSA是被研究得最广泛的公钥算法,从提出到现在已近二十年,经历了各种攻击的考验,逐渐为人们接受,普遍认为是目前最优秀的公钥方案之一 ...

  4. 银联手机支付(.Net Csharp),3DES加密解密,RSA加密解密,RSA私钥加密公钥解密,.Net RSA 3DES C#

    前段时间做的银联支付,折腾了好久,拼凑的一些代码,有需要的朋友可以参考,本人.Net新手,不保证准确性! 这个银联手机支付没有SDK提供,技术支持也没有.Net的,真心不好搞! RSA加解密,这里有个 ...

  5. RSA 加密算法 Java 公钥加密私钥解密 和 私钥加密公钥解密 的特点

    package com.smt.cipher.unsymmetry; import org.apache.commons.codec.binary.Base64; import org.apache. ...

  6. 基于私钥加密公钥解密的RSA算法C#实现

    RSA算法是第一个能同时用于加密和数字签名的算法,也易于理解和操作. RSA是被研究得最广泛的公钥算法,从提出到现在已近二十年,经历了各种攻击的考验,逐渐为人们接受,普遍认为是目前最优秀的公钥方案之一 ...

  7. 求求你们不要再用 RSA 私钥加密公钥解密了,这非常不安全!

    最近经常在网上看到有人说巨硬的 CNG(Cryptography Next Generation 即下一代加密技术) 只提供 RSA 公钥加密私钥解密,没有提供 RSA 私钥加密公钥解密,他们要自己封 ...

  8. golang 私钥"加密"公钥"解密"

    ---恢复内容开始---   之前工作主要使用C/C++与银行/第三方支付对接,但C/C++无法满足客户"当天给协议明天实盘上载"的开发速度以及现公司一些特殊情况,所以决定用go来 ...

  9. C# 基于大整数类的RSA算法实现(公钥加密私钥解密,私钥加密公钥解密)

    但是C#自带的RSA算法类RSACryptoServiceProvider只支持公钥加密私钥解密,即数字证书的使用. 所以参考了一些网上的资料写了一个RSA的算法实现.算法实现是基于网上提供的一个大整 ...

随机推荐

  1. SQL Fundamentals: 子查询 || WHERE,HAVING,FROM,SELECT子句中使用子查询,WITH子句

    SQL Fundamentals || Oracle SQL语言 子查询(基础) 1.认识子查询 2.WHERE子句中使用子查询 3.在HAVING子句中使用子查询 4.在FROM子句中使用子查询 5 ...

  2. day0318装饰器和内置函数

    一.装饰器 1.装饰器: 解释:装饰器的本事就是一个函数,不改动主代码的情况下,增加新功能.返回值也是一个函数对象. 2.装饰器工作过程 import time def func(): print(' ...

  3. todo: 改变字体的动画

    todo: 截取指定区域的图片, 改变字体的动画

  4. C++ Reflection Library

    C++ Reflection Library https://www.rttr.orghttps://github.com/rttrorg/rttr

  5. Javascript面向对象编程(二):构造函数的继承 作者:yuan一峰

    Javascript面向对象编程(二):构造函数的继承   作者: 阮一峰 日期: 2010年5月23日 这个系列的第一部分,主要介绍了如何"封装"数据和方法,以及如何从原型对象生 ...

  6. 1、用datetimepicker插件实现限定时间范围的选择 2、时间插件实现默认当天的时间和只能选择小于今天的日期

    一.用datetimepicker插件实现限定时间范围的选择 1.下面是要实现的效果图,让开始时间只能从  2018-7-1  到 2018-7-7 选择. 2.html的结构 <div cla ...

  7. _CSS Hack

    CSS Hack: 不到万不得已,不要使用.不易于维护. 有一些情况,需要一段特殊代码在遇到特殊浏览器环境才执行,而在其他条件下,不执行. 此时,CSS Hack 就能实现. CSS Hack 实际上 ...

  8. 【数据库】left join(左关联)、right join(右关联)、inner join(自关联)的区别

    left join(左关联).right join(右关联).inner join(自关联)的区别 用一张图说明三者的区别: 总结: left join(左联接) 返回包括左表中的所有记录和右表中关联 ...

  9. mysql 数据表操作 目录

    mysql 数据表操作 存储引擎介绍 mysql 使用存储引擎 mysql 数据表的增删改查 mysql 数据类型 mysql 约束条件

  10. rsyncd的配置和使用

    服务器端配置文件说明 # /etc/rsyncd: configuration file for rsync daemon mode # See rsyncd.conf man page for mo ...