URL-Safe BASE64 encoding

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.