Wrong AES decryption when trying to decrypt in another language

僤鯓⒐⒋嵵緔 提交于 2019-12-10 21:13:19

问题


When I try to Encrypt in C++ and decrypt in C# it gives me an error

The input data is not a complete block

But It doesn't make any sence to me, because if I try to decrypt the message in C++, the same language as I made the encryption it works fine.

So, some code from the C++ part:

int main(int argc, char* argv[]) {
 std::string szEncryptionKey = "Sixteen byte key";
 std::string szEncryptionIV = "Sixteen byte key";
 std::string str = "I do not like green eggs and ham I do not like them Sam-I-Am.";
 std::string str_encrypted = encrypt(str, szEncryptionKey, szEncryptionIV);
 std::string str_decrypted = decrypt(str_encrypted, szEncryptionKey, szEncryptionIV);

 std::cout << "str encrypted: " << str_encrypted << std::endl;
 std::cout << "str decrypted: " << str_decrypted << std::endl;

return 0;
}


std::string encrypt(const std::string& str_in, const std::string& key, const std::string& iv)
{

  std::string str_out;
  CryptoPP::CFB_Mode<CryptoPP::AES>::Encryption encryption((byte*)key.c_str(), key.length(), (byte*)iv.c_str());
  CryptoPP::StringSource encryptor(str_in, true,
    new CryptoPP::StreamTransformationFilter(encryption,
        new CryptoPP::Base64Encoder(
            new CryptoPP::StringSink(str_out),
            BlockPaddingSchemeDef::NO_PADDING
        )
     )
  );
 return str_out;
}

I'm taking the encrypted and base64 encoded string, and the KEY and IV to the C#:

        static void Main(string[] args)
    {
        byte[] bytes = Encoding.UTF8.GetBytes("Sixteen byte key");


            String msg2 = "cboiiqATFAU9KyK49BJMmkLLwiAyaP6yYjyM0oP09xbITLGaxRuLCsTYYzUKANydhO+JO7N00aVz0tmFTg==";
        byte[] msg = Convert.FromBase64String(msg2);
        DecryptStringFromBytes_Aes(msg, bytes, bytes);

static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
    {
        // Check arguments.
        if (cipherText == null || cipherText.Length <= 0)
            throw new ArgumentNullException("cipherText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("IV");

        // Declare the string used to hold
        // the decrypted text.

        using (Aes aesAlg = Aes.Create())
        {
            aesAlg.Mode = CipherMode.CFB;
            aesAlg.Padding = PaddingMode.None;
            aesAlg.Key = Key;
            aesAlg.IV = IV;

            // Check arguments.
            if (cipherText == null || cipherText.Length <= 0)
                throw new ArgumentNullException("plainText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("IV");
            byte[] encrypted = null;
            // Create an Aes object
            // with the specified key and IV.
            using (ICryptoTransform decrypt = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV))
            {
                byte[] decryptedText = decrypt.TransformFinalBlock(cipherText, 0, cipherText.Length);

               String s = Encoding.UTF8.GetString(decryptedText);

                Console.Write(s);
                return s;
            }
        }
    }

The error messages come at this line :

byte[] decryptedText = decrypt.TransformFinalBlock(cipherText, 0, cipherText.Length);

This line :

byte[] bytes = Encoding.UTF8.GetBytes("Sixteen byte key");

It's just a guess with the UTF8, but I have no idea if it's the right encoding.

Can anyone help me with this problem?


回答1:


The input data is not an exact multiple of the block size, 16-bytes for AES. msg2 when decoded is 61 bytes which is not a valid AES encrypted length, thus invalid and produces the error message: "The input data is not a complete block ".

AES is block based so if the data to be encrypted is not an exact multiple of the block size it must be padded. The general padding method used with AES is PKCS#7 sometimes named PKCS#5.

CFB mode requires padding, CFB8 mode does not.



来源:https://stackoverflow.com/questions/49175069/wrong-aes-decryption-when-trying-to-decrypt-in-another-language

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!