C # implements BASE64 processing of cryptographic decryption, encoding and decoding sample code

Source: Internet
Author: User
This article mainly describes the C # implementation of BASE64 processing of encryption and decryption, encoding and decoding, combined with the example of the implementation of C # Base64 encoding and decoding operation-related skills, the need for friends can refer to the following





The example of this paper describes the C # implementation of BASE64 processing of encryption and decryption, encoding and decoding. Share to everyone for your reference, as follows:


using System;
using System.Text;
namespace Common
{
  /// <summary>
  /// Implement Base64 encryption and decryption
  /// Author: Duke
  /// </ summary>
  public sealed class Base64
  {
    /// <summary>
    /// Base64 encryption
    /// </ summary>
    /// <param name = "codeName"> Encoding used for encryption </ param>
    /// <param name = "source"> plaintext 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
    /// </ summary>
    /// <param name = "source"> plaintext 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"> The encoding method used for decryption, pay attention to the same method used for encryption </ param>
    /// <param name = "result"> the ciphertext to be decrypted </ param>
    /// <returns> the 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 decryption, using utf8 encoding
    /// </ summary>
    /// <param name = "result"> the ciphertext to be decrypted </ param>
    /// <returns> the decrypted string </ returns>
    public static string DecodeBase64 (string result)
    {
      return DecodeBase64 (Encoding.UTF8, result);
    }
  }
} 
Related Article

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.