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

1,非对称加密RSA:

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

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

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

2,使用CryptoPP实现RSA:

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

 1 #include "..cryptopp562
andpool.h"
 2 #include "..cryptopp562osrng.h"
 3 #include "..cryptopp562
sa.h"
 4 
 5 //待加密的字符串
 6 string message = "http://my.oschina.net/xlplbo/blog";
 7 printf("message = %s, length = %d
", message.c_str(), strlen(message.c_str()));
 8 
 9 /*
10 //自动生成随机数据
11 byte seed[600] = "";
12 AutoSeededRandomPool rnd;
13 rnd.GenerateBlock(seed, sizeof(seed));
14 printf("seed = %s
", (char *)seed, strlen((char *)seed));
15 
16 //生成加密的高质量伪随机字节播种池一体化后的熵
17 RandomPool randPool;
18 randPool.Put(seed, sizeof(seed));
19 */
20 
21 AutoSeededRandomPool rnd;
22 InvertibleRSAFunction params;
23 params.GenerateRandomWithKeySize(rnd, 1024);
24 
25 RSA::PrivateKey privateKey(params);
26 RSA::PublicKey publicKey(params);
27     
28 //使用OAEP模式
29 //RSAES_OAEP_SHA_Decryptor pri(randPool, sizeof(seed));
30 //RSAES_OAEP_SHA_Encryptor pub(pri);
31 RSAES_OAEP_SHA_Decryptor pri(privateKey);
32 RSAES_OAEP_SHA_Encryptor pub(publicKey);
33 printf("max plaintext Length = %d,%d
", pri.FixedMaxPlaintextLength(), pub.FixedMaxPlaintextLength());
34 if (pub.FixedMaxPlaintextLength() > message.length())
35 {//待加密文本不能大于最大加密长度
36     string chilper;
37     StringSource(message, true, new PK_EncryptorFilter(rnd, pub, new StringSink(chilper)));
38     printf("chilper = %s, length = %d
", chilper.c_str(), strlen(chilper.c_str()));
39         
40     string txt;
41     StringSource(chilper, true, new PK_DecryptorFilter(rnd, pri, new StringSink(txt)));
42     printf("txt = %s, length = %d
", txt.c_str(), strlen(txt.c_str()));
43 }
44 
45 //使用PKCS1v15模式
46 //RSAES_PKCS1v15_Decryptor pri1(randPool, sizeof(seed));
47 //RSAES_PKCS1v15_Encryptor pub1(pri1);
48 RSAES_PKCS1v15_Decryptor pri1(privateKey);
49 RSAES_PKCS1v15_Encryptor pub1(publicKey);
50 printf("max plaintext Length = %d,%d
", pri1.FixedMaxPlaintextLength(), pub1.FixedMaxPlaintextLength());
51 if (pub1.FixedMaxPlaintextLength() > message.length())
52 {//待加密文本不能大于最大加密长度
53     string chilper;
54     StringSource(message, true, new PK_EncryptorFilter(rnd, pub1, new StringSink(chilper)));
55     printf("chilper = %s, length = %d
", chilper.c_str(), strlen(chilper.c_str()));
56 
57     string txt;
58     StringSource(chilper, true, new PK_DecryptorFilter(rnd, pri1, new StringSink(txt)));
59     printf("txt = %s, length = %d
", txt.c_str(), strlen(txt.c_str()));
60 }

    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等宏定义,具体使用方法请阅读源码。

原文地址:https://www.cnblogs.com/borey/p/5625897.html