使用RSA私钥加密、公钥解密的方法。

借助vue框架开发,以下则是使用node-rsa的加密及解密过程:

let NodeRSA = require("node-rsa");
Vue.prototype.$nodersa = NodeRSA;
const _pubKey = `-----BEGIN PUBLIC KEY-----
  MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCXCAjACBKL9BIkiawOJpy6Q3nr
  bI29b3tVxcRIEkr4DAkoGjnH5qg5lGsN9joxtx9bP819wfIu3/BkcmEu1uhcKbYP
  Rbch/k+W3X/FERm9ZNLa0PK2RvJcHRdE6r3kfxoWzu8OiRm4Fda2V0CRAa4ZP9fB
  2JD4exmJQvAcfKKeNQIDAQAB
  -----END PUBLIC KEY-----`;
const _priKey = `-----BEGIN RSA PRIVATE KEY-----
  MIICXAIBAAKBgQCXCAjACBKL9BIkiawOJpy6Q3nrbI29b3tVxcRIEkr4DAkoGjnH
  5qg5lGsN9joxtx9bP819wfIu3/BkcmEu1uhcKbYPRbch/k+W3X/FERm9ZNLa0PK2
  RvJcHRdE6r3kfxoWzu8OiRm4Fda2V0CRAa4ZP9fB2JD4exmJQvAcfKKeNQIDAQAB
  AoGAPavZLtjdAesLEYJqEGkx7FkrSrfL9QO1wCN+b9OQa1M21fpHh9om7UQupsfZ
  oX1eP3uBJI3yn7NvzQRrrJ2yyXWyqOgQuphLg4HAns1qPZDHvgSQov7SMdnjh2lB
  eSHiF74ENrzYKEfE0fkyEK8OwbwCigyn0BFPFhoU+Usvr+0CQQDeJSBcY+Cs3kss
  0wjz0umxoXlC2l069YkQ9BM9+t+JS4g55+ZZ4NsVYQSuxJwgNsRfwKmb7H6sIwGo
  +c65EdDTAkEArgxyGrgCWntGaqingi4bsddTwfnHUjYBJVwcHWKi+HlCVEjcFcll
  rwz+rNYg2u2g8CijcRhVj6SjshPjZ+Sv1wJBANIgHh8mXnRGBKfOqs4vt9vM2G3T
  LZLChDdkF/ttnRo6Nw5G+84SckkjrxE9akzSYSgNjqGtrxhWOJDEUrUeU/8CQFnk
  I6qZDt4cfSnpIVqi5brcM24jy0YyjjT1da/XmcQrHI7bco/uDI50b7Rkg2OPB2gC
  FSf8IXSm28TFcNXfztECQDqKiH85A09vxEq9iUUVd1mZzLF69GSE+cJiNxBbekMh
  G7KsyvgmFxgd+Zpjeah3OX6mSWKQZcd2WyA6wX0eqsE=
  -----END RSA PRIVATE KEY-----`;
let priKey = new this.$nodersa(_priKey);
// 私钥加密
let data = priKey.encryptPrivate("hello", 'base64');
// 公钥解密
let pubKey =  new this.$nodersa(_pubKey);  // 设置你的公钥
console.log(pubKey.decryptPublic(data, "utf8")); // 解密数据

在main.js中引用:

let NodeRSA = require("node-rsa");
Vue.prototype.$nodersa = NodeRSA;

附上node-rsa文档:https://www.npmjs.com/package/node-rsa#key-info

Logo

前往低代码交流专区

更多推荐