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;
}
}
}
}
"Jon Skeet [C# MVP]" <sk***@pobox.comschreef in bericht
news:MP************************@msnews.microsoft.c om...
Heron <no****@nospam.comwrote:
>Could someone explain me why the following code doesn't work? The
memorystream always remains with length 0.
MemoryStream input = new MemoryStream();
StreamWriter swriter = new StreamWriter(input);
swriter.Write("test");
swriter.Close();
Could you post a short but complete program which demonstrates the
problem?
See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
Here's a program which works:
using System;
using System.IO;
class Test
{
static void Main()
{
MemoryStream input = new MemoryStream();
StreamWriter swriter = new StreamWriter(input);
swriter.Write("test");
swriter.Close();
Console.WriteLine (input.ToArray().Length);
}
}
Note that you can't access the Length property after the MemoryStream
is closed, which the StreamWriter will do when it's closed.
If you want to access the Length property while the StreamWriter is
still open, you should call Flush() first:
using System;
using System.IO;
class Test
{
static void Main()
{
MemoryStream input = new MemoryStream();
StreamWriter swriter = new StreamWriter(input);
swriter.Write("test");
swriter.Flush();
Console.WriteLine (input.Length);
swriter.Close();
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too