裕隆城折扣合作@中興低碳 折扣報表
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

77 行
2.0KB

  1. using System.Globalization;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. namespace CouponReport.Service
  5. {
  6. public class ReportService
  7. {
  8. public string GetTenantCode(string externalSystemKey)
  9. {
  10. var result = DESDecode(externalSystemKey);
  11. var tenantNo = result.Split("|")[7];
  12. return tenantNo.Substring(0, 8);
  13. }
  14. public string GetInvoiceNo(string externalSystemKey)
  15. {
  16. var result = DESDecode(externalSystemKey);
  17. return result.Split("|")[3];
  18. }
  19. public decimal GetInvoiceMoney(string externalSystemKey)
  20. {
  21. var result = DESDecode(externalSystemKey);
  22. return decimal.Parse(result.Split("|")[4]);
  23. }
  24. public DateTime GetInvoiceDateTime(string externalSystemKey)
  25. {
  26. var result = DESDecode(externalSystemKey);
  27. return DateTime.ParseExact(result.Split("|")[5], "yyyyMMddHHmmss", CultureInfo.InvariantCulture);
  28. }
  29. private string DESDecode(string externalSystemKey)
  30. {
  31. if (string.IsNullOrWhiteSpace(externalSystemKey))
  32. {
  33. return string.Empty;
  34. }
  35. var key = "RAdWIMPs";
  36. var cipherHex = externalSystemKey.StartsWith("YLC", StringComparison.OrdinalIgnoreCase)
  37. ? externalSystemKey.Substring(3)
  38. : externalSystemKey;
  39. if (cipherHex.Length % 2 != 0)
  40. {
  41. throw new ArgumentException("Invalid coupon cipher text.", nameof(externalSystemKey));
  42. }
  43. var cipherBytes = new byte[cipherHex.Length / 2];
  44. for (var i = 0; i < cipherBytes.Length; i++)
  45. {
  46. cipherBytes[i] = Convert.ToByte(cipherHex.Substring(i * 2, 2), 16);
  47. }
  48. using var desProvider = new DESCryptoServiceProvider
  49. {
  50. Key = Encoding.ASCII.GetBytes(key),
  51. IV = Encoding.ASCII.GetBytes(key),
  52. Mode = CipherMode.CBC,
  53. Padding = PaddingMode.PKCS7
  54. };
  55. using var memoryStream = new MemoryStream();
  56. using (var cryptoStream = new CryptoStream(memoryStream, desProvider.CreateDecryptor(), CryptoStreamMode.Write))
  57. {
  58. cryptoStream.Write(cipherBytes, 0, cipherBytes.Length);
  59. cryptoStream.FlushFinalBlock();
  60. }
  61. return Encoding.UTF8.GetString(memoryStream.ToArray());
  62. }
  63. }
  64. }