DECRYPTBYCERT (Transact-SQL)
SQL Server 2008
Decrypts data with the private key of a certificate.
The following example selects rows from [AdventureWorks].[ProtectedData04] that are marked as data encrypted by certificate JanainaCert02. The example decrypts the ciphertext with the private key of certificate JanainaCert02, which it first decrypts with the password of the certificate, pGFD4bb925DGvbd2439587y. The decrypted data is converted from varbinary to nvarchar.
SELECT convert(nvarchar(max), DecryptByCert(Cert_Id('JanainaCert02'),
ProtectedData, N'pGFD4bb925DGvbd2439587y'))
FROM [AdventureWorks].[ProtectedData04]
WHERE Description
= N'data encrypted by certificate '' JanainaCert02''';
GO
