2014-09-24 135 views
1

我試圖保存一個公共的DLIES鑰匙到內存,然後再讀它,但我不斷收到異常BER decode error。我使用ArraySinkArraySourcechar[64]緩衝區來傳輸兩個CryptoPP::DLIES<>::PublicKey之間的密鑰。我甚至確認公鑰是好的。我錯過了什麼?從加密鑰匙從內存加密++

下面是無法正常工作的完整示例。如何修改它以便正確加載密鑰?

#include <iostream> 
#include <gfpcrypt.h> 
#include <filters.h> 
#include <osrng.h> 

int main() { 
    try { 
     CryptoPP::DefaultAutoSeededRNG rng; 

     CryptoPP::DLIES<>::PrivateKey privateKey; 
     privateKey.GenerateRandomWithKeySize(rng, 10); 

     CryptoPP::DLIES<>::PublicKey publicKey; 
     privateKey.MakePublicKey(publicKey); 

     if (!publicKey.Validate(rng, 3)) { 
      std::cout << "Something wrong with public key." << std::endl; 
      return 1; 
     } 

     byte buf[64]; 
     CryptoPP::ArraySink sink(buf, 64); 
     publicKey.Save(sink); 

     CryptoPP::ArraySource source((const char *)buf, sink.TotalPutLength()); 
     CryptoPP::DLIES<>::PublicKey pk; 
     pk.Load(source); 
    } catch (CryptoPP::Exception &ex) { 
     std::cout << ex.what() << std::endl; 
     return 1; 
    } 

    return 0; 
} 
+1

我已經看到了這個問題(或它的變體)頻繁發生。另外,'ArraySource'和'ArraySink'是一種新的。我認爲他們只適用於5.6及以上版本。如果您使用5.5或更低版本,則需要使用'StringSource'和'StringSink'。和使用'DLIES'的+1。我*認爲*這是我見過它使用的唯一時間。 – jww 2014-09-24 14:31:27

+1

我使用了DLIES,因爲我相信基於離散對數的密碼術比RSA更好(RSA,LUC)。此外,根據Crypto ++網站上的基準測試,它仍然比我真正喜歡使用的基於橢圓曲線的速度快得多。 – Xilexio 2014-09-24 20:32:41

+1

這是否使你的Crypto ++ wiki:[橢圓曲線集成加密方案](http://www.cryptopp.com/wiki/Elliptic_Curve_Integrated_Encryption_Scheme)?它是爲'ECIES'編寫的,但適用於'DLIES' - 只需換掉'DLIES'裝備的'ECIES'裝備。並且一定要在頁面底部看到有關Bouncy Castle的註釋。 – jww 2014-09-25 06:44:28

回答

1

的問題,而不是設置ArraySource構造pumpAll=true第三個參數撒謊。添加後,它工作。另一個工作解決方案是使用ByteQueue代替。爲了完整起見,我粘貼下面的兩個工作示例。

ArraySource版本:

#include <iostream> 
#include <gfpcrypt.h> 
#include <filters.h> 
#include <osrng.h> 

int main() { 
    try { 
     CryptoPP::DefaultAutoSeededRNG rng; 

     CryptoPP::DLIES<>::PrivateKey privateKey; 
     privateKey.GenerateRandomWithKeySize(rng, 10); 

     CryptoPP::DLIES<>::PublicKey publicKey; 
     privateKey.MakePublicKey(publicKey); 

     if (!publicKey.Validate(rng, 3)) { 
      std::cout << "Something wrong with sent public key." << std::endl; 
      return 1; 
     } 

     byte buf[64]; 
     CryptoPP::ArraySink sink(buf, 64); 
     publicKey.Save(sink); 

     CryptoPP::ArraySource source(buf, sink.TotalPutLength(), true); 
     CryptoPP::DLIES<>::PublicKey pk; 
     pk.Load(source); 

     if (!pk.Validate(rng, 3)) { 
      std::cout << "Something wrong with received public key." << std::endl; 
      return 1; 
     } 
    } catch (CryptoPP::Exception &ex) { 
     std::cout << ex.what() << std::endl; 
     return 1; 
    } 

    return 0; 
} 

ByteQueue版本(我發現到底更方便):

#include <iostream> 
#include <gfpcrypt.h> 
#include <filters.h> 
#include <osrng.h> 

int main() { 
    try { 
     CryptoPP::DefaultAutoSeededRNG rng; 

     CryptoPP::DLIES<>::PrivateKey privateKey; 
     privateKey.GenerateRandomWithKeySize(rng, 10); 

     CryptoPP::DLIES<>::PublicKey publicKey; 
     privateKey.MakePublicKey(publicKey); 

     if (!publicKey.Validate(rng, 3)) { 
      std::cout << "Something wrong with sent public key." << std::endl; 
      return 1; 
     } 

     CryptoPP::ByteQueue queue; 
     publicKey.Save(queue); 
     CryptoPP::lword size = queue.TotalBytesRetrievable(); 

     byte buf[64]; 
     queue.Get(buf, size); 

     CryptoPP::ByteQueue queue2; 
     queue2.Put(buf, size); 
     CryptoPP::DLIES<>::PublicKey pk; 
     pk.Load(queue2); 

     if (!pk.Validate(rng, 3)) { 
      std::cout << "Something wrong with received public key." << std::endl; 
      return 1; 
     } 
    } catch (CryptoPP::Exception &ex) { 
     std::cout << ex.what() << std::endl; 
     return 1; 
    } 

    return 0; 
} 
+1

*「問題在於未設置pumpAll = true ArraySource的第三個參數...」* - 該問題也出現在StringSource中。因爲'bool''pumpAll = true'被強制轉換爲整數參數(即使使用'-Wall -Wextra'也沒有警告)。當'bool'被強制到附加的'BufferedTransformation'中時,會發生其他對象(即'NULL')。 – jww 2014-09-24 18:00:08