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

时间:2014-06-05 21:55:40

标签: c# encryption error-handling cryptography

在我的visual 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();
        }

基本上我正在尝试做的是打开一个txt文件获取内部数据加密(工作正常)然后解密(我得到错误)当它给我错误我不知道是什么更改以解决此问题,因为数据的长度使用相同的键和填充以及其他适用的内容...

1 个答案:

答案 0 :(得分:0)

首先:

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

字符串'已加密'格式为

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

但是你试图解密格式化字符串的Utf8字节。那失败了;而且大多数时候由于长度不正确。您希望使用BitConverter将字符串转换为字节数组 - 不使用Utf8。

除此之外: 1)不要使用ECB 2)确实使用IV 3)使用文件的哈希作为关键是最好的问题

编辑:格式化