DECRYPTBYPASSPHRASE (Transact-SQL)

适用于:SQL ServerAzure SQL 数据库Azure SQL 托管实例

此函数可对最初使用密码加密的数据进行解密。

Transact-SQL 语法约定

语法

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

注意

若要查看 SQL Server 2014 (12.x) 及更早版本的 Transact-SQL 语法,请参阅早期版本文档

参数

passphrase
用于生成解密密钥的密码。

@passphrase
类型为 char、nchar、nvarchar 或 varchar 的变量,其中包含用于生成解密密钥的密码 。

'ciphertext'
使用密钥加密的数据字符串。 ciphertext 具有 varbinary 数据类型。

@ciphertext
varbinary 类型的变量,包含使用密钥加密的数据。 @ciphertext 变量的最大大小为 8,000 字节。

add_authenticator
指示原始加密过程是否包含验证器和纯文本以及是否对其进行加密。 如果加密过程使用验证器,则 add_authenticator 具有 1 值。 add_authenticator 具有 int 数据类型。

@add_authenticator
变量,指示原始加密过程是否包含验证器和纯文本以及是否对其进行加密。 如果加密过程使用验证器,则 @add_authenticator 具有值 1。 @add_authenticator 具有 int 数据类型。

authenticator
用作验证器生成基础的数据。 authenticator 具有 sysname 数据类型。

@authenticator
包含用作验证器生成基础的数据的变量。 @authenticator 具有 sysname 数据类型。

返回类型

varbinary(最大大小为 8,000 个字节)。

备注

DECRYPTBYPASSPHRASE 不需要执行权限。 如果 DECRYPTBYPASSPHRASE 收到错误的密码或错误的验证器信息,则返回 NULL。

DECRYPTBYPASSPHRASE 使用密码生成解密密钥。 此解密密钥不会保留。

如果在 ciphertext 加密时包含验证器,DECRYPTBYPASSPHRASE 必须接收该解密过程的同一验证器。 如果解密过程中提供的验证器值与最初用于加密数据的验证器值不匹配,则 DECRYPTBYPASSPHRASE 操作将失败。

示例

此示例解密 EncryptByPassPhrase 中更新的记录。

USE AdventureWorks2022;  
-- Get the passphrase from the user.  
DECLARE @PassphraseEnteredByUser NVARCHAR(128);  
SET @PassphraseEnteredByUser   
= 'A little learning is a dangerous thing!';  
  
-- Decrypt the encrypted record.  
SELECT CardNumber, CardNumber_EncryptedbyPassphrase   
    AS 'Encrypted card number', CONVERT(varchar,  
    DecryptByPassphrase(@PassphraseEnteredByUser, CardNumber_EncryptedbyPassphrase, 1   
    , CONVERT(varbinary, CreditCardID)))  
    AS 'Decrypted card number' FROM Sales.CreditCard   
    WHERE CreditCardID = '3681';  
GO  

另请参阅

选择加密算法
ENCRYPTBYPASSPHRASE (Transact-SQL)