0


国密SM2前端加密解密示例

一、 安装sm2依赖

npm install --save sm-crypto

二、编写代码

1、data中绑定数据

要改变的数据phone和过程数据copyphone,公钥publicKey和私钥privateKey

具体生成测试公钥私钥可参照SM2加解密

C1为65字节第1字节为压缩标识,这里固定为0x04

publicKey是'04'+公钥X+公钥Y

privateKey直接复制粘贴

实际运行情况下可参考RuoYi前后端分离(登录密码加密)更改为SM2加密,密钥由后端传输

export default {
    data() {
        return {
            copyphone: '',
            phone: '',
            publicKey: "",
            privateKey: "",
        }
    },

2、公钥加密

    getphone() {
            const sm2 = require('sm-crypto').sm2;
            var publicKey = this.publicKey;//加密使用
            var encrText = this.phone;//例如var enxrText = this.phone;
            const cipherMode = 1;
            let encryptData = sm2.doEncrypt(encrText, publicKey, cipherMode) // 加密结果
            this.copyphone = encryptData;
            return '04' + encryptData;//04可不要具体看后端要求
        },

3、私钥解密

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

4、按钮绑定一下,数据可见一下

<template>
    <div>
        <navigation_bar></navigation_bar>
        <el-tag>原始数据</el-tag><el-input v-model="phone" placeholder="请输入待转换数据"> 
        </el-input>
        <el-tag>过程数据</el-tag><el-input v-model="copyphone"></el-input>
        <el-tag>公钥</el-tag><el-input v-model="publicKey"></el-input>
        <el-tag>私钥</el-tag><el-input v-model="privateKey"></el-input>
        <el-button type="primary" @click="getphone()">sm2加密按钮</el-button>
        <el-button type="primary" @click="returnphone()">sm2解密按钮</el-button>
    </div>
</template>

三、完整代码

请放在Vue项目下运行

<template>
    <div>
        <el-tag>原始数据</el-tag><el-input v-model="phone" placeholder="请输入待转换数据"></el-input>
        <el-tag>过程数据</el-tag><el-input v-model="copyphone"></el-input>
        <el-tag>公钥</el-tag><el-input v-model="publicKey"></el-input>
        <el-tag>私钥</el-tag><el-input v-model="privateKey"></el-input>
        <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: {
        getphone() {
            const sm2 = require('sm-crypto').sm2;
            //C1为65字节第1字节为压缩标识,这里固定为0x04
            //publicKey是'04'+公钥X+公钥Y
            //密钥对生成https://i.goto327.top/CryptTools/SM2.aspx
            var publicKey = this.publicKey;//加密使用
            var encrText = this.phone;//例如var enxrText = 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>
标签: 前端 密码学 算法

本文转载自: https://blog.csdn.net/m0_55464684/article/details/132275142
版权归原作者 Huuuuuuuwow 所有, 如有侵权,请联系我们删除。

“国密SM2前端加密解密示例”的评论:

还没有评论