0

How to read DER file with private DSA key (4096 bit) into AsymmetricKeyParameter for usage in DSASigner ?

The following code I tried:

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

Result is an exception:

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)
kulatamicuda
  • 1,603
  • 2
  • 21
  • 40
  • is the DER file encoded in any way, or protected with password? – EpicPandaForce Feb 09 '15 at 14:49
  • according to the source, the following method is called: `PrivateKeyFactory.createKey(PrivateKeyInfo.getInstance(new ASN1InputStream(inputStream).readObject()));` please try to do this manually and see which step fails using `new FileInputStream("sign-key-private.der");` – EpicPandaForce Feb 09 '15 at 14:51
  • From the source, and the stacktrace, it's trying to parse the ANS encoded Algorithm Identifier, expecting a sequence, and getting an ASN encoded Integer. So... any chance you could use openssl to dump the contents of the .DER file and ensure that it can do so successfully? – Greycon Feb 09 '15 at 17:25
  • DER file is not protected with password and is generated with XCA 0.9.3. I have workarounded the problem by converting to PEM file – kulatamicuda Feb 10 '15 at 10:18

2 Answers2

1

The workaround solution I have finally used was to convert key to PEM format and use the following:

 @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());
kulatamicuda
  • 1,603
  • 2
  • 21
  • 40
1
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());
Karl Engel
  • 11
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 13 '21 at 04:58