22,207
社区成员
发帖
与我相关
我的任务
分享
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;
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
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