1.第一步就是安裝依賴
npm install --save sm-crypto
2.導(dǎo)入sm2
const sm2 = require('sm-crypto').sm2
3.先定義私鑰或者公鑰,私鑰是解密,公鑰是加密
var privateKey = "私鑰";//解密使用
var publicKey = "公鑰";//加密使用
4.設(shè)置加密模式
//cipherMode [加密模式 C1C3C2:1, C1C2C3:0]
const cipherMode = 1;//默認(rèn)是1
5.解密的使用全碼文章來源:http://www.zghlxwxcb.cn/news/detail-597096.html
頁面代碼直接@click綁定getphone即可單擊實(shí)現(xiàn)
data() {
return {
copyphone:'',
}}
methods: {
getphone(){
const sm2 = require('sm-crypto').sm2;
//
var privateKey = "私鑰";
var encrText = 需要解密的字段;
//有04要截 var encrText = val.substring(2);val是后臺傳過來的加密字段,將‘04’截取掉
const cipherMode = 1
let decryptData = sm2.doDecrypt(encrText, privateKey, cipherMode) // 解密結(jié)果
return decryptData ;
this.copyphone = decryptData;//賦值方便處理
console.log(this.copyphone);//直接打印出來看是否實(shí)現(xiàn)
}
}
6.加密的實(shí)現(xiàn)全碼文章來源地址http://www.zghlxwxcb.cn/news/detail-597096.html
頁面代碼直接@click綁定getphone即可單擊實(shí)現(xiàn)
data() {
return {
copyphone:'',
phone:'123545687',
}}
methods: {
getphone(){
const sm2 = require('sm-crypto').sm2;
var publicKey = "公鑰";//加密使用
var encrText = 需要加密的字段;//例如var enxrText = this.phone;
const cipherMode = 1;
let decryptData = sm2.doDecrypt(encrText, publicKey, cipherMode) // 加密結(jié)果
return '04' + decryptData;//04可不要具體看后端要求
}
}
到了這里,關(guān)于【vue+sm2】前端使用國密sm2,加解密的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!