簡體   English   中英

將密鑰傳遞給 Crypto++ 中的 AES 解密

[英]Passing a key to AES decryption in Crypto++

我已經為這個問題搜索了很多,但沒有找到任何解決方案。 在我當前的項目中,我必須使用發送者接收者表單加密圖像。 所以我必須在發件人部分生成一個密鑰來加密文件,我必須使用相同的密鑰(作為參數傳遞給 main)來獲取原始數據,以繼續程序執行。

我將密鑰保存在文本文件中:

   void GetKeyAndIv() {
// Initialize the key and IV
prng.GenerateBlock( key, key.size() );
prng.GenerateBlock(iv, iv.size());
};

    /*********************Begin of the Function***********************/
//Function encrypt a file (original file) and store the result in another file (encrypted_file)
void Encrypt(std::string original_file, std::string encrypted_file_hex,string encrypted_file,string binary) {

    ofstream out;
    out.open("Key.txt");
    out.clear();
    out<<"key = "<< key<<endl;
    out<<"iv = "<< iv<<endl;

    string cipher, encoded;

    //Getting the encryptor ready
    CBC_Mode< CryptoPP::AES >::Encryption e;
    e.SetKeyWithIV( key, key.size(), iv );


  try
  {

        ifstream infile(original_file.c_str(), ios::binary);
        ifstream::pos_type size = infile.seekg(0, std::ios_base::end).tellg();
        infile.seekg(0, std::ios_base::beg);

        //read the original file and print it
        string temp;
        temp.resize(size);
        infile.read((char*)temp.data(), temp.size());
        infile.close();


         // Encryption
CryptoPP::StringSource ss( temp, true,
   new CryptoPP::StreamTransformationFilter( e,
      new CryptoPP::StringSink( cipher )//,
      //CryptoPP::BlockPaddingSchemeDef::NO_PADDING
   ) // StreamTransformationFilter
); // StringSource




 std::ofstream outfile1(encrypted_file.c_str(),ios::out | ios::binary);
  outfile1.write(cipher.c_str() , cipher.size());


  }
catch( const CryptoPP::Exception& e )
{

    cout <<"Encryption Error:\n" <<e.what() << endl;
    system("pause");
    exit(1);
}

然后我使用以下代碼將其傳遞給客戶端:

int main(int argc, char* argv[])
{
.....
        string s1=argv[7];
        SecByteBlock b1(reinterpret_cast<const byte*>(&s1[0]), s1.size());
        string s2=argv[8];
        SecByteBlock iv1(reinterpret_cast<const byte*>(&s2[0]), s2.size());
.....
}

嘗試使用以下代碼解密文件時出錯

   void Decrypt(std::string encrypted_file,SecByteBlock key,SecByteBlock iv,string decrypted_file) {


        string recovered;
     try
     {
          // Read the encrypted file contents to a string as binary data.
      std::ifstream infile(encrypted_file.c_str(), std::ios::binary);
      const std::string cipher_text((std::istreambuf_iterator<char>(infile)),
                                     std::istreambuf_iterator<char>());
      infile.close();


      CBC_Mode< CryptoPP::AES >::Decryption d;
        d.SetKeyWithIV( key, key.size(), iv );

解密錯誤: StreamTransformationFilter:找到無效的 PKCS #7 塊填充

這意味着我在解密過程中有不同的密鑰。 為什么會發生這種情況,以及是否有人可以幫助解決此問題。

如果用於解密的密鑰與用於加密的密鑰不同,就會發生這種情況。

在解密過程中,在 PKCS#7 模式下,在解密最后一個 16 字節的塊后,會檢查填充字節以了解消息的原始長度(不一定是 16 字節的倍數):最后一個byte 應為 0x01,或最后兩個字節應等於 0x02,或最后三個字節應等於 0x03,...當解密密鑰與加密密鑰不同時,填充字節未正確解密這意味着解密時出現 PKCS#7 塊填充錯誤。

我將 CBC_Mode 改為另一種模式,ODB_Mode 對我有用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM