/// <summary> /// 加密 /// </summary> /// <param name="encryptString"></param> /// <param name="keys">密钥(8位)</param> /// <returns></returns> public static string DESEncrypt(string encryptString, string keys) { using (System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider()) { byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); des.Key = ASCIIEncoding.ASCII.GetBytes(keys); des.IV = ASCIIEncoding.ASCII.GetBytes(keys); System.IO.MemoryStream ms = new System.IO.MemoryStream(); using (System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write)) { cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); cs.Close(); } string str = Convert.ToBase64String(ms.ToArray()); ms.Close(); return str; } }
/// <summary> /// 解密 /// </summary> /// <param name="decryptString"></param> /// <param name="keys"></param> /// <returns></returns> public static string DESDecrypt(string decryptString, string keys) { if (decryptString == string.Empty) { return ""; } if (keys.Length < 8) { return ""; } try { byte[] inputByteArray = Convert.FromBase64String(decryptString); using (System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider()) { des.Key = ASCIIEncoding.ASCII.GetBytes(keys); des.IV = ASCIIEncoding.ASCII.GetBytes(keys); System.IO.MemoryStream ms = new System.IO.MemoryStream(); using (System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write)) { cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); cs.Close(); } string str = Encoding.UTF8.GetString(ms.ToArray()); ms.Close(); return str; } } catch { return ""; } }