2014-03-03 37 views
0

我想使用RSA加密許可我的軟件。我的軟件有幾個可執行文件,並且我打算在每個執行文件執行之前檢查一個通用許可文件的簽名。我的目標不是要規避許可保護,而是要讓它變得非常困難。我知道沒有人能讓它變得不可能。crypto ++將密鑰保存到字節隊列並返回到密鑰

可執行文件當前在Windows環境中運行,但它們只能在Linux環境中發佈。

我現在的計劃是把公鑰放在每個可執行文件中進行簽名驗證。這些程序已經有一個「安全」加密區域,用於放置密鑰。

我對這篇文章的問題是,我的實現方法是否有意義?有另外一種選擇嗎?將公鑰存放在單獨文件中的替代方法將允許黑客替換該文件並使用其自己的許可證文件和簽名。這似乎並不安全。

此外,我一直在閱讀crypto ++文檔並運行示例代碼來嘗試完成此任務。我無法獲得任何將密鑰放入非文件接收器並再次返回的代碼。所有示例都會寫入文件並讀取。我需要能夠保存和加載字符串或字節隊列。下面是一個簡單的嘗試,但是當它運行時,當r2.BERDecodePrivateKey()執行時出現此錯誤:

MyRSA.exe中的0x7630c41f未處理的異常:Microsoft C++異常:內存位置的CryptoPP :: BERDecodeErr 0x002ef32c ..

#include <osrng.h> 

#include "rsa.h" 
using CryptoPP::RSA; 

#include <queue.h> 
using CryptoPP::ByteQueue; 

int myCode_key2ByteQueueToKey(void) 
{ 
    //////////////////////////////////////////////////////////////////////// 
    // Generate the keys 
    AutoSeededRandomPool rnd; 

    CryptoPP::RSA::PrivateKey r1; 
    r1.GenerateRandomWithKeySize(rnd, 2048); 

    CryptoPP::RSA::PublicKey rsaPublic(r1); 

    //////////////////////////////////////////////////////////////////////// 
    // Put the 'inner' part of the key into a ByteQueue - whatever that is. 
    ByteQueue queue; 
    r1.DEREncodePublicKey(queue); 

    //////////////////////////////////////////////////////////////////////// 
    // Copy the byte queued inner key into another key 
    CryptoPP::RSA::PrivateKey r2; 
    r2.BERDecodePrivateKey(queue, false /*optParams*/, queue.MaxRetrievable()); 

    //////////////////////////////////////////////////////////////////////// 
    // Validate the key made the trip in and out of a byte queue ok. 
    if(!r1.Validate(rnd, 3)) printf("Validation of oringal key failed.\n"); 
    if(!r2.Validate(rnd, 3)) printf("Validation of reloaded key failed.\n"); 


    if(r1.GetModulus() != r2.GetModulus() || 
     r1.GetPublicExponent() != r2.GetPublicExponent() || 
     r1.GetPrivateExponent() != r2.GetPrivateExponent()) 
    { 
     printf("Key didn't survive round trip in and out of a byte queue."); 
    } 

    return 0; 
} 

我沒有上面的代碼修復。有些東西我對圖書館不瞭解,結果有些東西不見了,但我得走一步。

我以爲我會發布我找到的替代方案。它是Crypto ++ wiki上的一個例子,它將密鑰放入字符串(而不是文件)並重新放回。往返旅程顯示工作。

http://www.cryptopp.com/wiki/BERDecode

而不是

CryptoPP::RSA::PrivateKey 

它採用

CryptoPP::RSAES_OAEP_SHA_Decryptor 

,同樣的公共密鑰。這允許使用不適用於PrivateKey類的成員函數AccessKey()

如果任何人有原始代碼的修復,我敦促您請張貼它,因爲它會幫助我更好地瞭解這個庫。

+0

嗯,哪裏了'r2'私鑰從何而來?稀薄的空氣? –

+0

由行聲明:CryptoPP :: RSA :: PrivateKey r2;在下一行中,我試圖將它的值賦給字節隊列的值。 – user3341576

+0

把它扔到try catch中,看看例外情況如何。 –

回答

1

所以基本上你這樣做:

  1. 生成2048位私鑰
  2. 編碼DER格式從你的私鑰的指數一公共關鍵看你bytequeue
  3. 嘗試對其進行解碼作爲私人密鑰從字節隊列< - 這裏是錯誤
  4. 驗證密鑰..

您不能將公鑰解碼爲私鑰,某些編碼標誌不同。


至於CryptoPP::RSAES_OAEP_SHA_Decryptor

是用它來生成密鑰,因爲它知道安全素數的一個很好的做法。

這對使用通用解密任務來說也更簡單,因爲它包含了您在一個對象中需要的所有內容。

http://www.cryptopp.com/wiki/Keys_and_Formats#High_Level_Objects

希望這有助於即使這是一個遲到的回答;)