using System; using System.IO; using System.Text; using System.Security.Cryptography; namespace SHJX.Service.Common.Utils { public class Encrypt { //默认密钥向量 private static readonly byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; /// /// DES加密字符串 /// public static string EncryptDES(string encryptString, string encryptKey) { try { byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8)); byte[] rgbIV = Keys; byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); DESCryptoServiceProvider dCSP = new(); MemoryStream mStream = new(); CryptoStream cStream = new(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Convert.ToBase64String(mStream.ToArray()); } catch { return encryptString; } } /// /// DES解密字符串 /// public static string DecryptDES(string decryptString, string decryptKey) { try { byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey); byte[] rgbIV = Keys; byte[] inputByteArray = Convert.FromBase64String(decryptString); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Encoding.UTF8.GetString(mStream.ToArray()); } catch { return decryptString; } } /// /// MD5加密 /// /// 加密字符 /// 加密位数16/32 /// public static string GetMd5(string str, int Length = 32) { if (Length != 16 && Length != 32) throw new ArgumentException("Length参数无效,只能为16位或32位"); MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider(); byte[] b = MD5.ComputeHash(Encoding.UTF8.GetBytes(str)); StringBuilder StrB = new StringBuilder(); for (int i = 0; i < b.Length; i++) StrB.Append(b[i].ToString("x").PadLeft(2, '0')); if (Length == 16) return StrB.ToString(8, 16); else return StrB.ToString(); } /// /// 数据库密码解密 /// /// /// /// public static string Decrypt_asc(string decryptString, string key) { string retstr = string.Empty; string tempStr, tStr; tempStr = tStr = string.Empty; string curdestr = decryptString; int keyPtr = 0; int keylen = key.Length; int sourceLen = decryptString.Length; int tempVal, tempKey; for (int i = 0; i < sourceLen; i++) { tempVal = Asc(Right(decryptString, decryptString.Length - i)); tempKey = Asc(Right(key, keylen - keyPtr)); tempVal -= tempKey; do { if (tempVal < 0) { tempVal += 255; } } while (tempVal < 0); tStr = Chr(tempVal); retstr += tStr; keyPtr++; if (keyPtr > keylen) { keyPtr = 0; } } return retstr; } public static string Right(string sSource, int iLength) { return sSource.Substring(iLength > sSource.Length ? 0 : sSource.Length - iLength); } public static string Decrypt_asc(string decryptString) { string retstr = string.Empty; string[] strlist = decryptString.Split('.'); string curstr = string.Empty; foreach (string str in strlist) { if (!string.IsNullOrEmpty(str)) { curstr += Convert.ToChar(Convert.ToInt32(str)); } } retstr = Decrypt_asc(curstr, "5f945nfd721kdye95nsg327v3cb"); return retstr; } public static int Asc(string character) { int intAsciiCode = 0; if (!string.IsNullOrEmpty(character)) { //System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding(); intAsciiCode = Convert.ToInt16(character.ToCharArray()[0]); } return intAsciiCode; } public static string Chr(int asciiCode) { if (asciiCode >= 0 && asciiCode <= 255) { System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding(); byte[] byteArray = new byte[] { (byte)asciiCode }; string strCharacter = asciiEncoding.GetString(byteArray); return (strCharacter); } else { throw new Exception("ASCII Code is not valid."); } } } }