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 the Yandex.Cloud SDK

Encrypting data using the Yandex.Cloud SDK

  • Adding dependencies
  • Authentication
    • Authentication using the service account linked to the Yandex.Cloud VM
    • Authentication using any service account
    • Authentication using a Yandex account
  • Encrypting and decrypting data

You can use Key Management Servicewith the Yandex.Cloud SDK. The SDK is available for Java, Go, Python, and Node.js.

The Yandex.Cloud SDK is most convenient for encrypting small amounts of data (the limit on the size of plaintext is 32 KB). To encrypt larger amounts of data, we recommend using the AWS Encryption SDK or Google Tink. They encrypt data using envelope encryption.

Adding dependencies

Before you start, you need to add dependencies.

Java
Go

Add dependencies using Apache Maven:

<dependency>
    <groupId>com.yandex.cloud</groupId>
    <artifactId>sdk</artifactId>
    <version>1.2.1</version>
</dependency>

Install the SDK:

go get github.com/yandex-cloud/go-sdk

Authentication

You can authenticate using:

  • The service account linked to the Yandex.Cloud VM.
  • Any service account.
  • A Yandex account.

Authentication using the service account linked to the Yandex.Cloud VM

Java
Go

Authenticate using the service account linked to the VM:

Credentials credentials = Auth.fromMetadata();  

Authenticate using the service account linked to the VM:

credentials := ycsdk.InstanceServiceAccount()

Authentication using any service account

The key.json file must contain an authorized key for the service account. For information about how to create authorized keys, see Creating authorized keys.

Java
Go

Authenticate using any service account:

Credentials credentials = Auth.fromFile(Auth::apiKey, Paths.get("key.json"));

Authenticate using any service account:

authorizedKey, err := iamkey.ReadFromJSONFile("key.json")
if err != nil {...}
credentials, err := ycsdk.ServiceAccountKey(authorizedKey)
if err != nil {...}

Authentication using a Yandex account

The token variable is your OAuth token.

Java
Go

Authenticate using a Yandex account:

Credentials credentials = Auth.oauthToken(token);  

Authenticate using a Yandex account:

credentials := ycsdk.OAuthToken(token)

Encrypting and decrypting data

Use the encrypt and decrypt methods to encrypt and decrypt data. The code uses the following variables:

  • keyId: ID of the KMS key.
  • plaintext: Unencrypted text (no more than 32 KB).
  • ciphertext: Encrypted text.
  • aad: AAD context.
Java
Go
ServiceFactoryConfig config = ServiceFactoryConfig.builder()
    .credentials(credentials)
    .build();
SymmetricCryptoServiceBlockingStub symmetricCryptoService = new ServiceFactory(config).create(
    SymmetricCryptoServiceBlockingStub.class,
    SymmetricCryptoServiceGrpc::newBlockingStub
);
...

byte[] ciphertext = symmetricCryptoService.encrypt(SymmetricEncryptRequest.newBuilder()
    .setKeyId(keyId)
    .setPlaintext(ByteString.copyFrom(plaintext))
    .setAadContext(ByteString.copyFrom(aad))
    .build()
).getCiphertext().toByteArray();

...

byte[] plaintext = symmetricCryptoService.decrypt(SymmetricDecryptRequest.newBuilder()
    .setKeyId(keyId)
    .setCiphertext(ByteString.copyFrom(ciphertext))
    .setAadContext(ByteString.copyFrom(aad))
    .build()
).getPlaintext().toByteArray();
sdk, err := ycsdk.Build(context, ycsdk.Config{
  Credentials: credentials,
})
if err != nil {...}

...

response, err := sdk.KMSCrypto().SymmetricCrypto().Encrypt(context, &kms.SymmetricEncryptRequest{
  KeyId:      keyId,
  Plaintext:  plaintext,
  AadContext: aad,
})
if err != nil {...}
ciphertext := response.Ciphertext

...

response, err := sdk.KMSCrypto().SymmetricCrypto().Decrypt(context, &kms.SymmetricDecryptRequest{
  KeyId:      keyId,
  Ciphertext: ciphertext,
  AadContext: aad,
})
if err != nil {...}
plaintext := response.Plaintext
  • Yandex.Cloud Java SDK.
  • Examples of how to use KMS with the Java SDK.
  • Yandex.Cloud Go SDK.
In this article:
  • Adding dependencies
  • Authentication
  • Authentication using the service account linked to the Yandex.Cloud VM
  • Authentication using any service account
  • Authentication using a Yandex account
  • Encrypting and decrypting data
Language
Careers
Privacy policy
Terms of use
© 2021 Yandex.Cloud LLC