簡體   English   中英

Visual C#獨立解密錯誤:要解密的數據長度無效

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

在我的可視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文件,將數據加密到里面(可以正常工作),然后解密(得到錯誤),當它給我錯誤時我不知道要更改為什么解決此問題,因為數據長度使用相同的鍵和填充以及所有其他適用的方法...

首先:

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

字符串“ encrypted”的格式為

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

但是,您嘗試解密格式化字符串的Utf8字節。 那失敗了; 而且大多數情況下是由於長度不正確。 您想使用BitConverter而不是Utf8將字符串轉換為字節數組。

除此之外:1)不要使用ECB 2)一定要使用IV 3)使用文件的哈希值,因為密鑰最多是可疑的

編輯:格式化

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM