加密++;:用Python加密,用C+解密+;
我正在尝试以下操作:在python脚本中,我使用pycrypto lib对一些文本进行加密。然后将其保存到文件中。然后加载该文件,并使用python中使用的相同密钥对加密文本进行解码。在stfDecryptor.MessageEnd()处失败;错误如下: “CryptoCPP::内存位置[某些内存]处的无效密文” 这是我的密码: Python:加密++;:用Python加密,用C+解密+;,python,c++,encryption,Python,C++,Encryption,我正在尝试以下操作:在python脚本中,我使用pycrypto lib对一些文本进行加密。然后将其保存到文件中。然后加载该文件,并使用python中使用的相同密钥对加密文本进行解码。在stfDecryptor.MessageEnd()处失败;错误如下: “CryptoCPP::内存位置[某些内存]处的无效密文” 这是我的密码: Python: from Crypto.Cipher import AES BLOCK_SIZE = 16 PADDING = '{' # one-liner to
from Crypto.Cipher import AES
BLOCK_SIZE = 16
PADDING = '{'
# one-liner to sufficiently pad the text to be encrypted
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
EncodeAES = lambda c, s: c.encrypt(pad(s))
secret = 'MyKey123456789ab'
# create a cipher object using the random secret
cipher = AES.new(secret)
# encode a string
encoded = EncodeAES(cipher, textIn)
#save to file
fileOut = open("enc_shader.vert","w")
fileOut.write(encoded)
fileOut.close()
std::string key = "MyKey123456789ab";
std::string iv = "aaaaaaaaaaaaaaaa";
std::ifstream fileIn("enc_shader.vert");
std::stringstream buffer;
buffer << fileIn.rdbuf();
std::string ciphertext1 = buffer.str();
CryptoPP::AES::Decryption aesDecryption((byte*)key.c_str(), CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, (byte*)iv.c_str() );
CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedtext ) );
stfDecryptor.Put( reinterpret_cast<const unsigned char*>( ciphertext1.c_str() ), ciphertext1.size() );
stfDecryptor.MessageEnd();//fails here.
CPP:
from Crypto.Cipher import AES
BLOCK_SIZE = 16
PADDING = '{'
# one-liner to sufficiently pad the text to be encrypted
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
EncodeAES = lambda c, s: c.encrypt(pad(s))
secret = 'MyKey123456789ab'
# create a cipher object using the random secret
cipher = AES.new(secret)
# encode a string
encoded = EncodeAES(cipher, textIn)
#save to file
fileOut = open("enc_shader.vert","w")
fileOut.write(encoded)
fileOut.close()
std::string key = "MyKey123456789ab";
std::string iv = "aaaaaaaaaaaaaaaa";
std::ifstream fileIn("enc_shader.vert");
std::stringstream buffer;
buffer << fileIn.rdbuf();
std::string ciphertext1 = buffer.str();
CryptoPP::AES::Decryption aesDecryption((byte*)key.c_str(), CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, (byte*)iv.c_str() );
CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedtext ) );
stfDecryptor.Put( reinterpret_cast<const unsigned char*>( ciphertext1.c_str() ), ciphertext1.size() );
stfDecryptor.MessageEnd();//fails here.
解码CPP端的字符串。但解码的字符串包含填充字符。
所以如果原始字符串是“aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
解码的字符串如下所示:
“aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
将15个字节添加到pad中,以增加到32个字节
为什么Crypto++不在解密时删除它们?您的Python加密代码手动添加'{个字符填充到块大小。这不是定义的填充模式,因此Crypto++代码无法使用集成的填充方案删除填充。换句话说,您应该使用
NO_padding
解密,然后自己删除填充
但是最好让Python代码使用PKCS#7填充,这样您就可以在Crypto++中使用
PKCS#U填充
作为选项。Hmm,现在我需要弄清楚如何在Python中实现它。我注意到在Python版本的库中缺少许多方法。比如StreamTransformationFilterexample@MichaelIvanov,你能发布你的最终代码吗?已编辑的代码运行,但无法正确解密