如何读取 PKCS8 加密的私钥,它也用 bouncycastle 编码在 DER 中?
How read a PKCS8 encrypted Private key which is also encoded in DER with bouncycastle?
我试过这些问题的答案:
Bouncy Castle : PEMReader => PEMParser
Read an encrypted private key with bouncycastle/spongycastle
然而,当我调用
时,我的加密密钥是用 DER 编码的
Object object = pemParser.readObject();
对象为空。
我可以用这个 openssl 的命令将它转换为 PEM(它也解密密钥)
openssl pkcs8 -inform der -in pkey.key -out pkey.pem
但我需要读取原始文件中的密钥
这两个问题都是关于使用 OpenSSL 的 'legacy PEM' 加密解析和解密文件。您正在使用 PKCS8 加密,虽然相似但不同,因此 更接近。您可以在那里使用大部分方法,但跳过 PEM 解析:
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.pkcs.EncryptedPrivateKeyInfo; // NOT the
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo; // javax ones!
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import org.bouncycastle.openssl.jcajce.JceOpenSSLPKCS8DecryptorProviderBuilder;
import org.bouncycastle.operator.InputDecryptorProvider;
import org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo;
// args[0] = filename args[1] = password
FileInputStream fis = new FileInputStream(args[0]);
byte[] buff = new byte[9999]; int len = fis.read(buff); fis.close();
// could use File.readAllBytes in j8 but my dev machine is old
// create what PEMParser would have
ASN1Sequence derseq = ASN1Sequence.getInstance (Arrays.copyOf(buff,len));
PKCS8EncryptedPrivateKeyInfo encobj = new PKCS8EncryptedPrivateKeyInfo(EncryptedPrivateKeyInfo.getInstance(derseq));
// decrypt and convert key
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
InputDecryptorProvider decryptionProv = new JceOpenSSLPKCS8DecryptorProviderBuilder().build(args[1].toCharArray());
PrivateKeyInfo keyInfo = encobj.decryptPrivateKeyInfo(decryptionProv);
PrivateKey key = converter.getPrivateKey(keyInfo);
// now actually use key, this is just a dummy
System.out.println (key.getAlgorithm());
通过使用 SpongyCastle 我们可以获得很好的效果。我编写了一些代码来获得预期的结果。
SpongyCastle Jars 可以从下面下载:-
core, prov, pkix
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.Security;
// need core.jar, prov.jar, bcpkix-jdk15on.jar
import org.spongycastle.asn1.ASN1Sequence;
import org.spongycastle.asn1.pkcs.EncryptedPrivateKeyInfo;
import org.spongycastle.asn1.pkcs.PrivateKeyInfo;
import org.spongycastle.openssl.jcajce.JcaPEMKeyConverter;
import org.spongycastle.openssl.jcajce.JceOpenSSLPKCS8DecryptorProviderBuilder;
import org.spongycastle.operator.InputDecryptorProvider;
import org.spongycastle.pkcs.PKCS8EncryptedPrivateKeyInfo;
class Decrypt {
private static void usage() {
System.err.println(" Usage: encrypted.pk8 password out_decrypted.pk8");
System.exit(2);
}
public static byte[] copyOf(byte[] original, int newLength) {
byte[] copy = new byte[newLength];
System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
return copy;
}
public static void loadProvider(String providerClassName)
throws ClassNotFoundException, IllegalAccessException, InstantiationException {
Class providerClass = Class.forName(providerClassName);
Provider provider = (Provider)providerClass.newInstance();
// Security.insertProviderAt(provider, 1);
Security.addProvider(provider);
}
public static void main(String[] args) throws Exception {
if (args.length != 3) usage();
loadProvider("org.spongycastle.jce.provider.BouncyCastleProvider");
FileInputStream fis = new FileInputStream(args[0]);
byte[] buff = new byte[fis.available()];
int len = fis.read(buff);
fis.close();
// create what PEMParser would have
ASN1Sequence derseq = ASN1Sequence.getInstance(copyOf(buff, len));
EncryptedPrivateKeyInfo epkInfo = EncryptedPrivateKeyInfo.getInstance(derseq);
PKCS8EncryptedPrivateKeyInfo encobj = new PKCS8EncryptedPrivateKeyInfo(epkInfo);
// decrypt and convert key
InputDecryptorProvider decryptionProv = new JceOpenSSLPKCS8DecryptorProviderBuilder().build(args[1].toCharArray());
PrivateKeyInfo keyInfo = encobj.decryptPrivateKeyInfo(decryptionProv);
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("SC");
PrivateKey key = converter.getPrivateKey(keyInfo);
// now actually use key, this is just a dummy
System.out.println(key.getAlgorithm());
FileOutputStream fos = new FileOutputStream(args[2]);
fos.write(key.getEncoded());
fos.close();
}
}
我试过这些问题的答案:
Bouncy Castle : PEMReader => PEMParser
Read an encrypted private key with bouncycastle/spongycastle
然而,当我调用
时,我的加密密钥是用 DER 编码的Object object = pemParser.readObject();
对象为空。
我可以用这个 openssl 的命令将它转换为 PEM(它也解密密钥)
openssl pkcs8 -inform der -in pkey.key -out pkey.pem
但我需要读取原始文件中的密钥
这两个问题都是关于使用 OpenSSL 的 'legacy PEM' 加密解析和解密文件。您正在使用 PKCS8 加密,虽然相似但不同,因此
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.pkcs.EncryptedPrivateKeyInfo; // NOT the
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo; // javax ones!
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import org.bouncycastle.openssl.jcajce.JceOpenSSLPKCS8DecryptorProviderBuilder;
import org.bouncycastle.operator.InputDecryptorProvider;
import org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo;
// args[0] = filename args[1] = password
FileInputStream fis = new FileInputStream(args[0]);
byte[] buff = new byte[9999]; int len = fis.read(buff); fis.close();
// could use File.readAllBytes in j8 but my dev machine is old
// create what PEMParser would have
ASN1Sequence derseq = ASN1Sequence.getInstance (Arrays.copyOf(buff,len));
PKCS8EncryptedPrivateKeyInfo encobj = new PKCS8EncryptedPrivateKeyInfo(EncryptedPrivateKeyInfo.getInstance(derseq));
// decrypt and convert key
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
InputDecryptorProvider decryptionProv = new JceOpenSSLPKCS8DecryptorProviderBuilder().build(args[1].toCharArray());
PrivateKeyInfo keyInfo = encobj.decryptPrivateKeyInfo(decryptionProv);
PrivateKey key = converter.getPrivateKey(keyInfo);
// now actually use key, this is just a dummy
System.out.println (key.getAlgorithm());
通过使用 SpongyCastle 我们可以获得很好的效果。我编写了一些代码来获得预期的结果。 SpongyCastle Jars 可以从下面下载:- core, prov, pkix
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.Security;
// need core.jar, prov.jar, bcpkix-jdk15on.jar
import org.spongycastle.asn1.ASN1Sequence;
import org.spongycastle.asn1.pkcs.EncryptedPrivateKeyInfo;
import org.spongycastle.asn1.pkcs.PrivateKeyInfo;
import org.spongycastle.openssl.jcajce.JcaPEMKeyConverter;
import org.spongycastle.openssl.jcajce.JceOpenSSLPKCS8DecryptorProviderBuilder;
import org.spongycastle.operator.InputDecryptorProvider;
import org.spongycastle.pkcs.PKCS8EncryptedPrivateKeyInfo;
class Decrypt {
private static void usage() {
System.err.println(" Usage: encrypted.pk8 password out_decrypted.pk8");
System.exit(2);
}
public static byte[] copyOf(byte[] original, int newLength) {
byte[] copy = new byte[newLength];
System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
return copy;
}
public static void loadProvider(String providerClassName)
throws ClassNotFoundException, IllegalAccessException, InstantiationException {
Class providerClass = Class.forName(providerClassName);
Provider provider = (Provider)providerClass.newInstance();
// Security.insertProviderAt(provider, 1);
Security.addProvider(provider);
}
public static void main(String[] args) throws Exception {
if (args.length != 3) usage();
loadProvider("org.spongycastle.jce.provider.BouncyCastleProvider");
FileInputStream fis = new FileInputStream(args[0]);
byte[] buff = new byte[fis.available()];
int len = fis.read(buff);
fis.close();
// create what PEMParser would have
ASN1Sequence derseq = ASN1Sequence.getInstance(copyOf(buff, len));
EncryptedPrivateKeyInfo epkInfo = EncryptedPrivateKeyInfo.getInstance(derseq);
PKCS8EncryptedPrivateKeyInfo encobj = new PKCS8EncryptedPrivateKeyInfo(epkInfo);
// decrypt and convert key
InputDecryptorProvider decryptionProv = new JceOpenSSLPKCS8DecryptorProviderBuilder().build(args[1].toCharArray());
PrivateKeyInfo keyInfo = encobj.decryptPrivateKeyInfo(decryptionProv);
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("SC");
PrivateKey key = converter.getPrivateKey(keyInfo);
// now actually use key, this is just a dummy
System.out.println(key.getAlgorithm());
FileOutputStream fos = new FileOutputStream(args[2]);
fos.write(key.getEncoded());
fos.close();
}
}