UNPKG

gm-sm

Version:

sm2

32 lines (25 loc) 950 B
const {CryptoJS} = require('./core'); const {SM2Cipher} = require('./sm2'); const {BigInteger} = require('./jsbn'); function doEncrypt(msg,publickey,cipherMode = 1){ var msgData = CryptoJS.enc.Utf8.parse(msg); var pubkeyHex = publickey; if (pubkeyHex.length > 130) { pubkeyHex = pubkeyHex.substr(pubkeyHex.length - 130); } var cipher = new SM2Cipher(cipherMode); var userKey = cipher.CreatePoint(pubkeyHex); msgData = cipher.str2Bytes(msgData.toString()); var encryptData = cipher.Encrypt(userKey, msgData); return encryptData; } function doDecrypt(encryptData,prvkey,cipherMode = 1) { var privateKey = new BigInteger(prvkey, 16); var cipher = new SM2Cipher(cipherMode); var data = cipher.Decrypt(privateKey, encryptData); return data; } module.exports = { doEncrypt, doDecrypt };