This article describes the implementation of Base64-based encryption and decryption classes in C#. Share it for your reference. The details are as follows:
This C# class is an encryption and decryption class based on Base64. Users can use the default key to encrypt and decrypt, or set the key to encrypt and decrypt, which is very practical.
using System; using ; using ; namespace { /// <summary> /// Summary description of Encrypt. /// </summary> public class DEncrypt { /// <summary> /// Construct method /// </summary> public DEncrypt() { } #region Use default key string Encryption/decryption string /// <summary> /// Encrypt string with default key string /// </summary> /// <param name="original">Plaintext</param> /// <returns>ciphertext</returns> public static string Encrypt(string original) { return Encrypt(original,""); } /// <summary> /// Decrypt string using default key string /// </summary> /// <param name="original">ciphertext</param> /// <returns>Plain text</returns> public static string Decrypt(string original) { return Decrypt(original,"",); } #endregion #region Use the given key string Encryption/decryption string /// <summary> /// Encrypt string with the given key string /// </summary> /// <param name="original">original text</param> /// <param name="key">key</param> /// <param name="encoding">character encoding scheme</param> /// <returns>ciphertext</returns> public static string Encrypt(string original, string key) { byte[] buff = (original); byte[] kb = (key); return Convert.ToBase64String(Encrypt(buff,kb)); } /// <summary> /// Decrypt string using the given key string /// </summary> /// <param name="original">ciphertext</param> /// <param name="key">key</param> /// <returns>Plain text</returns> public static string Decrypt(string original, string key) { return Decrypt(original,key,); } /// <summary> /// Decrypt the string using the given key string, return the specified encoding method plaintext /// </summary> /// <param name="encrypted">ciphertext</param> /// <param name="key">key</param> /// <param name="encoding">character encoding scheme</param> /// <returns>Plain text</returns> public static string Decrypt(string encrypted, string key,Encoding encoding) { byte[] buff = Convert.FromBase64String(encrypted); byte[] kb = (key); return (Decrypt(buff,kb)); } #endregion #region Use default key string Encryption/decryption/byte[] /// <summary> /// Decrypt byte[] using default key string /// </summary> /// <param name="encrypted">ciphertext</param> /// <param name="key">key</param> /// <returns>Plain text</returns> public static byte[] Decrypt(byte[] encrypted) { byte[] key = (""); return Decrypt(encrypted,key); } /// <summary> /// Encrypt with default key string /// </summary> /// <param name="original">Raw data</param> /// <param name="key">key</param> /// <returns>ciphertext</returns> public static byte[] Encrypt(byte[] original) { byte[] key = (""); return Encrypt(original,key); } #endregion #region Use the given key to encrypt/decrypt/byte[] /// <summary> /// Generate MD5 summary /// </summary> /// <param name="original">Data source</param> /// <returns> Summary</returns> public static byte[] MakeMD5(byte[] original) { MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider(); byte[] keyhash = (original); hashmd5 = null; return keyhash; } /// <summary> /// Encrypt with the given key /// </summary> /// <param name="original">Plaintext</param> /// <param name="key">key</param> /// <returns>ciphertext</returns> public static byte[] Encrypt(byte[] original, byte[] key) { TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); = MakeMD5(key); = ; return ().TransformFinalBlock(original, 0, ); } /// <summary> /// Decrypt data with the given key /// </summary> /// <param name="encrypted">ciphertext</param> /// <param name="key">key</param> /// <returns>Plain text</returns> public static byte[] Decrypt(byte[] encrypted, byte[] key) { TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); = MakeMD5(key); = ; return ().TransformFinalBlock(encrypted, 0, ); } #endregion } }
I hope this article will be helpful to everyone's C# programming.