PC Review


Reply
Thread Tools Rate Thread

How comes this crypto method does not work (Rijndael)

 
 
~~~ .NET Ed ~~~
Guest
Posts: n/a
 
      25th Jun 2006
Anybody has any idea why this simple thing is not working? I pass a text
file as input to encrypt it, then pass the encrypted version to the same
function and get some garbled data not at all resembling the input file.

Rijndael rijndaelAlg = Rijndael.Create();

rijndaelAlg.BlockSize = 128; // 128 bits to comply with AES

rijndaelAlg.Padding = PaddingMode.PKCS7;

rijndaelAlg.Mode = CipherMode.CBC;



MD5CryptoServiceProvider m = new MD5CryptoServiceProvider();

PasswordDeriveBytes pdb = new PasswordDeriveBytes(sKey,
m.ComputeHash(System.Text.UTF8Encoding.UTF8.GetBytes(sKey)));

//Set secret key For AES algorithm.


//rijndaelAlg.Key = ASCIIEncoding.ASCII.GetBytes(sKey);

rijndaelAlg.Key = pdb.GetBytes(256/8);

//Set initialization vector.

//rijndaelAlg.IV = ASCIIEncoding.ASCII.GetBytes(sIV);

rijndaelAlg.IV = pdb.GetBytes(16);



FileStream fsIn = new FileStream(sInputFilename, FileMode.Open,
FileAccess.Read);

FileStream fsOut = new FileStream(sOutputFilename, FileMode.Create,
FileAccess.Write);



//Create an AES encryptor from the AES instance.

ICryptoTransform aesencrypt = rijndaelAlg.CreateEncryptor();



//Create crypto stream set to read and do an AES encryption transform on
incoming bytes.

CryptoStream cipherstream = new CryptoStream(fsOut, aesencrypt,
CryptoStreamMode.Write);

int data;

while ((data = fsIn.ReadByte()) != -1)

{

cipherstream.WriteByte((byte) data);

}

byte[] bytearrayinput = new byte[fsIn.Length];

fsIn.Read(bytearrayinput, 0, bytearrayinput.Length);

cipherstream.Write(bytearrayinput, 0, bytearrayinput.Length);

cipherstream.Close();

fsOut.Close();

fsIn.Close();


 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      25th Jun 2006
~~~ .NET Ed ~~~ <(E-Mail Removed)> wrote:
> Anybody has any idea why this simple thing is not working? I pass a text
> file as input to encrypt it, then pass the encrypted version to the same
> function and get some garbled data not at all resembling the input file.


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.

Most of the code is there, but it's a lot easier to diagnose this kind
of thing if it's already in a complete program.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Tom Spink
Guest
Posts: n/a
 
      25th Jun 2006
~~~ .NET Ed ~~~ wrote:

> Anybody has any idea why this simple thing is not working? I pass a text
> file as input to encrypt it, then pass the encrypted version to the same
> function and get some garbled data not at all resembling the input file.


<snippedy-doo-dah />

Hi .NET Ed,

This is because all you're doing is encrypting the encrypted file. You need
to decrypt the file, in order to get the original version. Rijndael is not
a cyclic encryption routine, like XOR'ing every byte by an arbitrary
number.

What you need to do is use ICryptoTransform and create a decryptor, with
everything else the same:

///
ICryptoTransform aesdecrypt = rijndaelAlg.CreateDecryptor();
///

And use that to decrypt the stream.

--
Hope this helps,
Tom Spink
 
Reply With Quote
 
~~~ .NET Ed ~~~
Guest
Posts: n/a
 
      25th Jun 2006
Jon,
It was not a complete program but just a method. The only missing things
where:

public class Test {
public void Encrypt(string sInFilename, string sOutFilename, string
sKey, string sIV)
{
the rest
}

[STAThread]
public void main(string[] args)
{ // for the purpose of testing
Test t = new Test();
t.Encrypt(args[0], args[2], args[3], args[4]);
}
}

TOM,
Thanks that was indeed the right answer. Since it was a symmetric
algorithm I was under the wrong assumption that encrypt/decrypt actually did
the same. Now I do get the original file. In short, adapt the code to use
the Decryptor method of Rijndael when decrypting the file. Had been a while
since I did crypto stuff.

Thanks!
Emilio

"Tom Spink" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> ~~~ .NET Ed ~~~ wrote:
>
>> Anybody has any idea why this simple thing is not working? I pass a text
>> file as input to encrypt it, then pass the encrypted version to the same
>> function and get some garbled data not at all resembling the input file.

>
> <snippedy-doo-dah />
>
> Hi .NET Ed,
>
> This is because all you're doing is encrypting the encrypted file. You
> need
> to decrypt the file, in order to get the original version. Rijndael is
> not
> a cyclic encryption routine, like XOR'ing every byte by an arbitrary
> number.
>
> What you need to do is use ICryptoTransform and create a decryptor, with
> everything else the same:
>
> ///
> ICryptoTransform aesdecrypt = rijndaelAlg.CreateDecryptor();
> ///
>
> And use that to decrypt the stream.
>
> --
> Hope this helps,
> Tom Spink



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      25th Jun 2006
~~~ .NET Ed ~~~ <(E-Mail Removed)> wrote:
> It was not a complete program but just a method. The only missing things
> where:


<snip>

No, that's not true. Without sample data, there would be nothing to
test. It makes life a lot easier if you can post *everything* required
to demonstrate the problem. In this case, probably the code to encrypt
as well as the code to decrypt would have been a good idea. It's also a
lot easier to cut and paste a complete program than assembly it from
bits and bobs, work out the imports etc.

> Thanks that was indeed the right answer. Since it was a symmetric
> algorithm I was under the wrong assumption that encrypt/decrypt actually did
> the same. Now I do get the original file. In short, adapt the code to use
> the Decryptor method of Rijndael when decrypting the file. Had been a while
> since I did crypto stuff.


I'm glad it's sorted out now.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Larry Lard
Guest
Posts: n/a
 
      26th Jun 2006

~~~ .NET Ed ~~~ wrote:
> Since it was a symmetric
> algorithm I was under the wrong assumption that encrypt/decrypt actually did
> the same.


The 'symmetric' here means only that the *keys* for encryption and
decryption are the same (or 'trivially related'). It doesn't mean the
actual processes of encryption and decryption are the same.
'Asymmetric' cryptography is where the keys themselves for encryption
and decryption differ. Wikipedia for more.

--
Larry Lard
Replies to group please

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GetObject method not work after Call Shell Method =?Utf-8?B?YmVu?= Microsoft Excel Programming 8 21st Feb 2006 03:45 PM
Why QUIT method doesn't work after COPY method? surotkin Microsoft Excel Programming 3 26th Oct 2005 04:32 PM
Rijndael crypto and TransformBlock? William Stacey [MVP] Microsoft C# .NET 5 20th Oct 2004 09:27 PM
Rijndael algo. available in MS Crypto API? Sean Kelly Microsoft VC .NET 4 22nd Apr 2004 12:37 PM
are crypto classes supposed to work on w2k? Gregory L Priem Microsoft Dot NET 2 5th Nov 2003 11:14 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:38 PM.