本篇内容介绍了“Vue中怎么使用jsencrypt进行RSA非对称加密”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
下载jsencrypt
cnpm i -S jsencrypt
引入
import { JSEncrypt } from "jsencrypt";
基本使用
注意:要加密的数据必须是字符串!!
提示:给公钥直接用双引号“”引起来后可能要手动删掉每行末尾多余的空格,有点麻烦,此时可以直接用``来引。只是这样可能排版显示不太好看
testRSA() { let publicKey = "-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC4aWAgASppilGGGPv6wdCp4AqTRbQPEMxU1mNwBXmP6v0hp2eHo8LMhjjM4IwzdzOnwyTR6v5YlznBb90cWKsr/DP1IdSI9ox7M7FCHGfURfLp/mGRvNLnJ5ds7YD+35rP0wLMZhNbkXHs3HMYOY96XnSE7bfcWkLiRrwiCrqLdQIDAQAB-----END PUBLIC KEY-----"; // 从后台获取公钥 let data = JSON.stringify({ username: "zhangsan", password: 123 }); // let data = "hello world"; // let data = 123 + "";//必须传字符串 console.log("加密前:", data); let encryptor = new JSEncrypt(); encryptor.setPublicKey(publicKey); let data_encrypted = encryptor.encrypt(data); console.log("加密后:", data_encrypted); let privateKey = "-----BEGIN PRIVATE KEY-----MIICeQIBADANBgkqhkiG9w0BAQEFAASCAmMwggJfAgEAAoGBALhpYCABKmmKUYYY+/rB0KngCpNFtA8QzFTWY3AFeY/q/SGnZ4ejwsyGOMzgjDN3M6fDJNHq/liXOcFv3RxYqyv8M/Uh2Ij2jHszsUIcZ9RF8un+YZG80ucnl2ztgP7fms/TAsxmE1uRcezccxg5j3pedITtt9xaQuJGvCIKuot1AgMBAAECgYEAti7cWSHtrZCJk6oRHhzlsT12FSKSjSOyScn3OM8f1i933gyNOjJtmzKq4LShraKAtKcUNBItLoZP96s0zPFIQCGjON0d3XyRxoGK2h4nEZT8NTqEcQVI2sbOy5PvJHAKT/Vrcqdkt/U9YteLWAKGZ5A8Ie8NZEjh0botUwIrOIECQQDd+Qv8IYiDSugZcOPEz/ijCR4Jg011X9/bqXVIEiqkIZ+8QToKCBuGt0EejhAzs0acni1XLPwBeDmAsmg8MmpVAkEA1K5NL3ZwuLjz4vs8EfF3BfscPTQZ9PRBhzGC9iqP7TyzMRyZjGpWfycOjki8isRzR3iNoFPIyLZfe5Wf4vL8oQJBALHia6BanzPYS7hXp7CJmg/NtqyY9PIjKxq16q8fH9z4tTIAc6qmZKjJIv6Biqpj+Sp5+IvsGOh0mGBymOXk4SECQQDDxSYbeGTORMGKwERNJ/2trbYSyHDUQDpJXdP1ELynXeZ8YJ0e2YuBQ8xsQJv9CFQtB9UmLbl2uBkwQ0yHta/BAkEAoKN1vdqkDTwAkSRpvFUOHkc7E6nEYdXwM+hhKnmQLCQb2aP8zW9GI2Q7EWPDEwN92TK30k1VSzpquSU7SUA32g==-----END PRIVATE KEY-----"; let decryptor = new JSEncrypt(); decryptor.setPrivateKey(privateKey); console.log("解密后:", decryptor.decrypt(data_encrypted)); }
“Vue中怎么使用jsencrypt进行RSA非对称加密”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!