ENCRYPTBYPASSPHRASE (Transact-SQL)
Encrypt data with a passphrase.
The following example updates a record in the SalesCreditCard table and encrypts the value of the credit card number stored in column CardNumber_EncryptedbyPassphrase, using the primary key as an authenticator.
USE AdventureWorks;
GO
-- Create a column in which to store the encrypted data.
ALTER TABLE Sales.CreditCard
ADD CardNumber_EncryptedbyPassphrase varbinary(256);
GO
-- First get the passphrase from the user.
DECLARE @PassphraseEnteredByUser nvarchar(128);
SET @PassphraseEnteredByUser
= 'A little learning is a dangerous thing!';
-- Update the record for the user's credit card.
-- In this case, the record is number 3681.
UPDATE Sales.CreditCard
SET CardNumber_EncryptedbyPassphrase = EncryptByPassPhrase(@PassphraseEnteredByUser
, CardNumber, 1, CONVERT( varbinary, CreditCardID))
WHERE CreditCardID = '3681';
GO
Although the maximum size of cleartext (or @cleartext as the case may be) is 8,000 bytes, the the true maximum size is slightly less. The true maximum size is lowered by the header information prepended to the encrypted to the encrypted ciphertext by SQL Server, as well as by the padding added by the encryption algorithm. The true maximum size of the cleartext is 7,975 bytes if no authenticator is used, or 7,955 bytes if an authenticator is used. If your cleartext exceeds this maximum size this function will return NULL.
The return value indicates a maximum return value size of 8,000 bytes, but it cannot exceed 7,996 bytes in practice.
- 4/4/2009
- Mike C_1
- 4/26/2009
- Thomas Lee
