c++ - 将 key 传递给 Crypto++ 中的 AES 解密

标签 c++ encryption crypto++

我已经为这个问题搜索了很多,但没有找到任何解决方案。在我当前的项目中,我必须使用发送者接收者表单来加密图像。所以我必须在发送方部分生成一个 key 来加密文件,并且我必须使用相同的 key (作为参数传递给 main)来获取原始数据,以继续执行程序。

我将 key 保存在一个文本文件中:

   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 block 填充

这意味着我在解密过程中有不同的 key 。为什么会这样,是否有人可以帮助解决这个问题。

最佳答案

如果用于解密的 key 与用于加密的 key 不同,则会发生这种情况。

在解密过程中,在 PKCS#7 模式下,在解密最后一个 16 字节 block 之后,检查填充字节以了解消息的原始长度(不一定是 16 字节的倍数) :最后一个字节应该是0x01,或者最后两个字节应该等于0x02,或者最后三个字节应该等于0x03,...当解密 key 与加密 key 不同时,填充字节是未正确解密,这意味着解密时出现 PKCS#7 block 填充错误。

关于c++ - 将 key 传递给 Crypto++ 中的 AES 解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51413024/

相关文章:

c++ - 解密密文时出现InvalidCiphertext异常

c++ - 在 C++ 运算符中调用析构函数以释放内存的正确方法是什么?

c++ - 如何在if语句中定义变量和比较值?

c++ - 如何在 Python 中遍历 C++ 集?

c++ - 什么时候通过引用传递 C++ 中的重载运算符?

Mysql 的 MD5 不能很好地处理编码

用于在浏览器中签署表单数据的 Javascript 加密库

java - 如何使用SunMSCAPI key 解密RSA-OAEP

c++ - 在 Release模式下编译错误但在 Debug模式下不编译

c++ - 由于 CryptoPP 中 undefined reference ,CMake 抛出异常