using System; using System.Security.Cryptography; using System.Text; namespace DotNet.Utilities { /// <summary> /// Encrypt 的摘要說明。 /// </summary> publ ...
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,"MATICSOFT"); } /// <summary> /// 使用預設密鑰字元串解密string /// </summary> /// <param name="original">密文</param> /// <returns>明文</returns> public static string Decrypt(string original) { return Decrypt(original,"MATICSOFT",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("MATICSOFT"); 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("MATICSOFT"); 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 } }DEncrypt
using System; using System.Security.Cryptography; using System.Text; namespace DotNet.Utilities { /// <summary> /// DES加密/解密類。 /// </summary> public class DESEncrypt { public DESEncrypt() { } #region ========加密======== /// <summary> /// 加密 /// </summary> /// <param name="Text"></param> /// <returns></returns> public static string Encrypt(string Text) { return Encrypt(Text,"MATICSOFT"); } /// <summary> /// 加密數據 /// </summary> /// <param name="Text"></param> /// <param name="sKey"></param> /// <returns></returns> public static string Encrypt(string Text,string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray; inputByteArray=Encoding.Default.GetBytes(Text); des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); System.IO.MemoryStream ms=new System.IO.MemoryStream(); CryptoStream cs=new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write); cs.Write(inputByteArray,0,inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret=new StringBuilder(); foreach( byte b in ms.ToArray()) { ret.AppendFormat("{0:X2}",b); } return ret.ToString(); } #endregion #region ========解密======== /// <summary> /// 解密 /// </summary> /// <param name="Text"></param> /// <returns></returns> public static string Decrypt(string Text) { return Decrypt(Text,"MATICSOFT"); } /// <summary> /// 解密數據 /// </summary> /// <param name="Text"></param> /// <param name="sKey"></param> /// <returns></returns> public static string Decrypt(string Text,string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); int len; len=Text.Length/2; byte[] inputByteArray = new byte[len]; int x,i; for(x=0;x<len;x++) { i = Convert.ToInt32(Text.Substring(x * 2, 2), 16); inputByteArray[x]=(byte)i; } des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); System.IO.MemoryStream ms=new System.IO.MemoryStream(); CryptoStream cs=new CryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write); cs.Write(inputByteArray,0,inputByteArray.Length); cs.FlushFinalBlock(); return Encoding.Default.GetString(ms.ToArray()); } #endregion } }DESEncrypt
using System; using System.Security.Cryptography; using System.IO; namespace DotNet.Utilities { /// <summary> /// 加密解密實用類。 /// </summary> public class Encrypt { //密鑰 private static byte[] arrDESKey = new byte[] {42, 16, 93, 156, 78, 4, 218, 32}; private static byte[] arrDESIV = new byte[] {55, 103, 246, 79, 36, 99, 167, 3}; /// <summary> /// 加密。 /// </summary> /// <param name="m_Need_Encode_String"></param> /// <returns></returns> public static string Encode(string m_Need_Encode_String) { if (m_Need_Encode_String == null) { throw new Exception("Error: \n源字元串為空!!"); } DESCryptoServiceProvider objDES = new DESCryptoServiceProvider(); MemoryStream objMemoryStream = new MemoryStream(); CryptoStream objCryptoStream = new CryptoStream(objMemoryStream,objDES.CreateEncryptor(arrDESKey,arrDESIV),CryptoStreamMode.Write); StreamWriter objStreamWriter = new StreamWriter(objCryptoStream); objStreamWriter.Write(m_Need_Encode_String); objStreamWriter.Flush(); objCryptoStream.FlushFinalBlock(); objMemoryStream.Flush(); return Convert.ToBase64String(objMemoryStream.GetBuffer(), 0, (int)objMemoryStream.Length); } /// <summary> /// 解密。 /// </summary> /// <param name="m_Need_Encode_String"></param> /// <returns></returns> public static string Decode(string m_Need_Encode_String) { if (m_Need_Encode_String == null) { throw new Exception("Error: \n源字元串為空!!"); } DESCryptoServiceProvider objDES = new DESCryptoServiceProvider(); byte[] arrInput = Convert.FromBase64String(m_Need_Encode_String); MemoryStream objMemoryStream = new MemoryStream(arrInput); CryptoStream objCryptoStream = new CryptoStream(objMemoryStream,objDES.CreateDecryptor(arrDESKey,arrDESIV),CryptoStreamMode.Read); StreamReader objStreamReader = new StreamReader(objCryptoStream); return objStreamReader.ReadToEnd(); } /// <summary> /// md5 /// </summary> /// <param name="encypStr"></param> /// <returns></returns> public static string Md5(string encypStr) { string retStr; MD5CryptoServiceProvider m5 = new MD5CryptoServiceProvider(); byte[] inputBye; byte[] outputBye; inputBye = System.Text.Encoding.ASCII.GetBytes(encypStr); outputBye = m5.ComputeHash(inputBye); retStr = Convert.ToBase64String(outputBye); return (retStr); } } }Encrypt
using System; using System.Text; using System.Security.Cryptography; namespace DotNet.Utilities { /// <summary> /// 得到隨機安全碼(哈希加密)。 /// </summary> public class HashEncode { public HashEncode() { // // TODO: 在此處添加構造函數邏輯 // } /// <summary> /// 得到隨機哈希加密字元串 /// </summary> /// <returns></returns> public static string GetSecurity() { string Security = HashEncoding(GetRandomValue()); return Security; } /// <summary> /// 得到一個隨機數值 /// </summary> /// <returns></returns> public static string GetRandomValue() { Random Seed = new Random(); string RandomVaule = Seed.Next(1, int.MaxValue).ToString(); return RandomVaule; } /// <summary> /// 哈希加密一個字元串 /// </summary> /// <param name="Security"></param> /// <returns></returns> public static string HashEncoding(string Security) { byte[] Value; UnicodeEncoding Code = new UnicodeEncoding(); byte[] Message = Code.GetBytes(Security); SHA512Managed Arithmetic = new SHA512Managed(); Value = Arithmetic.ComputeHash(Message); Security = ""; foreach(byte o in Value) { Security += (int) o + "O"; } return Security; } } }HashEncode
using System; using System.Text; using System.Security.Cryptography; using System.IO; using System.Text.RegularExpressions; using System.Collections; namespace DotNet.Utilities { /// <summary> /// MySecurity(安全類) 的摘要說明。 /// </summary> public class MySecurity { /// <summary> /// 初始化安全類 /// </summary> public MySecurity() { ///預設密碼 key = "0123456789"; } private string key; //預設密鑰 private byte[] sKey; private byte[] sIV; #region 加密字元串 /// <summary> /// 加密字元串 /// </summary> /// <param name="inputStr">輸入字元串</param> /// <param name="keyStr">密碼,可以為“”</param> /// <returns>輸出加密後字元串</returns> static public string SEncryptString(string inputStr, string keyStr) { MySecurity ws = new MySecurity(); return ws.EncryptString(inputStr, keyStr); } /// <summary> /// 加密字元串 /// </summary> /// <param name="inputStr">輸入字元串</param> /// <param name="keyStr">密碼,可以為“”</param> /// <returns>輸出加密後字元串</returns> public string EncryptString(string inputStr, string keyStr) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); if (keyStr == "") keyStr = key; byte[] inputByteArray = Encoding.Default.GetBytes(inputStr); byte[] keyByteArray = Encoding.Default.GetBytes(keyStr); SHA1 ha = new SHA1Managed(); byte[] hb = ha.ComputeHash(keyByteArray); sKey = new byte[8]; sIV = new byte[8]; for (int i = 0; i < 8; i++) sKey[i] = hb[i]; for (int i = 8; i < 16; i++) sIV[i - 8] = hb[i]; des.Key = sKey; des.IV = sIV; MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); foreach (byte b in ms.ToArray()) { ret.AppendFormat("{0:X2}", b); } cs.Close(); ms.Close(); return ret.ToString(); } #endregion #region 加密字元串 密鑰為系統預設 0123456789 /// <summary> /// 加密字元串 密鑰為系統預設 /// </summary> /// <param name="inputStr">輸入字元串</param> /// <returns>輸出加密後字元串</returns> static public string SEncryptString(string inputStr) { MySecurity ws = new MySecurity(); return ws.EncryptString(inputStr, ""); } #endregion #region 加密文件 /// <summary> /// 加密文件 /// </summary> /// <param name="filePath">輸入文件路徑</param> /// <param name="savePath">加密後輸出文件路徑</param> /// <param name="keyStr">密碼,可以為“”</param> /// <returns></returns> public bool EncryptFile(string filePath, string savePath, string keyStr) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); if (keyStr == "") keyStr = key; FileStream fs = File.OpenRead(filePath); byte[] inputByteArray = new byte[fs.Length]; fs.Read(inputByteArray, 0, (int)fs.Length); fs.Close(); byte[] keyByteArray = Encoding.Default.GetBytes(keyStr); SHA1 ha = new SHA1Managed(); byte[] hb = ha.ComputeHash(keyByteArray); sKey = new byte[8]; sIV = new byte[8]; for (int i = 0; i < 8; i++) sKey[i] = hb[i]; for (int i = 8; i < 16; i++) sIV[i - 8] = hb[i]; des.Key = sKey; des.IV = sIV; MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); fs = File.OpenWrite(savePath); foreach (byte b in ms.ToArray()) { fs.WriteByte(b); } fs.Close(); cs.Close(); ms.Close(); return true; } #endregion #region 解密字元串 /// <summary> /// 解密字元串 /// </summary> /// <param name="inputStr">要解密的字元串</param> /// <param name="keyStr">密鑰</param> /// <returns>解密後的結果</returns> static public string SDecryptString(string inputStr, string keyStr) { MySecurity ws = new MySecurity(); return ws.DecryptString(inputStr, keyStr); } /// <summary> /// 解密字元串 密鑰為系統預設 /// </summary> /// <param name="inputStr">要解密的字元串</param> /// <returns>解密後的結果</returns> static public string SDecryptString(string inputStr) { MySecurity ws = new MySecurity(); return ws.DecryptString(inputStr, ""); } /// <summary> /// 解密字元串 /// </summary> /// <param name="inputStr">要解密的字元串</param> /// <param name="keyStr">密鑰</param> /// <returns>解密後的結果</returns> public string DecryptString(string inputStr, string keyStr) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); if (keyStr == "") keyStr = key; byte[] inputByteArray = new byte[inputStr.Length / 2]; for (int x = 0; x < inputStr.Length / 2; x++) { int i = (Convert.ToInt32(inputStr.Substring(x * 2, 2), 16)); inputByteArray[x] = (byte)i; } byte[] keyByteArray = Encoding.Default.GetBytes(keyStr); SHA1 ha = new SHA1Managed(); byte[] hb = ha.ComputeHash(keyByteArray); sKey = new byte[8]; sIV = new byte[8]; for (int i = 0; i < 8; i++) sKey[i] = hb[i]; for (int i = 8; i < 16; i++) sIV[i - 8] = hb[i]; des.Key = sKey; des.IV = sIV; MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray