How to create symmetric encryption key with Google Tink?

白昼怎懂夜的黑 提交于 2020-01-13 04:28:28

问题


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?


回答1:


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/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.



来源:https://stackoverflow.com/questions/52171198/how-to-create-symmetric-encryption-key-with-google-tink

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!