· 6 years ago · Mar 13, 2019, 03:20 PM
1// Selects decryption mechanism. I've selected AES, CBC(Code Block Chaning) with Padding.
2Mechanism decryptionMechanism = Mechanism.get(PKCS11Constants.CKM_AES_CBC_PAD);
3// For some mechanisms there are parameters required to configure before performing the operation.
4InitializationVectorParameters encryptionIV = new InitializationVectorParameters(new byte[16]);
5// Set the parameters of the mechanism.
6decryptionMechanism.setParameters(encryptionIV);
7// Instantiate decryption. Mechanism and secret key is required.
8session.decryptInit(decryptionMechanism, secretKey);
9// Byte array of data to decrypt. Here, previously encrypted data is taken.
10byte[] dataToBeDecrypted = encryptedData
11// Returns a byte array of encrypted data.
12byte[] decryptedData = session.decrypt(dataToBeDecrypted);
13// Print the encrypted text.
14System.out.pintln("Decrypted text : " + new String(decryptedData);