Until now, I was using jasypt to encrypt a string before storing it on disk on app closing, and later when opening the app for decrypt the string after retrieving it from disk.
It was super easy with jasypt, this was the code:
private static final String JASYPT_PWD = "mypassword";
public static String encryptString(String string) {
StrongTextEncryptor textEncryptor = new StrongTextEncryptor();
textEncryptor.setPassword(JASYPT_PWD);
return textEncryptor.encrypt(string);
}
public static String decryptString(String string) {
StrongTextEncryptor textEncryptor = new StrongTextEncryptor();
textEncryptor.setPassword(JASYPT_PWD);
return textEncryptor.decrypt(string);
}
It worked perfectly, but now, jasypt is deprecated and I'm trying to migrate to the Google Tink library, the problem is that Google Tink seems to be much more complex for just encrypt and decrypt a string as easily as with jasypt.
I can't find in the Tink repo readme the simple way to encrypt and decrypt a string, just can find more complex operations which in fact I can't understand because my knowledge in encryption is totally empty. Because of that I was using a very easy library like jasypt.
This is the Tink repo: https://github.com/Google/tink
Is there an easy way, similar to my jasypt code, to encrypt and decrypt a string with Tink?
Note: The post refers to Tink version 1.2.2. The posted code is partially incompatible with later versions.
The
StrongTextEncryptor-class in yourjasypt-example-code uses thePBEWithMD5AndTripleDES-algorithm. This algorithm uses the symmetric-key block cipherTriple DESand derives the key from the password using theMD5hash function. The latter is called password-based encryption and this isn't supported inTink(at least as at 08/2018), see How to create symmetric encryption key with Google Tink?. Thus, it's impossible inTinkto encrypt by means of a password and the concept used so far in thejasypt-code couldn't be implemented. If password-based encryption is to be used in any case that is a deal-breaker forTink.Another approach is to directly use a key.
Tinkhas theAesGcmJce-class which usesAES-GCMfor encryption. Here the key must have a length of either 128 Bit or 256 bit:The use is simple and furthermore the used cipher (
AES-GCM) is secure. However, theTink-developers themselves don't recommend this approach because theAesGcmJce-class belongs to thecom.google.crypto.tink.subtle-package which may change at any time without further notice, (see also here, section Important Warnings). Therefore, also this approach isn't optimal.Well, how does
Tinktypically use symmetric encryption? This is shown in the following snippet from:The
generateNew-method generates a new key. However, the creation isn't based on a password or a byte-sequence and because of this, a key generated for the encryption can't be easily reconstructed for decryption. Therefore, the key used for encryption has to be persisted to a storage system, e.g. the file system, so it can be used later for decryption.Tinkallows the storing of cleartext keys (which is of course not recommended). A more secure approach is the encryption of keys with master keys stored in a remote key management system (this is in more detail explainedTink's JAVA-HOWTO, sections Storing Keysets and Loading Existing Keysets).Tink's key management concept (with the aim of avoiding accidental leakage of sensitive key material) makes it also somehow cumbersome (this may change in later versions). That's why I said in my comment that I'm not sure ifTinkfits your ideas concerning simplicity.