Home > Backend Development > C++ > How to Achieve URL-Safe Base64 Encoding and Decoding in C#?

How to Achieve URL-Safe Base64 Encoding and Decoding in C#?

Mary-Kate Olsen
Release: 2025-01-25 03:07:11
Original
970 people have browsed it

How to Achieve URL-Safe Base64 Encoding and Decoding in C#?

C#URL Security Base64 Code

When processing URL -based applications, the base64 encoding is critical to ensure data integrity and readability. In C#, the built -in Base64 encoding function provides a convenient way to convert binary data into text -based forms. However, it adds the characters ("==") to the coding string, which may cause problems when used in URL.

URL security encoding

In order to overcome this limit, the URL security encoding is deleted and filled characters, and the specific problem characters ("", "/", "=") are replaced with URL-friendly characters ("-", "_", "") Essence This ensures that the coding string can be transmitted to the URL parameter safely without causing any interpretation problems.

Implement URL security coding in C#

Unlike the CODEC library of Java, C#has no built -in URL security encoding function. However, you can achieve the same results through a few simple steps:

Use Execute the conventional base64 encoding.

    Use Delete the fill in the coding string.
  1. Convert.ToBase64String() Use to replace the problem character (for example, replace "" "- "and"/"to" _ ").
  2. TrimEnd() Example
  3. Replace()
  4. Note:
You can define the filling character array as a static reading field to improve efficiency.

Inverse process

byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes("StringToEncode");
string returnValue = System.Convert.ToBase64String(toEncodeAsBytes)
        .TrimEnd(new char[] {'='}).Replace('+', '-').Replace('/', '_');
Copy after login
Decoding the string of the URL security code back to its original form, just reverse this process:

Replace URL -friendly characters with its original corresponding characters.

Fill the character string to multiple times with the '=' character if necessary. Use to convert the string back to binary data.

Example
  1. By combining these steps, you can effectively implement URL security codes in C#. It is important to test whether the commonly used code library is consistent with this implementation to ensure compatibility in scenarios that require interoperability.

The above is the detailed content of How to Achieve URL-Safe Base64 Encoding and Decoding in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template