0

如何将带有私有 DSA 密钥(4096 位)的 DER 文件读入 AsymmetricKeyParameter 以在 DSASigner 中使用?

我尝试了以下代码:

 byte[] privateKeyBytes = FileUtils.readFileToByteArray(new File(
                    "sign-key-private.der"));
 AsymmetricKeyParameter privateKey = PrivateKeyFactory
                    .createKey(privateKeyBytes);

结果是一个例外:

java.lang.IllegalArgumentException: unknown object in getInstance: org.bouncycastle.asn1.ASN1Integer
    at org.bouncycastle.asn1.ASN1Sequence.getInstance(Unknown Source)
    at org.bouncycastle.asn1.x509.AlgorithmIdentifier.getInstance(Unknown Source)
    at org.bouncycastle.asn1.pkcs.PrivateKeyInfo.<init>(Unknown Source)
    at org.bouncycastle.asn1.pkcs.PrivateKeyInfo.getInstance(Unknown Source)
    at org.bouncycastle.crypto.util.PrivateKeyFactory.createKey(Unknown Source)
    at test.security.core.Program.main(Program.java:41)
4

2 回答 2

0

我最终使用的解决方法是将密钥转换为 PEM 格式并使用以下内容:

 @Cleanup
 FileReader privateKeyReader = new FileReader(new File("key.pem"));
 @Cleanup
 PEMParser parser = new PEMParser(privateKeyReader);

 PEMKeyPair keyPair = (PEMKeyPair) parser.readObject();
 AsymmetricKeyParameter privateKey = PrivateKeyFactory
     .createKey(keyPair.getPrivateKeyInfo());
 AsymmetricKeyParameter publicKey = PublicKeyFactory
     .createKey(keyPair.getPublicKeyInfo());
于 2015-02-10T10:23:34.687 回答
0
byte[] privateKeyBytes = FileUtils.readFileToByteArray(new File("sign-key-private.der"));
KeyFactory kf = KeyFactory.getInstance("RSA", "BC");
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
RSAPrivateKey rsaKey = (RSAPrivateKey) kf.generatePrivate(keySpec);
AsymmetricKeyParameter privateKeyParameter = new RSAKeyParameters(true, rsaKey.getModulus(), rsaKey.getPrivateExponent());
于 2021-10-13T01:50:00.917 回答