I am having some trouble with XML digital signature with a PFX file. I get an exception when I run this code:
KeyStore ks = KeyStore.getInstance("PKCS12");
fs = new FileInputStream(file);
ks.load(fs, "password".toCharArray());
// this line!
KeyStore.PrivateKeyEntry keyEntry = (KeyStore.PrivateKeyEntry) ks.getEntry("alias", new KeyStore.PasswordProtection("password".toCharArray()));
This is the exception:
java.security.UnrecoverableKeyException: Get Key failed:
java.security.InvalidKeyException: Invalid RSA private key
at sun.security.pkcs12.PKCS12KeyStore.engineGetKey(PKCS12KeyStore.java:435)
at sun.security.pkcs12.PKCS12KeyStore.engineGetEntry(PKCS12KeyStore.java:1306)
at java.security.KeyStore.getEntry(KeyStore.java:1521)
at app.ubl.xml.GenerateSignature.makeSignatureXML(GenerateSignature.java:88)
Caused by: java.io.IOException: DerInputStream.getLength(): Redundant length bytes found
at sun.security.util.DerInputStream.getLength(DerInputStream.java:606)
at sun.security.util.DerInputStream.getLength(DerInputStream.java:569)
at sun.security.util.DerInputStream.getPositiveBigInteger(DerInputStream.java:220)
at sun.security.rsa.RSAPrivateCrtKeyImpl.parseKeyBits(RSAPrivateCrtKeyImpl.java:205)
The real problem is that the code works in java 1.8.0_111
, but any higher version the error shows.
Exception when running keytool
Another issue that I found is that when I run this command using keytool
:
keytool -list -keystore file.pfx -storepass password -storetype PKCS12 -v
to show the details of the PFX file then again it only works on java 1.8.0_111
. Otherwise I get this exception:
java.util.IllegalFormatConversionException: d != java.lang.String
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302)
at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2793)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2747)
at java.util.Formatter.format(Formatter.java:2520)
at java.util.Formatter.format(Formatter.java:2455)
at java.lang.String.format(String.java:2940)
at sun.security.tools.keytool.Main.withWeak(Main.java:3076)
at sun.security.tools.keytool.Main.printX509Cert(Main.java:3125)
at sun.security.tools.keytool.Main.doPrintEntry(Main.java:1924)
at sun.security.tools.keytool.Main.doPrintEntries(Main.java:2236)
at sun.security.tools.keytool.Main.doCommands(Main.java:1123)
at sun.security.tools.keytool.Main.run(Main.java:366)
at sun.security.tools.keytool.Main.main(Main.java:359)
I don't know if this is relevant, but that is all I got.
Any thoughts?
PD: Sorry for my bad english.