DecryptionUtil.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Configuration;
  3. using System.Security.Cryptography;
  4. using System.IO;
  5. using System.Text;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. /*
  9. * create by superbee, 2015-07-16
  10. */
  11. public class DecryptionUtil
  12. {
  13. private static string skey = "UnityBa1";
  14. public static string GetKey()
  15. {
  16. return skey;
  17. }
  18. /// <summary>
  19. ///
  20. /// </summary>
  21. /// <param name="pToDecrypt"> 待解密的字符串</param>
  22. /// <param name="sKey"> 解密密钥,要求为8字节,和加密密钥相同</param>
  23. /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
  24. public static string Decryption(string pToDecrypt)
  25. {
  26. // HttpContext.Current.Response.Write(pToDecrypt + "<br>" + sKey);
  27. // HttpContext.Current.Response.End();
  28. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  29. byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
  30. for (int x = 0; x < pToDecrypt.Length / 2; x++)
  31. {
  32. int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
  33. inputByteArray[x] = (byte)i;
  34. }
  35. des.Key = ASCIIEncoding.ASCII.GetBytes(skey);
  36. des.IV = ASCIIEncoding.ASCII.GetBytes(skey);
  37. MemoryStream ms = new MemoryStream();
  38. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  39. cs.Write(inputByteArray, 0, inputByteArray.Length);
  40. cs.FlushFinalBlock();
  41. StringBuilder ret = new StringBuilder();
  42. return System.Text.Encoding.Default.GetString(ms.ToArray());
  43. }
  44. public static string Encryption(string pToEncrypt)
  45. {
  46. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  47. byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
  48. des.Key = ASCIIEncoding.ASCII.GetBytes(skey);
  49. des.IV = ASCIIEncoding.ASCII.GetBytes(skey);
  50. MemoryStream ms = new MemoryStream();
  51. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  52. cs.Write(inputByteArray, 0, inputByteArray.Length);
  53. cs.FlushFinalBlock();
  54. StringBuilder ret = new StringBuilder();
  55. foreach (byte b in ms.ToArray())
  56. {
  57. ret.AppendFormat("{0:X2}", b);
  58. }
  59. ret.ToString();
  60. return ret.ToString();
  61. }
  62. }