import md5 from 'js-md5' const { SM4 } = require('gm-crypto') export function encryptAndSign(appKey, appSecret, privateKey, content, crypt) { try { // 签名 appKey + appSecret + timestamp let timestamp = Date.now(); //时间戳 let str = appKey + appSecret + timestamp const sign = md5(str); // console.log("签名结果:", sign); //加密 let key = "" for (let i = 0; i < privateKey.length; i++) { key += privateKey.charCodeAt(i).toString(16); } let data = { sign, timestamp } if (crypt == "decrypt") { // 对数据进行解密 decrypt - 解密方法 // var decrypted = SM4.decryptData_ECB(content) data.decrypted = SM4.decrypt(content, key, { inputEncoding: 'hex', outputEncoding: 'utf8' }) // console.log("内容解密结果:" + data.decrypted); } else { // 对数据进行加密 encrypt - 加密方法 data.encrypted = SM4.encrypt(content, key, { inputEncoding: 'utf8', outputEncoding: 'hex' }) // console.log("内容加密结果:" + data.encrypted); } return data } catch (error) { return null } } module.exports = { encryptAndSign, };