Hi Guys I have problem on decoding my text, When I want decode VS Debugger shows me "The input data is not a complete block." I tried each way to fix this problem but i could not find :( Could anybody help me? Thanks.
//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);
This is you writing the bytes to your textbox.
utf8.GetBytes(richTextBox1.Text)
And this is how you get them out. Do you really think this will result in the same bytes? Take a debugger, and check both of your byte arrays when you have encrypted them and before you decrypt them. Then find a way to store your byte array without changing it. You could store it seperately or you could change your routine of writing it into the textbox or you could change your routine of reading it from the textbox. Pick the one you like best.