123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Security.Cryptography;
- using System.Text;
- namespace CZFW.Core.Security
- {
- public class DesEncrypt
- {
- private const string DesKey = "CZCMS_desencrypt_2016";
- #region ========加密========
- /// <summary>
- /// 加密
- /// </summary>
- /// <param name="text"></param>
- /// <returns></returns>
- public static string Encrypt(string text)
- {
- return Encrypt(text, DesKey);
- }
- /// <summary>
- /// 加密数据
- /// </summary>
- /// <param name="text"></param>
- /// <param name="sKey"></param>
- /// <returns></returns>
- public static string Encrypt(string text, string sKey)
- {
- DESCryptoServiceProvider des = new DESCryptoServiceProvider();
- var inputByteArray = Encoding.Default.GetBytes(text);
- des.Key = Encoding.ASCII.GetBytes(GetMd5Hash(DesKey).Substring(0, 8));
- des.IV = Encoding.ASCII.GetBytes(GetMd5Hash(DesKey).Substring(0, 8));
- MemoryStream ms = new 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();
- }
- #endregion
- #region ========解密========
- /// <summary>
- /// 解密
- /// </summary>
- /// <param name="text"></param>
- /// <returns></returns>
- public static string Decrypt(string text)
- {
- return !string.IsNullOrEmpty(text) ? Decrypt(text, DesKey) : "";
- }
- /// <summary>
- /// 解密数据
- /// </summary>
- /// <param name="text"></param>
- /// <param name="sKey"></param>
- /// <returns></returns>
- public static string Decrypt(string text, string sKey)
- {
- DESCryptoServiceProvider des = new DESCryptoServiceProvider();
- var len = text.Length / 2;
- byte[] inputByteArray = new byte[len];
- int x;
- for (x = 0; x < len; x++)
- {
- var i = Convert.ToInt32(text.Substring(x * 2, 2), 16);
- inputByteArray[x] = (byte)i;
- }
- des.Key = Encoding.ASCII.GetBytes(GetMd5Hash(DesKey).Substring(0, 8));
- des.IV = Encoding.ASCII.GetBytes(GetMd5Hash(DesKey).Substring(0, 8));
- MemoryStream ms = new MemoryStream();
- CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
- cs.Write(inputByteArray, 0, inputByteArray.Length);
- cs.FlushFinalBlock();
- return Encoding.Default.GetString(ms.ToArray());
- }
- public static string GetMd5Hash(string input)
- {
- MD5 md5Hash = MD5.Create();
- // Convert the input string to a byte array and compute the hash.
- byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
- // Create a new Stringbuilder to collect the bytes
- // and create a string.
- StringBuilder sBuilder = new StringBuilder();
- // Loop through each byte of the hashed data
- // and format each one as a hexadecimal string.
- for (int i = 0; i < data.Length; i++)
- {
- sBuilder.Append(data[i].ToString("X2"));
- }
- // Return the hexadecimal string.
- return sBuilder.ToString();
- }
- #endregion
- /// <summary>
- /// Base64加密
- /// </summary>
- /// <param name="encoding">编码格式</param>
- /// <param name="json">加密的字符串</param>
- /// <returns></returns>
- public static string Base64Encrypt(Encoding encoding,string json)
- {
- string encodeString = "";
- byte[] b = encoding.GetBytes(json);
- try {
- encodeString = Convert.ToBase64String(b);
- }
- catch (Exception ex)
- {
- encodeString = json;
- }
- return encodeString;
- }
- /// <summary>
- /// Base64解密
- /// </summary>
- /// <param name="encoding"></param>
- /// <param name="json"></param>
- /// <returns></returns>
- public static string BaseDecrypt(Encoding encoding, string json)
- {
- string decryptString = "";
- byte[] b = Convert.FromBase64String(json);
- try
- {
- decryptString = encoding.GetString(b);
- }
- catch (Exception ex)
- {
- decryptString = json;
- }
- return decryptString;
- }
- public static string byte2hex(byte[] abyte0)
- {
-
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i<abyte0.Length; i++)
- {
-
- if ((abyte0[i] & 0xff) < 16)
- {
- sb.Append("0");
- }
- sb.Append(Convert.ToString((long)abyte0[i] & (long)255, 16));
- }
- return sb.ToString();
- }
- public string MD5Base64(byte[] data)
- {
- //MD5 md5 = new MD5CryptoServiceProvider();
- //byte[] md5Str = md5.ComputeHash(data);
- var md5 = MD5.Create();
- var bs = md5.ComputeHash(data);
- var str = Convert.ToBase64String(bs);
- return str;
- }
- /// <summary>
- /// 重写Java 16进制MD5加密
- /// </summary>
- /// <param name="json"></param>
- /// <returns></returns>
- public static string JavaMD5(string json)
- {
- MD5 md5Hash = MD5.Create();
- // Convert the input string to a byte array and compute the hash.
- byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(json));
- return byte2hex(data);
- }
- }
- }
|