alipay-hk 配置文件位置修改

This commit is contained in:
2024-07-10 10:47:36 +08:00
parent a56510c35a
commit 46fbbd747e
4 changed files with 76 additions and 14 deletions

View File

@@ -1,5 +1,6 @@
package com.ai.da.common.utils;
import cn.hutool.core.io.resource.ClassPathResource;
import com.ai.da.model.dto.AlipayHKParametersDTO;
import com.ai.da.model.dto.AlipayHKRequestDTO;
import com.alibaba.fastjson.JSONObject;
@@ -14,6 +15,7 @@ import org.bouncycastle.crypto.util.PublicKeyFactory;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.util.io.pem.PemObject;
import org.bouncycastle.util.io.pem.PemReader;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@@ -165,7 +167,23 @@ public class AlipayHKEncryptionUtil {
* @throws Exception
*/
private static PrivateKey readPrivateKeyFromFile(String filePath) throws Exception {
BufferedReader reader = new BufferedReader(new FileReader(filePath));
ClassPathResource classPathResource = new ClassPathResource(filePath);
InputStream inputstream = classPathResource.getStream();
String privateKeyContent = getString(inputstream);
// 进行 Base64 解码
byte[] privateKeyBytes = Base64.getDecoder().decode(privateKeyContent);
// 根据 PKCS8 格式的私钥字节数组构造私钥对象
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
return keyFactory.generatePrivate(keySpec);
}
@NotNull
private static String getString(InputStream inputstream) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputstream));
// BufferedReader reader = new BufferedReader(new FileReader(filePath));
StringBuilder keyBuffer = new StringBuilder();
String line;
@@ -180,14 +198,7 @@ public class AlipayHKEncryptionUtil {
.replace("-----BEGIN PRIVATE KEY-----", "")
.replace("-----END PRIVATE KEY-----", "")
.replaceAll("\\s+", ""); // 去除空格、换行等字符
// 进行 Base64 解码
byte[] privateKeyBytes = Base64.getDecoder().decode(privateKeyContent);
// 根据 PKCS8 格式的私钥字节数组构造私钥对象
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
return keyFactory.generatePrivate(keySpec);
return privateKeyContent;
}
/**
@@ -233,7 +244,7 @@ public class AlipayHKEncryptionUtil {
// 使用 RSA 公钥加密 AES 密钥
String rsaPublicKeyPath = publicKeyPath;
PublicKey publicKey = readPublicKey(new File(rsaPublicKeyPath));
PublicKey publicKey = readPublicKey(rsaPublicKeyPath);
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
// Cipher rsaCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
@@ -313,11 +324,14 @@ public class AlipayHKEncryptionUtil {
System.out.println("数字签名验证结果:" + signatureValid);
}
public static PublicKey readPublicKey(File file) throws Exception {
// public static PublicKey readPublicKey(File file) throws Exception {
public static PublicKey readPublicKey(String fileName) throws Exception {
KeyFactory factory = KeyFactory.getInstance("RSA");
try (FileReader keyReader = new FileReader(file);
PemReader pemReader = new PemReader(keyReader)) {
ClassPathResource classPathResource = new ClassPathResource(fileName);
InputStream inputStream = classPathResource.getStream();
BufferedReader fileReader = new BufferedReader(new InputStreamReader(inputStream));
try (PemReader pemReader = new PemReader(fileReader)) {
PemObject pemObject = pemReader.readPemObject();
byte[] content = pemObject.getContent();
@@ -338,8 +352,10 @@ public class AlipayHKEncryptionUtil {
Base64.Decoder decoder = Base64.getDecoder();
// Verify key
try {
ClassPathResource classPathResource = new ClassPathResource(CODPublicKeyPath);
InputStream inputstream = classPathResource.getStream();
// 从指定的路径读取公钥文件
InputStreamReader isrPub = new InputStreamReader(new FileInputStream(CODPublicKeyPath));
InputStreamReader isrPub = new InputStreamReader(inputstream);
PEMParser pemParserPub = new PEMParser(isrPub);
// 使用 PEMParser 解析公钥文件
SubjectPublicKeyInfo pubInfo = (SubjectPublicKeyInfo) pemParserPub.readObject();