vue 使用rsa加密整理

由於安全原因一些敏感信息提交時都需要加密處理,之前一直用MD5加密,一些暴力軟件還是可以進行解密,而rsa加密安全性就比較高,它採用前端公鑰加密 後臺私鑰解密。下面就是整個rsa在項目中使用的過程

安裝

npm install jsencrypt

使用

像這種比較常用的工具基本都提取成工具方法

import JSEncrypt from 'jsencrypt';

//加密
export function encryptedData(publicKey, data) {
    // 新建JSEncrypt對象
    let encryptor = new JSEncrypt();
    // 設置公鑰
    encryptor.setPublicKey(publicKey);
    // 加密數據
    return encryptor.encrypt(data);
}
//解密
export function decryptedData(privateKey, data) {
    // 新建JSEncrypt對象
    let encryptor = new JSEncrypt();
    // 設置公鑰
    encryptor.setPrivateKey(privateKey)
    // 加密數據
    return encryptor.decrypt(data);
}

頁面上調用就直接引用(如需要加密處理的)

    const publicKey ="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCH6fhSFH...";
    import { encryptedData } from '@/util/jsencryptmethod'

    setpassword(){
         let product_password = encryptedData(publicKey,"123123");
         console.log(product_password);
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章