简体   繁体   English

AES 256解密错误

[英]AES 256 decryption error

I'm trying to decrypt an AES256 bit but it gives me this error "Length of the data to decrypt is invalid." 我正在尝试解密AES256位,但是它给了我这个错误“要解密的数据长度无效”。 On line Plain_Text = Stream_Read.ReadToEnd(); 在线Plain_Text = Stream_Read.ReadToEnd(); . My encryption method works but the decrypt doesn't. 我的加密方法有效,但解密无效。 Could someone help me? 有人可以帮我吗? Thankyou. 谢谢。

 private static string Decrypt(string stringCypher_Text, string stringKey, string stringIV)
    {
        //Hashes, and converts key to bytes
        //hash
        //convert
        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
        Byte[] Key = encoding.GetBytes(stringKey);

        //converts string IV to bytes
        Byte[] IV = encoding.GetBytes(stringIV);

        //converts cypher string to bytes
        Byte[] Cypher_Text = encoding.GetBytes(stringCypher_Text);

        RijndaelManaged Crypto = null;
        MemoryStream MemStream = null;
        ICryptoTransform Decryptor = null;
        CryptoStream Crypto_Stream = null;
        StreamReader Stream_Read = null;
        string Plain_Text;

        try
        {
            Crypto = new RijndaelManaged();
            Crypto.Key = Key;
            Crypto.IV = IV;

            MemStream = new MemoryStream(Cypher_Text);

            //Create Decryptor make sure if you are decrypting that this is here and you did not copy paste encryptor.
            Decryptor = Crypto.CreateDecryptor(Crypto.Key, Crypto.IV);

            //This is different from the encryption look at the mode make sure you are reading from the stream.
            Crypto_Stream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read);

            //I used the stream reader here because the ReadToEnd method is easy and because it return a string, also easy.
            Stream_Read = new StreamReader(Crypto_Stream);
            Plain_Text = Stream_Read.ReadToEnd();

        }
        finally
        {
            if (Crypto != null)
                Crypto.Clear();

            MemStream.Flush();
            MemStream.Close();

        }
        return Plain_Text;

    }

The problem is in your Encrypt method or more exactly in the interaction between Encrypt and Decrypt. 问题出在您的加密方法上,或更确切地说是在加密和解密之间的交互中。 You really don't want to use UTF8Encoding or ANY encoding for binary data . 您确实不想对二进制数据使用UTF8Encoding或ANY编码 Text encodings are used to turn text into binary data and back again. 文本编码用于将文本转换为二进制数据并再次返回。 Encrypted text is actually purely binary data. 加密的文本实际上是纯二进制数据。 What I would suggest is using Base64Strings. 我建议使用Base64Strings。

In your Encrypt method you most likely have a MemoryStream that you are returning encoded characters from. 在您的Encrypt方法中,您很可能具有从其返回编码字符的MemoryStream。 Instead return a Base64String like this... 而是像这样返回Base64String ...

string cipherText = Convert.ToBase64String(memoryStream.ToArray());
return cipherText;

Then in your Decrypt you take that cipherText and turn it back into a Byte[] like this... 然后在您的Decrypt中,采用该cipherText并将其变回Byte [],如下所示...

Byte[] Cypher_Text = Convert.FromBase64String(stringCypher_Text);

You should also pass your key and initialization vector as Base64Strings as well and after that your code should be good to go. 您还应该将密钥和初始化向量也作为Base64Strings传递,此后代码就可以了。

Try changing Plain_Text = Steam_Read.ReadToEnd(); 尝试更改Plain_Text = Steam_Read.ReadToEnd(); to

  byte[] plainText = new byte[Plain_Text.Length];
  int dByte = Stream_Read.Read(plainText, 0, plainText.Length);
  string decryptedText = Encoding.UTF8.GetString(plainText, 0, dByte);
  return descryptedText;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM