Yandex Cloud
Search
Contact UsGet started
  • Pricing
  • Customer Stories
  • Documentation
  • Blog
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • AI for business
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Start testing with double trial credits
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Center for Technologies and Society
    • Yandex Cloud Partner program
  • Pricing
  • Customer Stories
  • Documentation
  • Blog
© 2025 Direct Cursus Technology L.L.C.
Tutorials
    • All tutorials
    • Differentiation of access permissions for user groups
    • Inviting a new user and assigning roles
    • Creating an L7 load balancer with a Smart Web Security profile through an Application Load Balancer ingress controller
    • Centralized online publication and app protection against DDoS attacks
    • Basic SWS setup
    • Emergency DDoS protection in Application Load Balancer, L7
    • Delivering logs from a VM instance to Cloud Logging
    • Writing load balancer logs to PostgreSQL
    • Secure storage of GitLab CI passwords as Yandex Lockbox secrets
    • Service account with an OS Login profile for VM management via Ansible
    • Transferring logs from Container Optimized Image to Cloud Logging
    • Adding an HTML page to work with SmartCaptcha
    • Configuring alerts and dashboards in Monitoring
    • Uploading audit logs to MaxPatrol SIEM
    • Uploading audit logs to Splunk SIEM
    • Uploading audit logs to ArcSight SIEM
      • 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
    • Server-side encryption for an Object Storage bucket
    • Encrypting secrets in Hashicorp Terraform
    • Managing KMS keys with Hashicorp Terraform
    • Auto Unseal in Hashicorp Vault
    • Transferring Yandex MPP Analytics for PostgreSQL cluster logs to Yandex Cloud Logging

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
  • Data encryption and decryption
  1. Security
  2. Data encryption
  3. Encrypting data using the Yandex Cloud SDK

Encrypting data using the Yandex Cloud SDK

Written by
Yandex Cloud
Updated at November 5, 2025
  • Adding dependencies
  • Authentication
    • Authentication using the service account linked to the Yandex Cloud VM
    • Authentication using any service account
    • Authentication using a Yandex account
  • Data encryption and decryption

You can use Yandex Key Management Service with 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 dependenciesAdding dependencies

Before you start, you need to add dependencies.

Java
Go

Add dependencies using Apache Maven:

<dependency>
    <groupId>com.yandex.cloud</groupId>
    <artifactId>java-sdk-services</artifactId>
    <version>2.4.2</version>
</dependency>

Install the SDK:

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

AuthenticationAuthentication

You can authenticate using:

  • Service account linked to the Yandex Cloud VM.
  • Any service account.
  • Yandex account.

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

Java
Go

Get authenticated using the service account linked to the VM:

CredentialProvider credentialProvider = Auth.computeEngineBuilder().build();

Authenticate using the service account linked to the VM:

credentials := ycsdk.InstanceServiceAccount()

Authentication using any service accountAuthentication using any service account

key.json must contain the authorized service account key. For information on how to create an authorized key, see Creating an authorized key.

Java
Go

Authenticate using any service account:

CredentialProvider credentialProvider = Auth.apiKeyBuilder().fromFile(Paths.get("key.json")).build();

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 accountAuthentication using a Yandex account

The token variable is your OAuth token.

Java
Go

Authenticate using a Yandex account:

CredentialProvider credentialProvider = Auth.oauthTokenBuilder().build();

Authenticate using a Yandex account:

credentials := ycsdk.OAuthToken(token)

Data encryption and decryptionData encryption and decryption

Note

Changes caused by eventually consistent operations require up to three hours to become encryptable and decryptable.

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

  • endpoint: api.cloud.yandex.net:443.
  • keyId: ID of the KMS key.
  • plaintext: Plain text (no more than 32 KB).
  • ciphertext: Ciphertext.
  • aad: AAD context.
Java
Go
SymmetricCryptoServiceBlockingStub symmetricCryptoService = ServiceFactory.builder()
    .endpoint(endpoint)
    .credentialProvider(credentialProvider)
    .build()
    .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{
  Endpoint:    endpoint,
  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

See alsoSee also

  • Yandex Cloud Java SDK.
  • Examples of how to use KMS with the Java SDK.
  • Yandex Cloud Go SDK.

Was the article helpful?

Previous
Encrypting data using the Yandex Cloud CLI and API
Next
Encrypting data using the AWS Encryption SDK
© 2025 Direct Cursus Technology L.L.C.