Java
Chilkat2 Java
Example: Crypt2.RandomizeIV method
Demonstrates using a random initialization vector for AES GCM encryption.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Crypt2;
public class ChilkatExample {
public static void main(String[] args) {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Crypt2 crypt = new Crypt2();
crypt.setCryptAlgorithm("aes");
crypt.setCipherMode("gcm");
crypt.setKeyLength(256);
String K = "000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F";
String AAD = "feedfacedeadbeeffeedfacedeadbeefabaddad2";
String PT = "This is the text to be AES-GCM encrypted.";
// Generate a random IV.
crypt.randomizeIV();
String IV = crypt.getEncodedIV("hex");
crypt.setEncodedKey(K, "hex");
crypt.setEncodedAad(AAD, "hex");
// Return the encrypted bytes as base64
crypt.setEncodingMode("base64");
crypt.setCharset("utf-8");
String cipherText;
try {
cipherText = crypt.encryptStringENC(PT);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Get the GCM authenticated tag computed when encrypting.
String authTag = crypt.getEncodedAuthTag("base64");
System.out.println("Cipher Text: " + cipherText);
System.out.println("Auth Tag: " + authTag);
// Let's send the IV, CipherText, and AuthTag to the decrypting party.
// We'll send them concatenated like this: [IV || Ciphertext || AuthTag]
// In base64 format.
BinData bdEncrypted = new BinData();
bdEncrypted.appendEncoded(IV, "hex");
bdEncrypted.appendEncoded(cipherText, "base64");
bdEncrypted.appendEncoded(authTag, "base64");
String concatenatedGcmOutput = bdEncrypted.getEncoded("base64");
System.out.println("Concatenated GCM Output: " + concatenatedGcmOutput);
// Sample output so far:
// -------------------------------------------------------------------------------------
// Now let's GCM decrypt...
// -------------------------------------------------------------------------------------
Crypt2 decrypt = new Crypt2();
// The values shared and agreed upon by both sides beforehand are: algorithm, cipher mode, secret key, and AAD.
// Sometimes the IV can be a value already known and agreed upon, but in this case the encryptor sends the IV to the decryptor.
decrypt.setCryptAlgorithm("aes");
decrypt.setCipherMode("gcm");
decrypt.setKeyLength(256);
decrypt.setEncodedKey(K, "hex");
decrypt.setEncodedAad(AAD, "hex");
BinData bdFromEncryptor = new BinData();
bdFromEncryptor.appendEncoded(concatenatedGcmOutput, "base64");
int sz = bdFromEncryptor.getNumBytes();
// Extract the parts.
String extractedIV = bdFromEncryptor.getEncodedChunk(0, 16, "hex");
String extractedCipherText = bdFromEncryptor.getEncodedChunk(16, sz - 32, "base64");
String expectedAuthTag = bdFromEncryptor.getEncodedChunk(sz - 16, 16, "base64");
// Before GCM decrypting, we must set the authenticated tag to the value that is expected.
// The decryption will fail if the resulting authenticated tag is not equal to the expected result.
decrypt.setEncodedAuthTag(expectedAuthTag, "base64");
// Also set the IV.
decrypt.setEncodedIV(extractedIV, "hex");
// Decrypt..
decrypt.setEncodingMode("base64");
decrypt.setCharset("utf-8");
String decryptedText;
try {
decryptedText = decrypt.decryptStringENC(extractedCipherText);
} catch (ChilkatException e) {
// Failed. The resultant authenticated tag did not equal the expected authentication tag.
System.out.println(e.getLastErrorText());
return;
}
System.out.println("Decrypted: " + decryptedText);
}
}