How comes this crypto method does not work (Rijndael)

N

~~~ .NET Ed ~~~

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();
 
J

Jon Skeet [C# MVP]

~~~ .NET Ed ~~~ said:
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.
 
T

Tom Spink

~~~ .NET Ed ~~~ said:
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.
 
N

~~~ .NET Ed ~~~

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
 
J

Jon Skeet [C# MVP]

~~~ .NET Ed ~~~ said:
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.
 
L

Larry Lard

~~~ .NET Ed ~~~ said:
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.
 

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

Top