基于Tauri实现的国密算法桌面应用(一)

688 阅读1分钟

为了符合等保3级要求,系统中使用了国密算法对敏感信息进行加解密。用于信息中很多是密文,为了方便开发、测试人员排查问题,根据系统使用的国密算法实现了一个桌面应用工具。

技术选型:Tauri + Vue3 + TS

国密算法库:sm-crypto。该JS库有JAVA版本和小程序版本通用性强,方便前后端对接。

应用界面.png

功能

  • SM2 生成秘钥对

  • SM2加解密

  • SM4加解密

环境搭建

1.Tauri环境

参考Tauri官方文档:Tauri prerequisites。需要以下:

  • Microsoft Visual Studio C++ Build Tools

  • WebView2 (Windows 11自带)

  • Rust

2.前端环境

略。

核心逻辑

sm-crypto

安装sm-crypto

yarn add sm-crypto
yarn add @types/sm-crypto --dev

SM2

非对称加密,公钥加密,私钥解密。

生成秘钥对

const keypair = sm2.generateKeyPairHex();
const publicKey = keypair.publicKey;       // 公钥
const privateKey = keypair.privateKey;     // 私钥

SM2加密

const ciphertext = sm2.doEncrypt(plainText, publicKey, cipherMode);

SM2解密

const plaintext = sm2.doDecrypt(cipherText, privateKey, cipherMode);

这里加解密都默认是C1C2C3模式,即cipherMode值为1。

SM4

SM4加密

const ciphertext = sm4.encrypt(plainText, key);

SM4解密

const plaintext = sm4.decrypt(cipherText, key);

后续功能

  • SM2选择模式
  • SM3
  • JSON格式化

项目地址

[(GitHub - Hector526/tauri-sm-crypto-tool: a small tool for sm-crypto,国密加解密的小工具)