首页 > 数据库 > SQL Server > 正文

SQL Server存储函数的加密解密

2024-08-31 00:49:18
字体:
来源:转载
供稿:网友

存储过程、存储函数的加密:with encryption

  <!--[if !supportlinebreaknewline]-->

  <!--[endif]-->

  create procedure dbo.sp_xml_main

  @table_name nvarchar(260)='',

  @dirname nvarchar(20)=''

  with encryption

  as

  begin

  ....................................................

  end

  go

  存储过程、存储函数的解密(以下是一位绝世高人编写的代码)

  if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[sp_decrypt]') and objectproperty(id, n'isprocedure') = 1)

  drop procedure [dbo].[sp_decrypt]

  go

  /*--破解函数,过程,触发器,视图.仅限于sqlserver2000

  --作者:j9988--*/

  /*--调用示例

  --解密指定存储过程

  exec sp_decrypt 'appsp_test'

  --对所有的存储过程解密

  declare tb cursor for

  select name from sysobjects where xtype='p' and status>0 and name<>'sp_decrypt'

  declare @name sysname

  open tb

  fetch next from tb into @name

  while @@fetch_status=0

  begin

  print '/*-------存储过程 ['[email protected]+'] -----------*/'

  exec sp_decrypt @name

  fetch next from tb into @name

  end

  close tb

  deallocate tb

  --*/

  if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[sp_decrypt]') and objectproperty(id, n'isprocedure') = 1)

  drop procedure [dbo].[sp_decrypt]

  go

  create procedure sp_decrypt(@objectname varchar(50))

  as

  begin

  set nocount on

  --csdn:j9988 copyright:2004.04.15

  --v3.1

  --破解字节不受限制,适用于sqlserver2000存储过程,函数,视图,触发器

  --修正上一版视图触发器不能正确解密错误

  --发现有错,请e_mail:[email protected]

  begin tran

  declare @objectname1 varchar(100),@orgvarbin varbinary(8000)

  declare @sql1 nvarchar(4000),@sql2 varchar(8000),@sql3 nvarchar(4000),@sql4 nvarchar(4000)

  declare @origsptext1 nvarchar(4000), @origsptext2 nvarchar(4000) , @origsptext3 nvarchar(4000), @resultsp nvarchar(4000)

  declare @i int,@status int,@type varchar(10),@parentid int

  declare @colid int,@n int,@q int,@j int,@k int,@encrypted int,@number int

  select @type=xtype,@parentid=parent_obj from sysobjects where id=object_id(@objectname)

  create table #temp(number int,colid int,ctext varbinary(8000),encrypted int,status int)

  insert #temp select number,colid,ctext,encrypted,status from syscomments where id = object_id(@objectname)

  select @number=max(number) from #temp

  set @k=0

  while @k<[email protected]

  begin

  if exists(select 1 from syscomments where id=object_id(@objectname) and [email protected])

  begin

  if @type='p'

  set @sql1=(case when @number>1 then 'alter procedure '+ @objectname +';'+rtrim(@k)+' with encryption as '

  else 'alter procedure '+ @objectname+' with encryption as '

  end)

  if @type='tr'

  begin

  declare @parent_obj varchar(255),@tr_parent_xtype varchar(10)

  select @parent_obj=parent_obj from sysobjects where id=object_id(@objectname)

  select @tr_parent_xtype=xtype from sysobjects where [email protected]_obj

  if @tr_parent_xtype='v'

  begin

  set @sql1='alter trigger '[email protected]+' on '+object_name(@parentid)+' with encryption insterd of insert as print 1 '

  end

  else

  begin

  set @sql1='alter trigger '[email protected]+' on '+object_name(@parentid)+' with encryption for insert as print 1 '

  end

  end

  if @type='fn' or @type='tf' or @type='if'

  set @sql1=(case @type when 'tf' then

  'alter function '+ @objectname+'(@a char(1)) returns @b table(a varchar(10)) with encryption as begin insert @b select @a return end '

 

  when 'fn' then

  'alter function '+ @objectname+'(@a char(1)) returns char(1) with encryption as begin return @a end'

  when 'if' then

  'alter function '+ @objectname+'(@a char(1)) returns table with encryption as return select @a as a'

  end)

  if @type='v'

  set @sql1='alter view '[email protected]+' with encryption as select 1 as f'

  set @q=len(@sql1)

  set @[email protected]+replicate('-',[email protected])

select @sql2=replicate('-',8000)

  set @sql3='exec(@sql1'

  select @colid=max(colid) from #temp where [email protected]

  set @n=1

  while @n<=ceiling(1.0*(@colid-1)/2) and len(@sql3)<=3996

  begin

  set @[email protected]+'[email protected]'

  set @[email protected]+1

  end

  set @[email protected]+')'

  exec sp_executesql @sql3,n'@sql1 nvarchar(4000),@ varchar(8000)',@[email protected],@[email protected]

  end

  set @[email protected]+1

  end

  set @k=0

  while @k<[email protected]

  begin

  if exists(select 1 from syscomments where id=object_id(@objectname) and [email protected])

  begin

  select @colid=max(colid) from #temp where [email protected]

  set @n=1

  while @n<[email protected]

  begin

  select @origsptext1=ctext,@encrypted=encrypted,@status=status from #temp where [email protected] and [email protected]

  set @origsptext3=(select ctext from syscomments where id=object_id(@objectname) and [email protected] and [email protected])

  if @n=1

  begin

  if @type='p'

  set @origsptext2=(case when @number>1 then 'create procedure '+ @objectname +';'+rtrim(@k)+' with encryption as '

  else 'create procedure '+ @objectname +' with encryption as '

  end)

  if @type='fn' or @type='tf' or @type='if'

  set @origsptext2=(case @type when 'tf' then

  'create function '+ @objectname+'(@a char(1)) returns @b table(a varchar(10)) with encryption as begin insert @b select @a return end '

  when 'fn' then

  'create function '+ @objectname+'(@a char(1)) returns char(1) with encryption as begin return @a end'

  when 'if' then

  'create function '+ @objectname+'(@a char(1)) returns table with encryption as return select @a as a'

  end)

  if @type='tr'

  begin

  if @tr_parent_xtype='v'

  begin

  set @origsptext2='create trigger '[email protected]+' on '+object_name(@parentid)+' with encryption instead of insert as print 1 '

  end

  else

  begin

  set @origsptext2='create trigger '[email protected]+' on '+object_name(@parentid)+' with encryption for insert as print 1 '

  end

  end

  if @type='v'

  set @origsptext2='create view '[email protected]+' with encryption as select 1 as f'

  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))

  while @i<=datalength(@origsptext1)/2

  begin

  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

  set @orgvarbin=cast(@origsptext1 as varbinary(8000))

  set @resultsp=(case when @encrypted=1

  then @resultsp

  else convert(nvarchar(4000),case when @status&2=2 then uncompress(@orgvarbin) else @orgvarbin end)

  end)

  print @resultsp

  set @[email protected]+1

  end

  end

  set @[email protected]+1

  end

  drop table #temp

  rollback tran

  end

 



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