java - Java加载加密私钥
问题描述
我花了太长时间寻找可以加载由 openssl 证书创建自动生成的加密私钥的解决方案。
使用密码生成证书和新私钥:password
openssl req -newkey rsa:2048 -x509 -keyout test.key -out test.crt -days 365
我已经提取了代码的重要部分
package test;
import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
import org.bouncycastle.util.encoders.Base64;
import org.junit.jupiter.api.BeforeEach;
import javax.crypto.Cipher;
import javax.crypto.EncryptedPrivateKeyInfo;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.AlgorithmParameters;
import java.security.Key;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.KeySpec;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Log4j2
class pkShould {
private static final String PASS_PHRASE = "password";
@SneakyThrows
@BeforeEach
public void setup() {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Path pKPath = Paths.get(cl.getResource("keystore/test.key").toURI());
try(Stream<String> lines = Files.lines(pKPath)) {
String encrypted = lines.collect(Collectors.joining("\n"));
encrypted = encrypted.replace("-----BEGIN ENCRYPTED PRIVATE KEY-----", "");
encrypted = encrypted.replace("-----END ENCRYPTED PRIVATE KEY-----", "");
byte[] content = Base64.decode(encrypted);
EncryptedPrivateKeyInfo encryptPKInfo = new EncryptedPrivateKeyInfo(content);
Cipher cipher = Cipher.getInstance(encryptPKInfo.getAlgName());
PBEKeySpec pbeKeySpec = new PBEKeySpec(PASS_PHRASE.toCharArray());
SecretKeyFactory secFac = SecretKeyFactory.getInstance(encryptPKInfo.getAlgName());
Key pbeKey = secFac.generateSecret(pbeKeySpec);
AlgorithmParameters algParams = encryptPKInfo.getAlgParameters();
cipher.init(Cipher.DECRYPT_MODE, pbeKey, algParams);
KeySpec pkcs8KeySpec = encryptPKInfo.getKeySpec(cipher);
KeyFactory kf = KeyFactory.getInstance("RSA");
PrivateKey expectedPrivateKey = kf.generatePrivate(pkcs8KeySpec);
}
}
}
永远不要让它过去EncryptedPrivateKeyInfo
,据我所知,私钥形成正确并且它在 keyStore 中使用得很好。
堆栈跟踪
java.io.IOException: ObjectIdentifier() -- data isn't an object ID (tag = 48)
at sun.security.util.ObjectIdentifier.<init>(ObjectIdentifier.java:285)
at sun.security.util.DerInputStream.getOID(DerInputStream.java:321)
at com.sun.crypto.provider.PBES2Parameters.engineInit(PBES2Parameters.java:267)
at java.security.AlgorithmParameters.init(AlgorithmParameters.java:293)
at sun.security.x509.AlgorithmId.decodeParams(AlgorithmId.java:150)
at sun.security.x509.AlgorithmId.<init>(AlgorithmId.java:132)
at sun.security.x509.AlgorithmId.parse(AlgorithmId.java:416)
at javax.crypto.EncryptedPrivateKeyInfo.<init>(EncryptedPrivateKeyInfo.java:95)
任何帮助深表感谢。
解决方案
不幸的是,这个错误是非常无用的。它真正的意思是'java不喜欢它'。它可能意味着几乎任何东西:密钥受到密码短语密码和/或 java 不喜欢的比特大小的保护,密钥本身使用 java 不喜欢的密码/比特大小,密钥的格式不是 PKCS#8。
看起来您确实生成了 x509 格式的密钥,这可能无法正常工作。也许转换它会起作用;试试这个:
openssl pkcs8 -topk8 \
-inform PEM -outform PEM \
-in key.pem -out key-pkcs8.pem
推荐阅读
- firebase - 在 Heroku 上部署 Spring Boot 应用程序不工作
- sql - 使用 GROUP BY 在 SQL Server 中计算总数的百分比
- angular - Angular 在动态模块中使用 Fontawesome 指令
- php - 如何使用 mysql 将带有文本数据的图像插入 php 中的数据库。?
- php - 如何使用 php 从 JSON 中获取特定值
- angular - 如何以角度拦截所有http请求
- devexpress - XtraReport 保存文件
- python - 使用 if 条件 django 将图像字段重置为默认图像
- postgresql - 如何在 Aqueduct 和 Postgres 中使用不同的表名
- vba - Kofax 转换 VBA 脚本以更新字段值