BASE64 encoding can be used to pass longer identity information in an HTTP environment. In other applications, it is often necessary to encode binary data as appropriate in the form of URLs (including hidden form fields). At this time, the adoption of BASE64 encoding is not only short, but also has the non-readability, that is, the encoded data will not be directly visible to the naked eye.
However, the standard Base64 is not intended to be transmitted directly in the URL because the URL encoder will change the "/" and "+" characters in the standard Base64 into forms such as "%xx", and these "%" numbers will need to be converted when they are deposited into the database because ANSI SQL has "%" Used as a wildcard character.
To solve this problem, a modified Base64 encoding for the URL is used, which does not populate the ' = ' at the end, and changes the "+" and "/" in standard Base64 to "*" and "-", thus eliminating the conversion required for URL codec and database storage. Avoids the increase in the length of encoded information in this process, and unifies the format of object identifiers in databases, forms, and so on.
URL-Safe Base64 encoding is suitable for scenarios in which BASE64 encoding results are delivered in a URL manner. The basic process of encoding is to encode the content in Base64 format to a string, then examine the result string, replace the plus sign in the string with an +
underscore -
, and replace the slash with an underscore /
_
.
For detailed coding specifications, refer to the relevant description in the RFC4648 standard.
Add: For the "=" placeholder at the end, bouncy castle will use it instead, and Commons codes eliminate any of the placeholders. The following example code uses the bouncy castle method, using "=" with "." Replace.
An improved version of the BASE64 encoded C # implementation for URLs:
<summary>
Convert from binary character to Base64 encoded string for URL
</summary>
public static string Tobase64stringforurl (byte[] token)
{
return convert.tobase64string (token). Replace (' + ', ' * ')
. Replace ('/', '-')
. Replace (' = ', '. ');
}
URL-Safe BASE64 encoding