123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- using System.Threading.Tasks;
- namespace PacsView3D
- {
- public enum EncryptionKeyEnum
- {
- KeyA,
- KeyB
- }
- public class EncryptionHelper
- {
- string encryptionKeyA = "xie_Nasb";
- string encryptionKeyB = "liuyeQJJ";
- string md5Begin = "Tianxia";
- string md5End = "Zhangjie";
- string encryptionKey = string.Empty;
- public EncryptionHelper()
- {
- this.InitKey();
- }
- public EncryptionHelper(EncryptionKeyEnum key)
- {
- this.InitKey(key);
- }
- private void InitKey(EncryptionKeyEnum key = EncryptionKeyEnum.KeyA)
- {
- switch (key)
- {
- case EncryptionKeyEnum.KeyA:
- encryptionKey = encryptionKeyA;
- break;
- case EncryptionKeyEnum.KeyB:
- encryptionKey = encryptionKeyB;
- break;
- }
- }
- public string EncryptString(string str)
- {
- return Encrypt(str, encryptionKey);
- }
- public string DecryptString(string str)
- {
- return Decrypt(str, encryptionKey);
- }
- public string GetMD5String(string str)
- {
- str = string.Concat(md5Begin, str, md5End);
- MD5 md5 = new MD5CryptoServiceProvider();
- byte[] fromData = Encoding.Unicode.GetBytes(str);
- byte[] targetData = md5.ComputeHash(fromData);
- string md5String = string.Empty;
- foreach (var b in targetData)
- md5String += b.ToString("x2");
- return md5String;
- }
- private string Encrypt(string str, string sKey)
- {
- DESCryptoServiceProvider des = new DESCryptoServiceProvider();
- byte[] inputByteArray = Encoding.Default.GetBytes(str);
- des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
- des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
- MemoryStream ms = new MemoryStream();
- CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
- cs.Write(inputByteArray, 0, inputByteArray.Length);
- cs.FlushFinalBlock();
- StringBuilder ret = new StringBuilder();
- foreach (byte b in ms.ToArray())
- {
- ret.AppendFormat("{0:X2}", b);
- }
- ret.ToString();
- return ret.ToString();
- }
- private string Decrypt(string pToDecrypt, string sKey)
- {
- DESCryptoServiceProvider des = new DESCryptoServiceProvider();
- byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
- for (int x = 0; x < pToDecrypt.Length / 2; x++)
- {
- int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
- inputByteArray[x] = (byte)i;
- }
- des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
- des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
- MemoryStream ms = new MemoryStream();
- CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
- cs.Write(inputByteArray, 0, inputByteArray.Length);
- cs.FlushFinalBlock();
- StringBuilder ret = new StringBuilder();
- return System.Text.Encoding.Default.GetString(ms.ToArray());
- }
- }
- }
|