對於學術網絡應用程序,我想在兩個虛擬機之間建立一個RSA密鑰交換。我正在使用Crypto ++生成RSA::PublicKey
,現在我必須在自定義第2層框架內發送它(數據包將使用libcrafter
製作)。發送數據包有效載荷中的公鑰
的事情是,我不知道的如何在網絡,如接收器將密鑰寫入,嗅探數據包,能夠重新構建,不知何故,RSA::PublicKey
。
我試圖把它生保存在一個字符串,但正如他們所說here的PublicKey
類包含的其他數據,則只需將原始鍵(即我不需要數據)。不過,我管理的成功,但在接待我不能簡單地重建公鑰...
難道是可能的,不知何故,來連接模量,質數和公開指數,以重建publicKey
在接待處?
發件人
這是我在發送端使用的代碼。這是重要的線路,但我的程序有其他功能,並且在這裏完全發佈它會太長)。
AutoSeededRandomPool rng;
RSA::PrivateKey privateKey;
privateKey.GenerateRandomWithKeySize(rng, 3072);
RSA::PublicKey publicKey(privateKey);
cout << ">> Key generated" <<endl;
/* Convert key to string then to const char* */
std::string publicKeyString;
publicKey.BEREncode(StringSink(publicKeyString).Ref());
const char * publicKeyChar = publicKeyString.c_str();
cout <<"key size : "<<publicKeyString.size()<< endl;
/* Send Packet */
Crafter::RawLayer type1("K");
Crafter::RawLayer key_send(publicKeyChar);
//Crafter::RawLayer key_send(publicKeyString.c_str(), publicKeyString.length());
Crafter::Packet packet_key (ether_header/type1/key_send);
packet_key.Send(iface);
接收機
這裏是我的嘗試恢復的關鍵。
/* Extract Payload */
PayloadLayer *payload_rcv = pack_recu.getLayerOfType<PayloadLayer>();
size_t payload_size = payload_rcv->getPayloadLen() ;
Crafter::byte *payload = payload_rcv->getPayload();
cout << ">> Public Key recieved"<<endl;
// Convert into RSA::PublicKey
stringstream ss;
for (int i=0; i< payload_size; i++)
ss << payload[i];
string payload_string = ss.str();
cout << "Payload Size: "<<payload_size<<endl;
cin.get();
StringSource stringSource(payload_string, true);
RSA::PublicKey publicKey2;
publicKey2.BERDecode(stringSource);
data->publicKey = publicKey2;
這裏是程序運行的結果:
terminate called after throwing an instance of 'CryptoPP::BERDecodeErr'
what(): BER decode error
我敢肯定,錯誤來自從字符串到publicKey...
的BERDecode
功能戰原本以爲恢復關鍵從轉換一個文件 ...
有沒有人有解決方案?我認爲把所有的元素分開來重建密鑰可能會更好,但我不知道該怎麼做...
周圍的網頁不屑一顧庫看起來像我最糟糕的C++的噩夢。它似乎像DEREncode/BERDecode應該是使用的方法。 –