views:

884

answers:

4

I am encrypting a text and sending it via QueryString.

"8ZnSq13yv2yYVDsehDnNUNp/yIFqsAQh4XNPbV1eLMpk/dMWpc/YnMMEBy29MlgcYqpV2XPOf/Rpiz5S85VN/fkLbGTCkL/clBHh983Cp s="

The Decrypt Function is given Below

public static string Decrypt(string stringToDecrypt)//Decrypt the content
{
 try
 {
  byte[] key = Convert2ByteArray(DESKey);
  byte[] IV = Convert2ByteArray(DESIV);
  int len = stringToDecrypt.Length;
  byte[] inputByteArray = Convert.FromBase64String(stringToDecrypt);

  DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  MemoryStream ms = new MemoryStream(); 

  CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
  cs.Write(inputByteArray, 0, inputByteArray.Length);

  cs.FlushFinalBlock();

  Encoding encoding = Encoding.UTF8; return encoding.GetString(ms.ToArray());
 }
 catch (System.Exception ex)
 {
  throw ex;
 }
}

What should I do to resolve this issue?

A: 

I see a space at the end between p and s. That is not an allowed character in a base64 string.

Ronald Wildenberg
+4  A: 

The 3rd last character is a space. I'm guessing that it was a + in the original before it was put on the query string. + is a special character on URL representing a space, so the QueryString is converting this to a space on you.

Try passing your Base64 string through

Server.UrlEncode(string);

before redirecting & that will properly escape the + into a %urlchar and then pass it through

Server.UrlDecode(string);

before parsing it on teh other side

Eoin Campbell
Decoding the querystring values is done already when it's parsed into the Request.QueryString collection, so only the encoding is needed.
Guffa
A: 

The problem is likely that your string has characters that have special meaning on the request query. '/' , '=', and ' ' for instance.

You can encode the string prior to sending, or better yet, add it to the form body of the request and send it that way instead of the query string.

Geoff
A: 

Looks a character short to me... Base64 strings' length should be divisible by 4, padded by trailing '=' if necessary - are you missing a trailing '=', i.e. should it be "8ZnSq13yv2yYVDsehDnNUNp/yIFqsAQh4XNPbV1eLMpk/dMWpc/YnMMEBy29MlgcYqpV2XPOf/Rpiz5S85VN/fkLbGTCkL/clBHh983Cps=="?

although I didn't downvote the answer, it is indeed wrong. (the one who did downvote should add a comment why). The Base64 pads to make sure it's bits are divisible by 6. One '=' means the decoder should ignore the last 2 bits, two '=' means the decoder should ignore the last 4 bits.
Davy Landman
Thanks... this contradicts what I'd been taught about Base64 so when someone tells you you're wrong it's helpful to know why! Looks like I need to do some reading...