掛載全域方法
使用jsencrypt進行rsa加密
原文鏈接:Js引數RSA加密傳輸,jsencrypt.js的使用 - CSDN博客*
https://blog.csdn.net/p312011150/article/details/80264144
(原文處有一個地方不對,不需要轉換+,rsa已經做過base64轉碼了)
1.安裝依賴 npm install jsencrypt
2.在main.js引入 import { JSEncrypt } from 'jsencrypt'
3.掛載全域方法
//JSEncrypt加密方法
Vue.prototype.$encryptedData = https://www.cnblogs.com/eternityz/p/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);
}
}
站在巨人的肩膀上摘蘋果:
鏈接:https://www.jianshu.com/p/084548516410 ()
https://segmentfault.com/a/1190000018896698 (全域混入以及單檔案方法)
https://www.jianshu.com/p/084548516410 (全域注冊)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/150736.html
標籤:JavaScript
