Connecting Tech Pros Worldwide Forums | Help | Site Map

How comes this crypto method does not work (Rijndael)

~~~ .NET Ed ~~~
Guest
 
Posts: n/a
#1: Jun 25 '06
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.GetByt es(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();



Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#2: Jun 25 '06

re: How comes this crypto method does not work (Rijndael)


~~~ .NET Ed ~~~ <tiredofspam@abolishspam.now> wrote:[color=blue]
> 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.[/color]

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 - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Tom Spink
Guest
 
Posts: n/a
#3: Jun 25 '06

re: How comes this crypto method does not work (Rijndael)


~~~ .NET Ed ~~~ wrote:
[color=blue]
> 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.[/color]

<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
~~~ .NET Ed ~~~
Guest
 
Posts: n/a
#4: Jun 25 '06

re: How comes this crypto method does not work (Rijndael)


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" <tspink@gmail.com> wrote in message
news:%232ex3XHmGHA.3352@TK2MSFTNGP02.phx.gbl...[color=blue]
> ~~~ .NET Ed ~~~ wrote:
>[color=green]
>> 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.[/color]
>
> <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[/color]


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#5: Jun 25 '06

re: How comes this crypto method does not work (Rijndael)


~~~ .NET Ed ~~~ <tiredofspam@abolishspam.now> wrote:[color=blue]
> It was not a complete program but just a method. The only missing things
> where:[/color]

<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.
[color=blue]
> 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.[/color]

I'm glad it's sorted out now.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Larry Lard
Guest
 
Posts: n/a
#6: Jun 26 '06

re: How comes this crypto method does not work (Rijndael)



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

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

Closed Thread