简体   繁体   中英

Base 64 encoding and Decoding

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="/>

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.

eg: 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM