【问题标题】:Length of the data to decrypt is invalid when decrypting解密时要解密的数据长度无效
【发布时间】:2014-05-05 05:31:26
【问题描述】:

我需要加密一张图片,返回加密数据的字符串,然后解密

这是我的加密代码:

string plainText = ASCIIEncoding.ASCII.GetString(Imagebytes);
byte[] encrypted;
byte[] key = Encoding.UTF8.GetBytes("M02cnQ51Ji97vwT4");;
// Create an AesCryptoServiceProvider object 
using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
{
    aesAlg.Key = key;
    aesAlg.BlockSize = 128;

    // Create a decrytor to perform the stream transform.
    ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

    // Create the streams used for encryption. 
    using (MemoryStream msEncrypt = new MemoryStream())
    {
        using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
        {
            using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
            {

                //Write all data to the stream.
                swEncrypt.Write(plainText);
            }
            encrypted = msEncrypt.ToArray();

        }
    }
}

using (var aesAlg = new AesManaged())
{


    aesAlg.Key = new UTF8Encoding().GetBytes("M02cnQ51Ji97vwT4");
    aesAlg.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

    return UTF8Encoding.UTF8.GetString(encryptor.TransformFinalBlock(Imagebytes, 0, Imagebytes.Length)).Length;

}

我的解密工作正常(因为我可以完美接收图像/视频)

代码如下:

const  string BLOB_KEY = "TTAyY25RNTFKaTk3dndUNA==";
using (RijndaelManaged rm = new RijndaelManaged())
{
    rm.Mode = CipherMode.ECB;
    rm.Key = Convert.FromBase64String(BLOB_KEY);
    rm.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    rm.Padding = PaddingMode.Zeros;
    using (MemoryStream ms = new MemoryStream())
    {
        using (CryptoStream cs = new CryptoStream(ms, rm.CreateDecryptor(),
                    CryptoStreamMode.Write))
        {
            cs.Write(image, 0, image.Length);
            return ms.ToArray();
        }
    }
}

我的加密代码有什么问题?

【问题讨论】:

  • 我不确定您具体要问什么。您是否解密了您自己加密的数据并且您没有得到与以前相同的明文?您在谈论图像和视频 - 它们与您的问题有什么关系?

标签: c# image encryption


【解决方案1】:

你最开始的代码行已经错了:

string plainText = ASCIIEncoding.ASCII.GetString(Imagebytes);

图像不是字符串,明文也不一定是字符串。两者都由字节组成。所以你也不应该使用StreamWriter,只是一个普通的流。

转换过程中可能会丢失数据。

此外,您正在写入解密流,并且一侧使用 ECB 模式,另一侧使用 CBC 模式。

我强烈建议您阅读材料并重新开始。

【讨论】:

    【解决方案2】:

    谢谢,我把代码改成了:

    var aesAlg = new AesManaged
            {
                KeySize = 128,
                Key = key,
                BlockSize = 128,
                Mode = CipherMode.ECB,
                Padding = PaddingMode.Zeros,
                IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
            };
    
            ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
            return encryptor.TransformFinalBlock(Imagebytes, 0, Imagebytes.Length);
    

    而且效果很好!

    【讨论】:

      猜你喜欢
      • 2020-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-22
      • 1970-01-01
      • 1970-01-01
      • 2011-12-05
      相关资源
      最近更新 更多