다음 작업을 수행하려고합니다. python 스크립트에서 일부 텍스트를 암호화하기 위해 pycrypto lib를 사용합니다. 그런 다음 파일에 저장합니다. 그런 다음 파일을로드하고 암호화 된 텍스트를 디코딩합니다. 내가 파이썬에서 사용한 동일한 키를 사용한다. 이것은 stfDecryptor.MessageEnd()에서 실패한다. 오류가있는 :Crypto ++ : Python으로 암호화하고 C++에서 암호 해독
"CryptoCPP :: 메모리 위치에서 InvalidCiphertext [일부 메모리 여기서
내 코드입니다 :파이썬 :
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.Lay의 래퍼로 작동해야 CPP 측에서 패딩을 놓칠 수 있습니까?
는UPDATE :
CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink(decryptedtext) ,BlockPaddingSchemeDef::NO_PADDING);
디코딩 된 문자열이 패딩 문자를 포함 CPP side.But에 문자열을 디코딩 :
좋아, 나는 패딩 방식을 변경하는 것을 발견했다. 원래 문자열이었다 그렇다면 "aaaaaaaaaaaaaaaaa"
디코딩 된 문자열은 다음과 같습니다
"aaaaaaaaaaaaaaaaa {{{{{{{{{{{{{{{"
15 바이트했다 패드에 32 바이트가 추가되었습니다.
왜 Crypto ++가 암호 해독시 암호를 제거하지 않는가?
흠, 이제 파이썬에서 어떻게해야하는지 알아야합니다. 파이썬 버전의 lib에서 많은 메소드가 누락 된 것으로 나타났습니다. 예를 들어 StreamTransformationFilter처럼 –
예! 그랬습니다! –
@MichaelIvanov, 최종 코드를 게시 할 수 있습니까? 편집 된 코드는 실행되지만 제대로 해독하지 않습니다. – gregoiregentil