1. The first step is to install dependencies
npm install --save sm-crypto
2. import sm2
const sm2 = require('sm-crypto').sm2
3. First define the private key or public key, the private key is for decryption, and the public key is for encryption
var privateKey = "私钥";//解密使用
var publicKey = "公钥";//加密使用
4. Set the encryption mode
//cipherMode [加密模式 C1C3C2:1, C1C2C3:0]
const cipherMode = 1;//默认是1
5. Use full code for decryption
页面代码直接@click绑定getphone即可单击实现
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) // 解密结果
return decryptData ;
this.copyphone = decryptData;//赋值方便处理
console.log(this.copyphone);//直接打印出来看是否实现
}
}
6. The realization of encryption is full code
页面代码直接@click绑定getphone即可单击实现
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) // 加密结果
return '04' + decryptData;//04可不要具体看后端要求
}
}