By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,456 Members | 1,742 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,456 IT Pros & Developers. It's quick & easy.

How comes this crypto method does not work (Rijndael)

P: n/a
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();
Jun 25 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
~~~ .NET Ed ~~~ <ti*********@abolishspam.now> 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 - <sk***@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
Jun 25 '06 #2

P: n/a
~~~ .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
Jun 25 '06 #3

P: n/a
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" <ts****@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
~~~ .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

Jun 25 '06 #4

P: n/a
~~~ .NET Ed ~~~ <ti*********@abolishspam.now> 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 - <sk***@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
Jun 25 '06 #5

P: n/a

~~~ .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

Jun 26 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.