sqlserver解密函数,,,,

twvampire123 2015-03-17 11:53:21

const XorKey:array[0..7] of Byte=($B2,$09,$AA,$55,$93,$6D,$84,$47);
function Dec(Str:String):String;//字符解密函数vari,j:Integer;beginResult:='';j:=0;for i:=1 to Length(Str) div 2 dobeginResult:=Result+Char(StrToInt('$'+Copy(Str,i*2-1,2)) xor XorKey[j]);j:=(j+1) mod 8;end;end;

转sqlserver,,,,
...全文
274 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
oraclecaicai 2015-03-17
  • 打赏
  • 举报
回复
兄弟,这个是配合上次那个Enc一起使用的吧。。。那得自建一个十六进制字符转化十进制数字的映射表。供参考:

CREATE TABLE dbo.XorKeys
(
    id     tinyint
,   xorkey binary(1)
)

INSERT INTO XorKeys VALUES ( 0, 0xB2 )
INSERT INTO XorKeys VALUES ( 1, 0x09 )
INSERT INTO XorKeys VALUES ( 2, 0xBB )
INSERT INTO XorKeys VALUES ( 3, 0x55 )
INSERT INTO XorKeys VALUES ( 4, 0x93 )
INSERT INTO XorKeys VALUES ( 5, 0x6D )
INSERT INTO XorKeys VALUES ( 6, 0x44 )
INSERT INTO XorKeys VALUES ( 7, 0x47 )

CREATE TABLE dbo.HexToNum
(
    hex char(2)
,   num int
)

DECLARE @digit TABLE
(
    hex char(1)
,   num int
)

INSERT INTO @digit VALUES( '0',  0 )
INSERT INTO @digit VALUES( '1',  1 )
INSERT INTO @digit VALUES( '2',  2 )
INSERT INTO @digit VALUES( '3',  3 )
INSERT INTO @digit VALUES( '4',  4 )
INSERT INTO @digit VALUES( '5',  5 )
INSERT INTO @digit VALUES( '6',  6 )
INSERT INTO @digit VALUES( '7',  7 )
INSERT INTO @digit VALUES( '8',  8 )
INSERT INTO @digit VALUES( '9',  9 )
INSERT INTO @digit VALUES( 'A', 10 )
INSERT INTO @digit VALUES( 'B', 11 )
INSERT INTO @digit VALUES( 'C', 12 )
INSERT INTO @digit VALUES( 'D', 13 )
INSERT INTO @digit VALUES( 'E', 14 )
INSERT INTO @digit VALUES( 'F', 15 )

INSERT INTO dbo.HexToNum
(
    hex
,   num
)
SELECT
       h.hex + l.hex
,      h.num * 16 + l.num
FROM @digit h, @digit l

CREATE FUNCTION dbo.Dec
(
    @Str varchar(255)
)
RETURNS varchar(255)
AS
BEGIN
    
    DECLARE @XorKeys TABLE
    (
        id     tinyint
    ,   xorkey binary(1)
    )
    
    INSERT INTO @XorKeys SELECT id, xorkey FROM XorKeys
    
    DECLARE @i      int          = 1
    ,       @j      int          = 0
    ,       @hex    char(2)
    ,       @num    int
    ,       @xorkey binary(1)
    ,       @Result varchar(255) = ''
    
    WHILE @i <= LEN(@Str) / 2
    BEGIN
        SELECT @hex    = SUBSTRING(@Str, @i * 2 - 1, 2)
        ,      @xorkey = ( SELECT xorkey FROM @XorKeys WHERE id = @j )
        
        SELECT @num = ( SELECT num FROM HexToNum WHERE hex = UPPER(@hex) )
        
        SELECT @Result = @Result + CHAR(@num ^ @xorkey)
        
        SELECT @i = @i + 1
        ,      @j = (@j + 1) % 8
    END
    
    RETURN @Result
    
END
还在加载中灬 2015-03-17
  • 打赏
  • 举报
回复
我觉得,你只需要一个16进制字符串转整型的函数和一个整型转16进制字符串的函数即可 有了这两个,你就可以利用整型的异或了
CREATE  function dbo.HexToInt(@hexstr varchar(10))
returns bigint
as
begin
	if left(@hexstr,2) in ('0x','0X') set @hexstr=substring(@hexstr,3,10)
	declare @i int, @res bigint, @l int, @c char, @ascii0 int, @asciiF int
	select @i=1, @l=len(@hexstr), @res=0, @ascii0=ascii('0'), @asciiF=ascii('F')
	if @hexstr is null OR @l=0 return null
	while @i<=@l begin
		set @c=upper(substring(@hexstr,@i,1))
		if not ascii(@c) between @ascii0 and @asciiF return(null)
		set @res=@res+cast(1.0 as bigint)*case when isnumeric(@c)=1 then 
		cast(@c as int) else ascii(@c)-55 end*power(16,@l-@i) 
		set @i=@i+1
	end
	return(@res)
END

Create Function dbo.IntToHex(@IntNum int)
returns varchar(16)
as
begin
  declare @Mods int,@res varchar(16)
  set @res=''
  while @IntNum <> 0 
  begin
    set @Mods =@IntNum % 16
    if @Mods > 9 
      set @res = Char(Ascii('A')+@Mods-10)+@res
    else 
      set @res = Cast(@Mods as varchar(4)) + @res
    set @IntNum = @IntNum/16
  end
  return @res
end
twvampire123 2015-03-17
  • 打赏
  • 举报
回复
只写 Result:=Result+Char(StrToInt('$'+Copy(Str,i*2-1,2)) xor XorKey[j]); 这句,,,

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧