Invalid character in Base-64 string when concatenating and url-encoding string
I am trying to write an encryption code that gets passed through Url. For the sake of the problem Ive ruled out the actual encryption of the data and just showed the code causing the problem.
I take the salt value, convert it to a byte array and then convert it to a base64 string. This string I concatenate with another base64 string (previously it was a byte array).
These two base64 strings are then url encoded. Here is my code ...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Web;
class RHEncryption
{
private static readonly Encoding ASCII_ENCODING = new System.Text.ASCIIEncoding();
private static readonly string SECRET_KEY = "akey";
private static string md5(string text)
{
return BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(ASCII_ENCODING.GetBytes(text))).Replace("-", "").ToLower();
}
public string UrlEncodedData;
public RHEncryption()
{
// encryption object
RijndaelManaged aes192 = new RijndaelManaged();
aes192.KeySize = 192;
aes192.BlockSize = 192;
aes192.Mode = CipherMode.CBC;
aes192.Key = ASCII_ENCODING.GetBytes(md5(SECRET_KEY));
aes192.GenerateIV();
// convert Ivector to base64 for sending
string base64IV = Convert.ToBase64String(aes192.IV);
// salt value
string s = "maryhadalittlelamb";
string salt = s.Substring(0, 8);
// convert to byte array
// and base64 for sending
byte[] saltBytes = ASCII_ENCODING.GetBytes(salt.TrimEnd('\0'));
string base64Salt = Convert.ToBase64String(saltBytes);
//url encode concatenated base64 strings
UrlEncodedData = HttpUtility.UrlEncode(base64Salt + base64IV, ASCII_ENCODING);
}
public string UrlDecodedData()
{
// decode the url encode string
string s = HttpUtility.UrlDecode(UrlEncodedData, ASCII_ENCODING);
// convert back from base64
byte[] base64DecodedBytes = null;
try
{
base64DecodedBytes = Convert.FromBase64String(s);
}
catch (FormatException e)
{
Console.WriteLine(e.Message.ToString());
Console.ReadLine();
}
return s;
}
}
If I then call the UrlDecodedData method strong> I get
Invalid character in Base-64 string
an exception. This is because the base64Salt variable contains an invalid character (Im guessing the end of the line), but I can't seem to disable it.
Update 1
One thing I discovered after posting is that if I change the order of the encoded string, no exception is thrown. Therefore, by changing the following code from
UrlEncodedData = HttpUtility.UrlEncode(base64Salt + base64IV, ASCII_ENCODING);
For
UrlEncodedData = HttpUtility.UrlEncode(base64IV + base64Salt, ASCII_ENCODING);
Seems to work, unfortunately I cannot change the order as this information is being passed to a third party component.
a source to share
Here is your problem:
UrlEncodedData = HttpUtility.UrlEncode(base64Salt + base64IV, ASCII_ENCODING); ... string s = HttpUtility.UrlDecode(UrlEncodedData, ASCII_ENCODING); base64DecodedBytes = Convert.FromBase64String(s);
You are concatenating two Base64-encoded strings and then trying to Base64-decode the combination. This will only work if yours base64Salt
is a multiple of six bits, and in your case it isn't. Data that is not so plural is Base64 encoded with a trailing '=' character, in which case it puts the '=' in the middle of the string, which is not a valid Base64 string.
You must bind the original data before coding. This will only work if you know the original data lengths. If you don't, you will have to encode the salt and vector separately and bind them to a URL; for example http: // yoursite / yourPage? salt = mcMDaw% 20VUsZX & iv = VUsZXyg6n61e6SavmMrtAWa0A
a source to share