我想知道这在TSQL中是否可能
我知道vigenere(密码)加密可以对“你好,你好吗” 之类的值进行加密,成为“ Hfnlp,Jox crf aov” ,但是问题在于它保留未加密的数字“ 1234567” 将保持为“ 1234567”
有什么主意如何加密包括数字在内的所有内容并将其作为SQL Server中的功能/过程使用?
答案 0 :(得分:0)
我自己构建了脚本
加密
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