//////StevenDataEncryption 的摘要说明 /// public class StevenDataEncryption { public StevenDataEncryption() { // //TODO: 在此处添加构造函数逻辑 // } //默認密鈅加密算法 public static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; #region DES加密算法 /// /// DES加密方法 /// /// 要加密的字符串 /// 密鈅的位數 /// public static string EncryptionDES(string encryptString, string encryptKey) { try { byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8)); byte[] rgbIV = Keys; byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Convert.ToBase64String(mStream.ToArray()); } catch { return encryptString; } } #endregion #region DES解密算法 /// /// DES解密方法 /// /// 要解密的字符串 /// 密鈅的位數 /// public static string DecryptDES(string decryptString, string decryptKey) { try { byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8)); byte[] rgbIV = Keys; byte[] inputByteArray = Convert.FromBase64String(decryptString); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Encoding.UTF8.GetString(mStream.ToArray()); } catch { return decryptString; } } #endregion #region AES加密 /// /// AES加密 /// /// 加密字符 /// 加密的密码 /// 密钥(隨機生成) /// public static string AESEncrypt(string text, string password, string iv) { try { RijndaelManaged rijndaelCipher = new RijndaelManaged(); rijndaelCipher.Mode = CipherMode.CBC; rijndaelCipher.Padding = PaddingMode.PKCS7; rijndaelCipher.KeySize = 128; rijndaelCipher.BlockSize = 128; byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password); byte[] keyBytes = new byte[16]; int len = pwdBytes.Length; if (len > keyBytes.Length) len = keyBytes.Length; System.Array.Copy(pwdBytes, keyBytes, len); rijndaelCipher.Key = keyBytes; byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv); rijndaelCipher.IV = ivBytes; ICryptoTransform transform = rijndaelCipher.CreateEncryptor(); byte[] plainText = Encoding.UTF8.GetBytes(text); byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length); return Convert.ToBase64String(cipherBytes); } catch { return text; } } #endregion #region 随机生成密钥 /// /// 随机生成密钥 /// /// public static string GetIv(int n) { char[] arrChar = new char[] { 'a', 'b', 'd', 'c', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'p', 'r', 'q', 's', 't', 'u', 'v', 'w', 'z', 'y', 'x', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'Q', 'P', 'R', 'T', 'S', 'V', 'U', 'W', 'X', 'Y', 'Z' }; StringBuilder num = new StringBuilder(); Random rnd = new Random(DateTime.Now.Millisecond); for (int i = 0; i < n; i++) { num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString()); } return num.ToString(); } #endregion #region AES解密 /// /// AES解密 /// /// /// /// /// public static string AESDecrypt(string text, string password,string iv) { try { RijndaelManaged rijndaelCipher = new RijndaelManaged(); rijndaelCipher.Mode = CipherMode.CBC; rijndaelCipher.Padding = PaddingMode.PKCS7; rijndaelCipher.KeySize = 128; rijndaelCipher.BlockSize = 128; byte[] encryptedData = Convert.FromBase64String(text); byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password); byte[] keyBytes = new byte[16]; int len = pwdBytes.Length; if (len > keyBytes.Length) len = keyBytes.Length; System.Array.Copy(pwdBytes, keyBytes, len); rijndaelCipher.Key = keyBytes; byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv); rijndaelCipher.IV = ivBytes; ICryptoTransform transform = rijndaelCipher.CreateDecryptor(); byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length); return Encoding.UTF8.GetString(plainText); } catch { return text; } } #endregion #region 哈希加密 /// /// 哈希加密(解密哈希需要加密匹配字符串) /// /// 要加密的字符 /// public static string HSEncrypt(string text) { //將數據轉化為字節流 byte[] keyBytes = System.Text.Encoding.UTF32.GetBytes(text); SHA1 SHhash = new SHA1Managed(); byte[] arHashvalue; arHashvalue = SHhash.ComputeHash(keyBytes); StringBuilder sb = new StringBuilder(); for (int i = 0; i < arHashvalue.Length; i++) { sb.Append(arHashvalue[i].ToString()); } return sb.ToString(); } #endregion #region 哈希破解方法(破解數字) /// /// 哈希破解方法 /// /// 輸入加密后的哈希值 /// public static string HSDecrypt(string text) { //破解數字 string HSResult=""; int num = 1; for (int i = 0; i < num; i++) { HSResult=HSEncrypt(num.ToString()); if (HSResult == text) { return num.ToString(); } else { num++; } } return num.ToString() ; } #endregion }