JS 国密 sm4 加解密
import {sm4} from 'sm-crypto'
/**
* 国密 SM4 加密(对称加密)
* @param key 加密秘钥
* @param text 需要加密的内容
* @param options
* @link https://github.com/JuneAndGreen/sm-crypto#%E5%8A%A0%E5%AF%86
* @example sm4Encrypt("W28uEz@2lPqw871v","23018419910212241X"); 输出: 1192a2dd198f39b5239489deec11968f4ddc7ded635f4951e90be5df6201f6f6
* @returns string
*/
export function sm4Encrypt(key, text, options = {}) {
let keyHexCode = "";
for (let i = 0; i !== key.length; i++) {
keyHexCode += key.charCodeAt(i).toString(16)
}
return sm4.encrypt(text, keyHexCode, options)
}
/**
* 国密 SM4 解密(对称加密)
* @param key 解密秘钥
* @param text 需要解密的内容
* @param options
* @link https://github.com/JuneAndGreen/sm-crypto#%E8%A7%A3%E5%AF%86
* @example sm4Decrypt("W28uEz@2lPqw871v","1192a2dd198f39b5239489deec11968f4ddc7ded635f4951e90be5df6201f6f6"); 输出: 23018419910212241X
* @returns string
*/
export function sm4Decrypt(key, text, options = {}) {
let keyHexCode = "";
for (let i = 0; i !== key.length; i++) {
keyHexCode += key.charCodeAt(i).toString(16)
}
return sm4.decrypt(text, keyHexCode, options)
}