SQL Encrypt/Decrypt 个数字

SQL Encrypt/Decrypt numbers

我想知道这在 TSQL

中是否可行

我知道 vigenere(密码)加密可以将 "Hello, How are you" 这样的值加密成 "Hfnlp, Jox crf aov" 但是问题是它保持数字未加密 "1234567" 将保持 "1234567"

知道如何加密包括数字在内的所有内容并将其作为 SQL 服务器中的 Function/Procedure 吗?

我自己构建了脚本

加密

ALTER FUNCTION [dbo].[fnEncrypt] 
(
    @Str varchar(max),
    @key varchar(max)
)
RETURNS varchar(max)
AS
BEGIN
--Akram Mustafa
declare @i int = 1
declare @x int = 0
declare @Result varchar(max) = ''
declare @List varchar(100) = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
declare @TempKey varchar(max) = @key

WHILE LEN(@TempKey) < LEN(@Str)
BEGIN
    SET @TempKey = @TempKey + @key 
END

SET @key = @TempKey

WHILE (@i <= LEN(@Str)) 
BEGIN
    IF (CHARINDEX(SUBSTRING(@Str, @i, 1), @List) > 0)
    BEGIN
        SET @x = ((CHARINDEX(SUBSTRING(@Str, @i, 1), @List) +  CHARINDEX(SUBSTRING(@key, @i, 1), @List)) % LEN(@List)) + 1
        SET @Result = @Result + SUBSTRING(@List, @x, 1)
    END
    ELSE
    BEGIN
        SET @Result = @Result + SUBSTRING(@Str, @i, 1)
    END
    SET @i = @i + 1
END

RETURN @Result
END

解密

ALTER FUNCTION [dbo].[fnDecrypt] 
(
    @Str varchar(max),
    @key varchar(max)
)
RETURNS varchar(max)
AS
BEGIN
--Akram Mustafa
declare @i int = 1
declare @x int = 0
declare @Result varchar(max) = ''
declare @List varchar(100) = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
declare @TempKey varchar(max) = @key

WHILE LEN(@TempKey) < LEN(@Str)
BEGIN
    SET @TempKey = @TempKey + @key 
END

SET @key = @TempKey

WHILE (@i <= LEN(@Str)) 
BEGIN
    IF (CHARINDEX(SUBSTRING(@Str, @i, 1), @List) > 0)
    BEGIN
        SET @x = ((CHARINDEX(SUBSTRING(@Str, @i, 1), @List) -  CHARINDEX(SUBSTRING(@key, @i, 1), @List) + LEN(@List) - 1) % LEN(@List))
        IF @x = 0
        BEGIN
            SET @x = LEN(@List)
        END
        SET @Result = @Result + SUBSTRING(@List, @x, 1)
    END
    ELSE
    BEGIN
        SET @Result = @Result + SUBSTRING(@Str, @i, 1)
    END
    SET @i = @i + 1
END

RETURN @Result
END