SM2算法加密java代码
时间: 2023-11-10 16:50:59 浏览: 138
以下是Java代码实现SM2算法加密:
```java
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.engines.SM2Engine;
import org.bouncycastle.crypto.generators.ECKeyPairGenerator;
import org.bouncycastle.crypto.params.ECDomainParameters;
import org.bouncycastle.crypto.params.ECKeyGenerationParameters;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.crypto.params.ParametersWithRandom;
import org.bouncycastle.crypto.util.PrivateKeyFactory;
import org.bouncycastle.crypto.util.PublicKeyFactory;
import org.bouncycastle.math.ec.ECPoint;
import org.bouncycastle.util.encoders.Hex;
import java.security.SecureRandom;
public class SM2Util {
// 国密推荐的椭圆曲线参数
private static final String SM2_ECC_PARAM = "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF";
private static final String SM2_ECC_A = "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC";
private static final String SM2_ECC_B = "28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93";
private static final String SM2_ECC_GX = "32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE171F6241CACD9A2ED";
private static final String SM2_ECC_GY = "BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0";
private static final String SM2_ECC_N = "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123";
/**
* 生成SM2公私钥对
*
* @return 公私钥对
*/
public static SM2KeyPair generateKeyPair() {
ECDomainParameters domainParameters = new ECDomainParameters(
SM2Util.getCurve(),
SM2Util.getGPoint(),
SM2Util.getN());
ECKeyPairGenerator keyPairGenerator = new ECKeyPairGenerator();
ECKeyGenerationParameters keyGenerationParameters = new ECKeyGenerationParameters(domainParameters, new SecureRandom());
keyPairGenerator.init(keyGenerationParameters);
AsymmetricCipherKeyPair keyPair = keyPairGenerator.generateKeyPair();
ECPrivateKeyParameters privateKeyParameters = (ECPrivateKeyParameters) keyPair.getPrivate();
ECPublicKeyParameters publicKeyParameters = (ECPublicKeyParameters) keyPair.getPublic();
return new SM2KeyPair(Hex.toHexString(privateKeyParameters.getD().toByteArray()),
Hex.toHexString(publicKeyParameters.getQ().getEncoded(true)));
}
/**
* 加密数据
*
* @param publicKey 公钥
* @param data 待加密数据
* @return 加密结果
*/
public static String encrypt(String publicKey, String data) {
ECPublicKeyParameters publicKeyParameters = (ECPublicKeyParameters) PublicKeyFactory.createKey(Hex.decode(publicKey));
SM2Engine engine = new SM2Engine();
engine.init(true, new ParametersWithRandom(publicKeyParameters, new SecureRandom()));
byte[] input = data.getBytes();
byte[] output = engine.processBlock(input, 0, input.length);
return Hex.toHexString(output);
}
/**
* 解密数据
*
* @param privateKey 私钥
* @param data 待解密数据
* @return 解密结果
*/
public static String decrypt(String privateKey, String data) {
ECPrivateKeyParameters privateKeyParameters = (ECPrivateKeyParameters) PrivateKeyFactory.createKey(Hex.decode(privateKey));
SM2Engine engine = new SM2Engine();
engine.init(false, privateKeyParameters);
byte[] input = Hex.decode(data);
byte[] output = engine.processBlock(input, 0, input.length);
return new String(output);
}
/**
* 获取SM2曲线参数
*/
public static ECDomainParameters getCurve() {
return new ECDomainParameters(
SM2Util.getCurve(),
SM2Util.getGPoint(),
SM2Util.getN());
}
/**
* 获取SM2曲线
*/
private static org.bouncycastle.math.ec.ECCurve getCurve() {
return new org.bouncycastle.math.ec.ECCurve.Fp(
Hex.decode(SM2_ECC_PARAM),
Hex.decode(SM2_ECC_A),
Hex.decode(SM2_ECC_B));
}
/**
* 获取SM2曲线生成点G
*/
private static ECPoint getGPoint() {
org.bouncycastle.math.ec.ECCurve curve = SM2Util.getCurve();
return curve.createPoint(
new java.math.BigInteger(Hex.toHexString(SM2Util.getGx()), 16),
new java.math.BigInteger(Hex.toHexString(SM2Util.getGy()), 16));
}
/**
* 获取SM2曲线参数N
*/
private static java.math.BigInteger getN() {
return new java.math.BigInteger(Hex.toHexString(SM2_ECC_N), 16);
}
/**
* 获取SM2曲线生成点Gx
*/
private static byte[] getGx() {
return Hex.decode(SM2_ECC_GX);
}
/**
* 获取SM2曲线生成点Gy
*/
private static byte[] getGy() {
return Hex.decode(SM2_ECC_GY);
}
}
```
其中,`SM2KeyPair`为公私钥对的封装类,`generateKeyPair()`方法用于生成SM2公私钥对;`encrypt(String publicKey, String data)`方法用于加密数据,其中`publicKey`为公钥,`data`为待加密数据;`decrypt(String privateKey, String data)`方法用于解密数据,其中`privateKey`为私钥,`data`为待解密数据。
阅读全文