C# MD5,SHA1,SHA256,SHA384,SHA512加密,DES自定义加密
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
/// <summary> /// 传入明文,返回用MD5加密后的字符串 /// </summary> /// <param name="str">要加密的字符串</param> /// <returns>MD5加密后的字符串</returns> public static string ToMD5_32(string str) { string passwordFormat = System.Web.Configuration.FormsAuthPasswordFormat.MD5.ToString(); return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, passwordFormat); } /// <summary> /// 传入明文,返回用SHA1密后的字符串 /// </summary> /// <param name="str">要加密的字符串</param> /// <returns>SHA1加密后的字符串</returns> public static string ToSHA1(string str) { string passwordFormat = System.Web.Configuration.FormsAuthPasswordFormat.SHA1.ToString(); return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, passwordFormat); } /// <summary> /// 传入明文,返回用SHA256密后的字符串 /// </summary> /// <param name="str">要加密的字符串</param> /// <returns>SHA256加密后的字符串</returns> public static string ToSHA256(string str) { byte[] SHA256Data = Encoding.UTF8.GetBytes(str); SHA256Managed Sha256 = new SHA256Managed(); byte[] by = Sha256.ComputeHash(SHA256Data); Sha256.Dispose(); return BitConverter.ToString(by).Replace("-", ""); } /// <summary> /// 传入明文,返回用SHA384密后的字符串 /// </summary> /// <param name="str">要加密的字符串</param> /// <returns>SHA384加密后的字符串</returns> public static string ToSHA384(string str) { byte[] SHA384Data = Encoding.UTF8.GetBytes(str); SHA384Managed Sha384 = new SHA384Managed(); byte[] by = Sha384.ComputeHash(SHA384Data); Sha384.Dispose(); return BitConverter.ToString(by).Replace("-", ""); } /// <summary> /// 传入明文,返回用SHA512密后的字符串 /// </summary> /// <param name="str">要加密的字符串</param> /// <returns>SHA512加密后的字符串</returns> public static string ToSHA512(string str) { byte[] SHA512Data = Encoding.UTF8.GetBytes(str); SHA512Managed Sha512 = new SHA512Managed(); byte[] by = Sha512.ComputeHash(SHA512Data); Sha512.Dispose(); return BitConverter.ToString(by).Replace("-", ""); } /// <summary> /// DES加密,私有方法 /// </summary> /// <param name="str">要加密字符串</param> /// <param name="salt">盐值</param> /// <returns></returns> public static string DESEncrypt(string str,string salt) { try { byte[] key = Encoding.Unicode.GetBytes(salt); byte[] data = Encoding.Unicode.GetBytes(str); DESCryptoServiceProvider descsp = new DESCryptoServiceProvider(); MemoryStream MStream = new MemoryStream(); CryptoStream CStream = new CryptoStream(MStream, descsp.CreateEncryptor(key, key), CryptoStreamMode.Write); CStream.Write(data, 0, data.Length); CStream.FlushFinalBlock(); byte[] temp = MStream.ToArray(); CStream.Close(); MStream.Close(); descsp.Dispose(); return Convert.ToBase64String(temp); } catch { return ""; } } /// <summary> /// DES解密,私有方法 /// </summary> /// <param name="str">要解密的字符串</param> /// <param name="salt">盐值</param> /// <returns></returns> public static string DESDecrypt(string str,string salt) { try { byte[] key = Encoding.Unicode.GetBytes(salt); byte[] data = Convert.FromBase64String(str); DESCryptoServiceProvider descsp = new DESCryptoServiceProvider(); MemoryStream MStream = new MemoryStream(); CryptoStream CStream = new CryptoStream(MStream, descsp.CreateDecryptor(key, key), CryptoStreamMode.Write); CStream.Write(data, 0, data.Length); CStream.FlushFinalBlock(); byte[] temp = MStream.ToArray(); CStream.Close(); MStream.Close(); descsp.Dispose(); return Encoding.Unicode.GetString(temp); } catch { return ""; } }
评论前必须登录!
注册