Name Date Size #Lines LOC

..--

BUILD.bazelH A D25-Apr-20251.6 KiB4339

EncryptedKeysetExample.javaH A D25-Apr-20254.9 KiB12164

README.mdH A D25-Apr-20251.7 KiB6749

encrypted_keyset_example_test.shH A D25-Apr-20252.9 KiB9848

README.md

1# Java encrypted keysets example
2
3This example shows how to generate or load an encrypted keyset, obtain a
4primitive, and use the primitive to do crypto.
5
6## Build and run
7
8### Prequisite
9
10This example uses a Cloud KMS key as a key-encryption key (KEK) to
11encrypt/decrypt a keyset, which in turn is used to encrypt files.
12
13In order to run this example, you need to:
14
15*   Create a symmetric key on Cloud KMs. Copy the key URI which is in this
16    format:
17    `projects/<my-project>/locations/global/keyRings/<my-key-ring>/cryptoKeys/<my-key>`.
18
19*   Create and download a service account that is allowed to encrypt and decrypt
20    with the above key.
21
22### Bazel
23
24```shell
25git clone https://github.com/google/tink
26cd tink/examples/java_src
27bazel build ...
28```
29
30Generate an encrypted keyset:
31
32```shell
33# Replace `<my-key-uri>` in `gcp-kms://<my-key-uri>` with your key URI, and
34# my-service-account.json with your service account's credential JSON file.
35./bazel-bin/encryptedkeyset/encrypted_keyset_example \
36    generate \
37    aes128_gcm_test_encrypted_keyset.json \
38    gcp-kms://<my-key-uri> \
39    my-service-account.json
40```
41
42Encrypt a file:
43
44```shell
45echo "some data" > testdata.txt
46
47./bazel-bin/encryptedkeyset/encrypted_keyset_example \
48    encrypt \
49    aes128_gcm_test_encrypted_keyset.json \
50    gcp-kms://<my-key-uri> \
51    my-service-account.json \
52    testdata.txt testdata.txt.encrypted
53```
54
55Decrypt a file:
56
57```shell
58./bazel-bin/encryptedkeyset/encrypted_keyset_example \
59    decrypt \
60    aes128_gcm_test_encrypted_keyset.json \
61    gcp-kms://<my-key-uri> \
62    my-service-account.json \
63    testdata.txt.encrypted testdata.txt.decrypted
64
65diff testdata.txt testdata.txt.decrypted
66```
67