/**
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
// [START deterministic-aead-example]
package deterministicaead;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.crypto.tink.DeterministicAead;
import com.google.crypto.tink.InsecureSecretKeyAccess;
import com.google.crypto.tink.KeysetHandle;
import com.google.crypto.tink.TinkJsonProtoKeysetFormat;
import com.google.crypto.tink.daead.DeterministicAeadConfig;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* A command-line utility for encrypting small files with Deterministic AEAD.
*
*
It loads cleartext keys from disk - this is not recommended!
*
*
It requires the following arguments:
*
*
* - mode: Can be "encrypt" or "decrypt" to encrypt/decrypt the input to the output.
*
- key-file: Read the key material from this file.
*
- input-file: Read the input from this file.
*
- output-file: Write the result to this file.
*
- [optional] associated-data: Associated data used for the encryption or decryption.
*/
public final class DeterministicAeadExample {
private static final String MODE_ENCRYPT = "encrypt";
private static final String MODE_DECRYPT = "decrypt";
public static void main(String[] args) throws Exception {
if (args.length != 4 && args.length != 5) {
System.err.printf("Expected 4 or 5 parameters, got %d\n", args.length);
System.err.println(
"Usage: java DeterministicAeadExample encrypt/decrypt key-file input-file output-file"
+ " [associated-data]");
System.exit(1);
}
String mode = args[0];
Path keyFile = Paths.get(args[1]);
Path inputFile = Paths.get(args[2]);
Path outputFile = Paths.get(args[3]);
byte[] associatedData = new byte[0];
if (args.length == 5) {
associatedData = args[4].getBytes(UTF_8);
}
// Initialise Tink: register all Deterministic AEAD key types with the Tink runtime
DeterministicAeadConfig.register();
// Read the keyset into a KeysetHandle
KeysetHandle handle =
TinkJsonProtoKeysetFormat.parseKeyset(
new String(Files.readAllBytes(keyFile), UTF_8), InsecureSecretKeyAccess.get());
// Get the primitive
DeterministicAead daead = handle.getPrimitive(DeterministicAead.class);
// Use the primitive to encrypt/decrypt files.
if (MODE_ENCRYPT.equals(mode)) {
byte[] plaintext = Files.readAllBytes(inputFile);
byte[] ciphertext = daead.encryptDeterministically(plaintext, associatedData);
Files.write(outputFile, ciphertext);
} else if (MODE_DECRYPT.equals(mode)) {
byte[] ciphertext = Files.readAllBytes(inputFile);
byte[] plaintext = daead.decryptDeterministically(ciphertext, associatedData);
Files.write(outputFile, plaintext);
} else {
System.err.println("The first argument must be either encrypt or decrypt, got: " + mode);
System.exit(1);
}
System.exit(0);
}
private DeterministicAeadExample() {}
}
// [END deterministic-aead-example]