Re: what's wrong with this code?, getting lot of \0\0\0

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Sun, 11 Jan 2009 21:43:32 +0100, "Jeff"
<it_consultant1@xxxxxxxxxxxxxxxxxx> wrote:

hi

.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"
Rijndael is a 128 bit block cypher, so a block is 16 bytes. Including
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]


public static string Decrypt(string encryptedStr)
{
//unconvert encrypted string
byte[] encryptedStrAsBytes =
Convert.FromBase64String(encryptedStr);
byte[] initialText = new Byte[encryptedStrAsBytes.Length];
The length you are dealing with here may well be a full block (16
bytes) rather than just your text (5 bytes). Check lengths at this
point.



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);
If the initialText.length is a full block, 16 bytes, then you will try
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);
}


}


.



Relevant Pages

  • Trying to encrypt a string
    ... I'm trying to encrypt passwords in my app. ... Base64-encoded string. ... The decryption function does the reverse, ... thing is, if you enable the commented-out code in Main, it works ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Trying to encrypt a string
    ... > I'm trying to encrypt passwords in my app. ... > Base64-encoded string. ... The decryption function does the reverse, ... > character, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Trying to encrypt a string
    ... > I'm trying to encrypt passwords in my app. ... > Base64-encoded string. ... The decryption function does the reverse, ... > character, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Help, I am having difficulty implementing JCE/AES
    ... Mike Amling wrote: ... but am having problems getting the decryption to work. ... Using the following code to encrypt the string 1234567890123456 I get the ... Yet when I try to decrypt the string I get the foillowing error message ...
    (comp.lang.java.security)
  • Re: whats wrong with this code?, getting lot of
    ... Unsure how to remove the traling 11 bytes from encryptedStrAsBytes, ... able to handle string of any length, but I doubt the string will be longer ... Rijndael is a 128 bit block cypher, so a block is 16 bytes. ... that the problem is somewhere on the decryption side. ...
    (microsoft.public.dotnet.languages.csharp)