当前位置:首页 » 《资源分享》 » 正文

国密SM2前端加密解密示例

20 人参与  2024年04月11日 15:55  分类 : 《资源分享》  评论

点击全文阅读


目录

一、 安装sm2依赖

二、编写代码

1、data中绑定数据

2、公钥加密

3、私钥解密

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

三、完整代码


一、 安装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>


点击全文阅读


本文链接:http://zhangshiyu.com/post/93827.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 老公把上亿豪宅送养妹后,我把人和房都拆了(顾思思顾言洲)全书免费_(顾思思顾言洲)老公把上亿豪宅送养妹后,我把人和房都拆了后续(顾思思顾言洲)
  • 沈星悦傅时安_沈星悦傅时安
  • 离婚后,居然还能以旧换新?(陈汉李淼淼李思)全书浏览_离婚后,居然还能以旧换新?全书浏览
  • 完结文给女团主播狂刷百万反被骂穷逼,我反手送她队友出道列表_完结文给女团主播狂刷百万反被骂穷逼,我反手送她队友出道(秦薇)
  • 全书浏览老公将我第十个孩子送给情人后,我果断改嫁他绝嗣干爹(苏云遮盛炽)_老公将我第十个孩子送给情人后,我果断改嫁他绝嗣干爹(苏云遮盛炽)全书结局
  • 全文无边怨恨是她活下来的最大动力(江寒静顾榕赫)列表_全文无边怨恨是她活下来的最大动力
  • 全文爸爸死后,消失二十年的妈妈带着儿子回来跟我争家产(顾霆锋顾青卿)列表_全文爸爸死后,消失二十年的妈妈带着儿子回来跟我争家产
  • 霍晚清赵旭然_霍晚清赵旭然
  • 弟弟看的破茧时光沉淀后的深情相拥林悦苏然全书在线
  • 风雨里独自成长全书顾晚妍江知屿在线
  • 爸妈在我房间偷偷安了摄像头(周凝国家不保护废物),爸妈在我房间偷偷安了摄像头
  • 全书浏览雁过雪融向春洲(顾寻煜余芷鸢)_雁过雪融向春洲(顾寻煜余芷鸢)全书结局

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1