I am encrypting and decrypting query string. If i pass a long query string it gives error
'Invalid length for a Base-64 char array.' while decrypting . The encryption works fine. If i pass a short string the decryption works fine. Is there any solution fot this?
How long is the string that you are trying to decrypt? Can you check that it has all the characters that you expect it to? Note that Base64 encoding is not the same as URL encoding and that if it's been on the query string, there may be issues with it. You
will need to URL encode it as well as Base64 encode it.
Ammi
Member
38 Points
101 Posts
Invalid length for a Base-64 char array
Aug 22, 2008 03:27 PM|LINK
Hi,
I am encrypting and decrypting query string. If i pass a long query string it gives error 'Invalid length for a Base-64 char array.' while decrypting . The encryption works fine. If i pass a short string the decryption works fine. Is there any solution fot this?
my code for decrypting is:
public string Decrypt(string stringToDecrypt)
{
byte[] toDecrypt = System.Convert.FromBase64String(stringToDecrypt);
byte[] encryptionKey = System.Text.ASCIIEncoding.ASCII.GetBytes(key);
byte[] iVector = System.Text.ASCIIEncoding.ASCII.GetBytes(initVector);
MemoryStream memoryStream = new MemoryStream(toDecrypt.Length);
DESCryptoServiceProvider desCryptoServiceProvider = new DESCryptoServiceProvider();
CryptoStream cryptoStream = new CryptoStream(memoryStream, desCryptoServiceProvider.CreateDecryptor(encryptionKey, iVector), CryptoStreamMode.Read);
memoryStream.Write(toDecrypt, 0, toDecrypt.Length);
memoryStream.Position = 0;
string decrypted = new StreamReader(cryptoStream).ReadToEnd();
cryptoStream.Close();
return decrypted;
}
Thanks,
Ammi
MelvynHarbou...
Star
8005 Points
1288 Posts
Re: Invalid length for a Base-64 char array
Aug 22, 2008 03:33 PM|LINK
How long is the string that you are trying to decrypt? Can you check that it has all the characters that you expect it to? Note that Base64 encoding is not the same as URL encoding and that if it's been on the query string, there may be issues with it. You will need to URL encode it as well as Base64 encode it.
Ammi
Member
38 Points
101 Posts
Re: Invalid length for a Base-64 char array
Aug 22, 2008 04:01 PM|LINK
The string i pass ranges from 20 characters to 80 characters length and it contains spaces and '&'. And how do i URL encode it?
MelvynHarbou...
Star
8005 Points
1288 Posts
Re: Invalid length for a Base-64 char array
Aug 22, 2008 04:06 PM|LINK
http://msdn.microsoft.com/en-us/library/zttxte6w.aspx
And obviously the equivalent for decoding potentially.
Ammi
Member
38 Points
101 Posts
Re: Invalid length for a Base-64 char array
Aug 22, 2008 06:16 PM|LINK
It is not working. I used URL encode first and encryption. Same thing happens. Short strings work but nothe long strings. Any idea?
Ammi
Member
38 Points
101 Posts
Re: Invalid length for a Base-64 char array
Aug 25, 2008 03:44 PM|LINK
Thanks, Melvyn.
My problem is solved. I used both URL encode as well as encryption. This time i got it right.
Thanks a lot.
-Ammi