using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace Ips.Library.Basic { public static class SecurityUtil { public static string EncodeBase64(string source) { return Convert.ToBase64String(Activator.CreateInstance().GetBytes(source)); } public static string EncodeToMD5(string txt) { if (txt == null) txt = string.Empty; MD5 md = MD5.Create(); byte[] bytes = Encoding.Default.GetBytes(txt); byte[] buffer2 = md.ComputeHash(bytes); md.Clear(); return ToHex(buffer2); } public static string EncodeToMD5(byte[] bytes) { MD5 md = MD5.Create(); byte[] md5code = md.ComputeHash(bytes); md.Clear(); return ToHex(md5code); } public static string DecodeBase64(string source) { return Encoding.ASCII.GetString(Convert.FromBase64String(source)); } private static byte[] GetAesKey(string key) { if (string.IsNullOrEmpty(key)) { throw new ArgumentNullException("key", "Aes密钥不能为空"); } if (key.Length < 32) { key = key.PadRight(32, '0'); } if (key.Length > 32) { key = key.Substring(0, 32); } return Encoding.UTF8.GetBytes(key); } public static string EncryptAES(string source, string key) { using (var aesProvider = Aes.Create()) { aesProvider.Key = GetAesKey(key); aesProvider.Mode = CipherMode.ECB; aesProvider.Padding = PaddingMode.PKCS7; using (ICryptoTransform cryptoTransform = aesProvider.CreateEncryptor()) { byte[] inputBuffers = Encoding.UTF8.GetBytes(source); byte[] results = cryptoTransform.TransformFinalBlock(inputBuffers, 0, inputBuffers.Length); aesProvider.Clear(); aesProvider.Dispose(); return Convert.ToBase64String(results, 0, results.Length); } } } public static string DecryptAES(string source, string key) { using (var aesProvider = Aes.Create()) { aesProvider.Key = GetAesKey(key); aesProvider.Mode = CipherMode.ECB; aesProvider.Padding = PaddingMode.PKCS7; using (ICryptoTransform cryptoTransform = aesProvider.CreateDecryptor()) { byte[] inputBuffers = Convert.FromBase64String(source); byte[] results = cryptoTransform.TransformFinalBlock(inputBuffers, 0, inputBuffers.Length); aesProvider.Clear(); return Encoding.UTF8.GetString(results); } } } public static string EncryptRSA(string source, string key) { var cspp = new CspParameters { KeyContainerName = key }; var rsa = new RSACryptoServiceProvider(cspp) { PersistKeyInCsp = true }; byte[] bytes = rsa.Encrypt(Encoding.UTF8.GetBytes(source), true); return BitConverter.ToString(bytes); } public static string DecryptRSA(string source, string key) { var cspp = new CspParameters() { KeyContainerName = key }; var rsa = new RSACryptoServiceProvider(cspp) { PersistKeyInCsp = true }; string[] decryptArray = source.Split(new[] { "-" }, StringSplitOptions.None); byte[] decryptByteArray = Array.ConvertAll(decryptArray, (s => Convert.ToByte(byte.Parse(s, NumberStyles.HexNumber)))); byte[] bytes = rsa.Decrypt(decryptByteArray, true); return Encoding.UTF8.GetString(bytes); } public static string EncryptDES(string Text, string sKey) { DES des = DES.Create(); byte[] inputByteArray; inputByteArray = Encoding.UTF8.GetBytes(Text); des.Key = ASCIIEncoding.ASCII.GetBytes(EncodeToMD5(sKey).Substring(0, 8)); des.IV = ASCIIEncoding.ASCII.GetBytes(EncodeToMD5(sKey).Substring(0, 8)); System.IO.MemoryStream ms = new System.IO.MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Convert.ToBase64String(ms.ToArray()); } public static string DecryptDES(string Text, string sKey) { DES des = DES.Create(); int len; len = Text.Length / 2; byte[] inputByteArray = Convert.FromBase64String(Text); des.Key = ASCIIEncoding.ASCII.GetBytes(EncodeToMD5(sKey).Substring(0, 8)); des.IV = ASCIIEncoding.ASCII.GetBytes(EncodeToMD5(sKey).Substring(0, 8)); System.IO.MemoryStream ms = new System.IO.MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Encoding.UTF8.GetString(ms.ToArray()); } public static string ToHex(byte[] buffer) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < buffer.Length - 1; i++) { sb.Append(buffer[i].ToString("x").PadLeft(2, '0')); } return sb.ToString(); } } }