转载

[易飞]密码加密解密函数


 加密函数:

CREATE FUNCTION EncodePwd
( 
@user nvarchar(32), 
@userPwd nvarchar(32)
)
RETURNS nvarchar(128)
AS
BEGIN
DECLARE @fnil nvarchar(128),@FF1 NVARCHAR(128)
SET @fnil = '''(&.&!''%&{1}quot;''&)" ",&)$(%#$-$#$" '
SET @FF1 = ' !"#$%&''()*+,-./'
DECLARE @n int, @i int, @d int
DECLARE @Result nvarchar(100)
SET @i = LEN(@user)
SET @Result = SUBSTRING(@fnil,(@i-1)*2+1, 30-(@i-1)*2)
SET @n = @i-1
WHILE(@n>=0)
BEGIN SET @i = (ASCII(SUBSTRING(@user,@n+1,1)) - 32) % 16 
SET @d = (ASCII(SUBSTRING(@user,@n+1,1)) - 32) / 16 + 1 
SET @Result = @Result + CHAR(32 + @d + 1) + SUBSTRING(@FF1,@i+1,1) 
SET @n = @n-1
END
DECLARE @n1 int , @n2 int
DECLARE @FStr1 NCHAR(1), @FStr2 NCHAR(1), @FStr3 NCHAR(1), @Fchar1 NCHAR(1)
SET @i =1
WHILE(@i<=LEN(@userPwd))
BEGIN IF(@i<=4) 
BEGIN 
SET @FStr1 = SUBSTRING(@userPwd,@i,1) 
SET @FStr2 = SUBSTRING(@Result,@i,1) 
SET @FStr3 = SUBSTRING(@Result,28+@i,1) 
SET @n1 = ((ASCII(SUBSTRING(@userPwd,@i,1)) - 32) % 8) 
SET @n2 = ((ASCII(SUBSTRING(@userPwd,@i,1)) - 32) / 16) 
SET @Fchar1 = CHAR(@n2 * 16 + 32) 
SET @n2 = ASCII(@FStr2) ^ ASCII(@FStr1) 
SET @n2 = (@n2 & 0x0F) + 0x20 
SET @FStr2 =CHAR(@n2) 
SET @FStr3 = CHAR(ASCII(@Fchar1) + ((ASCII(@FStr3) + ASCII(@Fchar1)) % 16)) 
SET @Result = SUBSTRING(@Result,1, @i-1) + @FStr2 + Substring(@Result,@i + 1, 31 - @i+1) 
SET @Result = Substring(@Result,1, 28 + @i-1) + @FStr3 + Substring(@Result,29 + @i, 3 - @i+1) 
END 
ELSE 
BEGIN 
SET @FStr1 = SUBSTRING(@userPwd,@i,1) 
SET @FStr2 = SUBSTRING(@Result,@i,1) 
SET @FStr3 = SUBSTRING(@Result,@i-4,1) 
SET @n1 = ((ASCII(SUBSTRING(@userPwd,@i,1)) - 32) % 16) 
SET @n2 = ((ASCII(SUBSTRING(@userPwd,@i,1)) - 32) / 16) 
SET @Fchar1 = CHAR(@n2 * 16 + 32) 
SET @n2 = ASCII(@FStr2) ^ ASCII(@FStr1) 
SET @n2 = (@n2 & 0x0F) + 0x20 
SET @FStr2 =CHAR(@n2) 
SET @FStr3 = CHAR(ASCII(@Fchar1) + ((ASCII(@FStr3) + ASCII(@Fchar1)) % 16)) 
SET @Result = SUBSTRING(@Result,1, @i-1) + @FStr2 + Substring(@Result,@i + 1, 31 - @i+1) 
SET @Result = Substring(@Result,1, @i-4-1) + @FStr3 + Substring(@Result,@i-3, 35 - @i+1) 
END 
SET @i = @i+1 END
RETURN @Result
END
GO 

解密函数:

ALTER FUNCTION [dbo].[DecodePwd]
( 
	@user nvarchar(32), 
	@userPwd nvarchar(128)
)
RETURNS nvarchar(128)
AS
BEGIN
DECLARE @Return NVARCHAR(100)
SET @Return =''DECLARE @KeyLen INT,@i INT,@n1 INT,@n2 INT
DECLARE @FStr1 CHAR(1),@FStr2 CHAR(1),@FStr3 CHAR(1),@FStr4 CHAR(1)DECLARE @fnil nvarchar(128),@FF1 NVARCHAR(128)
SET @fnil = '''(&.&!''%&{1}quot;''&)" ",&)$(%#$-$#$" 'SET @FF1 = ' !"#$%&''()*+,-./'DECLARE @n int, @d int
DECLARE @Result nvarchar(100)
SET @i = LEN(@user)
SET @Result = SUBSTRING(@fnil,(@i-1)*2+1, 30-(@i-1)*2)
SET @n = @i-1WHILE(@n>=0)
BEGIN 
SET @i = (ASCII(SUBSTRING(@user,@n+1,1)) - 32) % 16 
SET @d = (ASCII(SUBSTRING(@user,@n+1,1)) - 32) / 16 + 1 
SET @Result = @Result + CHAR(32 + @d + 1) + SUBSTRING(@FF1,@i+1,1) 
SET @n = @n-1
END
SET @KeyLen =0
SET @i =28
WHILE(@i>=1)
BEGIN 
IF(SUBSTRING(@userPwd,@i,1)<>SUBSTRING(@Result,@i,1)) 
BEGIN 
SET @KeyLen = @i BREAK; 
END 
SET @i = @i-1
END
IF(@KeyLen =0) RETURN ''
SET @i = @KeyLen
WHILE(@i>=1)
BEGIN IF(@i<=4) 
BEGIN 
SET @FStr1 = SUBSTRING(@Result,@i,1) 
SET @FStr3 = SUBSTRING(@userPwd,@i,1) 
SET @FStr4 = SUBSTRING(@userPwd,32-4+@i,1) 
SET @n1 = ASCII(@FStr1) ^ ASCII(@FStr3) 
SET @n2 = ((ASCII(@FStr4)-32) / 16) 
SET @Return = CHAR((16*@n2)+32+@n1) + @Return 
SET @FStr4 = CHAR((ASCII(@FStr4) % 16)+32) 
SET @userPwd = SUBSTRING(@userPwd,1,32-4+@i-1)+@FStr4+SUBSTRING(@userPwd,32-4+@i+1,4-@i) 
END 
ELSE 
BEGIN 
SET @FStr1 = SUBSTRING(@Result,@i,1) 
SET @FStr3 = SUBSTRING(@userPwd,@i,1) 
SET @FStr4 = SUBSTRING(@userPwd,@i-4,1) 
SET @n1 = ASCII(@FStr1) ^ ASCII(@FStr3) 
SET @n2 = ((ASCII(@FStr4)-32) / 16) 
SET @Return = CHAR((16*@n2)+32+@n1) + @Return 
SET @FStr4 = CHAR((ASCII(@FStr4) % 16)+32) 
SET @userPwd = SUBSTRING(@userPwd,1,@i-4-1)+@FStr4+SUBSTRING(@userPwd,@i-4+1,32-@i+4)
 END SET @i = @i-1
END
RETURN @Return 
END 

文章最后发布于: 2011-06-02 09:15:00
展开阅读全文
0 个人打赏
私信求帮助

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 鲸 设计师: meimeiellie

分享到微信朋友圈

×

扫一扫,手机浏览