sql 加密解密函数

if object_ID ( 'fn_ACITEncryption' is not null 
    drop function fn_ACITEncryption 
go 
 
create function fn_ACITEncryption 
    @Str nvarchar ( 4000), -- 加密的字符串 
    @Flag bit = 1, --1 、加密 0 、解密 
    @Key nvarchar ( 50) -- 密文 
returns nvarchar ( 4000) -- 這里可轉換成二進制 
with Encryption 
as 
begin 
Declare @LenStr int , @i int , @Str2 nvarchar ( 4000), @Split nvarchar (2), @LenKey int 
select @Str= @Str+ 'A' , @LenStr= len ( @Str), @i= 1, @Str2= '' , @LenKey= Len ( @Key'A' )- 1 
while @i< @LenStr 
        select     @Split= substring ( @Str, @i, 1), 
                @Split= nchar (( unicode ( @Split)+ case @Flag    when then unicode ( substring ( @Key'A' , @i% @LenKey+ 1, 1))- 1                                                
                                                    when then 65535- unicode ( substring ( @Key'A' , @i% @LenKey+ 1, 1)) 
                                                    else end )% 65535+ cast ( @Flag as int )), 
                 @Str2= @Str2+ @Split, @i= @i+ 1 
return @Str2 
end 
go 
 
select dbo. fn_ACITEncryption( N'Roy' , 1, '123' as 加密后字符串 
 
 
select dbo. fn_ACITEncryption( N'Roy', 0, '123' as 解密后字符串
posted @ 2013-09-29 19:40  子萧  阅读(790)  评论(0编辑  收藏  举报