前提:秘钥长度=1024

==============================================

    对一片(117字节)明文加密

==============================================

// 公钥加密
std::string rsa_pub_encrypt(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_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_pri_decrypt(const std::string &cipherText, const std::string &priKey)
{
std::string strRet;
RSA *rsa = RSA_new();
BIO *keybio;
keybio = BIO_new_mem_buf((unsigned char *)priKey.c_str(), -); // 此处有三种方法
// 1, 读取内存里生成的密钥对,再从内存生成rsa
// 2, 读取磁盘里生成的密钥对文本文件,在从内存生成rsa
// 3,直接从读取文件指针生成rsa
rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa, NULL, NULL); int len = RSA_size(rsa);
char *decryptedText = (char *)malloc(len + );
memset(decryptedText, , len + ); // 解密函数
int ret = RSA_private_decrypt(cipherText.length(), (const unsigned char*)cipherText.c_str(), (unsigned char*)decryptedText, rsa, RSA_PKCS1_PADDING);
if (ret >= )
strRet = std::string(decryptedText, ret); // 释放内存
free(decryptedText);
BIO_free_all(keybio);
RSA_free(rsa); return strRet;
}

注:工作中只用到了 rsa私加公解,因此没有 针对全部明文的公加私解的代码实现,请参考附录。

附:rsa 私加公解

RSA加解密 公钥加密私钥解密 公加私解 && C++ 调用openssl库 的代码实例的更多相关文章

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

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

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

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

  3. RSA加解密工具类RSAUtils.java,实现公钥加密私钥解密和私钥解密公钥解密

    package com.geostar.gfstack.cas.util; import org.apache.commons.codec.binary.Base64; import javax.cr ...

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

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

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

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

  6. 使用Base64进行string的加密和解密 公钥加密—私钥签名

    使用Base64进行string的加密和解密   //字符串转bytesvar ebytes = System.Text.Encoding.Default.GetBytes(keyWord);//by ...

  7. C#中使用OpenSSL的公钥加密/私钥解密

    在C#中进行公钥加密/私钥解密,需要用RSACryptoServiceProvider,但是它不支持由OpenSSL生成的公钥/私钥字符串. 比如这样的公钥/私钥对( 公私钥生成方法见 http:// ...

  8. NetCore 生成RSA公私钥对,公钥加密私钥解密,私钥加密公钥解密

    using Newtonsoft.Json; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Encodings; using ...

  9. OpenSSL和Python实现RSA Key公钥加密私钥解密

    基于非对称算法的RSA Key主要有两个用途,数字签名和验证(私钥签名,公钥验证),以及非对称加解密(公钥加密,私钥解密).本文提供一个基于OpenSSL和Python进行非对称加解密的例子. 1. ...

随机推荐

  1. MySql数据库笔试题总结

    数据库面试题94577265 1,设有关系EMP(ENO,ENAME,SALARY,DNO),其中各属性的含义依次为职工号.姓名.工资和所在部门号,以及关系DEPT(DNO,DNAME,MANAGER ...

  2. shell脚本之tr命令使用

    tr命令用来进行对标准输入的内容做替换.例如 # echo 'HELLO WORLD!!!' | tr "A-Z" "a-z" hello world!!! 这 ...

  3. ruby-from-other-languages

    http://www.ruby-lang.org/en/documentation/ruby-from-other-languages/to-ruby-from-python/ http://www. ...

  4. Copycat - CopycatServer

    Server被拉起有两种方式, Address address = new Address("123.456.789.0", 5000); CopycatServer.Builde ...

  5. Apache Common Math Stat

    http://commons.apache.org/proper/commons-math/userguide/stat.html mark   DescriptiveStatistics maint ...

  6. python_flask 注册,登陆,退出思路 ---纯个人观点

    1注册逻辑首先查询数据库用户名 并判断用户是否存在,如不存在就插入数据 并返回响应给前端2前端模板获取注册信息 判断 用户名不能为空及密码不能为空,和密码不一致 拼接注册url 组成get获取对象 响 ...

  7. html5页面自适应移动端

    1. <!-- 这段代码的意思是,让 viewport 的宽度等于物理设备上的真实分辨率,不允许用户缩放,这样 dpi 肯定和设备上的真实分辨率是一样的,不做任何缩放,网页会因此显得更细腻. 1 ...

  8. CentOS 7 下安装jdk1.8(转)

    原文:https://blog.argcv.com/articles/3155.c CentOS 7下目前默认是jdk1.6和1.7.若需要更高版本的1.8,我们就需要一点额外的手段了. 首先,我们需 ...

  9. sublime phpfmt 的格式化

    php格式化有几种,这里只说phpfmt.这个插件只支持php7.0+,所以在安装php环境应该支持php7.0.至于低版本,在packagecontrol.io对应的插件页面也又提到. 在subli ...

  10. Vue通过build打包后 打开index.html页面是空白的

    最近在build打包vue项目遇到了几个问题,如下: 1.npm run build打包项目之后,我们通常是把dist文件里面被压缩后的static文件跟index.html提交到服务器,但最近发现直 ...