Home All Groups Group Topic Archive Search About
Author
22 Oct 2006 5:42 PM
Heron
Hi,

I have a problem with my decryption, the encryption seems to work fine but
when I try to decrypt the encrypted string I get an exception "Padding is
invalid and cannot be removed." thrown on sreader.ReadToEnd(); so could
anyone tell me what I'm doing wrong?

public class MyRijndael
{
    private byte[] _iv;
    public MyRijndael(byte[] iv)
    {
        this._iv = iv;
    }

    public string Encrypt(string input, byte[] key)
    {
        MemoryStream output = new MemoryStream();

        Rijndael rn = Rijndael.Create();

        CryptoStream scrypt = new CryptoStream(output,
rn.CreateEncryptor(key, this._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[] key)
    {
        MemoryStream input = new
MemoryStream(Convert.FromBase64String(input64));

        Rijndael rn = Rijndael.Create();

        CryptoStream scrypt = new CryptoStream(input,
rn.CreateDecryptor(key, this._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;
    }
}

Author
23 Oct 2006 11:41 PM
Alun Jones [MS-MVP - Windows Security]
Show quote Hide quote
"Heron" <nospam@nospam.com> wrote in message
news:OpDPrFg9GHA.3344@TK2MSFTNGP03.phx.gbl...
> I have a problem with my decryption, the encryption seems to work fine but
> when I try to decrypt the encrypted string I get an exception "Padding is
> invalid and cannot be removed." thrown on sreader.ReadToEnd(); so could
> anyone tell me what I'm doing wrong?
>
> public class MyRijndael
> {
>    private byte[] _iv;
>    public MyRijndael(byte[] iv)
>    {
>        this._iv = iv;
>    }
>
>    public string Encrypt(string input, byte[] key)
>    {
>        MemoryStream output = new MemoryStream();
>
>        Rijndael rn = Rijndael.Create();
>
>        CryptoStream scrypt = new CryptoStream(output,
> rn.CreateEncryptor(key, this._iv), CryptoStreamMode.Write);
>        scrypt.FlushFinalBlock();
>
>        StreamWriter swriter = new StreamWriter(scrypt);
>        swriter.Write(input);
>        swriter.Flush();


Sure - you're calling FlushFinalBlock before you've written anything to the
stream.  You are supposed to write everything to the stream, then call
FlushFinalBlock, then close the stream.

Alun.
~~~~
--
Texas Imperial Software   | Web: http://www.wftpd.com/
23921 57th Ave SE         | Blog: http://msmvps.com/alunj/
Woodinville WA 98072-8661 | WFTPD, WFTPD Pro are Windows FTP servers.
Fax/Voice +1(425)807-1787 | Try our NEW client software, WFTPD Explorer.