SQL Flow Field Guide

ENCRYPTBYKEY

Catalog support for SQL Server 2022 (16.x) and 2025 (17.x)

cryptographic transform or key lookup

What changes

Input
plaintext or ciphertext and a key, certificate or passphrase
Output or change
ciphertext, plaintext, hash, signature check, or key metadata
Return type
varbinary with a maximum size of 8,000 bytes. Returns NULL if the key is not open, if the key does not exist, or if the key is a deprecated RC4 key and the database is not in compatibility level 110 o

Syntax at a glance

EncryptByKey ( key_GUID , { 'cleartext' | @cleartext } [, { add_authenticator | @add_authenticator } , { authenticator | @authenticator } ] )

Work example

Encrypt a value with an opened symmetric key in an existing database design.

Use synthetic data in an isolated database. Follow the existing key hierarchy and keep keys and plaintext out of output.

Check your work Check key state and authenticator rules, and keep credentials and personal data out of a lab.

Open Microsoft Learn for full syntax, permissions, and details for your version.