const NodeRSA = require('node-rsa'); // 使用公钥加密 export function encrypt (msg,commonKey) { const key = new NodeRSA(`-----BEGIN PUBLIC KEY-----${commonKey}-----END PUBLIC KEY-----`); //如果使用公钥加密后,后台使用私钥解密失败,这里需要设置下对应的 encryptionScheme 值 key.setOptions({encryptionScheme: 'pkcs1'}); let encrypt = key.encrypt(msg, 'base64', 'utf8'); return encrypt } //使用公钥解密 export function decrypt (msg,commonKey) { const key = new NodeRSA(`-----BEGIN PUBLIC KEY-----${commonKey}-----END PUBLIC KEY-----`); key.setOptions({encryptionScheme: 'pkcs1'}); let decryptPublic= key.decryptPublic(msg, 'utf8'); return decryptPublic } export default { encrypt , decrypt }