/// <summary> /// 字符串加密解密类 /// </summary> public static class StringSecurity { /// <summary> /// DES加密 /// </summary> /// <param name="input"></param> /// <param name="keystr"></param> /// <param name="ivstr"></param> /// <param name="encod"></param> /// <returns></returns> public static string DESEncrypt(string input, string keystr, string ivstr, Encoding encod) { MemoryStream? ms = null; CryptoStream? cs = null; StreamWriter? sw = null; DES des = DES.Create(); try { byte[] key = Encoding.UTF8.GetBytes(keystr); byte[] iv = Encoding.UTF8.GetBytes(ivstr); ms = new MemoryStream(); cs = new CryptoStream(ms, des.CreateEncryptor(key, iv), CryptoStreamMode.Write); sw = new StreamWriter(cs, encod); sw.Write(input); sw.Flush(); cs.FlushFinalBlock(); return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length); } finally { sw?.Close(); cs?.Close(); ms?.Close(); } } /// <summary> /// DES解密 /// </summary> /// <param name="input"></param> /// <param name="keystr"></param> /// <param name="ivstr"></param> /// <param name="encod"></param> /// <returns></returns> public static string DESDecrypt(string input, string keystr, string ivstr, Encoding encod) { MemoryStream? ms = null; CryptoStream? cs = null; StreamReader? sr = null; DES des = DES.Create(); try { byte[] key = Encoding.UTF8.GetBytes(keystr); byte[] iv = Encoding.UTF8.GetBytes(ivstr); ms = new MemoryStream(Convert.FromBase64String(input)); cs = new CryptoStream(ms, des.CreateDecryptor(key, iv), CryptoStreamMode.Read); sr = new StreamReader(cs, encod); return sr.ReadToEnd(); } finally { sr?.Close(); cs?.Close(); ms?.Close(); } } /// <summary> /// SHA1加密 /// </summary> /// <param name="input"></param> /// <param name="encod"></param> /// <returns></returns> public static string SHA1Encrypt(string input, Encoding encod) { SHA1 sha1 = SHA1.Create(); byte[] encryptedBytes = sha1.ComputeHash(encod.GetBytes(input)); StringBuilder sb = new(); for (int i = 0; i < encryptedBytes.Length; i++) { sb.AppendFormat("{0:x2}", encryptedBytes[i]); } return sb.ToString(); } /// <summary> /// MD5加密(32位) /// </summary> /// <param name="input"></param> /// <param name="encod"></param> /// <returns></returns> public static string MD5Encrypt(string input, Encoding encod) { MD5 md5 = MD5.Create(); byte[] source = md5.ComputeHash(encod.GetBytes(input)); StringBuilder sb = new(); for (int i = 0; i < source.Length; i++) { sb.Append(source[i].ToString("X"));//x小写 X大写 x2填充对齐 } return sb.ToString(); } /// <summary> /// Base64加密 /// </summary> /// <param name="input"></param> /// <param name="encod"></param> /// <returns></returns> public static string Base64Encrypt(string input, Encoding encod) { return Convert.ToBase64String(encod.GetBytes(input)); } /// <summary> /// Base64解密 /// </summary> /// <param name="input"></param> /// <param name="encod"></param> /// <returns></returns> public static string Base64Decrypt(string input, Encoding encod) { return encod.GetString(Convert.FromBase64String(input)); } }
标签:加密,string,Encoding,C#,encod,解密,ms,cs,input From: https://www.cnblogs.com/smartnn/p/17214640.html