problem with memorystream and streamwriter

  • Thread starter Thread starter Heron
  • Start date Start date
H

Heron

Hi,



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();
 
nm, found it :)
Must user Flush() instead of Close() and keep the streamwriter open.
 
Heron said:
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();
}
}
 
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;
}
}
}
}
 
Heron said:
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.

You're calling FlushFinalBlock before you've actually written any data.
Call it after calling Flush on the StreamWriter instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top