Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create symmetric encryption key with Google Tink?

I have a key (say) "thisist0psecret" that I want to use as a symmetric encryption/decryption key with the Google Tink library. I am baffled that I am unable to do this simple thing. I can generate new keys (using various templates AES128_GCM, etc.), serialize them and then read them back with KeysetReader. But, for the life of me, I cannot figure out how to create a symmetric key with the specific key bytes that I specify.

I am able to do the following, for example, with Tink:

KeysetHandle ksh = KeysetHandle.generateNew(AeadKeyTemplates.AES128_GCM);
Aead aead = AeadFactory.getPrimitive(ksh);
String pt = "hello, world!";
byte[] encbytes = aead.encrypt(pt.getBytes(), null);
byte[] decbytes = aead.decrypt(encbytes, null);
String orig = new String(decbytes);
assert(pt.equals(orig));

But I want to set the symmetric key string to be a set of bytes that I specify such as "thisist0psecret" and then encrypt this key with the public key of the user who will do the decryption.

Any Google Tink experts here that can shed some light?

like image 666
Will P. Avatar asked Sep 04 '18 17:09

Will P.


1 Answers

I'm the lead developer for Tink.

If your key is randomly generated, you can use the subtle API directly, see: https://github.com/google/tink/blob/master/java_src/src/main/java/com/google/crypto/tink/subtle/AesGcmJce.java.

This is not recommended because the subtle layer might change without notice (thought it's been relatively stable in the history of Tink).

If your key is a password you want to derive a key from it using something like Scrypt or PBKDF2. We haven't yet support native password-based encryption in Tink, please file a feature request and we'll see how we can help.

like image 110
Thai Duong Avatar answered Oct 25 '22 06:10

Thai Duong