javasm2国密加解密工具类
时间: 2023-08-23 11:04:36 浏览: 145
java sm2国密工具类
javasm2国密加解密工具类可以用于对数据进行国密SM2算法的加解密、签名和验签操作。以下是一个简单的示例代码:
```java
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.crypto.engines.SM2Engine;
import org.bouncycastle.crypto.generators.AsymmetricKeyPairGenerator;
import org.bouncycastle.crypto.generators.ECKeyPairGenerator;
import org.bouncycastle.crypto.params.*;
import org.bouncycastle.crypto.signers.SM2Signer;
import org.bouncycastle.crypto.util.PrivateKeyFactory;
import org.bouncycastle.crypto.util.PublicKeyFactory;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;
import java.security.Security;
public class SM2Util {
static {
Security.addProvider(new BouncyCastleProvider());
}
public static AsymmetricCipherKeyPair generateKeyPair() {
ECKeyPairGenerator gen = new ECKeyPairGenerator();
gen.init(new ECKeyGenerationParameters(
SM2UtilParams.sm2ECParams, SM2UtilParams.random));
return gen.generateKeyPair();
}
public static String encrypt(byte[] data, ECPublicKeyParameters publicKey) throws Exception {
SM2Engine engine = new SM2Engine();
CipherParameters param = new ParametersWithRandom(publicKey, SM2UtilParams.random);
engine.init(true, param);
byte[] enc = engine.processBlock(data, 0, data.length);
return Hex.toHexString(enc);
}
public static byte[] decrypt(String data, ECPrivateKeyParameters privateKey) throws Exception {
SM2Engine engine = new SM2Engine();
CipherParameters param = new ParametersWithRandom(privateKey, SM2UtilParams.random);
engine.init(false, param);
byte[] dec = Hex.decode(data);
return engine.processBlock(dec, 0, dec.length);
}
public static String sign(byte[] data, ECPrivateKeyParameters privateKey) throws Exception {
SM2Signer signer = new SM2Signer();
signer.init(true, new ParametersWithRandom(privateKey, SM2UtilParams.random));
signer.update(data, 0, data.length);
byte[] sig = signer.generateSignature();
return Hex.toHexString(sig);
}
public static boolean verify(byte[] data, String sign, ECPublicKeyParameters publicKey) throws Exception {
SM2Signer signer = new SM2Signer();
signer.init(false, publicKey);
signer.update(data, 0, data.length);
byte[] sig = Hex.decode(sign);
return signer.verifySignature(sig);
}
public static void main(String[] args) throws Exception {
AsymmetricCipherKeyPair keyPair = SM2Util.generateKeyPair();
ECPublicKeyParameters publicKey = (ECPublicKeyParameters) keyPair.getPublic();
ECPrivateKeyParameters privateKey = (ECPrivateKeyParameters) keyPair.getPrivate();
String message = "hello world";
byte[] data = message.getBytes();
String encData = SM2Util.encrypt(data, publicKey);
System.out.println("encData: " + encData);
byte[] decData = SM2Util.decrypt(encData, privateKey);
System.out.println("decData: " + new String(decData));
String sign = SM2Util.sign(data, privateKey);
System.out.println("sign: " + sign);
boolean verifyResult = SM2Util.verify(data, sign, publicKey);
System.out.println("verifyResult: " + verifyResult);
}
}
```
注意:该示例代码仅供参考,具体实现应根据具体需求进行修改和完善。同时,使用国密算法需要遵守相关法律法规。
阅读全文