|
@@ -1,14 +1,16 @@
|
|
|
package com.fs.qw.utils;
|
|
package com.fs.qw.utils;
|
|
|
|
|
|
|
|
|
|
+import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
|
|
|
|
|
+import org.bouncycastle.openssl.PEMParser;
|
|
|
|
|
+import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
import javax.crypto.Cipher;
|
|
import javax.crypto.Cipher;
|
|
|
|
|
+import java.io.StringReader;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.charset.StandardCharsets;
|
|
|
-import java.security.KeyFactory;
|
|
|
|
|
import java.security.PrivateKey;
|
|
import java.security.PrivateKey;
|
|
|
-import java.security.spec.PKCS8EncodedKeySpec;
|
|
|
|
|
import java.util.Base64;
|
|
import java.util.Base64;
|
|
|
|
|
|
|
|
@Component
|
|
@Component
|
|
@@ -16,12 +18,6 @@ public class WeChatSpaceDecryptUtil {
|
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(WeChatSpaceDecryptUtil.class);
|
|
private static final Logger log = LoggerFactory.getLogger(WeChatSpaceDecryptUtil.class);
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * 解密 encrypted_secret_key
|
|
|
|
|
- * @param encryptedSecretKey Base64 编码的 RSA 密文
|
|
|
|
|
- * @param privateKeyPem PEM 格式私钥字符串(含 -----BEGIN/END-----)
|
|
|
|
|
- * @return 原始 AES 密钥字符串
|
|
|
|
|
- */
|
|
|
|
|
public static String decryptSecretKey(String encryptedSecretKey, String privateKeyPem) throws Exception {
|
|
public static String decryptSecretKey(String encryptedSecretKey, String privateKeyPem) throws Exception {
|
|
|
if (privateKeyPem == null || privateKeyPem.isEmpty()) {
|
|
if (privateKeyPem == null || privateKeyPem.isEmpty()) {
|
|
|
throw new IllegalArgumentException("私钥不能为空");
|
|
throw new IllegalArgumentException("私钥不能为空");
|
|
@@ -35,15 +31,21 @@ public class WeChatSpaceDecryptUtil {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private static PrivateKey parsePrivateKey(String privateKeyPem) throws Exception {
|
|
private static PrivateKey parsePrivateKey(String privateKeyPem) throws Exception {
|
|
|
- String privateKeyBase64 = privateKeyPem
|
|
|
|
|
- .replace("-----BEGIN PRIVATE KEY-----", "")
|
|
|
|
|
- .replace("-----END PRIVATE KEY-----", "")
|
|
|
|
|
- .replace("-----BEGIN RSA PRIVATE KEY-----", "")
|
|
|
|
|
- .replace("-----END RSA PRIVATE KEY-----", "")
|
|
|
|
|
- .replaceAll("\\s", "");
|
|
|
|
|
- byte[] keyBytes = Base64.getDecoder().decode(privateKeyBase64);
|
|
|
|
|
- PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
|
|
|
|
|
- KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
|
|
|
|
- return keyFactory.generatePrivate(spec);
|
|
|
|
|
|
|
+ try (PEMParser pemParser = new PEMParser(new StringReader(privateKeyPem))) {
|
|
|
|
|
+ Object object = pemParser.readObject();
|
|
|
|
|
+ PrivateKeyInfo privateKeyInfo = null;
|
|
|
|
|
+ if (object instanceof PrivateKeyInfo) {
|
|
|
|
|
+ privateKeyInfo = (PrivateKeyInfo) object; // PKCS#8
|
|
|
|
|
+ } else if (object instanceof org.bouncycastle.openssl.PEMKeyPair) {
|
|
|
|
|
+ // PKCS#1 格式
|
|
|
|
|
+ org.bouncycastle.openssl.PEMKeyPair keyPair = (org.bouncycastle.openssl.PEMKeyPair) object;
|
|
|
|
|
+ privateKeyInfo = keyPair.getPrivateKeyInfo();
|
|
|
|
|
+ }
|
|
|
|
|
+ if (privateKeyInfo == null) {
|
|
|
|
|
+ throw new IllegalArgumentException("Unsupported private key format");
|
|
|
|
|
+ }
|
|
|
|
|
+ JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
|
|
|
|
|
+ return converter.getPrivateKey(privateKeyInfo);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|