Chinaunix首页 | 论坛 | 博客
  • 博客访问: 611907
  • 博文数量: 825
  • 博客积分: 5000
  • 博客等级: 大校
  • 技术积分: 4980
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-27 14:19
文章分类

全部博文(825)

文章存档

2011年(1)

2008年(824)

我的朋友

分类:

2008-10-27 14:25:05


  create view v_rand
  as
  select c=unicode(cast(round(rand()*255,0) as tinyint))
  go
  create function f_jmstr(@str varchar(8000),@type bit)returns varchar(8000)
  /*
  
  *参数说明
  *str:要加密的字符串或已经加密后的字符
  *type:操作类型--0加密--解密
  *返回值说明
  *当操作类型为加密时(type--0):返回为加密后的str,即存放于数据库中的字符串
  *当操作类型为解密时(type--1):返回为实际字符串,即加密字符串解密后的原来字符串
  */
  As
  begin
    declare @re varchar(8000)--返回值
    declare @c int--加密字符
    declare @i int
  /*
  *加密方法为原字符异或一个随机ASCII字符
  */
    if @type=0--加密
    begin
    select @c=c,@re='',@i=len(@str) from v_rand
    while @i>0
    select @re=nchar(unicode(substring(@str,@i,1))^@c^@i)+@re
                     ,@i=@i-1
    set @re=@re+nchar(@c)
    end
    else--解密
    begin
    select @i=len(@str)-1,@c=unicode(substring(@str,@i+1,1)),@re=''
    while @i>0
    select @re=nchar(unicode(substring(@str,@i,1))^@c^@i)+@re ,@i=@i-1
    end
    return(@re)
  end
  go
  
  --测试
  declare @tempstr varchar(20)
  set @tempstr=' 1 2  3aA'
  select dbo.f_jmstr(dbo.f_jmstr(@tempstr,0),1)
  输出结果
   1 2  3aA
  
  (完)
【责编:admin】

--------------------next---------------------

阅读(280) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~