Google Tink is a tool which provides an End to End solution for Encryption/Decryption.
Steps:
Step #1: Create the Encryption key.
Goto https://developers.google.com/tink/install-tinkey & unzip to a folder
The Encryption key can be generated in Binary or JSON format.
tinkey.bat create-keyset --key-template AES256_GCM --out keyset.bin --out-format binary
in case you want to generate in json format you can use below command
tinkey.bat create-keyset --key-template AES256_GCM --out keyset.json
private static Aead aead =null;
static {
try {
AeadConfig.register();
KeysetHandle keysetHandle = CleartextKeysetHandle.read(BinaryKeysetReader.withFile(new File("<path to binary file>")));
//use below in case reading from json file
// KeysetHandle keysetHandle = CleartextKeysetHandle.read(JsonKeysetReader.withBytes(Hex.decode("<path to json file>")));
aead = AeadFactory.getPrimitive(keysetHandle);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static String encryptData(String data) throws GeneralSecurityException {
return Hex.encode(aead.encrypt(data.getBytes(),null));
}
public static String decryptData(String data) throws GeneralSecurityException {
return new String(aead.decrypt(Hex.decode(data),null));
}
You can pass associated key also for encrypt & decrypt
Further Reading:
https://www.baeldung.com/google-tink
https://developers.google.com/tink
https://woodpecker-ci.org/docs/1.0/administration/encryption
https://fuchsia.googlesource.com/third_party/tink/+/HEAD/docs/TINKEY.md