Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mattparksjr/62dafaf4736808f6386d4c07e136a2d1 to your computer and use it in GitHub Desktop.
Save mattparksjr/62dafaf4736808f6386d4c07e136a2d1 to your computer and use it in GitHub Desktop.
Error :)
package codes.matthewp.todofx.data.security;
import com.google.crypto.tink.*;
import com.google.crypto.tink.aead.AeadConfig;
import com.google.crypto.tink.aead.AeadKeyTemplates;
import com.google.crypto.tink.config.TinkConfig;
import com.google.crypto.tink.proto.KeyTemplate;
import java.io.File;
import java.io.IOException;
import java.security.GeneralSecurityException;
/**
* Note: Major security flaw. If someone is able to access the computer running the server, they can read the keyset easily. To combat this, add a key from some KMS place.
* https://github.com/google/tink/blob/master/docs/JAVA-HOWTO.md#storing-keysets
*/
public class EncryptCore {
private File keySetFile;
private KeysetHandle keysetHandle;
public EncryptCore() throws GeneralSecurityException, IOException {
AeadConfig.register();
keySetFile = new File("keyset.json");
if (keySetFile.exists()) {
keysetHandle = CleartextKeysetHandle.read(
JsonKeysetReader.withFile(keySetFile));
} else {
KeyTemplate keyTemplate = AeadKeyTemplates.AES128_GCM;
keysetHandle = KeysetHandle.generateNew(keyTemplate);
CleartextKeysetHandle.write(keysetHandle, JsonKeysetWriter.withFile(
keySetFile));
}
}
public String encrypt(String string) {
// 2. Get the primitive, error getPrimitive does not exist. Maybe ask?
Aead aead = keysetHandle.getPrimitive(Aead.class);
// 3. Use the primitive to encrypt a plaintext,
byte[] ciphertext = aead.encrypt(plaintext, aad);
// ... or to decrypt a ciphertext.
byte[] decrypted = aead.decrypt(ciphertext, aad);
return "";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment