crypto.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. const CryptoJS = require('crypto-js')
  2. const forge = require('node-forge')
  3. const iv = '0102030405060708'
  4. const presetKey = '0CoJUm6Qyw8W8jud'
  5. const linuxapiKey = 'rFgB&h#%2?^eDg:Q'
  6. const base62 = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
  7. const publicKey = `-----BEGIN PUBLIC KEY-----
  8. MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDgtQn2JZ34ZC28NWYpAUd98iZ37BUrX/aKzmFbt7clFSs6sXqHauqKWqdtLkF2KexO40H1YTX8z2lSgBBOAxLsvaklV8k4cBFK9snQXE9/DDaFt6Rr7iVZMldczhC0JNgTz+SHXT6CBHuX3e9SdB1Ua44oncaTWz7OBGLbCiK45wIDAQAB
  9. -----END PUBLIC KEY-----`
  10. const eapiKey = 'e82ckenh8dichen8'
  11. const aesEncrypt = (text, mode, key, iv, format = 'base64') => {
  12. let encrypted = CryptoJS.AES.encrypt(
  13. CryptoJS.enc.Utf8.parse(text),
  14. CryptoJS.enc.Utf8.parse(key),
  15. {
  16. iv: CryptoJS.enc.Utf8.parse(iv),
  17. mode: CryptoJS.mode[mode.toUpperCase()],
  18. padding: CryptoJS.pad.Pkcs7,
  19. },
  20. )
  21. if (format === 'base64') {
  22. return encrypted.toString()
  23. }
  24. return encrypted.ciphertext.toString().toUpperCase()
  25. }
  26. const aesDecrypt = (ciphertext, key, iv, format = 'base64') => {
  27. let bytes
  28. if (format === 'base64') {
  29. bytes = CryptoJS.AES.decrypt(ciphertext, CryptoJS.enc.Utf8.parse(key), {
  30. iv: CryptoJS.enc.Utf8.parse(iv),
  31. mode: CryptoJS.mode.ECB,
  32. padding: CryptoJS.pad.Pkcs7,
  33. })
  34. } else {
  35. bytes = CryptoJS.AES.decrypt(
  36. { ciphertext: CryptoJS.enc.Hex.parse(ciphertext) },
  37. CryptoJS.enc.Utf8.parse(key),
  38. {
  39. iv: CryptoJS.enc.Utf8.parse(iv),
  40. mode: CryptoJS.mode.ECB,
  41. padding: CryptoJS.pad.Pkcs7,
  42. },
  43. )
  44. }
  45. return bytes.toString(CryptoJS.enc.Utf8)
  46. }
  47. const rsaEncrypt = (str, key) => {
  48. const forgePublicKey = forge.pki.publicKeyFromPem(key)
  49. const encrypted = forgePublicKey.encrypt(str, 'NONE')
  50. return forge.util.bytesToHex(encrypted)
  51. }
  52. const weapi = (object) => {
  53. const text = JSON.stringify(object)
  54. let secretKey = ''
  55. for (let i = 0; i < 16; i++) {
  56. secretKey += base62.charAt(Math.round(Math.random() * 61))
  57. }
  58. return {
  59. params: aesEncrypt(
  60. aesEncrypt(text, 'cbc', presetKey, iv),
  61. 'cbc',
  62. secretKey,
  63. iv,
  64. ),
  65. encSecKey: rsaEncrypt(secretKey.split('').reverse().join(''), publicKey),
  66. }
  67. }
  68. const linuxapi = (object) => {
  69. const text = JSON.stringify(object)
  70. return {
  71. eparams: aesEncrypt(text, 'ecb', linuxapiKey, '', 'hex'),
  72. }
  73. }
  74. const eapi = (url, object) => {
  75. const text = typeof object === 'object' ? JSON.stringify(object) : object
  76. const message = `nobody${url}use${text}md5forencrypt`
  77. const digest = CryptoJS.MD5(message).toString()
  78. const data = `${url}-36cd479b6b5-${text}-36cd479b6b5-${digest}`
  79. return {
  80. params: aesEncrypt(data, 'ecb', eapiKey, '', 'hex'),
  81. }
  82. }
  83. const eapiResDecrypt = (encryptedParams) => {
  84. // 使用aesDecrypt解密参数
  85. const decryptedData = aesDecrypt(encryptedParams, eapiKey, '', 'hex')
  86. return JSON.parse(decryptedData)
  87. }
  88. const eapiReqDecrypt = (encryptedParams) => {
  89. // 使用aesDecrypt解密参数
  90. const decryptedData = aesDecrypt(encryptedParams, eapiKey, '', 'hex')
  91. // 使用正则表达式解析出URL和数据
  92. const match = decryptedData.match(/(.*?)-36cd479b6b5-(.*?)-36cd479b6b5-(.*)/)
  93. if (match) {
  94. const url = match[1]
  95. const data = JSON.parse(match[2])
  96. return { url, data }
  97. }
  98. // 如果没有匹配到,返回null
  99. return null
  100. }
  101. const decrypt = (cipher) => {
  102. const decipher = CryptoJS.AES.decrypt(
  103. {
  104. ciphertext: CryptoJS.enc.Hex.parse(cipher),
  105. },
  106. eapiKey,
  107. {
  108. mode: CryptoJS.mode.ECB,
  109. },
  110. )
  111. const decryptedBytes = CryptoJS.enc.Utf8.stringify(decipher)
  112. return decryptedBytes
  113. }
  114. module.exports = {
  115. weapi,
  116. linuxapi,
  117. eapi,
  118. decrypt,
  119. aesEncrypt,
  120. aesDecrypt,
  121. eapiReqDecrypt,
  122. eapiResDecrypt,
  123. }