<template>
<div>
<el-button type="primary" @click="getphone()">sm2加密按钮</el-button>
<el-button type="primary" @click="returnphone()">sm2解密按钮</el-button>
</div>
</template>
<script>
//下载安装 npm install --save sm-crypto
//解密使用 var privateKey = "私钥";
//加密使用 var publicKey = "公钥";
export default {
data() {
return {
copyphone: '',
phone: '',
publicKey: "公钥自己生成好的填进来",
privateKey: "私钥自己生成好的填进来",
}
},
methods: {
//C1为65字节第1字节为压缩标识,这里固定为0x04
//publicKey是'04'+公钥X+公钥Y
//密钥对生成https://i.goto327.top/CryptTools/SM2.aspx
getphone() {
const sm2 = require('sm-crypto').sm2;
var publicKey = this.publicKey;//公钥加密使用
var encrText = this.phone;
const cipherMode = 1;
let encryptData = sm2.doEncrypt(encrText, publicKey, cipherMode) // 加密结果
this.copyphone = encryptData;
return '04' + encryptData; //04可不要具体看后端要求
},
returnphone() {
const sm2 = require('sm-crypto').sm2;
var privateKey = this.privateKey;//私钥解密使用
var encrText = this.copyphone;
const cipherMode = 1
let decryptData = sm2.doDecrypt(encrText, privateKey, cipherMode) // 解密结果
this.copyphone = decryptData;
return decryptData;
}
}
}
</script>