Re: what's wrong with this code?, getting lot of \0\0\0
- From: rossum <rossum48@xxxxxxxxxxxx>
- Date: Sun, 11 Jan 2009 21:53:16 +0000
On Sun, 11 Jan 2009 21:43:32 +0100, "Jeff"
<it_consultant1@xxxxxxxxxxxxxxxxxx> wrote:
hiRijndael is a 128 bit block cypher, so a block is 16 bytes. Including
.NET 3.5
I have created some code which encrypt and decrypt text. The decryption
doesn't return the exact same string as the original...
If I have the string "sambi" and encrypt it... after decrypting the
encrypted string I get this. "sambi\0\0\0\0\0\0\0\0\0\0\0"
the extra null bytes, what you have above is 16 bytes so at some point
you are taking an entire block rather than your actual text, which
only forms part of a block.
Checking RijndaelManaged shows that it defaults to CBC mode and PKCS7
padding. What you have above is not PKCS7 padding so I would suspect
that the problem is somewhere on the decryption side.
So I wonder what in my code produces all those \0\0\0\0\0\0\0\0\0\0\0
Here is my code:
[snip part code]
The length you are dealing with here may well be a full block (16
public static string Decrypt(string encryptedStr)
{
//unconvert encrypted string
byte[] encryptedStrAsBytes =
Convert.FromBase64String(encryptedStr);
byte[] initialText = new Byte[encryptedStrAsBytes.Length];
bytes) rather than just your text (5 bytes). Check lengths at this
point.
If the initialText.length is a full block, 16 bytes, then you will try
RijndaelManaged rijndael = new RijndaelManaged();
MemoryStream memStream = new MemoryStream(encryptedStrAsBytes);
if (savedKey == null || savedIV == null)
{
throw (new NullReferenceException("savedKey and savedIV must
be non-null"));
}
//create decryptor, and stream objects
ICryptoTransform rdTransform =
rijndael.CreateDecryptor((byte[])savedKey.Clone(), (byte[])savedIV.Clone());
CryptoStream cryptoStream = new CryptoStream(memStream,
rdTransform, CryptoStreamMode.Read);
//read in decrypted string as a byte[]
cryptoStream.Read(initialText, 0, initialText.Length);
to read 16 bytes here. CryptoStream.Read() returns the number of
bytes actually read. I suggest that you capture and check that number
- I suspect that it will be 5. Only the first 5 bytes of initialText
will come from the Read(), the rest will be left over from the
declaration. That would explain why they are zero.
rossum
//release all resources
memStream.Close();
cryptoStream.Close();
rdTransform.Dispose();
rijndael.Clear();
//convert byte[] to string
string decryptedStr = Encoding.ASCII.GetString(initialText);
return (decryptedStr);
}
}
.
- Follow-Ups:
- References:
- Prev by Date: Re: lock
- Next by Date: Re: lock
- Previous by thread: Re: what's wrong with this code?, getting lot of \0\0\0
- Next by thread: Re: what's wrong with this code?, getting lot of \0\0\0
- Index(es):
Relevant Pages
|