¶Ô±ÈÐÂÎļþ |
| | |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | | using System.Security.Cryptography; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | using WIDESEA_Core.Const; |
| | | using WIDESEA_Core.DB; |
| | | |
| | | namespace WIDESEA_Core.Helper |
| | | { |
| | | public static class SecurityEncDecryptHelper |
| | | { |
| | | private static byte[] Keys = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }; |
| | | /// <summary> |
| | | /// DESå å¯å符串 |
| | | /// </summary> |
| | | /// <param name="encryptString">å¾
å å¯çå符串</param> |
| | | /// <param name="encryptKey">å å¯å¯é¥,è¦æ±ä¸º16ä½</param> |
| | | /// <returns>å 坿åè¿åå å¯åçå符串ï¼å¤±è´¥è¿åæºä¸²</returns> |
| | | |
| | | public static string EncryptDES(this string encryptString, string encryptKey) |
| | | { |
| | | try |
| | | { |
| | | byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 16)); |
| | | byte[] rgbIV = Keys; |
| | | byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); |
| | | |
| | | using (var DCSP = Aes.Create()) |
| | | { |
| | | using (MemoryStream mStream = new MemoryStream()) |
| | | { |
| | | using (CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write)) |
| | | { |
| | | cStream.Write(inputByteArray, 0, inputByteArray.Length); |
| | | cStream.FlushFinalBlock(); |
| | | return Convert.ToBase64String(mStream.ToArray()).Replace('+', '_').Replace('/', '~'); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | throw new Exception("å¯ç å å¯å¼å¸¸" + ex.Message); |
| | | } |
| | | |
| | | } |
| | | |
| | | /// <summary> |
| | | /// DESè§£å¯å符串 |
| | | /// </summary> |
| | | /// <param name="decryptString">å¾
è§£å¯çå符串</param> |
| | | /// <param name="decryptKey">è§£å¯å¯é¥,è¦æ±ä¸º16ä½,åå å¯å¯é¥ç¸å</param> |
| | | /// <returns>è§£å¯æåè¿åè§£å¯åçå符串ï¼å¤±è´¥è¿æºä¸²</returns> |
| | | |
| | | public static string DecryptDES(this string decryptString, string decryptKey) |
| | | { |
| | | if (decryptKey == AppSecret.DB && !AppSettings.app(MainDb.ConnectionStringsEncryption).ObjToBool()) |
| | | return decryptString; |
| | | |
| | | byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 16)); |
| | | byte[] rgbIV = Keys; |
| | | byte[] inputByteArray = Convert.FromBase64String(decryptString.Replace('_', '+').Replace('~', '/')); |
| | | using (var DCSP = Aes.Create()) |
| | | { |
| | | using (MemoryStream mStream = new MemoryStream()) |
| | | { |
| | | using (CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write)) |
| | | { |
| | | byte[] inputByteArrays = new byte[inputByteArray.Length]; |
| | | cStream.Write(inputByteArray, 0, inputByteArray.Length); |
| | | cStream.FlushFinalBlock(); |
| | | return Encoding.UTF8.GetString(mStream.ToArray()); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | public static bool TryDecryptDES(this string decryptString, string decryptKey, out string result) |
| | | { |
| | | result = ""; |
| | | try |
| | | { |
| | | result = DecryptDES(decryptString, decryptKey); |
| | | return true; |
| | | } |
| | | catch |
| | | { |
| | | return false; |
| | | } |
| | | } |
| | | } |
| | | } |