You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.6 KiB

  1. 
  2. namespace CounsellorBL.Helper
  3. {
  4. using OT.COM.Encryption;
  5. using OT.COM.LogisticsUtil;
  6. using System;
  7. public static class EncryptHelper
  8. {
  9. private static readonly byte[] _Key = System.Text.Encoding.Unicode.GetBytes(Util.GetSettingString("KEY"));
  10. private static readonly byte[] _IV = System.Text.Encoding.Unicode.GetBytes(Util.GetSettingString("IV"));
  11. private static readonly AES _aes = new AES();
  12. /// <summary>
  13. /// 函式名稱:Encrypt
  14. /// 函式說明:加密API
  15. /// 起始作者:Hercules
  16. /// 起始日期:2016/06/17
  17. /// 最新修改人: Hercules
  18. /// 最新修日期: 2016/06/17
  19. /// </summary>
  20. /// <param name="i_sPlaintext">明碼</param>
  21. /// <param name="o_sCiphertext">暗碼</param>
  22. /// <returns>錯誤訊息,若成功則為Null</returns>
  23. public static string Encrypt(string i_sPlaintext, out string o_sCiphertext)
  24. {
  25. string sMsg = null;
  26. string sRes = null;
  27. try
  28. {
  29. do
  30. {
  31. byte[] baRes = _aes.EncryptStringToByte(
  32. i_sPlaintext,
  33. _Key,
  34. _IV);
  35. sRes = Convert.ToBase64String(baRes);
  36. }
  37. while (false);
  38. }
  39. finally
  40. {
  41. o_sCiphertext = sRes;
  42. }
  43. return sMsg;
  44. }
  45. /// <summary>
  46. /// 函式名稱:Decrypt
  47. /// 函式說明:解密API
  48. /// 起始作者:Hercules
  49. /// 起始日期:2016/06/17
  50. /// 最新修改人: Hercules
  51. /// 最新修日期: 2016/06/17
  52. /// </summary>
  53. /// <param name="i_sCiphertext">暗碼</param>
  54. /// <param name="o_sPlaintext">明碼</param>
  55. /// <returns>錯誤訊息,若成功則為Null</returns>
  56. public static string Decrypt(string i_sCiphertext, out string o_sPlaintext)
  57. {
  58. string sMsg = null;
  59. string sRes = null;
  60. try
  61. {
  62. do
  63. {
  64. byte[] baCiphertext = Convert.FromBase64String(i_sCiphertext);
  65. sRes = _aes.DecryptByteToString(
  66. baCiphertext,
  67. _Key,
  68. _IV);
  69. }
  70. while (false);
  71. }
  72. finally
  73. {
  74. o_sPlaintext = sRes;
  75. }
  76. return sMsg;
  77. }
  78. }
  79. }