sql 簡單加密函式

洪岐發表於2011-12-10

用到了之前的數字轉換為16進位制數函式 IntToHex

 

create function [dbo].[IntToHex](@num int)
returns varchar(100)
as
begin
declare @re varchar(100)
set @re=''
while @num>0
select @re=substring('0123456789ABCDEF',@num%16+1,1)+@re
,@num=@num/16
return(@re)
end

 
create function [dbo].[GetUserCode](@word varchar(256))
 RETURNS VARCHAR(512)ASBEGIN
DECLARE @key varchar(256)--金鑰
SET @key='drsf45fwq'
DECLARE @return varchar(256)--返回值
DECLARE @i int,@j int,@temp int
SELECT
 @i=1,@j=1,@return=''
WHILE(@i<=len(@word))
BEGIN
 SET
 @return=@return+dbo.IntToHex(ascii(substring(@word,@i,1))+ascii(substring(@key,@j,1))%256)
 --SELECT @temp,@i,@j
 IF(@j<len(@key)) SET
 @j=@j+1
 ELSE SET @j=1
 SET @i=@i+1
END
RETURN @returnEND

相關文章