Name Date Size #Lines LOC

..--

BUILD.bazelH A D25-Apr-20251.9 KiB6256

README.mdH A D25-Apr-20251.9 KiB6246

encrypted_keyset.pyH A D25-Apr-20254.2 KiB13082

encrypted_keyset_test.shH A D25-Apr-20255.5 KiB181106

README.md

1# Python example: working with encrypted keysets
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 service account that is allowed to encrypt and decrypt with the above
20    key and download a JSON credentials file.
21
22### Bazel
23
24```shell
25$ git clone https://github.com/google/tink
26$ cd tink/python/examples
27$ bazel build ...
28```
29
30You can generate 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/encrypted_keyset/encrypted_keyset --mode generate \
36    --keyset_path aes128_gcm_test_encrypted_keyset.json \
37    --kek_uri gcp-kms://<my-key-uri> \
38    --gcp_credential_path my-service-account.json
39```
40
41You can then encrypt a file:
42
43```shell
44$ echo "some data" > testdata.txt
45$ ./bazel-bin/encrypted_keyset/encrypted_keyset --mode encrypt \
46    --keyset_path aes128_gcm_test_encrypted_keyset.json \
47    --kek_uri gcp-kms://<my-key-uri> \
48    --gcp_credential_path my-service-account.json \
49    --input_path testdata.txt --output_path testdata.txt.encrypted
50```
51
52Or decrypt the file with:
53
54```shell
55$ ./bazel-bin/encrypted_keyset/encrypted_keyset --mode decrypt \
56    --keyset_path aes128_gcm_test_encrypted_keyset.json \
57    --kek_uri gcp-kms://<my-key-uri> \
58    --gcp_credential_path my-service-account.json \
59    --input_path testdata.txt.encrypted --output_path testdata.txt.decrypted
60$ diff testdata.txt testdata.txt.decrypted
61```
62