How to decrypt string data in C ++ using Crypto ++, where the original string is encrypted in Python using pyCrypto

I just encrypted the data string using pyCrypto, but I don’t know how to decrypt it in crypto ++. Can anyone help with a sample decryption code in C ++ using crypto ++? Here is my python code:

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)
+3
source share
2 answers

This code is from an example since 2005, but it should give you a good starting point:

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 all in decryptedText now
+1
source

the same approach as @Jon is a bit simplified

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 )
        )
);

The parameter trueon CryptoPP::StringSourcemeans "consumes all input"

, () ++, IV, . IV python, IV .

+2

Source: https://habr.com/ru/post/1783395/


All Articles