UNPKG

@skylp/wds-encrypt

Version:

前端加解密

573 lines (572 loc) 10.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isJSON = exports.parseRes = exports.getUrlParams = exports.IsNotEmpty = exports.rsaEncrypt = exports.getPan = exports.rsaDecrypt = exports.aesDecrypt = exports.aesEncrypt = exports.randomStr = exports.encryptionScheme = exports.Utf8 = exports.utf8 = exports.base64 = exports.Base64 = exports.AES = exports.backdoor = exports.decryptPublic = exports.pkcs1 = exports.Pkcs7 = exports.CBC = exports.decrypt = exports.encrypt = exports.pan = exports.dora = exports.transDic = exports.getDic = exports.randomNum = void 0; const CryptoJS = require('crypto-js'); const NodeRSA = require('node-rsa'); function randomNum() { return 23; } exports.randomNum = randomNum; function getDic() { const l = {}; for (let i = 0; i < 128; i++) { const k = randomNum() + i; l[k] = String.fromCharCode(i); } return l; } exports.getDic = getDic; function transDic(arr) { const l = getDic(); let result = ''; if (arr && arr.length) { arr.forEach((item) => { if (l[item]) { result += l[item]; } }); return result; } else { return arr; } } exports.transDic = transDic; exports.dora = transDic([123, 134, 137, 120]); exports.pan = transDic([135, 120, 133]); exports.encrypt = transDic([124, 133, 122, 137, 144, 135, 139]); exports.decrypt = transDic([123, 124, 122, 137, 144, 135, 139]); exports.CBC = transDic([90, 89, 90]); exports.Pkcs7 = transDic([103, 130, 122, 138, 78]); exports.pkcs1 = transDic([135, 130, 122, 138, 72]); exports.decryptPublic = transDic([123, 124, 122, 137, 144, 135, 139, 103, 140, 121, 131, 128, 122]); exports.backdoor = transDic([121, 120, 122, 130, 123, 134, 134, 137]); exports.AES = transDic([88, 92, 106]); exports.Base64 = transDic([89, 120, 138, 124, 77, 75]); exports.base64 = transDic([121, 120, 138, 124, 77, 75]); exports.utf8 = transDic([140, 139, 125, 79]); exports.Utf8 = transDic([108, 139, 125, 79]); exports.encryptionScheme = transDic([ 124, 133, 122, 137, 144, 135, 139, 128, 134, 133, 106, 122, 127, 124, 132, 124, ]); function randomStr() { const S4 = function () { return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1); }; return `${S4()}${S4()}${S4()}${S4()}`; } exports.randomStr = randomStr; function aesEncrypt(encryptData, aesKey) { let result = ''; const secretPassphrase = CryptoJS.enc[exports.Utf8].parse(aesKey); try { const cipherText = CryptoJS[exports.AES][exports.encrypt](encryptData, secretPassphrase, { mode: CryptoJS.mode[exports.CBC], padding: CryptoJS.pad[exports.Pkcs7], iv: secretPassphrase, }); result = CryptoJS.enc[exports.Base64].stringify(cipherText.ciphertext); } catch (error) { console.log(error); } return result; } exports.aesEncrypt = aesEncrypt; function aesDecrypt(decryptedData, aesKey) { const secretPassphrase = CryptoJS.enc[exports.Utf8].parse(aesKey); let result = ''; try { const decrypted = CryptoJS[exports.AES][exports.decrypt](decryptedData, secretPassphrase, { mode: CryptoJS.mode[exports.CBC], padding: CryptoJS.pad[exports.Pkcs7], iv: secretPassphrase, }); result = decrypted.toString(CryptoJS.enc[exports.Utf8]); } catch (error) { console.log(error); } return result; } exports.aesDecrypt = aesDecrypt; function rsaDecrypt(decryptedData) { let result = ''; const publicKey = getPan(); const key = new NodeRSA(publicKey, {}, {}); try { const o = {}; o[exports.encryptionScheme] = exports.pkcs1; key.setOptions(o); result = key[exports.decryptPublic](decryptedData, exports.utf8); } catch (error) { console.log(error); } return result; } exports.rsaDecrypt = rsaDecrypt; function getPan() { let baseStr1 = transDic([ 107, 108, 131, 95, 113, 130, 72, 89, 100, 92, 123, 91, 108, 74, 93, 95, 108, 71, 131, 128, 100, 71, 105, 105, 105, 108, 97, 89, 108, 109, 109, 89, 104, 107, 105, 95, 107, 130, 93, 92, 104, 71, 97, 135, 108, 108, 139, 90, 113, 72, 93, 91, 105, 91, 113, 144, 120, 95, 123, 112, 108, 133, 123, 77, 124, 110, 127, 140, 109, 93, 113, 73, ]); let baseStr2 = transDic([ 107, 73, 127, 96, 110, 107, 92, 72, 100, 94, 71, 76, 104, 110, 123, 94, 100, 92, 93, 141, 108, 95, 93, 110, 105, 129, 109, 92, 99, 73, 72, 140, 124, 133, 101, 107, 104, 145, 93, 144, 105, 108, 96, 74, 124, 130, 105, 71, 109, 129, 101, 143, 106, 132, 113, 143, 122, 92, 76, 99, 124, 108, 76, 143, 100, 145, 93, 144, 122, 111, 105, 144, ]); let baseStr3 = transDic([ 108, 95, 113, 101, 121, 108, 72, 142, 123, 93, 123, 143, 106, 130, 76, 108, 121, 110, 130, 137, 101, 71, 131, 111, 120, 111, 101, 74, 112, 108, 134, 73, 100, 131, 113, 128, 101, 110, 72, 73, 120, 131, 97, 73, 109, 132, 109, 74, 109, 132, 93, 132, 124, 108, 108, 75, 120, 129, 97, 127, 123, 73, 97, 139, 110, 91, 89, 72, 106, 145, 100, 141, ]); let baseStr4 = transDic([ 120, 92, 76, 93, 112, 108, 113, 95, 123, 94, 80, 72, 108, 108, 101, 134, 112, 73, 113, 102, 109, 92, 101, 127, 100, 93, 105, 98, 101, 92, 139, 99, 112, 129, 88, 71, 113, 132, 75, 141, 100, 131, 131, 127, 105, 133, 113, 112, 122, 71, 127, 92, 109, 110, 135, 92, 108, 132, 113, 134, 123, 132, 93, 74, 106, 108, 105, 89, 108, 108, 93, 90, ]); let baseStr5 = transDic([ 99, 106, 71, 139, 99, 106, 72, 90, 105, 108, 123, 97, 107, 128, 89, 104, 109, 108, 97, 100, 106, 108, 100, 126, 106, 71, 109, 113, 99, 106, 71, 139, 99, 106, 71, 84, ]); let baseStr6 = transDic([ 99, 106, 71, 139, 99, 106, 72, 93, 107, 130, 104, 126, 108, 93, 109, 90, 107, 92, 131, 91, 96, 92, 139, 93, 110, 106, 71, 139, 99, 106, 71, 139, ]); let baseArr = [baseStr5, baseStr1, baseStr3, baseStr2, baseStr4, baseStr6]; let textString = ''; baseArr.forEach((item) => { let words = CryptoJS.enc[exports.Base64].parse(item); textString += CryptoJS.enc[exports.Utf8].stringify(words); }); return textString; } exports.getPan = getPan; function rsaEncrypt(encryptData) { let result = ''; const publicKey = getPan(); const key = new NodeRSA(publicKey, {}, {}); try { const o = {}; o[exports.encryptionScheme] = exports.pkcs1; key.setOptions(o); result = key[exports.encrypt](encryptData, exports.base64, exports.utf8); } catch (error) { console.log(error); } return result; } exports.rsaEncrypt = rsaEncrypt; function IsNotEmpty(str) { return str !== '' || str !== null || str !== undefined || str !== 'undefined' || str !== 'null'; } exports.IsNotEmpty = IsNotEmpty; function getUrlParams(obj) { let result = ''; for (let i in obj) { if (IsNotEmpty(obj[i])) { result += result == '' ? `${i}=${obj[i]}` : `&${i}=${obj[i]}`; } } return result; } exports.getUrlParams = getUrlParams; function parseRes(result) { if (!result) { return; } let key = ''; if (result && result.headers && result.headers[exports.pan]) { key = rsaDecrypt(result.headers[exports.pan]); } if (result.data && result.data[exports.dora] && key) { let resultStr = aesDecrypt(result.data[exports.dora], key); if (resultStr) { return isJSON(resultStr) ? JSON.parse(resultStr) : { message: resultStr, }; } else { return result.data; } } else { return result.data; } } exports.parseRes = parseRes; function isJSON(str) { if (typeof str === 'string') { try { JSON.parse(str); return true; } catch (e) { return false; } } else { return false; } } exports.isJSON = isJSON;