1.下載jar包
https://www.bouncycastle.org/latest_releases.html 找了半天在官網上沒找到,是找的其他的csdn的網頁
二級標題將下載的兩個jar包複製到 %JAVA_HOME%\jre\lib\ext 目錄下面
3.修改配置檔案%JAVA_HOME%\jre\lib\security\java.security ,在末尾新增security.provider.11=org.bouncycastle.jce.provider.BouncyCastleProvider ;
在專案中匯入jar包: bcprov‐ext‐jdk15to18‐168.jar
以下是程式碼
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.security.*;
import java.security.spec.ECGenParameterSpec;
import java.util.Arrays;
public class demo {
private static String M = "ycy20211105";
public static void main(String[] args) throws Exception {
SM2Util sm2 = new SM2Util();
final ECGenParameterSpec sm2Spec = new ECGenParameterSpec("sm2p256v1");
// 獲取一個橢圓曲線型別的金鑰對生成器
final KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", new BouncyCastleProvider());
// 使用SM2引數初始化生成器
kpg.initialize(sm2Spec);
// 獲取金鑰對
KeyPair keyPair = kpg.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
System.out.println("資料:" + M);
String data = sm2.encrypt(publicKey, M);
System.out.println("公鑰加密:" + data);
String text = sm2.decrypt(privateKey, data);
System.out.println("私鑰解密:" + text);
SM3Util sm3 = new SM3Util();
byte[] digest = sm3.digest(M);
System.out.println("SM3摘要:" + Arrays.toString(digest));
SM4Util sm4 = new SM4Util();
byte[] key1 = SM4Key.generateKey();
byte[] iv = null;
byte[] sm4data = sm4.encrypt(digest, key1, SM4ModeAndPaddingEnum.SM4_ECB_NoPadding, iv);
System.out.println("SM4加密:" + Arrays.toString(sm4data));
byte[] sm4text = sm4.decrypt(sm4data, key1, SM4ModeAndPaddingEnum.SM4_ECB_NoPadding, iv);
System.out.println("SM4解密:" + Arrays.toString(sm4text));
}
}
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.nio.charset.StandardCharsets;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
public class SM2Util {
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(StandardCharsets.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, StandardCharsets.UTF_8);
} catch (Exception e) {
System.out.println("SM2解密時出現異常");
}
return result;
}
}
import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.security.Security;
public class SM3Util {
static {
Security.addProvider(new BouncyCastleProvider());
}
public byte[] digest(String input) {
SM3Digest sm3Digest = new SM3Digest();
int length = input.length();
byte[] data = input.getBytes();
sm3Digest.update(data, 0, length);
byte[] ret = new byte[sm3Digest.getDigestSize()];
sm3Digest.doFinal(ret, 0);
return ret;
}
}
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.KeyGenerator;
import java.security.SecureRandom;
import java.security.Security;
public class SM4Key {
static {
Security.addProvider(new BouncyCastleProvider());
}
public static byte[] generateKey() throws Exception {
KeyGenerator kg = KeyGenerator.getInstance("SM4", BouncyCastleProvider.PROVIDER_NAME);
kg.init(128, new SecureRandom());
return kg.generateKey().getEncoded();
}
}
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Security;
public class SM4Util {
static {
Security.addProvider(new BouncyCastleProvider());
}
public byte[] encrypt(byte[] input, byte[] key, SM4ModeAndPaddingEnum sm4ModeAndPaddingEnum, byte[] iv)
throws Exception {
return sm4(input, key, sm4ModeAndPaddingEnum, iv, Cipher.ENCRYPT_MODE);
}
public byte[] decrypt(byte[] input, byte[] key, SM4ModeAndPaddingEnum sm4ModeAndPaddingEnum, byte[] iv)
throws Exception {
return sm4(input, key, sm4ModeAndPaddingEnum, iv, Cipher.DECRYPT_MODE);
}
private static byte[] sm4(byte[] input, byte[] key, SM4ModeAndPaddingEnum sm4ModeAndPaddingEnum, byte[] iv,
int mode) throws Exception {
IvParameterSpec ivParameterSpec = null;
if (iv != null) {
ivParameterSpec = new IvParameterSpec(iv);
}
SecretKeySpec sm4Key = new SecretKeySpec(key, "SM4");
Cipher cipher = Cipher.getInstance(sm4ModeAndPaddingEnum.getName(), BouncyCastleProvider.PROVIDER_NAME);
if (ivParameterSpec == null) {
cipher.init(mode, sm4Key);
} else {
cipher.init(mode, sm4Key, ivParameterSpec);
}
return cipher.doFinal(input);
}
}
public enum SM4ModeAndPaddingEnum {
SM4_ECB_NoPadding("SM4/ECB/NoPadding"), SM4_ECB_PKCS5Padding("SM4/ECB/PKCS5Padding"),
SM4_ECB_PKCS7Padding("SM4/ECB/PKCS7Padding"), SM4_CBC_NoPadding("SM4/CBC/NoPadding"),
SM4_CBC_PKCS5Padding("SM4/CBC/PKCS5Padding"), SM4_CBC_PKCS7Padding("SM4/CBC/PKCS7Padding");
private String name;
SM4ModeAndPaddingEnum(String name) {
this.name = name;
}
public String getName() {
return name;
}
}