首页 技术 正文
技术 2022年11月13日
0 收藏 546 点赞 3,910 浏览 2628 个字

1,非对称加密RSA:

(1)乙方生成两把密钥(公钥和私钥)。公钥是公开的,任何人都可以获得,私钥则是保密的。

(2)甲方获取乙方的公钥,然后用它对信息加密。

(3)乙方得到加密后的信息,用私钥解密。

2,使用CryptoPP实现RSA:

CryptoPP是一套非常完整的加密解密开源解决方案,如何使用这里就不多说了,请自行Google。

 #include "..\cryptopp562\randpool.h"
#include "..\cryptopp562\osrng.h"
#include "..\cryptopp562\rsa.h" //待加密的字符串
string message = "http://my.oschina.net/xlplbo/blog";
printf("message = %s, length = %d\n", message.c_str(), strlen(message.c_str())); /*
//自动生成随机数据
byte seed[600] = "";
AutoSeededRandomPool rnd;
rnd.GenerateBlock(seed, sizeof(seed));
printf("seed = %s\n", (char *)seed, strlen((char *)seed)); //生成加密的高质量伪随机字节播种池一体化后的熵
RandomPool randPool;
randPool.Put(seed, sizeof(seed));
*/ AutoSeededRandomPool rnd;
InvertibleRSAFunction params;
params.GenerateRandomWithKeySize(rnd, ); RSA::PrivateKey privateKey(params);
RSA::PublicKey publicKey(params); //使用OAEP模式
//RSAES_OAEP_SHA_Decryptor pri(randPool, sizeof(seed));
//RSAES_OAEP_SHA_Encryptor pub(pri);
RSAES_OAEP_SHA_Decryptor pri(privateKey);
RSAES_OAEP_SHA_Encryptor pub(publicKey);
printf("max plaintext Length = %d,%d\n", pri.FixedMaxPlaintextLength(), pub.FixedMaxPlaintextLength());
if (pub.FixedMaxPlaintextLength() > message.length())
{//待加密文本不能大于最大加密长度
string chilper;
StringSource(message, true, new PK_EncryptorFilter(rnd, pub, new StringSink(chilper)));
printf("chilper = %s, length = %d\n", chilper.c_str(), strlen(chilper.c_str())); string txt;
StringSource(chilper, true, new PK_DecryptorFilter(rnd, pri, new StringSink(txt)));
printf("txt = %s, length = %d\n", txt.c_str(), strlen(txt.c_str()));
} //使用PKCS1v15模式
//RSAES_PKCS1v15_Decryptor pri1(randPool, sizeof(seed));
//RSAES_PKCS1v15_Encryptor pub1(pri1);
RSAES_PKCS1v15_Decryptor pri1(privateKey);
RSAES_PKCS1v15_Encryptor pub1(publicKey);
printf("max plaintext Length = %d,%d\n", pri1.FixedMaxPlaintextLength(), pub1.FixedMaxPlaintextLength());
if (pub1.FixedMaxPlaintextLength() > message.length())
{//待加密文本不能大于最大加密长度
string chilper;
StringSource(message, true, new PK_EncryptorFilter(rnd, pub1, new StringSink(chilper)));
printf("chilper = %s, length = %d\n", chilper.c_str(), strlen(chilper.c_str())); string txt;
StringSource(chilper, true, new PK_DecryptorFilter(rnd, pri1, new StringSink(txt)));
printf("txt = %s, length = %d\n", txt.c_str(), strlen(txt.c_str()));
}

Cryptopp提供两种RSA的padding模式,分别是OAEP和PK1v15,padding模式跟安全性其实是紧密挂钩的,有兴趣的朋友可以去了解一下。

值得注意的是seed的大小决定了能够加密的文本长度,可以通过修改seed的大小,运行查看结果。seed越大,安全性越好,消耗的时间也越长,超过2048一般就能明显感觉到时间很长了,一般使用1024已经具有足够的安全性,反之seed太小的话FixedMaxPlaintextLength为0,将不能加密任何文本,测试数据参照下表:

Crypto++应用:非对称加密RSA

RSA的安全性依赖于大数分解,由于进行的都是大数计算,使得RSA最快的情况也比DES慢上好几倍,无论是软件还是硬件实现。速度一直是RSA的缺陷。一般来说只用于少量数据加密。

CryptoPP不只是提供加密解密算法,还提供很多易用的工具,如AutoSeededRandomPool, RandomPool, StringSource,StringSink,SocketSource,SocketSink,FileSource,FileSink等类,RSAES_OAEP_SHA_Decryptor, RSAES_OAEP_SHA_Encryptor等宏定义,具体使用方法请阅读源码。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,489
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290