如何实现对单个字段进行加密

转载 2008年04月27日 07:24:00
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

createviewv_rand

as

selectc=unicode(cast(round(rand()*255,0)astinyint))

go

 

createfunctionf_jmstr(@strvarchar(8000),@typebit)returnsvarchar(8000)

/*

*参数说明

*str:要加密的字符串或已经加密后的字符

*type:操作类型--0加密--解密

*返回值说明

*当操作类型为加密时(type--0):返回为加密后的str,即存放于数据库中的字符串

*当操作类型为解密时(type--1):返回为实际字符串,即加密字符串解密后的原来字符串

*/

As

begin

        declare@revarchar(8000)--返回值

        declare@cint--加密字符

        declare@iint

/*

*加密方法为原字符异或一个随机ASCII字符

*/

   if@type=0--加密

   begin

                  select@c=c,@re='',@i=len(@str)fromv_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@tempstrvarchar(20)

set@tempstr=' 12  3aA'

selectdbo.f_jmstr(dbo.f_jmstr(@tempstr,0),1)

输出结果

 12  3aA

 

(完) 

<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
收藏助手
不良信息举报
您举报文章:如何实现对单个字段进行加密
举报原因:
原因补充:

(最多只允许输入30个字)