/// <summary> /// 字符串简单加密(DES算法),字符串长度尽量控制在 64 个字符内。 /// 使用默认的加密向量和加密密钥 /// </summary> /// <param name="AStrSource">需要加密的字符串</param> /// <returns>如果返回值 与 传入的值 相等,则说明加密失败</returns> public static string EncryptStringY(string AStrSource) { string LStrReturn = string.Empty; try { DESCryptoServiceProvider LDESCrypto = new DESCryptoServiceProvider(); byte[] LByteArraySource = Encoding.Unicode.GetBytes(AStrSource); LDESCrypto.Key = UnicodeEncoding.ASCII.GetBytes(CreateMD5HashString("Y0ungEncryptKEY").Substring(0, 8)); LDESCrypto.IV = UnicodeEncoding.ASCII.GetBytes(CreateMD5HashString("Y0ungEncryptIV").Substring(0, 8)); MemoryStream LMemoryStream = new MemoryStream(); CryptoStream LCryptoStream = new CryptoStream(LMemoryStream, LDESCrypto.CreateEncryptor(), CryptoStreamMode.Write); LCryptoStream.Write(LByteArraySource, 0, LByteArraySource.Length); LCryptoStream.FlushFinalBlock(); StringBuilder LStrBuilder = new StringBuilder(); foreach (byte LByte in LMemoryStream.ToArray()) { LStrBuilder.Append(LByte.ToString("X2").ToUpper()); } LStrReturn = LStrBuilder.ToString(); } catch { LStrReturn = AStrSource; } return(LStrReturn); }
/// <summary> /// 将输入的字符串转换成32位的hash值 /// </summary> /// <param name="AStrPassword"></param> /// <returns></returns> private static string CreateMD5HashString(string AStrPassword) { string LStrHashPassword = string.Empty; try { MD5CryptoServiceProvider LMD5Crypto = new MD5CryptoServiceProvider(); byte[] LByteArray = Encoding.Unicode.GetBytes(AStrPassword); LByteArray = LMD5Crypto.ComputeHash(LByteArray); StringBuilder LStrBuilder = new StringBuilder(); foreach (byte LByte in LByteArray) { LStrBuilder.Append(LByte.ToString("X2").ToUpper()); } LStrHashPassword = LStrBuilder.ToString(); } catch { LStrHashPassword = "******"; } return(LStrHashPassword); }