123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- using System;
- using System.IO;
- using System.Security.Cryptography;
- using System.Text;
- namespace AILicenseGenerator.Helper
- {
- internal class DesBuilder
- {
- private const string EncryptKey = "V1NN0KEY";
- private static readonly byte[] Keys = { 0x56, 0x34, 0x90, 0xCD, 0xEF, 0xAB, 0x12, 0x78 };
- /// <summary>
- /// 加密
- /// </summary>
- /// <param name="value"></param>
- /// <returns></returns>
- public static string Encrypt(string value)
- {
- if (string.IsNullOrEmpty(value)) return string.Empty;
- try
- {
- byte[] rgbKey = Encoding.UTF8.GetBytes(EncryptKey);
- byte[] rgbIv = Keys;
- byte[] inputByteArray = Encoding.UTF8.GetBytes(value);
- var dCsp = new DESCryptoServiceProvider();
- using (var stream = new MemoryStream())
- {
- using (var cStream = new CryptoStream(stream, dCsp.CreateEncryptor(rgbKey, rgbIv), CryptoStreamMode.Write))
- {
- cStream.Write(inputByteArray, 0, inputByteArray.Length);
- cStream.FlushFinalBlock();
- var output = Convert.ToBase64String(stream.ToArray());
- return "{" + output + "}";
- }
- }
- }
- catch (Exception ex)
- {
- Logger.Error($"Encrypt {value} error : {ex}");
- return value;
- }
- }
- /// <summary>
- /// 解密
- /// </summary>
- /// <param name="value"></param>
- /// <returns></returns>
- public static string Decrypt(string value)
- {
- var exceptionReturnResult = value;
- if (string.IsNullOrEmpty(value)) return string.Empty;
- try
- {
- var frontBrace = value.IndexOf('{');
- var backBrace = value.IndexOf('}');
- value = value.Substring(1, backBrace - frontBrace - 1);
- byte[] rgbKey = Encoding.UTF8.GetBytes(EncryptKey);
- byte[] rgbIv = Keys;
- byte[] inputByteArray = Convert.FromBase64String(value);
- var dcsp = new DESCryptoServiceProvider();
- using (var stream = new MemoryStream())
- {
- using (var cStream = new CryptoStream(stream, dcsp.CreateDecryptor(rgbKey, rgbIv), CryptoStreamMode.Write))
- {
- cStream.Write(inputByteArray, 0, inputByteArray.Length);
- cStream.FlushFinalBlock();
- return Encoding.UTF8.GetString(stream.ToArray());
- }
- }
- }
- catch (Exception ex)
- {
- Logger.Error($"Decrypt {exceptionReturnResult} error:{ex}");
- return exceptionReturnResult;
- }
- }
- /// <summary>
- /// 加密
- /// </summary>
- /// <param name="value"></param>
- /// <returns></returns>
- public static byte[] BytesEncrypt(byte[] value, string encryptKey = null)
- {
- if (value == null) return new byte[0];
- try
- {
- byte[] rgbKey = Encoding.UTF8.GetBytes(string.IsNullOrWhiteSpace(encryptKey) ? EncryptKey : encryptKey);
- byte[] rgbIv = Keys;
- byte[] inputByteArray = value;
- var dCsp = new DESCryptoServiceProvider();
- using (var stream = new MemoryStream())
- {
- using (var cStream = new CryptoStream(stream, dCsp.CreateEncryptor(rgbKey, rgbIv), CryptoStreamMode.Write))
- {
- cStream.Write(inputByteArray, 0, inputByteArray.Length);
- cStream.FlushFinalBlock();
- var output = stream.ToArray();
- return output;
- }
- }
- }
- catch (Exception ex)
- {
- Logger.Error($"BytesEncrypt {ex}");
- return value;
- }
- }
- /// <summary>
- /// 解密
- /// </summary>
- /// <param name="value"></param>
- /// <returns></returns>
- public static byte[] BytesDecrypt(byte[] value, string encryptKey = null)
- {
- if (value == null) return new byte[0];
- try
- {
- byte[] rgbKey = Encoding.UTF8.GetBytes(string.IsNullOrWhiteSpace(encryptKey) ? EncryptKey : encryptKey);
- byte[] rgbIv = Keys;
- byte[] inputByteArray = value;
- var dcsp = new DESCryptoServiceProvider();
- using (var stream = new MemoryStream())
- {
- using (var cStream = new CryptoStream(stream, dcsp.CreateDecryptor(rgbKey, rgbIv), CryptoStreamMode.Write))
- {
- cStream.Write(inputByteArray, 0, inputByteArray.Length);
- cStream.FlushFinalBlock();
- return stream.ToArray();
- }
- }
- }
- catch (Exception ex)
- {
- Logger.Error($"BytesDecrypt {ex}");
- return new byte[0];
- }
- }
- }
- }
|