1. 程式人生 > >Crypto++應用:非對稱加密RSA

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等巨集定義,具體使用方法請閱讀原始碼。