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

∥☆過路亽.° 提交于 2020-04-12 17:59:43

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, 1024);

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,将不能加密任何文本,测试数据参照下表:

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

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


参考链接:

    http://www.cryptopp.com/

    http://www.ruanyifeng.com/blog/2013/06/rsa_algorithm_part_one.html

    http://www.ruanyifeng.com/blog/2013/07/rsa_algorithm_part_two.html


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!