在 C# 上使用 AES 解码时出现问题
Problems decoding using AES on C#
大家好我在解码我的文本时遇到问题,当我想要解码时 VS 调试器会显示 "The input data is not a complete block."
我尝试了各种方法来解决这个问题,但我找不到:(
有人可以帮助我吗?
谢谢
//Encoding
try
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
UTF8Encoding utf8 = new UTF8Encoding();
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
aes.KeySize = 256;
aes.BlockSize = 128;
string str = System.IO.File.ReadAllText(pathread);
aes.IV = md5.ComputeHash(utf8.GetBytes(textBox3.Text));
aes.Key = md5.ComputeHash(utf8.GetBytes(textBox4.Text));
aes.Mode = System.Security.Cryptography.CipherMode.CBC;
aes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ICryptoTransform ic2 = aes.CreateEncryptor();
byte[] enc = ic2.TransformFinalBlock(utf8.GetBytes(str), 0, utf8.GetBytes(str).Length);
richTextBox1.Text = BitConverter.ToString(enc);
//string toraj = BitConverter.ToString(ic2.TransformFinalBlock(utf8.GetBytes(str), 0, utf8.GetBytes(str).Length));
}
catch (Exception e1)
{
MessageBox.Show(e1.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
//Decoding
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
UTF8Encoding utf8 = new UTF8Encoding();
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
aes.KeySize = 256;
aes.BlockSize = 128;
string str = richTextBox1.Text.ToString();
aes.IV = md5.ComputeHash(utf8.GetBytes(textBox3.Text));
aes.Key = md5.ComputeHash(utf8.GetBytes(textBox4.Text));
aes.Mode = System.Security.Cryptography.CipherMode.CBC;
MessageBox.Show(str);
byte[] encrypted = Encoding.UTF8.GetBytes(str);
aes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ICryptoTransform ic5 = aes.CreateDecryptor();
//for (int x = 0; x <= encrypted.Length; x++)
//{
// Array.Reverse(encrypted);
//}
MessageBox.Show(encrypted[0].ToString("X"));
MessageBox.Show( utf8.GetString(ic5.TransformFinalBlock(utf8.GetBytes(richTextBox1.Text), 0, utf8.GetBytes(richTextBox1.Text).Length)));
richTextBox1.Text = BitConverter.ToString(enc);
这是您将字节写入文本框。
utf8.GetBytes(richTextBox1.Text)
这就是你把它们弄出来的方法。你真的认为这会产生相同的字节吗?使用调试器,在加密后和解密前检查两个字节数组。然后找到一种方法来存储您的字节数组而不更改它。您可以单独存储它 或 您可以更改将其写入文本框的例程 或 您可以更改从文本框读取它的例程。选一个你最喜欢的。
大家好我在解码我的文本时遇到问题,当我想要解码时 VS 调试器会显示 "The input data is not a complete block." 我尝试了各种方法来解决这个问题,但我找不到:( 有人可以帮助我吗? 谢谢
//Encoding
try
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
UTF8Encoding utf8 = new UTF8Encoding();
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
aes.KeySize = 256;
aes.BlockSize = 128;
string str = System.IO.File.ReadAllText(pathread);
aes.IV = md5.ComputeHash(utf8.GetBytes(textBox3.Text));
aes.Key = md5.ComputeHash(utf8.GetBytes(textBox4.Text));
aes.Mode = System.Security.Cryptography.CipherMode.CBC;
aes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ICryptoTransform ic2 = aes.CreateEncryptor();
byte[] enc = ic2.TransformFinalBlock(utf8.GetBytes(str), 0, utf8.GetBytes(str).Length);
richTextBox1.Text = BitConverter.ToString(enc);
//string toraj = BitConverter.ToString(ic2.TransformFinalBlock(utf8.GetBytes(str), 0, utf8.GetBytes(str).Length));
}
catch (Exception e1)
{
MessageBox.Show(e1.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
//Decoding
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
UTF8Encoding utf8 = new UTF8Encoding();
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
aes.KeySize = 256;
aes.BlockSize = 128;
string str = richTextBox1.Text.ToString();
aes.IV = md5.ComputeHash(utf8.GetBytes(textBox3.Text));
aes.Key = md5.ComputeHash(utf8.GetBytes(textBox4.Text));
aes.Mode = System.Security.Cryptography.CipherMode.CBC;
MessageBox.Show(str);
byte[] encrypted = Encoding.UTF8.GetBytes(str);
aes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ICryptoTransform ic5 = aes.CreateDecryptor();
//for (int x = 0; x <= encrypted.Length; x++)
//{
// Array.Reverse(encrypted);
//}
MessageBox.Show(encrypted[0].ToString("X"));
MessageBox.Show( utf8.GetString(ic5.TransformFinalBlock(utf8.GetBytes(richTextBox1.Text), 0, utf8.GetBytes(richTextBox1.Text).Length)));
richTextBox1.Text = BitConverter.ToString(enc);
这是您将字节写入文本框。
utf8.GetBytes(richTextBox1.Text)
这就是你把它们弄出来的方法。你真的认为这会产生相同的字节吗?使用调试器,在加密后和解密前检查两个字节数组。然后找到一种方法来存储您的字节数组而不更改它。您可以单独存储它 或 您可以更改将其写入文本框的例程 或 您可以更改从文本框读取它的例程。选一个你最喜欢的。