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 
有帮助吗?

解决方案

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.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top