C Realizes the Encryption Decryption Encoding Decoding Example of Base64 Processing

  • 2021-11-29 08:19:30
  • OfStack

This article describes the example of C # to achieve Base64 processing of encryption, decryption, encoding and decoding. Share it for your reference, as follows:


using System;
using System.Text;
namespace Common
{
  /// <summary>
  ///  Realization Base64 Encryption and decryption 
  ///  Author: Duke Zhou 
  /// </summary>
  public sealed class Base64
  {
    /// <summary>
    /// Base64 Encryption 
    /// </summary>
    /// <param name="codeName"> Encoding method used for encryption </param>
    /// <param name="source"> Clear text to be encrypted </param>
    /// <returns></returns>
    public static string EncodeBase64(Encoding encode, string source)
    {
      byte[] bytes = encode.GetBytes(source);
      try
      {
        encode = Convert.ToBase64String(bytes);
      }
      catch
      {
        encode = source;
      }
      return encode;
    }
    /// <summary>
    /// Base64 Encryption, using utf8 Encoding encryption 
    /// </summary>
    /// <param name="source"> Clear text to be encrypted </param>
    /// <returns> Encrypted string </returns>
    public static string EncodeBase64(string source)
    {
      return EncodeBase64(Encoding.UTF8, source);
    }
    /// <summary>
    /// Base64 Decryption 
    /// </summary>
    /// <param name="codeName"> Encoding method, attention and encryption method used for decryption 1 To </param>
    /// <param name="result"> Ciphertext to be decrypted </param>
    /// <returns> Decrypted string </returns>
    public static string DecodeBase64(Encoding encode, string result)
    {
      string decode = "";
      byte[] bytes = Convert.FromBase64String(result);
      try
      {
        decode = encode.GetString(bytes);
      }
      catch
      {
        decode = result;
      }
      return decode;
    }
    /// <summary>
    /// Base64 Decrypt, adopt utf8 Encoding decryption 
    /// </summary>
    /// <param name="result"> Ciphertext to be decrypted </param>
    /// <returns> Decrypted string </returns>
    public static string DecodeBase64(string result)
    {
      return DecodeBase64(Encoding.UTF8, result);
    }
  }
}

PS: Here are several practical base64 online encoding and decoding tools for everyone to use:

BASE64 encoding and decoding tool:
http://tools.ofstack.com/transcoding/base64

Online image conversion BASE64 tool:
http://tools.ofstack.com/transcoding/img2base64

Base64 Online Coding and Decoding UTF-8 Version:
http://tools.ofstack.com/tools/base64_decode-utf8.php

Base64 Online Coding and Decoding gb2312 Version:
http://tools.ofstack.com/tools/base64_decode-gb2312.php

For more readers interested in C # related content, please check the topics on this site: "Summary of C # Coding Operation Skills", "Summary of XML File Operation Skills in C #", "Tutorial on Usage of C # Common Controls", "Tutorial on Usage of WinForm Controls", "Tutorial on Data Structures and Algorithms", "Introduction to C # Object-Oriented Programming" and "Summary of Thread Use Skills in C # Programming"

I hope this article is helpful to everyone's C # programming.


Related articles: