Question

This class encrypt data values, to do this first I get the cipher as follows,

KeyStore primaryKeyStore = getKeyStore(keyStoreFile, password, keyType, provider);
java.security.cert.Certificate certs = primaryKeyStore.getCertificate(aliasName);
cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, certs);

But the last line of the above code gives the following error. What does the wrong key usage means? Can't I use this certificate for encryption purposes? Is there separate certificate for encryption?

Caused by: java.security.InvalidKeyException: Wrong key usage 
at javax.crypto.Cipher.init(Cipher.java:1640) 
at javax.crypto.Cipher.init(Cipher.java:1549) 
at my.test.ciphertool.CipherTool.initCipher(CipherTool.java:167) 
... 1 more 
Was it helpful?

Solution

It means exactly what it says it means in the Javadoc:

If the certificate is of type X.509 and has a key usage extension field marked as critical, and the value of the key usage extension field implies that the public key in the certificate and its corresponding private key are not supposed to be used for the operation represented by the value of opmode, an InvalidKeyException is thrown.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top