JS实现国密算法SM2加密,后端Java解密
项⽬涉及保密传输,要求使⽤国密算法,⼀般遇到类似问题⾸先想到的就是使⽤⾮对称加密,后端⽣成密钥对,将公钥交给前端,前端⽤公钥加密数据,后端⽤
私钥对数据解密。项⽬的复杂度在于国密的⾮对称加密算法SM2的Java及JS实现。
后端⾸先引⼊bouncycastle,Maven配置如下:
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.65</version>
</dependency>
后端Java代码如下:
//⽣成密钥对
X9ECParameters sm2ECParameters = ByName("sm2p256v1");
ECDomainParameters domainParameters = new Curve(), G(), N()); ECKeyPairGenerator keyPairGenerator = new ECKeyPairGenerator();
keyPairGenerator.init(new ECKeyGenerationParameters(domainParameters, Instance("SHA1PRNG")));
AsymmetricCipherKeyPair asymmetricCipherKeyPair = ateKeyPair();
//私钥,16进制格式,⾃⼰保存,格式如a2081b5b81fbea0b6b973a3ab6dbbbc65b1164488bf22d8ae2ff0b8260f64853
BigInteger privatekey = ((ECPrivateKeyParameters) Private()).getD();
String privateKeyHex = String(16);
//公钥,16进制格式,发给前端,格式如04813d4d97ad31bd9d18d785f337f683233099d5abed09cb397152d50ac28cc0ba43711960e811d90453db5f5a9518d6608 ECPoint ecPoint = ((ECPublicKeyParameters) Public()).getQ();
String publicKeyHex = Encoded(false));
前端Javascript⽰例代码,写了个页⾯:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>SM2-TEST</title>
<script src="crypto-js.js"></script>
<script src="sm2.js"></script>
<script>
function encrypt() {
/
/公钥,16进制格式,由后端⽣成
var pubkeyHex = "04813d4d97ad31bd9d18d785f337f683233099d5abed09cb397152d50ac28cc0ba43711960e811d90453db5f5a9518d660858a8d0c
57e359a8bf83427760ebcbba";
var encryptData = sm2Encrypt("SM2 Encryption Test", pubkeyHex, 0);
console.log(encryptData);
}
</script>
</head>
<body οnlοad="encrypt()">
</body>
</html>
执⾏会⽣成密⽂,每次⽣成都会不同,⽐如我的环境某次⽣成如下:
04be17bf6fe47da1f34a01ad0ff67901241b72d103e998f2f7cc78a004703bdfb8d2c6e3939f4f708f3a57d872d58ec5c41bbe5976666bcb01acea43f5a1c68a6
后端尝试解密:
//JS加密产⽣的密⽂
String cipherData = "04be17bf6fe47da1f34a01ad0ff67901241b72d103e998f2f7cc78a004703bdfb8d2c6e3939f4f708f3a57d872d58ec5c41bbe5976666bcb01acea byte[] cipherDataByte = Hex.decode(cipherData);
js代码加密软件//刚才的私钥Hex,先还原私钥
String privateKey = "a2081b5b81fbea0b6b973a3ab6dbbbc65b1164488bf22d8ae2ff0b8260f64853";
BigInteger privateKeyD = new BigInteger(privateKey, 16);
ECPrivateKeyParameters privateKeyParameters = new ECPrivateKeyParameters(privateKeyD, domainParameters);
/
/⽤私钥解密
SM2Engine sm2Engine = new SM2Engine();
sm2Engine.init(false, privateKeyParameters);
//processBlock得到Base64格式,记得解码
byte[] arrayOfBytes = Decoder().decode(sm2Engine.processBlock(cipherDataByte, 0, cipherDataByte.length));
//得到明⽂:SM2 Encryption Test
String data = new String(arrayOfBytes);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。