CREATE FUNCTION dbo.EncryptPass(@EnPass VARCHAR(32))
RETURNS VARBINARY(MAX)
AS
BEGIN
DECLARE @Result VARBINARY(MAX)
SET @Result=EncryptByPassPhrase('HelloWorld',@EnPass)
RETURN @Result
END
1.3、解密函数
CREATE FUNCTION dbo.DecryptPass(@DePass VARBINARY(MAX))
RETURNS VARCHAR(32)
AS
BEGIN
DECLARE @Result VARCHAR(32)
SET @Result=CONVERT(VARCHAR,DecryptByPassPhrase('HelloWorld',@DePass))
RETURN @Result
END
1.4、结果测试
SELECT dbo.EncryptPass('测试字符串') AS RESULT
SELECT dbo.DecryptPass(0x0100000045FE8873D07BFB747D33D81D1C1E06CA3EC10D718CF603E395D4493BFBFC5683) AS RESULT
SELECT dbo.DecryptPass(dbo.EncryptPass('测试字符串')) AS RESULT
CREATE CERTIFICATE TestCert
ENCRYPTION BY PASSWORD='HelloWorld'
WITH SUBJECT='Hello World', START_DATE='2021-01-20', EXPIRY_DATE='2021-12-31'
2.3、加密函数
CREATE FUNCTION dbo.EncryptCert(@EnPass VARCHAR(32))
RETURNS VARBINARY(MAX)
AS
BEGIN
DECLARE @Result VARBINARY(MAX)
SET @Result=EncryptByCert(Cert_ID('TestCert'),@EnPass)
RETURN @Result
END
2.4、解密函数
CREATE FUNCTION dbo.DecryptCert(@DePass VARBINARY(MAX))
RETURNS VARCHAR(32)
AS
BEGIN
DECLARE @Result VARCHAR(32)
SET @Result=CONVERT(VARCHAR,DECRYPTBYCERT(Cert_ID('TestCert'),@DePass,N'HelloWorld'))
RETURN @Result
END
2.5、结果测试
SELECT dbo.EncryptCert('测试字符串') AS RESULT
SELECT dbo.DecryptCert(0x836021834D8D25E4DAD0CD26767A77A7FFF9CE9BDCA7BDE72E418118DB58DFB8AF6325B7A9D1D6C2D5D43800120A5C5D212BC4A645D10A065A147C5E97B419DAF6DC203598E376AE4A520B45D145F2C2A6BAE23B49AD3EA45BDAAC1FBD78E8402195DDA0842CA5A89942D03C5D7B82F7E8241D8BD698115C78CEBD5AC0066C6C) AS RESULT
SELECT dbo.DecryptCert(dbo.EncryptCert('测试字符串')) AS RESULT