Padding is invalid and cannot be removed
Hi I try to write simple code about encryption-decryption some text.
It encrypts well but after decryption it throws en exception "Padding is invalid and cannot be removed" during stream reader reads.That line is : "textBox1.text = sr.ReadToEnd();"
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream(@"C:\enkripcija.txt", FileMode.Open, FileAccess.ReadWrite);
RijndaelManaged rm = new RijndaelManaged();
CryptoStream cs = new CryptoStream(fs, rm.CreateEncryptor(rm.Key, rm.IV), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cs, Encoding.UTF8);
sw.Write("Something written for encryption");
sw.Close();
cs.Close();
fs.Close();
}
private void button2_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream(@"C:\enkripcija.txt", FileMode.Open, FileAccess.Read);
RijndaelManaged rm = new RijndaelManaged();
CryptoStream cs = new CryptoStream(fs, rm.CreateDecryptor(rm.Key, rm.IV), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs, Encoding.UTF8);
textBox1.text = sr.ReadToEnd();
sr.Close();
cs.Close();
fs.Close();
}
}