123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- using System.Threading.Tasks;
- namespace PackingPress.Common
- {
- public 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
- {
- return value;
- }
- }
- /// <summary>
- /// 解密
- /// </summary>
- /// <param name="value"></param>
- /// <returns></returns>
- public static string Decrypt(string value)
- {
- if (string.IsNullOrEmpty(value)) return string.Empty;
- try
- {
- value = value.Substring(1, value.Length - 2);
- 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
- {
- return value;
- }
- }
- }
- }
|