后端代码:
String gmMsg = "HelloGM";
String gmPrivateKey = org.web3j.utils.Numeric.cleanHexPrefix(KeyPresenter.asString(gmPkey.getPrivateKey()));
String gmPublicKey = org.web3j.utils.Numeric.cleanHexPrefix(KeyPresenter.asString(gmPkey.getPublicKey().getPublicKey()));
System.out.println("gmPrivateKey = " + gmPrivateKey);
System.out.println("gmPublicKey = " + gmPublicKey);
SM2SignService sm2SignService = new SM2SignService();
String gmSignature = sm2SignService.sign(gmMsg, gmPrivateKey);
System.out.println("gm signature:" + gmSignature);
boolean gmVerifyResult = sm2SignService.verify(gmMsg, gmSignature, gmPublicKey);
System.out.println("gm verify result:" + gmVerifyResult);
前端代码:
const sm2 = require('sm-crypto').sm2;
const msg = 'HelloGM'
const publicKey = '048bcaaccc295247e3807a6bec6fc8f8354a4ff1bb386dab9c8d3ce12aa2c8e03725b1d33357c29692062c22ab515fb3417b70788f99911209b56575f0dc72a774' // 公钥
const privateKey = '26d740a3bc50161e4af05d809381f1ba126051f490e4f1ce2052b8359fb616c3' // 私钥
const verifyData=sm2.doSignature(msg, privateKey, { hash:true, der:true });
console.log(verifyData)
//这里的vsign是java生成的
const vSign = 'c5076deddf7cb5465d6788ad9903dd73cb61c1e1deaeedea586d2628a8d6ac5244af8d637914f2ea096fd37e36697f23040542f864541ef72c5d71083deaeba1'
const ver2 = sm2.doVerifySignature(msg, vSign, publicKey,{ hash:true, der:true } )
console.log(ver2)