Home All Groups Group Topic Archive Search About

SymmetricAlgorithm/CryptoStream screwed? "Length of data to encrypt is invalid"

Author
6 Jun 2005 11:40 PM
ep
This happens when I used Rijndael 128 or TripleDES 192.... The following
code always throws "Length of the data to encrypt is invalid".

ICryptoTransform trans = algorithm.CreateEncryptor(algorithm.Key,
algorithm.IV);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, trans, CryptoStreamMode.Write);
cs.Write(inputBytes, 0, inputBytes.Length);
cs.FlushFinalBlock();

algorithm is a RijManaged or TDES object, inputBytes is a six-byte array in
this case.  Shouldn't this work?

Author
7 Jun 2005 1:25 PM
Pieter Philippaerts
"ep" <re***@newgroup.only.please> wrote
> This happens when I used Rijndael 128 or TripleDES 192.... The following
> code always throws "Length of the data to encrypt is invalid".

How did you setup the 'algorithm' object? In particular, what padding mode
are you using?
Your code works perfectly, unless I set the PaddingMode to 'None', in which
case it is normal that the symmetric algorithm throws an exception.

Regards,
Pieter Philippaerts
Are all your drivers up to date? click for free checkup

Author
8 Jun 2005 12:16 PM
Fabio Cozzolino [MCAD]
How inputBytes are encoding?
You must encoding the string to encrypt. To do this you can use the
following code (for example):

byte[] inputBytes = Encoding.UTF8.GetBytes(inputString);


HTH
ciauz

Bookmark and Share