0

I want to decode special characters in Base 64 and I want to include space into it. Please tell me how to handle space in base 64 encoding and decoding.

<add key="SpecialCharacter" value="w6J8YSzDoXxhLMOlfGEsw6R8YSzDo3xhLMOmfGFlLMSNfGMsw6l8ZSzDqHxlLMOqfGUsw6t8ZSzDu3x1LMO6fHUsw7x8dSzDuHxvLMOzfG8sw7R8byzDtnxvLMOyfG8sxaF8byxgfCwnfCzFgnxsLMW8fHo="/>
3
  • Space is not a valid Base64 character in the most commonly used character set. Are you wanting to expand it to Base65 or substitute the space for one of the other characters? Commented Oct 19, 2015 at 6:43
  • I can substitute but how it can be? Commented Oct 19, 2015 at 6:45
  • That is easier and probably better. I think the easiest way to do it, and an efficient one, is to set up conversion methods to encode and decode like so: The encode method first converts bytes to the standard Base64 characters, and then you call string.Replace to put your space in place of the character you are substituting out. For decode, string.Replace the standard Base64 char back in for the spaces and then use the .NET Base64 conversion to byte array from there. Commented Oct 19, 2015 at 6:52

1 Answer 1

5

Take a look into these functions:

public static string ToBase64(this string value)
{
    byte[] bytes = Encoding.Default.GetBytes(value);
    return Convert.ToBase64String(bytes);
}

public static string FromBase64(this string value)
{
    byte[] bytes = Convert.FromBase64String(value);
    return Encoding.Default.GetString(bytes);
}

The first one converts a string to a base 64 string.

e.g.: string base64 = "Hello World!".ToBase64()

The second one returns it to a 'normal' string: string original = base64.FromBase64()

The core functionality is in Convert.FromBase64String(string value). It returns a byte array, which has to converted to a string with Encoding. When you know the used encoding, you shuld use it and not the default (UTF-16, i think).

I tested some encodings. In you case, the string is encoded in UTF8 and results in:

â|a,á|a,å|a,ä|a,ã|a,æ|ae,č|c,é|e,è|e,ê|e,ë|e,û|u,ú|u,ü|u,ø|o,ó|o,ô|o,ö|o,ò|o,š|o,`|,'|,ł|l,ż|z

In addition to the comment to @WDS here again: Spaces should get converted to base64, too. No need for spacial handing them.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.