123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- using System;
- using System.Text;
- using System.Security.Cryptography;
- namespace WMS.Util
- {
- /// <summary>
- /// MD5加密类
- /// </summary>
- public class SecurityUtil
- {
- #region MD5加密
- /// <summary>
- /// 给一个字符串进行MD5加密
- /// </summary>
- /// <param name="strText">待加密字符串</param>
- /// <returns>加密后的字符串</returns>
- public static string MD5Encrypt(string strText)
- {
- MD5 md5 = new MD5CryptoServiceProvider();
- byte[] result = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(strText));
- return System.Text.Encoding.Default.GetString(result);
- }
- /// <summary>
- /// MD5加密
- /// </summary>
- /// <param name="str">加密字符</param>
- /// <param name="code">加密位数16/32</param>
- /// <returns></returns>
- public static string MD5(string str, int code)
- {
- MD5CryptoServiceProvider crypto = new MD5CryptoServiceProvider();
- byte[] bytes = Encoding.UTF7.GetBytes(str);
- bytes = crypto.ComputeHash(bytes);
- StringBuilder sb = new StringBuilder();
- foreach (byte num in bytes)
- {
- sb.AppendFormat("{0:x2}", num);
- }
- if (code == 16)
- {
- return sb.ToString().Substring(8, 16);
- }
- else
- {
- return sb.ToString();
- }
- }
- #endregion
- #region DES加密/解密
- /// <summary>
- /// DES加密数据
- /// </summary>
- /// <param name="Text"></param>
- /// <param name="sKey"></param>
- /// <returns></returns>
- public static string DesEncrypt(string Text, string sKey)
- {
- DESCryptoServiceProvider des = new DESCryptoServiceProvider();
- byte[] inputByteArray;
- inputByteArray = Encoding.Default.GetBytes(Text);
- des.Key = ASCIIEncoding.ASCII.GetBytes(MD5Encrypt(sKey).Substring(0, 8));
- des.IV = ASCIIEncoding.ASCII.GetBytes(MD5Encrypt(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();
- StringBuilder ret = new StringBuilder();
- foreach (byte b in ms.ToArray())
- {
- ret.AppendFormat("{0:X2}", b);
- }
- return ret.ToString();
- }
- /// <summary>
- /// DES解密数据
- /// </summary>
- /// <param name="Text"></param>
- /// <param name="sKey"></param>
- /// <returns></returns>
- public static string DesDecrypt(string Text, string sKey)
- {
- DESCryptoServiceProvider des = new DESCryptoServiceProvider();
- int len;
- len = Text.Length / 2;
- byte[] inputByteArray = new byte[len];
- int x, i;
- for (x = 0; x < len; x++)
- {
- i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
- inputByteArray[x] = (byte)i;
- }
- des.Key = ASCIIEncoding.ASCII.GetBytes(MD5Encrypt(sKey).Substring(0, 8));
- des.IV = ASCIIEncoding.ASCII.GetBytes(MD5Encrypt(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.Default.GetString(ms.ToArray());
- }
- #endregion
- #region Base64 编码
- /// <summary>
- /// Base64 编码
- /// </summary>
- /// <param name="encode">编码方式</param>
- /// <param name="source">要编码的字符串</param>
- /// <returns>返回编码后的字符串</returns>
- public static string EncodeBase64(Encoding encode, string source)
- {
- string result = "";
- byte[] bytes = encode.GetBytes(source);
- try
- {
- result = Convert.ToBase64String(bytes);
- }
- catch
- {
- result = source;
- }
- return result;
- }
- /// <summary>
- /// Base64 解码
- /// </summary>
- /// <param name="encode">解码方式</param>
- /// <param name="source">要解码的字符串</param>
- /// <returns>返回解码后的字符串</returns>
- public static string DecodeBase64(Encoding encode, string source)
- {
- string result = "";
- byte[] bytes = Convert.FromBase64String(source);
- try
- {
- result = encode.GetString(bytes);
- }
- catch
- {
- result = source;
- }
- return result;
- }
- #endregion
- #region AES加密/解密
- //AES加密
- public static string AesEncrypt(string value, string key, string iv = "")
- {
- if (string.IsNullOrEmpty(value)) return string.Empty;
- if (key == null) throw new Exception("未将对象引用设置到对象的实例。");
- if (key.Length < 16) throw new Exception("指定的密钥长度不能少于16位。");
- if (key.Length > 32) throw new Exception("指定的密钥长度不能多于32位。");
- if (key.Length != 16 && key.Length != 24 && key.Length != 32) throw new Exception("指定的密钥长度不明确。");
- if (!string.IsNullOrEmpty(iv))
- {
- if (iv.Length < 16) throw new Exception("指定的向量长度不能少于16位。");
- }
- var _keyByte = Encoding.UTF8.GetBytes(key);
- var _valueByte = Encoding.UTF8.GetBytes(value);
- using (var aes = new RijndaelManaged())
- {
- aes.IV = !string.IsNullOrEmpty(iv) ? Encoding.UTF8.GetBytes(iv) : Encoding.UTF8.GetBytes(key.Substring(0, 16));
- aes.Key = _keyByte;
- aes.Mode = CipherMode.CBC;
- aes.Padding = PaddingMode.PKCS7;
- var cryptoTransform = aes.CreateEncryptor();
- var resultArray = cryptoTransform.TransformFinalBlock(_valueByte, 0, _valueByte.Length);
- return Convert.ToBase64String(resultArray, 0, resultArray.Length);
- }
- }
- //AES解密
- public static string AesDecrypt(string value, string key, string iv = "")
- {
- if (string.IsNullOrEmpty(value)) return string.Empty;
- if (key == null) throw new Exception("未将对象引用设置到对象的实例。");
- if (key.Length < 16) throw new Exception("指定的密钥长度不能少于16位。");
- if (key.Length > 32) throw new Exception("指定的密钥长度不能多于32位。");
- if (key.Length != 16 && key.Length != 24 && key.Length != 32) throw new Exception("指定的密钥长度不明确。");
- if (!string.IsNullOrEmpty(iv))
- {
- if (iv.Length < 16) throw new Exception("指定的向量长度不能少于16位。");
- }
- var _keyByte = Encoding.UTF8.GetBytes(key);
- var _valueByte = Convert.FromBase64String(value);
- using (var aes = new RijndaelManaged())
- {
- aes.IV = !string.IsNullOrEmpty(iv) ? Encoding.UTF8.GetBytes(iv) : Encoding.UTF8.GetBytes(key.Substring(0, 16));
- aes.Key = _keyByte;
- aes.Mode = CipherMode.CBC;
- aes.Padding = PaddingMode.PKCS7;
- var cryptoTransform = aes.CreateDecryptor();
- var resultArray = cryptoTransform.TransformFinalBlock(_valueByte, 0, _valueByte.Length);
- return Encoding.UTF8.GetString(resultArray);
- }
- }
- #endregion
- }
- }
|