C implementation based on Base64 encryption and decryption class instance

  • 2021-01-25 07:51:40
  • OfStack

The example of this article tells the C# implementation based on Base64 encryption and decryption class. Share with you for your reference. The details are as follows:

This C# class is a encryption and decryption class based on Base64. Users can use the default secret key for encryption and decryption, or they can set their own secret key for encryption and decryption. It is very practical


using System;
using System.Security.Cryptography; 
using System.Text;
namespace DotNet.Utilities
{
 /// <summary>
 /// Encrypt  Summary of the description. 
 /// </summary>
 public class DEncrypt
 {
  /// <summary>
  ///  A constructor 
  /// </summary>
  public DEncrypt() 
  { 
  } 
  #region  use   Default key string   encryption / decryption string
  /// <summary>
  ///  Use default key string encryption string
  /// </summary>
  /// <param name="original"> clear </param>
  /// <returns> cipher </returns>
  public static string Encrypt(string original)
  {
   return Encrypt(original,"sharejs.com");
  }
  /// <summary>
  ///  Decryption using the default key string string
  /// </summary>
  /// <param name="original"> cipher </param>
  /// <returns> clear </returns>
  public static string Decrypt(string original)
  {
   return Decrypt(original,"sharejs.com",System.Text.Encoding.Default);
  }
  #endregion
  #region  use   Given key string   encryption / decryption string
  /// <summary>
  ///  Encryption using the given key string string
  /// </summary>
  /// <param name="original"> The original text </param>
  /// <param name="key"> The key </param>
  /// <param name="encoding"> Character encoding scheme </param>
  /// <returns> cipher </returns>
  public static string Encrypt(string original, string key) 
  { 
   byte[] buff = System.Text.Encoding.Default.GetBytes(original); 
   byte[] kb = System.Text.Encoding.Default.GetBytes(key);
   return Convert.ToBase64String(Encrypt(buff,kb));   
  }
  /// <summary>
  ///  Decryption using the given key string string
  /// </summary>
  /// <param name="original"> cipher </param>
  /// <param name="key"> The key </param>
  /// <returns> clear </returns>
  public static string Decrypt(string original, string key)
  {
   return Decrypt(original,key,System.Text.Encoding.Default);
  }
  /// <summary>
  ///  Decryption using the given key string string, Returns the specified encoding mode plaintext 
  /// </summary>
  /// <param name="encrypted"> cipher </param>
  /// <param name="key"> The key </param>
  /// <param name="encoding"> Character encoding scheme </param>
  /// <returns> clear </returns>
  public static string Decrypt(string encrypted, string key,Encoding encoding) 
  {    
   byte[] buff = Convert.FromBase64String(encrypted); 
   byte[] kb = System.Text.Encoding.Default.GetBytes(key);
   return encoding.GetString(Decrypt(buff,kb));   
  } 
  #endregion
  #region  use   Default key string   encryption / decryption /byte[]
  /// <summary>
  ///  Decryption using the default key string byte[]
  /// </summary>
  /// <param name="encrypted"> cipher </param>
  /// <param name="key"> The key </param>
  /// <returns> clear </returns>
  public static byte[] Decrypt(byte[] encrypted) 
  { 
   byte[] key = System.Text.Encoding.Default.GetBytes("sharejs.com"); 
   return Decrypt(encrypted,key);   
  }
  /// <summary>
  ///  Use default key string encryption 
  /// </summary>
  /// <param name="original"> The original data </param>
  /// <param name="key"> The key </param>
  /// <returns> cipher </returns>
  public static byte[] Encrypt(byte[] original) 
  { 
   byte[] key = System.Text.Encoding.Default.GetBytes("sharejs.com"); 
   return Encrypt(original,key);   
  } 
  #endregion
  #region  use   A given key   encryption / decryption /byte[]
  /// <summary>
  ///  generate MD5 Abstract 
  /// </summary>
  /// <param name="original"> The data source </param>
  /// <returns> Abstract </returns>
  public static byte[] MakeMD5(byte[] original)
  {
   MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();  
   byte[] keyhash = hashmd5.ComputeHash(original);    
   hashmd5 = null; 
   return keyhash;
  }
  /// <summary>
  ///  Encrypt with the given key 
  /// </summary>
  /// <param name="original"> clear </param>
  /// <param name="key"> The key </param>
  /// <returns> cipher </returns>
  public static byte[] Encrypt(byte[] original, byte[] key) 
  { 
   TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();    
   des.Key = MakeMD5(key);
   des.Mode = CipherMode.ECB; 
   return des.CreateEncryptor().TransformFinalBlock(original, 0, original.Length);   
  } 
  /// <summary>
  ///  Decrypt data using the given key 
  /// </summary>
  /// <param name="encrypted"> cipher </param>
  /// <param name="key"> The key </param>
  /// <returns> clear </returns>
  public static byte[] Decrypt(byte[] encrypted, byte[] key) 
  { 
   TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); 
   des.Key = MakeMD5(key);  
   des.Mode = CipherMode.ECB; 
   return des.CreateDecryptor().TransformFinalBlock(encrypted, 0, encrypted.Length);
  } 
  #endregion
 }
}

I hope this article will be helpful to your C# programming.


Related articles: