feat: crypto.js 重构 #1839

This commit is contained in:
binaryify
2023-11-26 17:08:30 +08:00
parent b4a9c8ece1
commit af3846658a
4 changed files with 64 additions and 36 deletions

View File

@ -1,67 +1,83 @@
const crypto = require('crypto')
const iv = Buffer.from('0102030405060708')
const presetKey = Buffer.from('0CoJUm6Qyw8W8jud')
const linuxapiKey = Buffer.from('rFgB&h#%2?^eDg:Q')
const CryptoJS = require('crypto-js')
const forge = require('node-forge')
const iv = '0102030405060708'
const presetKey = '0CoJUm6Qyw8W8jud'
const linuxapiKey = 'rFgB&h#%2?^eDg:Q'
const base62 = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
const publicKey =
'-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDgtQn2JZ34ZC28NWYpAUd98iZ37BUrX/aKzmFbt7clFSs6sXqHauqKWqdtLkF2KexO40H1YTX8z2lSgBBOAxLsvaklV8k4cBFK9snQXE9/DDaFt6Rr7iVZMldczhC0JNgTz+SHXT6CBHuX3e9SdB1Ua44oncaTWz7OBGLbCiK45wIDAQAB\n-----END PUBLIC KEY-----'
const publicKey = `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDgtQn2JZ34ZC28NWYpAUd98iZ37BUrX/aKzmFbt7clFSs6sXqHauqKWqdtLkF2KexO40H1YTX8z2lSgBBOAxLsvaklV8k4cBFK9snQXE9/DDaFt6Rr7iVZMldczhC0JNgTz+SHXT6CBHuX3e9SdB1Ua44oncaTWz7OBGLbCiK45wIDAQAB
-----END PUBLIC KEY-----`
const eapiKey = 'e82ckenh8dichen8'
const aesEncrypt = (buffer, mode, key, iv) => {
const cipher = crypto.createCipheriv('aes-128-' + mode, key, iv)
return Buffer.concat([cipher.update(buffer), cipher.final()])
const aesEncrypt = (text, mode, key, iv, format = 'base64') => {
let encrypted = CryptoJS.AES.encrypt(
CryptoJS.enc.Utf8.parse(text),
CryptoJS.enc.Utf8.parse(key),
{
iv: CryptoJS.enc.Utf8.parse(iv),
mode: CryptoJS.mode[mode.toUpperCase()],
padding: CryptoJS.pad.Pkcs7,
},
)
if (format === 'base64') {
return encrypted.toString()
}
return encrypted.ciphertext.toString().toUpperCase()
}
const rsaEncrypt = (buffer, key) => {
buffer = Buffer.concat([Buffer.alloc(128 - buffer.length), buffer])
return crypto.publicEncrypt(
{ key: key, padding: crypto.constants.RSA_NO_PADDING },
buffer,
)
const rsaEncrypt = (str, key) => {
const forgePublicKey = forge.pki.publicKeyFromPem(key)
const encrypted = forgePublicKey.encrypt(str, 'NONE')
return forge.util.bytesToHex(encrypted)
}
const weapi = (object) => {
const text = JSON.stringify(object)
const secretKey = crypto
.randomBytes(16)
.map((n) => base62.charAt(n % 62).charCodeAt())
let secretKey = ''
for (let i = 0; i < 16; i++) {
secretKey += base62.charAt(Math.round(Math.random() * 61))
}
return {
params: aesEncrypt(
Buffer.from(
aesEncrypt(Buffer.from(text), 'cbc', presetKey, iv).toString('base64'),
),
aesEncrypt(text, 'cbc', presetKey, iv),
'cbc',
secretKey,
iv,
).toString('base64'),
encSecKey: rsaEncrypt(secretKey.reverse(), publicKey).toString('hex'),
),
encSecKey: rsaEncrypt(secretKey.split('').reverse().join(''), publicKey),
}
}
const linuxapi = (object) => {
const text = JSON.stringify(object)
return {
eparams: aesEncrypt(Buffer.from(text), 'ecb', linuxapiKey, '')
.toString('hex')
.toUpperCase(),
eparams: aesEncrypt(text, 'ecb', linuxapiKey, '', 'hex'),
}
}
const eapi = (url, object) => {
const text = typeof object === 'object' ? JSON.stringify(object) : object
const message = `nobody${url}use${text}md5forencrypt`
const digest = crypto.createHash('md5').update(message).digest('hex')
const digest = CryptoJS.MD5(message).toString()
const data = `${url}-36cd479b6b5-${text}-36cd479b6b5-${digest}`
return {
params: aesEncrypt(Buffer.from(data), 'ecb', eapiKey, '')
.toString('hex')
.toUpperCase(),
params: aesEncrypt(data, 'ecb', eapiKey, '', 'hex'),
}
}
const decrypt = (cipherBuffer) => {
const decipher = crypto.createDecipheriv('aes-128-ecb', eapiKey, '')
return Buffer.concat([decipher.update(cipherBuffer), decipher.final()])
const decrypt = (cipher) => {
const decipher = CryptoJS.AES.decrypt(
{
ciphertext: CryptoJS.enc.Hex.parse(cipher),
},
eapiKey,
{
mode: CryptoJS.mode.ECB,
},
)
const decryptedBytes = CryptoJS.enc.Utf8.stringify(decipher)
return decryptedBytes
}
module.exports = { weapi, linuxapi, eapi, decrypt }
module.exports = { weapi, linuxapi, eapi, decrypt, aesEncrypt }