3

我刚刚用 pyCrypto 轻松加密了一个数据字符串,但不知道如何在 crypto++ 中解密它。任何人都可以使用crypto++帮助提供C++中的示例解密代码吗?这是我的python代码:

key = '0123456789abcdef' 
data = "aaaaaaaaaaaaaaaa" 
iv = ''.join(chr(random.randint(0, 0xFF)) for i in range(16)) 
encryptor = AES.new(key, AES.MODE_CBC, iv) 
enc = encryptor.encrypt(data)
4

2 回答 2

2

与@Jon 相同的方法,有点简化

std::string ciphertext = "..."; // what Python encryption produces
std::string decryptedtext;

byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];

// populate key and iv with the correct values

CryptoPP::CBC_Mode< CryptoPP::AES >::Decryption decryptor;
decryptor.SetKeyWithIV(key, sizeof(key), iv);


CryptoPP::StringSource(ciphertext, true,
        new CryptoPP::StreamTransformationFilter( decryptor,
            new CryptoPP::StringSink( decryptedtext )
        )
);

true参数 to表示“CryptoPP::StringSource消耗整个输入”

请注意,(显然)您需要 C++ 解密器才能知道用于加密的 IV。由于您在 python 中生成随机 IV,因此流行的技术是将 IV 预先添加到加密文本中。

于 2011-01-04T14:02:16.267 回答
1

此代码来自 2005 年的示例,但它应该为您提供一个很好的起点:

std::string ciphertext = "..."; // what Python encryption produces
std::string decryptedtext;

byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];

// populate key and iv with the correct values

CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, iv );

CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedtext ) );
stfDecryptor.Put( reinterpret_cast<const unsigned char*>( ciphertext.c_str() ), ciphertext.size() );
stfDecryptor.MessageEnd();

// it's all in decryptedText now
于 2011-01-04T13:23:20.490 回答