程序员的资源宝库

网站首页 > gitee 正文

国密sm2、sm3、sm4的js使用(sm4在线加密工具)

sanyeah 2024-04-01 11:21:52 gitee 3 ℃ 0 评论

安装:

npm install sm-crypto
Or yarn add sm-crypto

sm2:

获取密钥对:

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) // 验证公钥

加密解密:

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(封装)

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:

const sm3 = require('sm-crypto').sm3

let hashData = sm3('abc') // 杂凑

// hmac
hashData = sm3('abc', {
    key: '0123456789abcdefghijklmn9876543210', // 要求为 16 进制串或字节数组
})  

sm4:

加密:

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

解密:

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

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表