加密算法 全数卷帘加密解密,MD5加密,DES加密解密
#region 公共函数 静态成员 提供字符串MD5加密功能 // MD5加密,通常用在用户密码的加密和验证 ,暂时不可用 public static string MD5_Encrypt(string val) { string EncryptPassword = ""; //EncryptPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(val, "MD5"); return EncryptPassword; } #endregion #region 公共函数 静态成员 提供字符串DES加密功能 // DES加密 public static string DES_Encrypt(string val) { string Code = ""; if (!val.Equals("")) { DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, cryptoProvider.CreateEncryptor(Key_64, Iv_64), CryptoStreamMode.Write); StreamWriter sw = new StreamWriter(cs); sw.Write(val); sw.Flush(); cs.FlushFinalBlock(); ms.Flush(); int Length = (int)ms.Length; Code = Convert.ToBase64String(ms.GetBuffer(), 0, Length); } return Code; } #endregion #region 公共函数 静态成员 提供字符串DES解密功能 // DES解密 public static string DES_Decrypt(string val) { string Code = ""; if (!val.Equals("")) { DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); byte[] buffer = Convert.FromBase64String(val); MemoryStream ms = new MemoryStream(buffer); CryptoStream cs = new CryptoStream(ms, cryptoProvider.CreateDecryptor(Key_64, Iv_64), CryptoStreamMode.Read); StreamReader sr = new StreamReader(cs); Code = sr.ReadToEnd(); } return Code; } #endregion #region 线性加密 #region 私有函数 整型 尾列加密 private static int Sha_End(int val) { return T1[val]; } #endregion #region 私有函数 整型 尾列解密 private static int Sha_GetEnd(int val) { return T2[val]; } #endregion #region 私有函数 整型 单步卷帘加密 private static int Sha_Next(int val, int pre, bool flag) { int result = flag ? ((val KeyArray1[pre]) % 10) : ((val KeyArray2[pre]) % 10); return (result 1) % 10; } #endregion #region 私有函数 整型 单步卷帘解密 private static int Sha_Next2(int val, int pre, bool flag) { int result; val = (val 9) % 10; if (flag) { val = val >= KeyArray1[pre] ? val : val 10; result = val - KeyArray1[pre]; } else { val = val >= KeyArray2[pre] ? val : val 10; result = val - KeyArray2[pre]; } return result; } #endregion #region 公有方法 整型 全数卷帘加密 public static string Line_Encrypt(int intval) { return Line_Encrypt((long)intval); } public static string Line_Encrypt(long intval) { string val = intval.ToString(); int i; string temp = ""; bool flag = true; int temp2, temp3, temp4; //先求出最后一后数 int pre = Sha_End(Convert.ToInt32(val.Substring(val.Length - 1, 1))); //从倒数第二位开始 temp4 = pre; for (i = val.Length - 2; i >= 0; i--) { //取出该位数字 temp3 = Convert.ToInt32(val.Substring(i, 1)); //算出加密码,即倒数第二位加密后放在第一位。 temp2 = Sha_Next(temp3, pre, flag); temp = temp2; pre = temp2; flag = !flag; } return temp temp4; } #endregion #region 公有方法 整型 全数卷帘解密 public static string Line_Decrypt(int intval) { string val = intval.ToString(); int i; string temp = ""; bool flag = true; int temp2, temp3, temp4; //先求出最后一后数 int pre = Convert.ToInt32(val.Substring(val.Length - 1, 1)); //从倒数第二位开始 temp4 = Sha_GetEnd(pre); for (i = 0; i < val.Length - 1; i ) { //取出该位数字 temp3 = Convert.ToInt32(val.Substring(i, 1)); //算出加密码,即倒数第二位加密后放在第一位。 temp2 = Sha_Next2(temp3, pre, flag); temp = temp2 temp; pre = temp3; flag = !flag; } return temp temp4; } #endregion #endregion #endregion // By Simon #region MD5, 3DES encrypt and decrypt methods /// <summary> /// MD5加密方法 /// </summary> /// <param name="a_strValue">string to be encrypted</param> /// <returns>encrypted string</returns> public static string EncryptMD5String(string a_strValue) { try { //change to bytearray Byte[] hba = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")). ComputeHash(StringToByteArray(a_strValue)); return ByteArrayToString(hba); } catch (Exception ex) { throw (ex); } }
评论