Crypto++應用:非對稱加密RSA


1,非對稱加密RSA:

    (1)乙方生成兩把密鑰(公鑰和私鑰)。公鑰是公開的,任何人都可以獲得,私鑰則是保密的。

    (2)甲方獲取乙方的公鑰,然后用它對信息加密。

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

2,使用CryptoPP實現RSA:

    CryptoPP是一套非常完整的加密解密開源解決方案,如何使用這里就不多說了,請自行Google。

 1 #include "..\cryptopp562\randpool.h"
 2 #include "..\cryptopp562\osrng.h"
 3 #include "..\cryptopp562\rsa.h"
 4 
 5 //待加密的字符串
 6 string message = "http://my.oschina.net/xlplbo/blog";
 7 printf("message = %s, length = %d\n", 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\n", (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\n", 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\n", 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\n", 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\n", 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\n", 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\n", 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等宏定義,具體使用方法請閱讀源碼。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM