xref: /aosp_15_r20/external/tink/java_src/examples/hybrid/HybridExample.java (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 /**
2  * Copyright 2021 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 // [START hybrid-example]
15 package hybrid;
16 
17 import static java.nio.charset.StandardCharsets.UTF_8;
18 
19 import com.google.crypto.tink.HybridDecrypt;
20 import com.google.crypto.tink.HybridEncrypt;
21 import com.google.crypto.tink.InsecureSecretKeyAccess;
22 import com.google.crypto.tink.KeysetHandle;
23 import com.google.crypto.tink.TinkJsonProtoKeysetFormat;
24 import com.google.crypto.tink.hybrid.HybridConfig;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.nio.file.Paths;
28 
29 /**
30  * A command-line utility for hybrid encryption.
31  *
32  * <p>It loads cleartext keys from disk - this is not recommended!
33  *
34  * <p>It requires the following arguments:
35  *
36  * <ul>
37  *   <li>mode: either 'encrypt' or 'decrypt'.
38  *   <li>key-file: Read the key material from this file.
39  *   <li>input-file: Read the input from this file.
40  *   <li>output-file: Write the result to this file.
41  *   <li>[optional] contex-info: Bind the encryption to this context info.
42  */
43 public final class HybridExample {
main(String[] args)44   public static void main(String[] args) throws Exception {
45     if (args.length != 4 && args.length != 5) {
46       System.err.printf("Expected 4 or 5 parameters, got %d\n", args.length);
47       System.err.println(
48           "Usage: java HybridExample encrypt/decrypt key-file input-file output-file context-info");
49       System.exit(1);
50     }
51 
52     String mode = args[0];
53     if (!mode.equals("encrypt") && !mode.equals("decrypt")) {
54       System.err.println("Incorrect mode. Please select encrypt or decrypt.");
55       System.exit(1);
56     }
57     Path keyFile = Paths.get(args[1]);
58     Path inputFile = Paths.get(args[2]);
59     byte[] input = Files.readAllBytes(inputFile);
60     Path outputFile = Paths.get(args[3]);
61     byte[] contextInfo = new byte[0];
62     if (args.length == 5) {
63       contextInfo = args[4].getBytes(UTF_8);
64     }
65 
66     // Register all hybrid encryption key types with the Tink runtime.
67     HybridConfig.register();
68 
69     // Read the keyset into a KeysetHandle.
70     KeysetHandle handle =
71         TinkJsonProtoKeysetFormat.parseKeyset(
72             new String(Files.readAllBytes(keyFile), UTF_8), InsecureSecretKeyAccess.get());
73 
74     if (mode.equals("encrypt")) {
75       // Get the primitive.
76       HybridEncrypt encryptor = handle.getPrimitive(HybridEncrypt.class);
77 
78       // Use the primitive to encrypt data.
79       byte[] ciphertext = encryptor.encrypt(input, contextInfo);
80       Files.write(outputFile, ciphertext);
81     } else {
82       HybridDecrypt decryptor = handle.getPrimitive(HybridDecrypt.class);
83 
84       // Use the primitive to decrypt data.
85       byte[] plaintext = decryptor.decrypt(input, contextInfo);
86       Files.write(outputFile, plaintext);
87     }
88   }
89 
HybridExample()90   private HybridExample() {}
91 }
92 // [END hybrid-example]
93