smEncryption.js
1.1 KB
export function doEncrypt(msgString) {
let msg = msgString;
if (typeof (msgString) !== 'string') {
msg = JSON.stringify(msgString);
}
let sm2 = require('sm-crypto').sm2;
let keypair = sm2.generateKeyPairHex();
let publicKey = keypair.publicKey; // 公钥
let cipherMode = 0; // 1 - C1C3C2,0 - C1C2C3,默认为1
//let publicKey = 'MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAExVea1qP1qKJf5UyrTwmO9fnkGCjELqsodZUBEgecIaC68ELV1pvIk0wYEevJ98UnVeuo5aN+MNdgu7FxPmblgw=='; // 公钥
let encryptData = sm2.doEncrypt(msg, publicKey, cipherMode) + '04'; // 加密结果
return encryptData;
}
/**
* SM2解密
*/
export function decryptSM2(pass) {
let sm2 = require('sm-crypto').sm2;
console.log(sm2)
let keypair = sm2.generateKeyPairHex();
let privateKey = keypair.privateKey; // 私钥
//let privateKey = 'MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAExVea1qP1qKJf5UyrTwmO9fnkGCjELqsodZUBEgecIaC68ELV1pvIk0wYEevJ98UnVeuo5aN+MNdgu7FxPmblgw=='; // 公钥
let cipherMode = 0; // 1 - C1C3C2,0 - C1C2C3,默认为1
let decryptData = sm2.doDecrypt(pass, privateKey, cipherMode) // 解密结果
console.log(decryptData)
return decryptData
}