国密SM2前端加密解密

<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>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值