[英]Java - loading private key from .DER file
I originally have a password-protected PEM file with a private key that is read into a Java application using BouncyCastle. 我最初有一个带有私钥的受密码保护的PEM文件,可以使用BouncyCastle将其读取到Java应用程序中。 The PEM file begins with
PEM文件开头为
-----BEGIN RSA PRIVATE KEY----- ----- BEGIN RSA私钥-----
which leads me to believe it is in the PKCS#1 format. 这使我相信它是PKCS#1格式。 Instead of using the PEM file, I want to generate a binary file and read the private key into the Java program.
我不想使用PEM文件,而是要生成一个二进制文件并将私钥读入Java程序。 As per here , I used the following openssl code to generate a DER file:
按照这里 ,我使用以下openssl代码生成DER文件:
openssl pkcs8 -topk8 -nocrypt -in private.pem -outform der -out private.der
Then used this Java code to try to read in the DER file: 然后使用此Java代码尝试读取DER文件:
Path path = Paths.get(privateKeyLocation);
byte[] byteArray = Files.readAllBytes(path);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(byteArray);
PrivateKey privKey;
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
privKey = keyFactory.generatePrivate(keySpec);
} catch (InvalidKeySpecException e) {
logger.error("error with jwt", e);
return null;
} catch (NoSuchAlgorithmException e) {
logger.error("error with jwt", e);
return null;
}
But I'm running into this error: 但是我遇到了这个错误:
java.lang.NoClassDefFoundError: com/rsa/asn1/ASN_Exception
at com.rsa.jsafe.provider.JS_KeyFactory.b(Unknown Source)
at com.rsa.jsafe.provider.JS_KeyFactory.engineGeneratePrivate(Unknown Source)
at java.security.KeyFactory.generatePrivate(KeyFactory.java:372)
...
I'm not sure what is causing this error and wonder if there's a better way to use BouncyCastle to read in a DER file? 我不确定是什么原因导致此错误,并且想知道是否存在使用BouncyCastle读取DER文件的更好方法?
It ended up being a maven issue that was changing the path to the private.der
file. 最终成为一个专家问题,该问题正在更改
private.der
文件的路径。 Using the absolute path solved this issue. 使用绝对路径解决了此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.