Yandex.Cloud
  • Services
  • Why Yandex.Cloud
  • Pricing
  • Documentation
  • Contact us
Get started
Yandex Key Management Service
  • Getting started
  • Step-by-step instructions
    • All instructions
    • Keys
    • Key version
    • Data encryption
  • Concepts
    • Overview
    • Keys
    • Key version
    • Encryption
    • Envelope encryption
    • Key consistency
    • Quotas and limits
  • Use cases
    • All use cases
    • Data encryption
      • Which encryption method should I choose?
      • Encrypting data using the Yandex.Cloud CLI and API
      • Encrypting data using the Yandex.Cloud SDK
      • Encrypting data using the AWS Encryption SDK
      • Encrypting data using Google Tink
    • Encrypting secrets in Managed Service for Kubernetes
    • KMS key management with Hashicorp Terraform
    • Encrypting secrets in Hashicorp Terraform
  • Access management
  • Pricing policy
  • API reference
    • Authentication in the API
    • gRPC
      • Overview
      • SymmetricCryptoService
      • SymmetricKeyService
      • OperationService
    • REST
      • Overview
      • SymmetricCrypto
        • Overview
        • decrypt
        • encrypt
        • generateDataKey
        • reEncrypt
      • SymmetricKey
        • Overview
        • cancelVersionDestruction
        • create
        • delete
        • get
        • list
        • listAccessBindings
        • listOperations
        • listVersions
        • rotate
        • scheduleVersionDestruction
        • setAccessBindings
        • setPrimaryVersion
        • update
        • updateAccessBindings
  • Questions and answers
  1. Use cases
  2. Data encryption
  3. Encrypting data using Google Tink

Encrypting data using Google Tink

  • Adding dependencies
  • Encryption and decryption

Tink is Google's cryptographic library, an alternative to AWS Encryption. The library helps you focus on encrypting and decrypting data without the need to choose the correct encryption algorithm and parameters.

It supports Java and Go Tink client versions, which provide encryption and decryption of data using Yandex.Cloud KMS keys. Data is encrypted using envelope encryption (the size of plaintext is not limited).

Adding dependencies

Before you start, you need to add dependencies.

Java
Go

Add dependencies using Apache Maven:

<dependency>
    <groupId>com.yandex.cloud</groupId>
    <artifactId>kms-provider-tink</artifactId>
    <version>1.0</version>
</dependency>

Run the command:

go get github.com/yandex-cloud/kms-clients-go/yckmstink

Encryption and decryption

The code uses the following variables:

  • credentials: Determines the authentication method (for more information, see Authentication in the Yandex.Cloud SDK).
  • keyId: ID of the key in KMS.
  • plaintext: Unencrypted text.
  • ciphertext: Encrypted text.
  • aad: AAD context.
Java
Go

Create an Aead object and use the encrypt and decrypt methods for data encryption and decryption:

AeadConfig.register(); 
KmsClients.add(new YcKmsClient(() -> credentials));

String keyUri = "yc-kms://" + keyId;
Aead kmsAead = KmsClients.get(keyUri).getAead(keyUri);
Aead aead = new KmsEnvelopeAead(AeadKeyTemplates.AES256_GCM, kmsAead);

...

byte[] ciphertext = aead.encrypt(plaintext, aad);

...

byte[] plaintext = aead.decrypt(ciphertext, aad);

Create an Aead object and use the encrypt and decrypt methods for data encryption and decryption:

sdk, err := ycsdk.Build(context, ycsdk.Config{
  Credentials: credentials,
})
if err != nil {...}

kmsAead := yckmstink.NewYCAEAD(keyId, sdk)
aead := aead.NewKMSEnvelopeAEAD(*aead.AES256GCMKeyTemplate(), kmsAead)

...

ciphertext, err := aead.Encrypt(plaintext, aad)
if err != nil {...}

...

plaintext, err := aead.Decrypt(ciphertext, aad)
if err != nil {...}

See also

  • Google Tink.
  • Java client for Tink.
  • Examples of using the Java client for Tink.
  • Go client for Tink.
  • Examples of using the Go client for Tink.
In this article:
  • Adding dependencies
  • Encryption and decryption
Language
Careers
Privacy policy
Terms of use
© 2021 Yandex.Cloud LLC