C#完成基於Base64的加密解密類實例。本站提示廣大學習愛好者:(C#完成基於Base64的加密解密類實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成基於Base64的加密解密類實例正文
本文實例講述了C#完成基於Base64的加密解密類。分享給年夜家供年夜家參考。詳細以下:
這個C#類是一個基於Base64的加密息爭密類,用戶可以可使用默許的秘鑰停止加密、解密,也能夠本身設定秘鑰停止加密息爭密,異常適用
using System; using System.Security.Cryptography; using System.Text; namespace DotNet.Utilities { /// <summary> /// Encrypt 的摘要解釋。 /// </summary> public class DEncrypt { /// <summary> /// 結構辦法 /// </summary> public DEncrypt() { } #region 應用 缺省密鑰字符串 加密/解密string /// <summary> /// 應用缺省密鑰字符串加密string /// </summary> /// <param name="original">明文</param> /// <returns>密文</returns> public static string Encrypt(string original) { return Encrypt(original,"sharejs.com"); } /// <summary> /// 應用缺省密鑰字符串解密string /// </summary> /// <param name="original">密文</param> /// <returns>明文</returns> public static string Decrypt(string original) { return Decrypt(original,"sharejs.com",System.Text.Encoding.Default); } #endregion #region 應用 給定密鑰字符串 加密/解密string /// <summary> /// 應用給定密鑰字符串加密string /// </summary> /// <param name="original">原始文字</param> /// <param name="key">密鑰</param> /// <param name="encoding">字符編碼計劃</param> /// <returns>密文</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> /// 應用給定密鑰字符串解密string /// </summary> /// <param name="original">密文</param> /// <param name="key">密鑰</param> /// <returns>明文</returns> public static string Decrypt(string original, string key) { return Decrypt(original,key,System.Text.Encoding.Default); } /// <summary> /// 應用給定密鑰字符串解密string,前往指定編碼方法明文 /// </summary> /// <param name="encrypted">密文</param> /// <param name="key">密鑰</param> /// <param name="encoding">字符編碼計劃</param> /// <returns>明文</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 應用 缺省密鑰字符串 加密/解密/byte[] /// <summary> /// 應用缺省密鑰字符串解密byte[] /// </summary> /// <param name="encrypted">密文</param> /// <param name="key">密鑰</param> /// <returns>明文</returns> public static byte[] Decrypt(byte[] encrypted) { byte[] key = System.Text.Encoding.Default.GetBytes("sharejs.com"); return Decrypt(encrypted,key); } /// <summary> /// 應用缺省密鑰字符串加密 /// </summary> /// <param name="original">原始數據</param> /// <param name="key">密鑰</param> /// <returns>密文</returns> public static byte[] Encrypt(byte[] original) { byte[] key = System.Text.Encoding.Default.GetBytes("sharejs.com"); return Encrypt(original,key); } #endregion #region 應用 給定密鑰 加密/解密/byte[] /// <summary> /// 生成MD5摘要 /// </summary> /// <param name="original">數據源</param> /// <returns>摘要</returns> public static byte[] MakeMD5(byte[] original) { MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider(); byte[] keyhash = hashmd5.ComputeHash(original); hashmd5 = null; return keyhash; } /// <summary> /// 應用給定密鑰加密 /// </summary> /// <param name="original">明文</param> /// <param name="key">密鑰</param> /// <returns>密文</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> /// 應用給定密鑰解密數據 /// </summary> /// <param name="encrypted">密文</param> /// <param name="key">密鑰</param> /// <returns>明文</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 } }
願望本文所述對年夜家的C#法式設計有所贊助。