国密sm2、sm3、sm4的js使用

安装:

1
2
npm install sm-crypto<br>Or
yarn add sm-crypto

sm2:

获取密钥对:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const sm2 = require('sm-crypto').sm2
 
let keypair = sm2.generateKeyPairHex()
 
publicKey = keypair.publicKey // 公钥
privateKey = keypair.privateKey // 私钥
 
// 默认生成公钥 130 位太长,可以压缩公钥到 66 位
const compressedPublicKey = sm2.compressPublicKeyHex(publicKey) // compressedPublicKey 和 publicKey 等价
sm2.comparePublicKeyHex(publicKey, compressedPublicKey) // 判断公钥是否等价
 
// 自定义随机数,参数会直接透传给 jsbn 库的 BigInteger 构造器
// 注意:开发者使用自定义随机数,需要自行确保传入的随机数符合密码学安全
let keypair2 = sm2.generateKeyPairHex('123123123123123')
let keypair3 = sm2.generateKeyPairHex(256, SecureRandom)
 
let verifyResult = sm2.verifyPublicKey(publicKey) // 验证公钥
verifyResult = sm2.verifyPublicKey(compressedPublicKey) // 验证公钥

加密解密:

1
2
3
4
5
6
7
8
const sm2 = require('sm-crypto').sm2
const cipherMode = 1 // 1 - C1C3C2,0 - C1C2C3,默认为1
 
let encryptData = sm2.doEncrypt(msgString, publicKey, cipherMode) // 加密结果
let decryptData = sm2.doDecrypt(encryptData, privateKey, cipherMode) // 解密结果
 
encryptData = sm2.doEncrypt(msgArray, publicKey, cipherMode) // 加密结果,输入数组
decryptData = sm2.doDecrypt(encryptData, privateKey, cipherMode, {output: 'array'}) // 解密结果,输出数组

ps:密文会在解密时自动补充 04,如遇到其他工具补充的 04 需手动去除再传入。

sm2.js(封装)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const sm2 = require('sm-crypto').sm2
const cipherMode = 0 // 1 - C1C3C2,0 - C1C2C3,默认为1
 
export const { publicKey, privateKey } = sm2.generateKeyPairHex()
 
// 加密
export function encrypt(value) {
  // 给后端传值时需要在加密的密文前面加04 ,这样后端才能解密正确不报错
  return '04' + sm2.doEncrypt(value, publicKey, cipherMode)
}
 
// 解密
export function decrypt(value) {
  // 后端传输过来的密文开头的两个字符通常也为04,因此解密时需要删除
  return sm2.doDecrypt(value.slice(2, value.length), privateKey, cipherMode)
}

sm3:

1
2
3
4
5
6
7
8
const sm3 = require('sm-crypto').sm3
 
let hashData = sm3('abc') // 杂凑
 
// hmac
hashData = sm3('abc', {
    key: '0123456789abcdefghijklmn9876543210', // 要求为 16 进制串或字节数组
})  

sm4:

加密:

1
2
3
4
5
6
7
8
const sm4 = require('sm-crypto').sm4
const msg = 'hello world' // 可以为 utf8 串或字节数组
const key = '0123456789abcdefg9876543210' // 可以为 16 进制串或字节数组,要求为 128 比特
 
let encryptData = sm4.encrypt(msg, key) // 加密,默认输出 16 进制字符串,默认使用 pkcs#7 填充(传 pkcs#5 也会走 pkcs#7 填充)
let encryptData = sm4.encrypt(msg, key, {padding: 'none'}) // 加密,不使用 padding
let encryptData = sm4.encrypt(msg, key, {padding: 'none', output: 'array'}) // 加密,不使用 padding,输出为字节数组
let encryptData = sm4.encrypt(msg, key, {mode: 'cbc', iv: 'abcdefg123456gfedcba'}) // 加密,cbc 模式

解密:

1
2
3
4
5
6
7
8
const sm4 = require('sm-crypto').sm4
const encryptData = '' // 加密后的数据
const key = '0123456789abcdefg9876543210' // 可以为 16 进制串或字节数组,要求为 128 比特
 
let decryptData = sm4.decrypt(encryptData, key) // 解密,默认输出 utf8 字符串,默认使用 pkcs#7 填充(传 pkcs#5 也会走 pkcs#7 填充)
let decryptData = sm4.decrypt(encryptData, key, {padding: 'none'}) // 解密,不使用 padding
let decryptData = sm4.decrypt(encryptData, key, {padding: 'none', output: 'array'}) // 解密,不使用 padding,输出为字节数组
let decryptData = sm4.decrypt(encryptData, key, {mode: 'cbc', iv: 'abcdefg123456gfedcba'}) // 解密,cbc 模式

  

本文摘自:https://github.com/JuneAndGreen/sm-crypto/blob/master/README.md

posted @   zaijinyang  阅读(9255)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示