|
@@ -1,3 +1,4 @@
|
|
|
|
|
+/*
|
|
|
package com.ruoyi.common.utils;
|
|
package com.ruoyi.common.utils;
|
|
|
|
|
|
|
|
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
|
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
|
@@ -8,9 +9,11 @@ import java.security.spec.PKCS8EncodedKeySpec;
|
|
|
import java.security.spec.X509EncodedKeySpec;
|
|
import java.security.spec.X509EncodedKeySpec;
|
|
|
import java.util.Base64;
|
|
import java.util.Base64;
|
|
|
|
|
|
|
|
|
|
+*/
|
|
|
/**
|
|
/**
|
|
|
* SM2非对称加密工具类 - 兼容版本
|
|
* SM2非对称加密工具类 - 兼容版本
|
|
|
- */
|
|
|
|
|
|
|
+ *//*
|
|
|
|
|
+
|
|
|
@Component
|
|
@Component
|
|
|
public class SM2Util {
|
|
public class SM2Util {
|
|
|
|
|
|
|
@@ -18,9 +21,11 @@ public class SM2Util {
|
|
|
Security.addProvider(new BouncyCastleProvider());
|
|
Security.addProvider(new BouncyCastleProvider());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
|
|
+ */
|
|
|
|
|
+/**
|
|
|
* 生成SM2密钥对
|
|
* 生成SM2密钥对
|
|
|
- */
|
|
|
|
|
|
|
+ *//*
|
|
|
|
|
+
|
|
|
public static KeyPair generateKeyPair() {
|
|
public static KeyPair generateKeyPair() {
|
|
|
try {
|
|
try {
|
|
|
// 使用标准的KeyPairGenerator
|
|
// 使用标准的KeyPairGenerator
|
|
@@ -35,9 +40,11 @@ public class SM2Util {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
|
|
+ */
|
|
|
|
|
+/**
|
|
|
* 使用私钥对数据进行签名
|
|
* 使用私钥对数据进行签名
|
|
|
- */
|
|
|
|
|
|
|
+ *//*
|
|
|
|
|
+
|
|
|
public static String sign(byte[] data, PrivateKey privateKey) {
|
|
public static String sign(byte[] data, PrivateKey privateKey) {
|
|
|
try {
|
|
try {
|
|
|
Signature signature = Signature.getInstance("SM3withSM2", "BC");
|
|
Signature signature = Signature.getInstance("SM3withSM2", "BC");
|
|
@@ -50,9 +57,11 @@ public class SM2Util {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
|
|
+ */
|
|
|
|
|
+/**
|
|
|
* 使用公钥验证签名
|
|
* 使用公钥验证签名
|
|
|
- */
|
|
|
|
|
|
|
+ *//*
|
|
|
|
|
+
|
|
|
public static boolean verify(byte[] data, String signature, PublicKey publicKey) {
|
|
public static boolean verify(byte[] data, String signature, PublicKey publicKey) {
|
|
|
try {
|
|
try {
|
|
|
Signature verifier = Signature.getInstance("SM3withSM2", "BC");
|
|
Signature verifier = Signature.getInstance("SM3withSM2", "BC");
|
|
@@ -65,9 +74,11 @@ public class SM2Util {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
|
|
+ */
|
|
|
|
|
+/**
|
|
|
* 从Base64字符串加载私钥
|
|
* 从Base64字符串加载私钥
|
|
|
- */
|
|
|
|
|
|
|
+ *//*
|
|
|
|
|
+
|
|
|
public static PrivateKey loadPrivateKey(String base64PrivateKey) {
|
|
public static PrivateKey loadPrivateKey(String base64PrivateKey) {
|
|
|
try {
|
|
try {
|
|
|
byte[] keyBytes = Base64.getDecoder().decode(base64PrivateKey);
|
|
byte[] keyBytes = Base64.getDecoder().decode(base64PrivateKey);
|
|
@@ -75,13 +86,16 @@ public class SM2Util {
|
|
|
KeyFactory keyFactory = KeyFactory.getInstance("EC", "BC");
|
|
KeyFactory keyFactory = KeyFactory.getInstance("EC", "BC");
|
|
|
return keyFactory.generatePrivate(keySpec);
|
|
return keyFactory.generatePrivate(keySpec);
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
|
|
+ System.out.println(ExceptionUtil.getRootErrorMessage(e));
|
|
|
throw new RuntimeException("加载SM2私钥失败", e);
|
|
throw new RuntimeException("加载SM2私钥失败", e);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
|
|
+ */
|
|
|
|
|
+/**
|
|
|
* 从Base64字符串加载公钥
|
|
* 从Base64字符串加载公钥
|
|
|
- */
|
|
|
|
|
|
|
+ *//*
|
|
|
|
|
+
|
|
|
public static PublicKey loadPublicKey(String base64PublicKey) {
|
|
public static PublicKey loadPublicKey(String base64PublicKey) {
|
|
|
try {
|
|
try {
|
|
|
byte[] keyBytes = Base64.getDecoder().decode(base64PublicKey);
|
|
byte[] keyBytes = Base64.getDecoder().decode(base64PublicKey);
|
|
@@ -93,17 +107,21 @@ public class SM2Util {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
|
|
+ */
|
|
|
|
|
+/**
|
|
|
* 将私钥转换为Base64字符串
|
|
* 将私钥转换为Base64字符串
|
|
|
- */
|
|
|
|
|
|
|
+ *//*
|
|
|
|
|
+
|
|
|
public static String privateKeyToString(PrivateKey privateKey) {
|
|
public static String privateKeyToString(PrivateKey privateKey) {
|
|
|
return Base64.getEncoder().encodeToString(privateKey.getEncoded());
|
|
return Base64.getEncoder().encodeToString(privateKey.getEncoded());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
|
|
+ */
|
|
|
|
|
+/**
|
|
|
* 将公钥转换为Base64字符串
|
|
* 将公钥转换为Base64字符串
|
|
|
- */
|
|
|
|
|
|
|
+ *//*
|
|
|
|
|
+
|
|
|
public static String publicKeyToString(PublicKey publicKey) {
|
|
public static String publicKeyToString(PublicKey publicKey) {
|
|
|
return Base64.getEncoder().encodeToString(publicKey.getEncoded());
|
|
return Base64.getEncoder().encodeToString(publicKey.getEncoded());
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
|
|
+}*/
|