Back to Tink

Java hybrid encryption example

java_src/examples/hybrid/README.md

1.7.01.1 KB
Original Source

Java hybrid encryption example

This example shows how to encrypt data with Tink using hybrid encryption.

It demonstrates the basic steps of using Tink, namely loading key material, obtaining a primitive, and using the primitive to do crypto.

The key material was generated with Tinkey:

shell
tinkey create-keyset \
    --key-template DHKEM_X25519_HKDF_SHA256_HKDF_SHA256_AES_256_GCM \
    --out-format JSON --out hybrid_test_private_keyset.json

tinkey create-public-keyset --in hybrid_test_private_keyset.json \
    --in-format JSON --out-format JSON --out hybrid_test_public_keyset.json

Build and run

Bazel

shell
git clone https://github.com/google/tink
cd tink/java_src/examples
bazel build hybrid_example

Encrypt a file:

shell
echo "some data" > testdata.txt
../bazel-bin/hybrid/hybrid_example encrypt \
    ../hybrid/hybrid_test_public_keyset.json \
    testdata.txt testdata.txt.encrypted

Decrypt a file:

shell
../bazel-bin/hybrid/hybrid_example decrypt \
    ../hybrid/hybrid_test_private_keyset.json \
    testdata.txt.encrypted testdata.txt.decrypted

diff testdata.txt testdata.txt.decrypted