1

Weird problem and I verified it is reading the same file.

This does not work:

keystore = KeyStore.getInstance("PKCS12");
InputStream inputStream = ClassLoader.getSystemResourceAsStream("keystores/active.pfx");
keystore.load(inputStream, "the_password".toCharArray());

This, however, does work:

keystore = KeyStore.getInstance("PKCS12");
InputStream inputStream = new FileInputStream(new File("src/main/resources/keystores/active.pfx"));
keystore.load(inputStream, "the_password".toCharArray());

I get the following error:

DER length more than 4 bytes: 111

It's that change of the input stream and I can't figure out what the difference is. I triple-checked the file to make sure it was using the same file. Why is Java treating these streams differently? If I figure out that, I can probably figure out how to fix the problem.

el n00b
  • 1,957
  • 7
  • 37
  • 64
  • 2
    _Why is Java treating these streams differently?_ Why do you assume they should do the same thing? – Sotirios Delimanolis Nov 12 '14 at 21:53
  • My suggestion, read the API. You are using classes assuming you know what they do. If you read the API, you will find out the problem is not weird at all. – hfontanez Nov 12 '14 at 21:56
  • File from classpath is corrupted, check it size https://stackoverflow.com/questions/19500458/maven-resource-binary-changes-file-size-after-build – Valeriy Nov 28 '18 at 14:16

1 Answers1

1

The two approaches are not reading the exact same file. The resource code reads it from the JAR file or wherever the classes got compiled to. Clearly the file got corrupted somehow during the build process.

user207421
  • 305,947
  • 44
  • 307
  • 483