all groups > dotnet security > july 2005 >
You're in the

dotnet security

group:

Cryptography implementation using memeorystream


Cryptography implementation using memeorystream rajkumar
7/18/2005 12:00:01 AM
dotnet security:
I tried to implement cryptographic using memorystream instead of other stream
like file stream etc. Encryption is ok but could not get original data on
decryption.
Same logic works if i use filestream for cryptostream!

Re: Cryptography implementation using memeorystream William Stacey [MVP]
7/18/2005 9:36:50 AM
hmm. Are you resetting the current Position back to 0 after filling the
memorystream?

--
William Stacey [MVP]

[quoted text, click to view]

Re: Cryptography implementation using memeorystream rajbharati
7/19/2005 12:17:02 AM
Because memeorystream cannot support read/write at the same time, i copied
the encrypted buffer to byte array and re-instantiate another memorystream
passing that byte array which in tern is used to intantiate cryptostream. I
think i could not use seek method to reposition this cryptostream object. But
i call seek method from memorystream object before passing to cryptostream.
But no difference!

[quoted text, click to view]
Re: Cryptography implementation using memeorystream William Stacey [MVP]
7/19/2005 8:18:58 AM
Here is how I do it:

RijndaelManaged rm = new RijndaelManaged();
byte[] data = Encoding.UTF8.GetBytes("Hello");
ICryptoTransform encryptor = rm.CreateEncryptor();
ICryptoTransform decryptor = rm.CreateDecryptor();
byte[] encData = RijndaelEncrypt(encryptor, data);
byte[] clearData = RijndaelDecrypt(decryptor, encData);
Console.WriteLine("Data:" + Encoding.UTF8.GetString(clearData));

private static byte[] RijndaelDecrypt(ICryptoTransform decryptor,
byte[] encrypted)
{
if ( decryptor == null )
throw new ArgumentNullException("decryptor");
if ( encrypted == null )
throw new ArgumentNullException("encrypted");

using ( MemoryStream msDecrypt = new MemoryStream(encrypted) )
using ( CryptoStream csDecrypt = new CryptoStream(msDecrypt,
decryptor, CryptoStreamMode.Read) )
{
byte[] fromEncrypt = new byte[encrypted.Length];

int read = csDecrypt.Read(fromEncrypt, 0,
fromEncrypt.Length);
if ( read < fromEncrypt.Length )
{
byte[] clearBytes = new byte[read];
Buffer.BlockCopy(fromEncrypt, 0, clearBytes, 0, read);
return clearBytes;
}
return fromEncrypt;
}
}

private static byte[] RijndaelEncrypt(ICryptoTransform encryptor,
byte[] data)
{
if ( encryptor == null )
throw new ArgumentNullException("encryptor");
if ( data == null )
throw new ArgumentNullException("data");

//Encrypt the data.
using ( MemoryStream msEncrypt = new MemoryStream() )
using ( CryptoStream csEncrypt = new CryptoStream(msEncrypt,
encryptor, CryptoStreamMode.Write) )
{
//Write all data to the crypto stream and flush it.
csEncrypt.Write(data, 0, data.Length);
csEncrypt.FlushFinalBlock();

//Get encrypted array of bytes.
byte[] encrypted = msEncrypt.ToArray();
return encrypted;
}
}

--
William Stacey [MVP]

[quoted text, click to view]

Re: Cryptography implementation using memeorystream rajbharati
7/20/2005 12:00:02 AM
Thanks a lot!
By the way does encoding(you used UTF8) affect?
I usually use ASCII encoding!

Raj

[quoted text, click to view]
Re: Cryptography implementation using memeorystream Joe Kaplan (MVP - ADSI)
7/21/2005 12:06:47 AM
ASCII encoding for string data has the serious down side that it cannot
properly encode non-ASCII string data. Given that .NET strings are unicode,
you run a risk when converting to ASCII that you will lose data unless you
are absolutely certain that the string will never contain non-ASCII data.

UTF8 can encode all unicode characters properly and has the benefit of
producing the same encoding as ASCII for ASCII characters, so it doesn't
waste any additional space in that case.

Joe K.

[quoted text, click to view]

AddThis Social Bookmark Button