Re: Can't get basic encryption to work



Thanks both of you. I was doing that BEFORE the write command, and was then
getting a padding error exception in the decryption routine. Now, the
encryption and decryption both work.

So, here is my final code (the stuff above it is the same):

mStream = new MemoryStream();
cStream = new CryptoStream(mStream, encryptor, CryptoStreamMode.Write);
sWriter = new StreamWriter(cStream);
sWriter.Flush();
sWriter.Write(dataToEncrypt);
sWriter.Flush();
cStream.FlushFinalBlock();

this.encryptedData = mStream.ToArray();



Thanks!


"Joe Kaplan" wrote:

You should always call FlushFinalBlock when working with block ciphers
(which AES/Rjindael is one of). Block ciphers need to add padding to data
to make the final output be an even block size, so they need to know when
you are done adding data.

Joe K.

--
Joe Kaplan-MS MVP Directory Services Programming
Co-author of "The .NET Developer's Guide to Directory Services Programming"
http://www.directoryprogramming.net
--
"DXRick" <DXRick@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:10F02053-2B27-4D99-A518-5AC9C4355861@xxxxxxxxxxxxxxxx
Where did you add those? If I try to close the writer before the
sWriter.Write() command I get an exception that it cannot write to a
closed
stream.

If I just include this command, I will get a 16 byte array in hookie:

cStream.FlushFinalBlock();

I have no clue why that would be needed.

Thanks.

"Dominick Baier" wrote:

adding an

sWriter.Flush();
sWriter.Close();

works for me...


-----
Dominick Baier (http://www.leastprivilege.com)

Developing More Secure Microsoft ASP.NET 2.0 Applications
(http://www.microsoft.com/mspress/books/9989.asp)

Those did not work.

This:

int sttr = (int)mStream.Length;

give me a 0 length. The MemoryStream is not being used?

Thanks.

"Dominick Baier" wrote:

just some ideas:

try to flush the streamwriter first...
if that doesn't help, try to position the memstream to the beginning
(using
..Seek)
-----
Dominick Baier (http://www.leastprivilege.com)
Developing More Secure Microsoft ASP.NET 2.0 Applications
(http://www.microsoft.com/mspress/books/9989.asp)

I am just trying to encrypt some data and am pretty much copying the
code that the MSDN docs have for the RijndaelManaged class:

RijndaelManaged cryptAlg = null;
MemoryStream mStream = null;
CryptoStream cStream = null;
StreamWriter sWriter = null;
try
{
cryptAlg = new RijndaelManaged();
ICryptoTransform encryptor = cryptAlg.CreateEncryptor(cryptAlg.Key,
cryptAlg.IV);
mStream = new MemoryStream();
cStream = new CryptoStream(mStream, encryptor,
CryptoStreamMode.Write);
sWriter = new StreamWriter(cStream);
sWriter.Write(dataToEncrypt); // encrypt a string
byte[] hookie = mStream.ToArray(); ***GETTING NOTHING HERE***
}
The MemoryStream mStream has nothing in it after this. The Length
is
0 and I get no data from it. Since I just copied the code from the
MSDN example, I can't figure out what is wrong. I am just doing
this
in a console app right now. So nothing fancy here.
Please help!

Thanks!







.



Relevant Pages

  • Re: Cant get basic encryption to work
    ... If I just include this command, I will get a 16 byte array in hookie: ... Dominick Baier ... ICryptoTransform encryptor = cryptAlg.CreateEncryptor(cryptAlg.Key, ... The MemoryStream mStream has nothing in it after this. ...
    (microsoft.public.dotnet.security)
  • Re: Cant get basic encryption to work
    ... You should always call FlushFinalBlock when working with block ciphers ... Dominick Baier ... ICryptoTransform encryptor = cryptAlg.CreateEncryptor(cryptAlg.Key, ... The MemoryStream mStream has nothing in it after this. ...
    (microsoft.public.dotnet.security)
  • Re: Cant get basic encryption to work
    ... try to flush the streamwriter first... ... StreamWriter sWriter = null; ... ICryptoTransform encryptor = cryptAlg.CreateEncryptor(cryptAlg.Key, ... The MemoryStream mStream has nothing in it after this. ...
    (microsoft.public.dotnet.security)
  • Re: Cant get basic encryption to work
    ... Dominick Baier ... Developing More Secure Microsoft ASP.NET 2.0 Applications ... ICryptoTransform encryptor = cryptAlg.CreateEncryptor(cryptAlg.Key, ...
    (microsoft.public.dotnet.security)

Quantcast