简体   繁体   English

Visual C#独立解密错误:要解密的数据长度无效

[英]Visual C# Independant Decryption Error: length of data to decrypt is invalid

In my visual C# encryption/decryption program I am trying to encrypt and decrypt independantly while I try to decrypt the previously encrypted data I get a length of data to decrypt is invalid error 在我的可视C#加密/解密程序中,我尝试独立加密和解密,同时尝试解密以前的加密数据,但我得到一段要解密的数据是无效错误

String filename;
        String filename2;

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK) {
                filename = ofd.FileName;
                textBox1.Text = filename;
                button2.Enabled = true;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            StreamReader sr = new StreamReader(filename);
            String toencrypt = sr.ReadToEnd();
            sr.Dispose();

            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            UTF8Encoding utf8 = new UTF8Encoding();
            TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
            tripleDES.Key = md5.ComputeHash(utf8.GetBytes(toencrypt));
            tripleDES.Mode = CipherMode.ECB;
            tripleDES.Padding = PaddingMode.PKCS7;
            ICryptoTransform trans = tripleDES.CreateEncryptor();
            String encrypted = BitConverter.ToString(trans.TransformFinalBlock(utf8.GetBytes(textBox2.Text), 0, utf8.GetBytes(textBox2.Text).Length));

            StreamWriter sw = new StreamWriter(filename);
            sw.Write(encrypted);
            sw.Dispose();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                filename2 = ofd.FileName;
                textBox3.Text = filename2;
                button4.Enabled = true;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            StreamReader sr = new StreamReader(filename2);
            String todecrypt = sr.ReadToEnd();
            sr.Dispose();

            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            UTF8Encoding utf8 = new UTF8Encoding();
            TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
            tripleDES.Key = md5.ComputeHash(utf8.GetBytes(textBox4.Text));
            tripleDES.Mode = CipherMode.ECB;
            tripleDES.Padding = PaddingMode.PKCS7;
            ICryptoTransform trans = tripleDES.CreateDecryptor();
            String decrypted = BitConverter.ToString(trans.TransformFinalBlock(utf8.GetBytes(todecrypt), 0, utf8.GetBytes(todecrypt).Length));

            StreamWriter sw = new StreamWriter(filename2);
            sw.Write(decrypted);
            sw.Dispose();
        }

Basically what i'm trying to do here is open up a txt file get the data inside to encrypt (which works fine) then decrypt (were I get the error) when it gives me the error I don't know what to change to fix this because the length of data is using the same key and padding and everything else that applies... 基本上我想在这里做的是打开一个txt文件,将数据加密到里面(可以正常工作),然后解密(得到错误),当它给我错误时我不知道要更改为什么解决此问题,因为数据长度使用相同的键和填充以及所有其他适用的方法...

First of all: 首先:

String encrypted = BitConverter.ToString(trans.TransformFinalBlock(utf8.GetBytes(textBox2.Text), 0, utf8.GetBytes(textBox2.Text).Length));

The string 'encrypted' is formatted as 字符串“ encrypted”的格式为

0C-4C-9B-01-00-0D-81-EC-D6-C5-2C-9A-EC-CE-08-95

But you try to decrypt the Utf8 bytes of the formatted string. 但是,您尝试解密格式化字符串的Utf8字节。 That fails; 那失败了; and most of the time due to an incorrect length. 而且大多数情况下是由于长度不正确。 You want to convert the string to a byte-array using BitConverter - NOT using Utf8. 您想使用BitConverter而不是Utf8将字符串转换为字节数组。

Apart from that: 1) Dont use ECB 2) do use an IV 3) using the hash of a file as key is questionable at best 除此之外:1)不要使用ECB 2)一定要使用IV 3)使用文件的哈希值,因为密钥最多是可疑的

Edit: formatting 编辑:格式化

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

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