Vue使用JSEncrypt实现rsa加密及挂载方法

作者:未月廿三 时间:2024-06-05 10:02:30 

挂载全局方法

使用jsencrypt进行rsa加密

原文链接:Js参数RSA加密传输,jsencrypt.js的使用 *
https://www.aspxhome.com/article/179813.htm
(原文处有一个地方不对,不需要转换+,rsa已经做过base64转码了)


1.安装依赖 npm install jsencrypt
2.在main.js引入 import { JSEncrypt } from 'jsencrypt'
3.挂载全局方法
//JSEncrypt加密方法
Vue.prototype.$encryptedData = function(publicKey, data) {
//new一个对象
let encrypt = new JSEncrypt()
//设置公钥
encrypt.setPublicKey(publicKey)
//password是要加密的数据,此处不用注意+号,因为rsa自己本身已经base64转码了,不存在+,全部是二进制数据
let result = encrypt.encrypt(password)
return result
}
//JSEncrypt解密方法
Vue.prototype.$decryptData = function(privateKey, data) {
// 新建JSEncrypt对象
let decrypt = new JSEncrypt()
// 设置私钥
decrypt.setPrivateKey(privateKey)
// 解密数据
let result = decrypt.decrypt(secretWord)
return result
}

全局混合

使用yarn安装至Vue项目

yarn add jsencrypt --dep

或者使用npm

npm install jsencrypt --dep

混入


import { JSEncrypt } from 'jsencrypt'
export const RsaMixin = {
methods: {
 // 加密
 encryptedData(publicKey, data) {
  // 新建JSEncrypt对象
  let encryptor = new JSEncrypt();
  // 设置公钥
  encryptor.setPublicKey(publicKey);
  // 加密数据
  return encryptor.encrypt(data);
 },
 // 解密
 decryptData(privateKey,data){
  // 新建JSEncrypt对象
  let decrypt= new JSEncrypt();
  // 设置私钥
  decrypt.setPrivateKey(privateKey);
  // 解密数据
  decrypt.decrypt(secretWord);
 }
}
}

引入


<script>
import InvoiceRecordModal from './modules/InvoiceRecordModal'
import { RsaMixin } from '@/mixins/RsaMixin'

export default {
name: "InvoiceRecordList",
//此时可以直接调用混入的方法
mixins:[RsaMixin],
data(){},
computed:{}
}
</script>

封装为单VUE文件中的方法

使用yarn安装至Vue项目

yarn add jsencrypt --dep

或者使用npm

npm install jsencrypt --dep

引入jsencrypt

import { JSEncrypt } from 'jsencrypt'

方法


methods: {
// 加密
encryptedData(publicKey, data) {
 // 新建JSEncrypt对象
 let encryptor = new JSEncrypt();
 // 设置公钥
 encryptor.setPublicKey(publicKey);
 // 加密数据
 return encryptor.encrypt(data);
},
// 解密
decryptData(privateKey,data){
 // 新建JSEncrypt对象
 let decrypt= new JSEncrypt();
 // 设置私钥
 decrypt.setPrivateKey(privateKey);
 // 解密数据
 decrypt.decrypt(secretWord);
}
}

总结

以上所述是小编给大家介绍的Vue使用JSEncrypt实现rsa加密及挂载方法,希望对大家有所帮助!

来源:https://www.cnblogs.com/eternityz/archive/2020/02/07/12272563.html

标签:vue,rsa,加密,挂载
0
投稿

猜你喜欢

  • mysql存储过程如何利用临时表返回结果集

    2024-01-13 07:39:05
  • Python Pandas高级教程之时间处理

    2021-08-12 14:04:49
  • python DataFrame获取行数、列数、索引及第几行第几列的值方法

    2023-08-19 18:12:13
  • SQL中 patindex函数的用法详解

    2024-01-23 14:55:39
  • Python金融数据可视化汇总

    2023-04-12 21:27:41
  • Python标准库之循环器(itertools)介绍

    2023-09-16 02:41:43
  • Python入门教程 超详细1小时学会Python

    2023-06-13 23:34:12
  • Elasticsearches通过坐标位置实现对附近人的搜索

    2023-05-23 16:16:12
  • python实现对excel进行数据剔除操作实例

    2022-09-28 13:53:22
  • JS将滑动门改为选项卡(需鼠标点击)的实现方法

    2024-05-22 10:36:17
  • 交互设计实用指南系列(3)—“有效性”之“适时帮助”

    2009-12-25 14:29:00
  • javascript将扁平的数据转为树形结构的高效率算法

    2024-02-24 05:26:01
  • MySQL最基本的命令使用汇总

    2024-01-28 06:18:30
  • 深入理解go slice结构

    2024-04-26 17:27:07
  • sql获取分组排序后数据的脚本

    2024-01-20 09:07:58
  • 详解MySql存储过程参数的入门使用

    2024-01-25 23:24:06
  • Python实现监控内存使用情况和代码执行时间

    2023-05-02 15:42:02
  • 如何用Python Beautiful Soup解析HTML内容

    2021-04-24 10:20:00
  • JavaScript中利用构造器函数模拟类的方法

    2023-07-02 05:30:13
  • ADO.NET通用数据库访问类

    2024-01-28 03:26:19
  • asp之家 网络编程 m.aspxhome.com