Here is my code
KeysetHandle handle = KeysetHandle.generateNew(KeyTemplates.get("AES256_GCM"));
Aead aead = handle.getPrimitive(Aead.class);
How can I get the byte /string representation of the key tha KeysetHandle uses here?
Here is my code
KeysetHandle handle = KeysetHandle.generateNew(KeyTemplates.get("AES256_GCM"));
Aead aead = handle.getPrimitive(Aead.class);
How can I get the byte /string representation of the key tha KeysetHandle uses here?
Copyright © 2021 Jogjafile Inc.
My answer is a little bit tricky and "hacky" but it works in my cases.
Google Tink tries to avoid any direct contact to the key data so there is no direct access to the data but with a little trick using a combination of "ByteArrayOutputStream" and "ObjectOutputStream" you are been able to retrieve the data as JSON string or byte array.
Kindly note: my source code does not handle any exceptions and the usage of strings as holder of keys is discouraged.
This is the function to archive the data from a keysetHandle:
Just call the method with your generated keysetHandle like:
As the data is a JSON byte array (or string) you need to find the field "value" - here is a sample keysetHandle in JSON encoding:
Just for convenience - here is a "rough" function that extracts the key value - beware that strings are inmutable when using key material:
Call this function like this and you retrieve the generated AES GCM 256 key in Base64 encoding:
The result will be (again sample data):