C# implements Base64 processing encryption and decryption, encoding and decoding sample code

黄舟
Release: 2017-03-29 11:38:07
Original
2695 people have browsed it

This article mainly introduces C# to implement encryption, decryption, encoding and decoding of Base64 processing, and analyzes the related skills of base64 encoding and decoding operations based on C# in the form of examples. Friends in need can refer to the following

The example of this article describes the C# implementation of encryption, decryption, encoding and decoding of Base64 processing. Share it with everyone for your reference, the details are as follows:

using System;
using System.Text;
namespace Common
{
  /// <summary>
  /// 实现Base64加密解密
  /// 作者:周公
  /// </summary>
  public sealed class Base64
  {
    /// <summary>
    /// Base64加密
    /// </summary>
    /// <param name="codeName">加密采用的编码方式</param>
    /// <param name="source">待加密的明文</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加密,采用utf8编码方式加密
    /// </summary>
    /// <param name="source">待加密的明文</param>
    /// <returns>加密后的字符串</returns>
    public static string EncodeBase64(string source)
    {
      return EncodeBase64(Encoding.UTF8, source);
    }
    /// <summary>
    /// Base64解密
    /// </summary>
    /// <param name="codeName">解密采用的编码方式,注意和加密时采用的方式一致</param>
    /// <param name="result">待解密的密文</param>
    /// <returns>解密后的字符串</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解密,采用utf8编码方式解密
    /// </summary>
    /// <param name="result">待解密的密文</param>
    /// <returns>解密后的字符串</returns>
    public static string DecodeBase64(string result)
    {
      return DecodeBase64(Encoding.UTF8, result);
    }
  }
}
Copy after login

The above is the detailed content of C# implements Base64 processing encryption and decryption, encoding and decoding sample code. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!