DecryptionUtil.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. return System.Text.Encoding.Default.GetString(ms.ToArray());
  42. }
  43. public static string Encryption(string pToEncrypt)
  44. {
  45. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  46. byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
  47. des.Key = ASCIIEncoding.ASCII.GetBytes(skey);
  48. des.IV = ASCIIEncoding.ASCII.GetBytes(skey);
  49. MemoryStream ms = new MemoryStream();
  50. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  51. cs.Write(inputByteArray, 0, inputByteArray.Length);
  52. cs.FlushFinalBlock();
  53. StringBuilder ret = new StringBuilder();
  54. foreach (byte b in ms.ToArray())
  55. {
  56. ret.AppendFormat("{0:X2}", b);
  57. }
  58. ret.ToString();
  59. return ret.ToString();
  60. }
  61. }