我正在嘗試執行以下操作:在python腳本中,我使用pycrypto lib加密一些文本。然後將其保存到文件中。然後加載該文件並解密加密的文本使用我在Python中使用的相同的密鑰。它在stfDecryptor.MessageEnd()失敗。與錯誤:Crypto ++:在Python中加密,在C++中解密
「CryptoCPP :: InvalidCiphertext內存位置[一些內存]
這裏是我的代碼:
的Python:
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()
CPP:
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.
從我讀到的這些內容到終結點應該像pycrypto一樣只是CryptoCPP lib的封裝。可能我錯過了CPP方面的填充?
UPDATE:
好吧,我發現改變填充方案:
CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink(decryptedtext) ,BlockPaddingSchemeDef::NO_PADDING);
上CPP side.But解碼字符串包含填充字符字符串解碼。 因此,如果原始字符串是 「aaaaaaaaaaaaaaaaa」
解碼後的字符串看起來是這樣的:
「aaaaaaaaaaaaaaaaa {{{{{{{{{{{{{{{」
15個字節是添加到32字節的填充。
爲什麼Crypto ++不會刪除那些解密?
嗯,現在我需要弄清楚如何做到這一點的Python.I注意到,在Python的lib許多方法版本是missing.Like StreamTransformationFilter例如 – 2014-09-04 05:53:23
呀!這是它! – 2014-09-04 07:37:17
@MichaelIvanov,你可以發佈你的最終代碼?編輯後的代碼運行但不正確解密 – gregoiregentil 2017-04-18 17:29:51