KMIP Encrypt and Decrypt Sample
The following example demonstrates KMIP Encrypt and Decrypt operations using GCM algorithm.
Open a valid KMIP session.
session = KMIPSession.getSession(new NAEClientCertificate(certName, certAlias.toCharArray()));
Take a instance for GCM.
KMIPCipher cipher = KMIPCipher.getInstance("AES/GCM/NoPadding");
Create a spec for GCM.
KMIPGCMSpec spec = new KMIPGCMSpec(tagLength, iv.getBytes());
Initialize KMIP Cipher with the given key name/UID, spec and session in encrypt mode.
cipher.init(KMIPCipher.ENCRYPT_MODE, keyName, spec, session); OR #!bash cipher.init(KMIPCipher.ENCRYPT_MODE, KMIPIvSpec spec, KMIPSession session, String uid);
Perform cipher operation and return the result in
KMIPCryptoResult
object.KMIPCryptoResult result = cipher.doFinal(data.getBytes());
Encrypted result is returned in hex format.
System.out.println(IngrianProvider.byteArray2Hex(result.getData()));
Take the GCM cipher instance for decryption.
KMIPCipher deCipher = KMIPCipher.getInstance("AES/GCM/NoPadding");
Initialize KMIP Cipher with the given key name, spec and session in decrypt mode.
deCipher.init(KMIPCipher.DECRYPT_MODE, keyName, spec, session);
Decrypted result is returned.
KMIPCryptoResult decResult = deCipher.doFinal(result.getData());
Print the decrypted result.
System.out.println(new String(decResult.getData()));