DECRYPTBYKEY(Transact-SQL)

대칭 키를 사용하여 데이터를 해독합니다.

항목 링크 아이콘Transact-SQL 구문 표기 규칙

구문

DecryptByKey ( { 'ciphertext' | @ciphertext } 
    [ , add_authenticator, { authenticator | @authenticator } ] )

인수

  • ciphertext
    키로 암호화된 데이터입니다. ciphertext는 varbinary입니다.

  • @ciphertext
    키로 암호화된 데이터를 포함하는 varbinary 형식 변수입니다.

  • add_authenticator
    인증자가 일반 텍스트와 함께 암호화되었는지 여부를 나타냅니다. 데이터 암호화 시 EncryptByKey로 전달된 값과 같아야 합니다. add_authenticator는 int입니다.

  • authenticator
    인증자가 생성될 데이터입니다. EncryptByKey에 제공된 값과 일치해야 합니다. authenticator는 sysname입니다.

  • @authenticator
    인증자가 생성될 데이터를 포함하는 변수입니다. EncryptByKey에 제공된 값과 일치해야 합니다.

반환 형식

최대 크기가 8,000바이트인 varbinary

주의

DecryptByKey는 대칭 키를 사용합니다. 이 대칭 키는 데이터베이스에서 이미 열려 있어야 합니다. 동시에 여러 개의 키를 열어 둘 수 있습니다. ciphertext를 해독하기 직전에 키를 열 필요는 없습니다.

대칭 암호화 및 암호 해독은 비교적 속도가 빠르며 대량의 데이터 작업 시 적합합니다.

사용 권한

현재 세션에서 대칭 키가 열려 있어야 합니다. 자세한 내용은 OPEN SYMMETRIC KEY(Transact-SQL)를 참조하십시오.

1. 대칭 키를 사용한 해독

다음 예에서는 대칭 키를 사용하여 ciphertext를 해독합니다.

-- First, open the symmetric key with which to decrypt the data.
OPEN SYMMETRIC KEY SSN_Key_01
   DECRYPTION BY CERTIFICATE HumanResources037;
GO

-- Now list the original ID, the encrypted ID, and the 
-- decrypted ciphertext. If the decryption worked, the original
-- and the decrypted ID will match.
SELECT NationalIDNumber, EncryptedNationalID 
    AS 'Encrypted ID Number',
    CONVERT(nvarchar, DecryptByKey(EncryptedNationalID)) 
    AS 'Decrypted ID Number'
    FROM HumanResources.Employee;
GO

2. 대칭 키 및 인증 해시를 사용한 해독

다음 예에서는 인증자를 사용하여 암호화된 데이터를 해독합니다.

-- First, open the symmetric key with which to decrypt the data
OPEN SYMMETRIC KEY CreditCards_Key11
   DECRYPTION BY CERTIFICATE Sales09;
GO

-- Now list the original card number, the encrypted card number,
-- and the decrypted ciphertext. If the decryption worked, 
-- the original number will match the decrypted number.
SELECT CardNumber, CardNumber_Encrypted 
    AS 'Encrypted card number', CONVERT(nvarchar,
    DecryptByKey(CardNumber_Encrypted, 1 , 
    HashBytes('SHA1', CONVERT(varbinary, CreditCardID)))) 
    AS 'Decrypted card number' FROM Sales.CreditCard;
GO