Search code examples
c++vb.netencryptioncryptographycrypto++

Decryption results in gibberish with Rijndael and 256 block size


so i have some old messages encrypted with this old code:

'the ecryption algorithm with specific settings
        Dim myRijndael As New RijndaelManaged
        myRijndael.Padding = PaddingMode.Zeros
        myRijndael.Mode = CipherMode.CBC
        myRijndael.KeySize = 256
        myRijndael.BlockSize = 256

        'declared byte arrays for the message string and the key and IV for the encryption algorithm
        Dim encrypted() As Byte
        Dim toEncrypt() As Byte
        Dim key() As Byte
        Dim IV() As Byte

        'populating the arryas with the needed bytes for the encryption algorithm
        key = System.Text.Encoding.ASCII.GetBytes(prm_key)
        IV = System.Text.Encoding.ASCII.GetBytes(prm_iv)

        'the actual instance of the ecryption algorithm
        Dim encryptor As ICryptoTransform = myRijndael.CreateEncryptor(key, IV)

        'streams for the encrypted byte array
        Dim msEncrypt As New MemoryStream()
        Dim csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)

        toEncrypt = System.Text.Encoding.ASCII.GetBytes(text_to_encrypt)

        csEncrypt.Write(toEncrypt, 0, toEncrypt.Length)

        'make sure we have all the blocks
        csEncrypt.FlushFinalBlock()

        'turn encrypted stream into byte array
        encrypted = msEncrypt.ToArray()
        'return a base 64 string so we can upload it to the server
        Return (Convert.ToBase64String(encrypted))

and i am trying to decrypt it with Crypto++

this is the code that i came up with:

std::string coded = "pCyWPA5Enc3F0NAkowrt206brSfMrOgKMTXI1pKhCUY=";
//std::string coded = "F9uvtbK3Ue67Gbe9si5yvDn8a50bYnTovjfWali+Xjo=";

std::string coded2;
std::string ciphertext;
std::string decryptedtext;


CryptoPP::StringSource sss(coded, true,
   new CryptoPP::Base64Decoder(
       new CryptoPP::StringSink(ciphertext)
   ) // Base64Decoder
); // StringSource
CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::MAX_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, iv );

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

//
// Dump Decrypted Text
//
std::cout << "Decrypted Text: " << std::endl;
std::cout << decryptedtext;
std::cout << std::endl << std::endl;

but all i get in return is gibberish. The padding modes are set, the key is correct. I'm out of ideas.

The commented out "coded" string actually get decrypted, but it was encrypted with c++ and crypto++. The text is identical. so why are the Base64 encrypted stings different?

Im thinking that maybe since VB code encrypted bytes in ASCII that that might be the issue. but i don't know how to make crypto++ use ASCII encoding.

Can someone please point out the issue here?


Solution

  • AES uses a 128 bit block size. The VB code uses Rijndael with 256 bit block size.