Hi Jon,
Thanks for replying. Below is a complete program as requested.
My problem was in the encrypt method which is solved now but i'm still
having a problem with the decrypt method, the sreader.ReadToEnd(); method
throws me a "Padding is invalid and cannot be removed." error.
using System;
using System.IO;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.Text;
namespace testconsole
{
class Program
{
static void Main(string[] args)
{
byte[] key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16 };
byte[] iv = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17 };
MyRijndael rn = new MyRijndael(key);
string enc = rn.Encrypt("abcdefghijklmnopqrstuvwxyz", iv);
string dec = rn.Decrypt(enc, iv);
}
public class MyRijndael
{
private byte[] _key;
public MyRijndael(byte[] key)
{
this._key = key;
}
public string Encrypt(string input, byte[] iv)
{
MemoryStream output = new MemoryStream();
Rijndael rn = Rijndael.Create();
CryptoStream scrypt = new CryptoStream(output,
rn.CreateEncryptor(this._key, iv), CryptoStreamMode.Write);
scrypt.FlushFinalBlock();
StreamWriter swriter = new StreamWriter(scrypt);
swriter.Write(input);
swriter.Flush();
byte[] buffer = output.ToArray();
swriter.Close();
scrypt.Close();
output.Close();
return Convert.ToBase64String(buffer);
}
public string Decrypt(string input64, byte[] iv)
{
MemoryStream input = new
MemoryStream(Convert.FromBase64String(input64));
Rijndael rn = Rijndael.Create();
CryptoStream scrypt = new CryptoStream(input,
rn.CreateDecryptor(this._key, iv), CryptoStreamMode.Read);
StreamReader sreader = new StreamReader(scrypt);
string res = sreader.ReadToEnd(); //<-- error
"Padding is invalid and cannot be removed."
sreader.Close();
scrypt.Close();
input.Close();
return res;
}
}
}
}