ENCRYPTBYPASSPHRASE (Transact-SQL)

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

通过使用 TRIPLE DES 算法以及 128 密钥位长度的通行短语对数据加密。

注意

SQL Server 2017 及更高版本使用 AES256 密钥通过通行短语加密数据。

Transact-SQL 语法约定

语法

EncryptByPassPhrase ( { 'passphrase' | @passphrase }   
    , { 'cleartext' | @cleartext }  
  [ , { add_authenticator | @add_authenticator }  
    , { authenticator | @authenticator } ] )  

注意

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

参数

passphrase
用于生成对称密钥的通行短语。

@passphrase
类型为 nvarchar、char、varchar、binary、varbinary 或 nchar 的变量,其中包含用于生成对称密钥的通行短语

cleartext
要加密的明文。

@cleartext
类型为 nvarchar、char、varchar、binary、varbinary 或 nchar 的变量,其中包含明文。 最大大小为 8000 个字节。

add_authenticator
指示是否将验证器与明文一起加密。 如果将添加验证器,则为 1。 int。

@add_authenticator
指示是否将哈希与明文一起加密。

authenticator
用于派生验证器的数据。 sysname。

@authenticator
包含验证器所源自的数据的变量。

返回类型

varbinary(最大大小为 8000 个字节)

注解

通行短语是包含空格的密码。 使用通行短语的优点在于,与相对较长的字符串相比,有含义的短语或句子更容易记忆。

此函数不检查密码复杂性。

示例

以下示例将更新 SalesCreditCard 表中的记录,并使用主键作为验证器来加密存储在 CardNumber_EncryptedbyPassphrase 列中的信用卡号的值。

USE AdventureWorks2022;  
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  

另请参阅

DECRYPTBYPASSPHRASE (Transact-SQL)
加密层次结构