使用OpenEuler x86_64 实现Bouncycastle SM2加解密
使用OpenEuler x86_64 实现Bouncycastle SM2加解密
一、安装运行环境
-
安装java和maven
sudo yum install java-17-openjdk sudo yum install maven
安装完成后,你就可以在 OpenEuler 上使用 Maven 来管理 Java 项目了。
二、创建项目工程
-
在项目根目录下创建pom.xml文件用于添加Bouncycastle的依赖项。
vi pom.xml
加入内容:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>MySM2Project</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15to18</artifactId>
<version>1.77</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-ext-jdk15to18</artifactId>
<version>1.77</version>
</dependency>
- 将源代码放入src/main/java/BC目录下。
sm2_demo.java:
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.security.*;
import java.security.spec.ECGenParameterSpec;
public class sm2_demo {
static {
Security.addProvider(new BouncyCastleProvider());
}
private static final String M = "lzc_SM2_demo";
public static void main(String[] args) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
try {
SM2Util sm2 = new SM2Util();
final ECGenParameterSpec sm2Spec = new ECGenParameterSpec("sm2p256v1");
final KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", "BC");
kpg.initialize(sm2Spec);
KeyPair keyPair = kpg.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
String data = sm2.encrypt(publicKey, M);
System.out.println(data);
String text = sm2.decrypt(privateKey, data);
System.out.println(text);
} catch (NoSuchProviderException e) {
e.printStackTrace();
}
}
}
SMUtil.java:
import java.security.Security;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.security.Provider;
import org.bouncycastle.crypto.engines.SM2Engine;
import org.bouncycastle.crypto.params.*;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;
import org.bouncycastle.jce.spec.ECParameterSpec;
import org.bouncycastle.util.encoders.Hex;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
public class SM2Util {
static {
Provider provider = new BouncyCastleProvider();
Security.addProvider(provider);
}
public String encrypt(PublicKey publicKey, String data) {
ECPublicKeyParameters ecPublicKeyParameters = null;
if (publicKey instanceof BCECPublicKey) {
BCECPublicKey bcecPublicKey = (BCECPublicKey) publicKey;
ECParameterSpec ecParameterSpec = bcecPublicKey.getParameters();
ECDomainParameters ecDomainParameters = new ECDomainParameters(
ecParameterSpec.getCurve(), ecParameterSpec.getG(), ecParameterSpec.getN());
ecPublicKeyParameters = new ECPublicKeyParameters(bcecPublicKey.getQ(), ecDomainParameters);
}
SM2Engine sm2Engine = new SM2Engine();
sm2Engine.init(true, new ParametersWithRandom(ecPublicKeyParameters, new SecureRandom()));
byte[] arrayOfBytes = null;
try {
byte[] in = data.getBytes("utf-8");
arrayOfBytes = sm2Engine.processBlock(in, 0, in.length);
} catch (Exception e) {
System.out.println("SM2加密时出现异常:");
}
return Hex.toHexString(arrayOfBytes);
}
public String decrypt(PrivateKey privateKey, String cipherData) {
byte[] cipherDataByte = Hex.decode(cipherData);
BCECPrivateKey bcecPrivateKey = (BCECPrivateKey) privateKey;
ECParameterSpec ecParameterSpec = bcecPrivateKey.getParameters();
ECDomainParameters ecDomainParameters = new ECDomainParameters(
ecParameterSpec.getCurve(), ecParameterSpec.getG(), ecParameterSpec.getN());
ECPrivateKeyParameters ecPrivateKeyParameters = new ECPrivateKeyParameters(
bcecPrivateKey.getD(), ecDomainParameters);
SM2Engine sm2Engine = new SM2Engine();
sm2Engine.init(false, ecPrivateKeyParameters);
String result = null;
try {
byte[] arrayOfBytes = sm2Engine.processBlock(cipherDataByte, 0, cipherDataByte.length);
return new String(arrayOfBytes, "utf-8");
} catch (Exception e) {
System.out.println("SM2解密时出现异常");
}
return result;
}
}
-
创建lib文件夹,并将从官网上下载的bcprov-ext-jdk15to18-177.jar和bcprov-jdk15to18-177.jar放入其中。
Bouncycastle官网:https://www.bouncycastle.org/latest_releases.html
三、编译和运行
- 编译
使用mvn系列命令进行编译:
mvn clean install
最终项目结构为:
- 运行
使用命令运行:
java -cp target/classes:target/MySM2Project-1.0-SNAPSHOT.jar:lib/* sm2_demo
- 运行结果