|
| 1 | +package lars.spielplatz.tink; |
| 2 | + |
| 3 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 4 | + |
| 5 | +import com.google.crypto.tink.Aead; |
| 6 | +import com.google.crypto.tink.InsecureSecretKeyAccess; |
| 7 | +import com.google.crypto.tink.KeysetHandle; |
| 8 | +import com.google.crypto.tink.RegistryConfiguration; |
| 9 | +import com.google.crypto.tink.TinkJsonProtoKeysetFormat; |
| 10 | +import com.google.crypto.tink.aead.AeadConfig; |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.nio.file.Path; |
| 13 | +import java.nio.file.Paths; |
| 14 | + |
| 15 | +/** |
| 16 | + * A command-line utility for encrypting small files with AEAD. |
| 17 | + * |
| 18 | + * <p>It loads cleartext keys from disk - this is not recommended! |
| 19 | + * |
| 20 | + * <p>It requires the following arguments: |
| 21 | + * |
| 22 | + * <ul> |
| 23 | + * <li>mode: Can be "encrypt" or "decrypt" to encrypt/decrypt the input to the output. |
| 24 | + * <li>key-file: Read the key material from this file. |
| 25 | + * <li>input-file: Read the input from this file. |
| 26 | + * <li>output-file: Write the result to this file. |
| 27 | + * <li>[optional] associated-data: Associated data used for the encryption or decryption. |
| 28 | + */ |
| 29 | +public final class AeadExample { |
| 30 | + private static final String MODE_ENCRYPT = "encrypt"; |
| 31 | + private static final String MODE_DECRYPT = "decrypt"; |
| 32 | + |
| 33 | + public static void main(String[] args) throws Exception { |
| 34 | + if (args.length != 4 && args.length != 5) { |
| 35 | + System.err.printf("Expected 4 or 5 parameters, got %d\n", args.length); |
| 36 | + System.err.println( |
| 37 | + "Usage: java AeadExample encrypt/decrypt key-file input-file output-file" |
| 38 | + + " [associated-data]"); |
| 39 | + System.exit(1); |
| 40 | + } |
| 41 | + String mode = args[0]; |
| 42 | + Path keyFile = Paths.get(args[1]); |
| 43 | + Path inputFile = Paths.get(args[2]); |
| 44 | + Path outputFile = Paths.get(args[3]); |
| 45 | + byte[] associatedData = new byte[0]; |
| 46 | + if (args.length == 5) { |
| 47 | + associatedData = args[4].getBytes(UTF_8); |
| 48 | + } |
| 49 | + // Register all AEAD key types with the Tink runtime. |
| 50 | + AeadConfig.register(); |
| 51 | + |
| 52 | + // Read the keyset into a KeysetHandle. |
| 53 | + KeysetHandle handle = |
| 54 | + TinkJsonProtoKeysetFormat.parseKeyset( |
| 55 | + new String(Files.readAllBytes(keyFile), UTF_8), InsecureSecretKeyAccess.get()); |
| 56 | + |
| 57 | + // Get the primitive. |
| 58 | + Aead aead = handle.getPrimitive(RegistryConfiguration.get(), Aead.class); |
| 59 | + |
| 60 | + // Use the primitive to encrypt/decrypt files. |
| 61 | + if (MODE_ENCRYPT.equals(mode)) { |
| 62 | + byte[] plaintext = Files.readAllBytes(inputFile); |
| 63 | + byte[] ciphertext = aead.encrypt(plaintext, associatedData); |
| 64 | + Files.write(outputFile, ciphertext); |
| 65 | + } else if (MODE_DECRYPT.equals(mode)) { |
| 66 | + byte[] ciphertext = Files.readAllBytes(inputFile); |
| 67 | + byte[] plaintext = aead.decrypt(ciphertext, associatedData); |
| 68 | + Files.write(outputFile, plaintext); |
| 69 | + } else { |
| 70 | + System.err.println("The first argument must be either encrypt or decrypt, got: " + mode); |
| 71 | + System.exit(1); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private AeadExample() {} |
| 76 | +} |
0 commit comments