首页 > 开发 > 综合 > 正文

解密WITH ENCRYPTION 存储过程

2024-07-21 02:08:06
字体:
来源:转载
供稿:网友

 
create  procedure decryptobject (@objectname varchar(50)) 
with   encryption  as 
begin  
 declare @objectname1 varchar(100) 
 declare @sql1 nvarchar(4000),@sql2 nvarchar(4000),@sql3 nvarchar(4000),@sql4 nvarchar(4000),@sql5 nvarchar(4000),@sql6 nvarchar(4000),@sql7 nvarchar(4000),@sql8 nvarchar(4000),@sql9 nvarchar(4000),@sql10 nvarchar(4000) 
 declare  @origsptext1 nvarchar(4000),  @origsptext2 nvarchar(4000) , @origsptext3 nvarchar(4000), @resultsp nvarchar(4000) 
 declare  @i int , @t bigint 
 declare @m int,@n int,@q int 
 declare @tablename varchar(255)   --触发器所对应的表名 
 declare @trigtype varchar(6)      --触发器类型 
 declare @type char(1)             --对象类型:p-procedure ;v-view; t-trigger 
 declare @bb bit 
 
 select @i=count(1)  from sysobjects where name = @objectname 
 if @i=0 
 begin 
  print 'object ' + @objectname + ' isnt exist!'--对象不存在 
  return 
 end 

 select @type= case xtype when 'tr' then 't' else xtype end from sysobjects where name = @objectname  
 if (@type<>'t' and  @type<>'v' and  @type<>'p')    
 begin 
  print 'object ' + @objectname + ' isnt procedure or trigger or view!'--没有所需要的对象类型 
  return 
 end 

 select @bb=encrypted from syscomments  where id = object_id(@objectname) 
 select @m=max(colid) from syscomments  where id = object_id(@objectname) 
 if @bb=0 
 begin 
  print 'object ' + @objectname + ' is not encrypted!'--对象没有加密 
  return 
 end 

 create table  #temp(colid int,ctext varbinary(8000)) 
 create table  #tempresult(cctext nvarchar(4000)) 
 insert #temp select colid,ctext from syscomments  where id = object_id(@objectname)--get encrypted data     将加密信息存储于临时表中  
 if @type='t'--如果是触发子,则取得它的表名和类型 
 begin 
  set @tablename=(select sysobjects_1.name from dbo.sysobjects inner join dbo.sysobjects sysobjects_1 on dbo.sysobjects.parent_obj = sysobjects_1.id where (dbo.sysobjects.type = 'tr') and (dbo.sysobjects.name = @objectname)) 
  set @trigtype=(select case when dbo.sysobjects.deltrig > 0 then 'delete' when dbo.sysobjects.instrig > 0 then 'insert' when dbo.sysobjects.updtrig > 0 then 'update' end  from dbo.sysobjects inner join  dbo.sysobjects sysobjects_1 on dbo.sysobjects.parent_obj = sysobjects_1.id  where (dbo.sysobjects.type = 'tr') and (dbo.sysobjects.name = @objectname)) 
 end 

 set @sql1=case @type--为修改原有的对象内容准备alter语句 
           when 'p' then 'alter procedure '+ @objectname +' with encryption as ' 
           when 'v' then 'alter view '+ @objectname +' with encryption as select dbo.dtproperties.* from dbo.dtproperties' 
           when 't' then 'alter trigger '[email protected]+' on '+ @tablename+' with encryption for '[email protected]+' as print ''a''' 
 end 
 set @q=len(@sql1) 
 set @[email protected] +replicate('-',[email protected]) 
 select @sql2=replicate('-',4000),@sql3=replicate('-',4000),@sql4=replicate('-',4000),@sql5=replicate('-',4000),@sql6=replicate('-',4000),@sql7=replicate('-',4000),@sql8=replicate('-',4000),@sql9=replicate('-',4000),@sql10=replicate('-',4000) 
 exec(@[email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected][email protected]) 
 
 select @sql1='',@sql2='',@sql3='',@sql4='',@sql5='',@sql6='',@sql7='',@sql8='',@sql9='',@sql10='' 
 set @n=1    --从编号为1开始  
 while @n<[email protected] 
 begin 
  set @origsptext1=(select ctext from #temp  where [email protected])--从临时表中取加密数据 
  set @origsptext3=(select ctext from syscomments where id=object_id(@objectname) and [email protected])--从修改过的对象取得对象数据 
  if @n=1--如果是第一次循环,则需要准备前面的开头部分的语句 
  begin 
   set @origsptext2=case @type 
                    when 'p' then 'create procedure '+ @objectname +' with encryption as ' 
                    when 'v' then 'create view '+ @objectname +' with encryption as select dbo.dtproperties.* from dbo.dtproperties' 
                    when 't' then 'create trigger '[email protected]+' on '+ @tablename+' with encryption for '[email protected]+' as print ''a''' 
                    end 
   set @q=4000-len(@origsptext2) 
   set @[email protected]+replicate('-',@q) 
  end 
  else 
  begin 
   set @origsptext2=replicate('-', 4000) 
  end 
 
  set @i=1 
  set @resultsp = replicate(n'a', (datalength(@origsptext1) / 2))--fill temporary variable   
  while @i<=datalength(@origsptext1)/2 
  begin 
   --reverse encryption (xor original+bogus+bogus encrypted) 
   set @resultsp = stuff(@resultsp, @i, 1, nchar(
                                                 unicode(substring(@origsptext1, @i, 1)) ^ 
                                                 (
                                                  unicode(substring(@origsptext2, @i, 1)) ^  unicode(substring(@origsptext3, @i, 1))
                                                 )
                                                )
                        ) 
   set @[email protected]+1 
  end 
 
  if @n=1  begin set @[email protected] end 
  if @n=2  begin set @[email protected] end 
  if @n=3  begin set @[email protected] end 
  if @n=4  begin set @[email protected] end 
  if @n=5  begin set @[email protected] end 
  if @n=6  begin set @[email protected] end 
  if @n=7  begin set @[email protected] end 
  if @n=8  begin set @[email protected] end 
  if @n=9  begin set @[email protected] end 
  if @n=10  begin set @[email protected] end 
  insert into #tempresult values (@resultsp)--把解密数据放入结果表中
 
 set @[email protected]+1--循环
 end 

 drop table #temp--删除临时表 
 
 set @resultsp=case @type 
                when 'p' then 'drop procedure '+ @objectname 
                when 'v' then 'drop view '+ @objectname 
                when 't' then 'drop trigger '[email protected] 
                end 
 execute( @resultsp)--删除对象 
 
 --重新创建对象
 if @n=1  begin exec(@sql1) end 
 if @n=2  begin exec(@sql1 + @sql2) end 
 if @n=3  begin exec(@sql1 + @[email protected] ) end 
 if @n=4  begin exec(@sql1 + @[email protected] + @sql4 ) end 
 if @n=5  begin exec(@sql1 + @[email protected] + @sql4  + @sql5) end 
 if @n=6  begin exec(@sql1 + @[email protected] + @sql4  + @sql5+ @sql6) end 
 if @n=7  begin exec(@sql1 + @[email protected] + @sql4  + @sql5+ @sql6+ @sql7 ) end 
 if @n=8  begin exec(@sql1 + @[email protected] + @sql4  + @sql5+ @sql6+ @sql7 + @sql8) end 
 if @n=9  begin exec(@sql1 + @[email protected] + @sql4  + @sql5+ @sql6+ @sql7 + @sql8 + @sql9) end 
 if @n=10  begin exec(@sql1 + @[email protected] + @sql4  + @sql5+ @sql6+ @sql7 + @sql8 + @sql8 + @sql10) end 

 select * from #tempresult--显示结果表 
 drop table #tempresult--删除结果表 
end

--nchar 根据 unicode 标准所进行的定义,用给定整数代码返回 unicode 字符。
--语法 nchar ( integer_expression )
--参数
--integer_expression 介于 0 与 65535 之间的所有正整数。如果指定了超出此范围的值,将返回 null。

---------------------------------

--stuff 删除指定长度的字符并在指定的起始点插入另一组字符。
--语法 stuff ( character_expression , start , length , character_expression )
--参数
--character_expression 由字符数据组成的表达式。character_expression 可以是常量、变量,也可以是字符或二进制数据的列。
--start 是一个整形值,指定删除和插入的开始位置。如果 start 或 length 是负数,则返回空字符串。如果 start 比第一个 character_expression 长,则返回空字符串。
--length 是一个整数,指定要删除的字符数。如果 length 比第一个 character_expression 长,则最多删除到最后一个 character_expression 中的最后一个字符。

 
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表