Home > Backend Development > C++ > How to Implement Modified Base64 URL Encoding and Decoding in ASP.NET?

How to Implement Modified Base64 URL Encoding and Decoding in ASP.NET?

Linda Hamilton
Release: 2025-01-11 21:46:41
Original
958 people have browsed it

How to Implement Modified Base64 URL Encoding and Decoding in ASP.NET?

ASP.NET Framework: URL-Safe Base64 Encoding and Decoding

This technique modifies standard Base64 encoding to create URL-friendly strings, eliminating padding and characters like ' ' and '/' that can interfere with URI templates. Here's an ASP.NET implementation:

<code class="language-csharp">/// <summary>
/// URL-safe Base64 encoding using UTF-8.
/// </summary>
/// <param name="str">The string to encode.</param>
/// <returns>The URL-safe Base64 encoded string.</returns>
public static string UrlSafeBase64Encode(string str)
{
    byte[] encodedBytes = Encoding.UTF8.GetBytes(str);
    return HttpServerUtility.UrlTokenEncode(encodedBytes);
}

/// <summary>
/// URL-safe Base64 decoding using UTF-8.
/// </summary>
/// <param name="str">The URL-safe Base64 encoded string.</param>
/// <returns>The decoded string.</returns>
public static string UrlSafeBase64Decode(string str)
{
    byte[] decodedBytes = HttpServerUtility.UrlTokenDecode(str);
    return Encoding.UTF8.GetString(decodedBytes);
}</code>
Copy after login

To utilize this URL-safe Base64 encoding, simply replace your existing Base64 encoding and decoding methods with the functions provided above.

Important Considerations:

  • The output isn't strictly compliant with standard Base64, as URL-unsafe characters are substituted.
  • This differs from the RFC 4648 base64url standard; padding ('=') is replaced with '0', '1', or '2' based on the number of padding characters removed.

The above is the detailed content of How to Implement Modified Base64 URL Encoding and Decoding in ASP.NET?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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