标签:des style blog color os io ar div
/// <summary> /// DES加密 /// </summary> /// <param name="sourceString">需要加密字符串</param> /// <param name="key">加密密钥</param> /// <param name="iv"></param> /// <returns></returns> public static string Encrypt(string sourceString, string key, string iv) { try { key = key.Substring(0,8); iv = iv.Substring(0, 8); byte[] btKey = Encoding.UTF8.GetBytes(key); byte[] btIV = Encoding.UTF8.GetBytes(iv); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); using (MemoryStream ms = new MemoryStream()) { byte[] inData = Encoding.UTF8.GetBytes(sourceString); try { using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(inData, 0, inData.Length); cs.FlushFinalBlock(); } return Convert.ToBase64String(ms.ToArray()); } catch { return sourceString; } } } catch(Exception ex) { return ex+"cath DES加密出错"; } } /// <summary> /// DES解密字符串 /// </summary> /// <param name="encryptedString"></param> /// <param name="key"></param> /// <param name="iv"></param> /// <returns></returns> public static string Decrypt(string encryptedString, string key, string iv) { key = key.Substring(0, 8); iv = iv.Substring(0, 8); byte[] btKey = Encoding.UTF8.GetBytes(key); byte[] btIV = Encoding.UTF8.GetBytes(iv); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); using (MemoryStream ms = new MemoryStream()) { byte[] inData = Convert.FromBase64String(encryptedString); try { using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(inData, 0, inData.Length); cs.FlushFinalBlock(); } return Encoding.UTF8.GetString(ms.ToArray()); } catch { return encryptedString; } } }
标签:des style blog color os io ar div
原文地址:http://www.cnblogs.com/yhdkzy/p/3927165.html