小程序 RSA 加密

第三方工具:wxmp-rsa - npm

         1、构建 npm 包

                

        2、安装

        3、封装加密方法(rsa.js )

import WxmpRsa from 'wxmp-rsa'
/* 秘钥 */
const privateKey = '秘钥字符串, 跟后端对接'
/* 公钥 */
const publicKey = '公钥字符串, 跟后端对接'

/* RSA加密 */
const rsaEncrypt = (word) => {
  const encryptor = new WxmpRsa();
  encryptor.setPublicKey(publicKey);
  return encryptor.encryptLong(word);
}

/* RSA解密 */
const rsaDecrypt = (word) => {
  const decryptor = new WxmpRsa();
  decryptor.setPrivateKey(privateKey);
  return decryptor.decryptLong(word);
}

/* 暴露接口 */
module.exports = {
  rsaEncrypt,
  rsaDecrypt
}

        4、使用

const RSA = require('./rsa')

// 加密
RSA.rsaEncrypt(''加密数据')

// 解密
RSA.rsaDecrypt('解密数据')

小程序官方方法:API安全 | 微信开放文档

猜你喜欢

转载自blog.csdn.net/weixin_46258341/article/details/131577648
今日推荐