DecryptByKey (Transact-SQL)

対称キーを使用してデータを暗号化解除します。

トピック リンク アイコンTransact-SQL 構文表記規則

構文

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

引数

  • ciphertext
    キーで暗号化されたデータを指定します。 varbinary.
  • @ciphertext
    キーで暗号化されたデータを含む varbinary 型の変数を指定します。
  • add_authenticator
    認証子がプレーン テキストと共に暗号化されているかどうかを示します。この値は、データの暗号化時に EncryptByKey へ渡された値と同じである必要があります。データ型は int です。
  • authenticator
    認証子を生成する基のデータを指定します。EncryptByKey へ渡された値と一致する必要があります。データ型は sysname です。
  • @authenticator
    認証子の生成元のデータを含む変数を指定します。EncryptByKey へ渡された値と一致する必要があります。

戻り値の型

varbinary 型 (最大サイズは 8,000 バイト)

解説

DecryptByKey では対称キーが使用されます。この対称キーはデータベースで開かれている必要があります。複数のキーを同時に開いておくことができます。暗号化テキストの暗号化解除をする直前にキーを開く必要はありません。

対称キーの暗号化と暗号化解除は比較的高速なので、データが大きい場合に適しています。

A. 対称キーを使用して暗号化解除する

次の例では、対称キーを使用して暗号化テキストを暗号化解除します。

-- 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

B. 対称キーと認証ハッシュを使用して暗号化解除する

次の例では、認証子と共に暗号化されたデータを暗号化解除します。

-- 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

参照

関連項目

EncryptByKey (Transact-SQL)
CREATE SYMMETRIC KEY (Transact-SQL)
ALTER SYMMETRIC KEY (Transact-SQL)
DROP SYMMETRIC KEY (Transact-SQL)

その他の技術情報

暗号化階層
暗号化アルゴリズムの選択

ヘルプおよび情報

SQL Server 2005 の参考資料の入手