/** * txt:要加解密的字符串 * * @return {string} */ const sm2 = require('sm-crypto').sm2 // 加密 export const encrypt = (txt) => { const publicKey = '042DBA45E7B03394F603CADAFCDDEC854D3E01A4E9C52CD799B85B1A14BDB970137AE58BA553D79F058604DC1CD4B77DE5408BA3308E767584100C2B663510C819' const cipherMode = 0 // 1 - C1C3C2,0 - C1C2C3,默认为1 const encryptData = sm2.doEncrypt(txt, publicKey, cipherMode) // 加密结果 return encryptData } // 解密 export const decrypt = (txt) => { const privateKey = 'BF1F907B4E0487F798DC80AFD7BC2A6201E8514233002272EA3BE2FC6F797843' const cipherMode = 0 // 1 - C1C3C2,0 - C1C2C3,默认为1 if (!txt) { return } txt = txt.substr(2) const decryptData = sm2.doDecrypt(txt, privateKey, cipherMode) // 解密结果 return decryptData }