Create signature with bouncycastle api. Key always null

159 views Asked by At

I'm using bouncycastle for generating detached signature for XML's signing. For key initialize I use this code:

Security.addProvider(new BouncyCastleProvider());

    KeyStore ks = KeyStore.getInstance(KEYSTORE_INSTANCE);
    ks.load(new FileInputStream(KEYSTORE_FILE), KEYSTORE_PWD.toCharArray());
    Key key = ks.getKey(CERT_ALIAS, KEYSTORE_PWD.toCharArray());

I have JKS keystore with certificate. But if I do this:

Key key = ks.getKey(CERT_ALIAS, KEYSTORE_PWD.toCharArray());

key stay always null and I have InvalidKeyException Where's my mistake? I new in crypto

1

There are 1 answers

0
Maurice van Ree On

I can't comment due to too low reputation. So I'll answer/edit instead.

The above example works fine, the error is probably in one of the constants being used. What are they, and what is the exact error you're getting?

I mean something like: java.security.InvalidKeyException: Illegal key size

Here is the working example I tried while loading a KeyStore from a file: 'secret1' is the store password, 'secret2' is the key password and 'myKey' is the key alias.

    KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
    keyStore.load(new FileInputStream(keyStoreFile.getAbsolutePath()), "secret1".toCharArray());
    Key key = keyStore.getKey("myKey", "secret2".toCharArray());