用sm2公钥加密数据的Java代码
时间: 2023-08-09 20:04:50 浏览: 131
下面是用 Java 实现使用 SM2 公钥加密数据的代码示例:
```java
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.generators.ECKeyPairGenerator;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;
import java.security.Security;
public class SM2EncryptionDemo {
public static void main(String[] args) throws Exception {
Security.addProvider(new BouncyCastleProvider());
// 生成 SM2 密钥对
ECKeyPairGenerator gen = new ECKeyPairGenerator();
gen.initialize(CryptoUtils.getSM2Params());
AsymmetricCipherKeyPair keyPair = gen.generateKeyPair();
ECPrivateKeyParameters privateKey = (ECPrivateKeyParameters) keyPair.getPrivate();
ECPublicKeyParameters publicKey = (ECPublicKeyParameters) keyPair.getPublic();
// 待加密的数据
String plainText = "Hello, world!";
byte[] plainBytes = plainText.getBytes("UTF-8");
// 使用公钥加密数据
byte[] cipherBytes = CryptoUtils.SM2Encrypt(publicKey, plainBytes);
// 打印加密结果
System.out.println("Plain text: " + plainText);
System.out.println("Cipher text: " + Hex.toHexString(cipherBytes));
}
}
```
其中,`CryptoUtils` 中的 `SM2Encrypt` 方法用于使用 SM2 公钥加密数据,代码如下:
```java
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.engines.SM2Engine;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.crypto.params.ParametersWithRandom;
import java.security.SecureRandom;
public class CryptoUtils {
// 获取 SM2 参数
public static SM2Parameters getSM2Params() {
return SM2Util.getDefaultParameters();
}
// 使用 SM2 公钥加密数据
public static byte[] SM2Encrypt(ECPublicKeyParameters publicKey, byte[] plainText) throws InvalidCipherTextException {
SM2Engine engine = new SM2Engine();
ParametersWithRandom params = new ParametersWithRandom(publicKey, new SecureRandom());
engine.init(true, params);
return engine.processBlock(plainText, 0, plainText.length);
}
}
```
需要注意的是,上述代码中使用的 `SM2Util` 是我自己封装的一个工具类,用于获取 SM2 的默认参数,代码如下:
```java
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.generators.ECKeyPairGenerator;
import org.bouncycastle.crypto.params.ECDomainParameters;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.math.BigInteger;
import java.security.Security;
public class SM2Util {
// 获取 SM2 默认参数
public static SM2Parameters getDefaultParameters() {
Security.addProvider(new BouncyCastleProvider());
ECDomainParameters ecParams = new ECDomainParameters(
new BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF", 16), // p
new BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC", 16), // a
new BigInteger("28E9FA9E9D9F5E34411C1D2F8F98F06F1ECF40D9BFD648968B3E23FB4C776293", 16), // b
new BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123", 16), // n
new BigInteger("1", 16) // h
);
ECKeyPairGenerator gen = new ECKeyPairGenerator();
gen.initialize(ecParams);
AsymmetricCipherKeyPair keyPair = gen.generateKeyPair();
ECPrivateKeyParameters privateKey = (ECPrivateKeyParameters) keyPair.getPrivate();
ECPublicKeyParameters publicKey = (ECPublicKeyParameters) keyPair.getPublic();
return new SM2Parameters(ecParams, publicKey, privateKey);
}
}
```
阅读全文