尝试用另一种语言解密时错误的AES解密 [英] Wrong AES decryption when trying to decrypt in another language

查看:175
本文介绍了尝试用另一种语言解密时错误的AES解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在C ++中加密并在C#中解密时,它给我一个错误

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


输入数据不是完整的块

The input data is not a complete block

但这对我没有任何意义,因为如果我尝试用C ++解密消息,则与我使用的语言相同

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.

因此,C ++部分中的一些代码:

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

我正在使用经过加密和base64编码的字符串,以及KEY和IV到C#:

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

错误消息来自此行:

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

此行:

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

这只是对UTF8的猜测,但我不知道它是否是正确的编码。

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?

推荐答案

输入数据是不是块大小的精确倍数,AES为16字节。 msg2 解码后为61个字节,不是有效的AES加密长度,因此无效,并产生错误消息:输入数据不是完整的块

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是基于块的,因此,如果要加密的数据不是块大小的精确倍数,则必须对其进行填充。 AES使用的通用填充方法是PKCS#7,有时也称为PKCS#5。

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模式需要填充,而CFB8模式则不需要填充。

CFB mode requires padding, CFB8 mode does not.

这篇关于尝试用另一种语言解密时错误的AES解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆