| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- 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 };
- /// <summary>
- /// DES加密字符串
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// DES解密字符串
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// MD5加密
- /// </summary>
- /// <param name="str">加密字符</param>
- /// <param name="code">加密位数16/32</param>
- /// <returns></returns>
- 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();
- }
- /// <summary>
- /// 数据库密码解密
- /// </summary>
- /// <param name="decryptString"></param>
- /// <param name="key"></param>
- /// <returns></returns>
- 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.");
- }
- }
- }
- }
|