熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> SQL Server >> 正文

道與魔:SqlServer存儲過程/函數加/解密

2022-06-13   來源: SQL Server 

  存儲過程存儲函數的加密WITH ENCRYPTION
  <![if !supportLineBreakNewLine]>
  <![endif]>
  
  CREATE procedure dbosp_XML_main
  
  @table_name nvarchar()=
  
  @dirname nvarchar()=
  
  WITH ENCRYPTION
  
  as
  
  begin
  
  
  
  end
  
  go
  
  存儲過程存儲函數的解密(以下是一位絕世高人編寫的代碼)
  
  if exists (select * from dbosysobjects where id = object_id(N[dbo][sp_decrypt]) and OBJECTPROPERTY(id NIsProcedure) = )
  
  drop procedure [dbo][sp_decrypt]
  
  GO
  
  /*破解函數過程觸發器視圖僅限於SQLSERVER
  
  作者:J All rights reserved*/
  
  /*調用示例
  
  解密指定存儲過程
  
  exec sp_decrypt AppSP_test
  
  對所有的存儲過程解密
  
  declare tb cursor for
  
  select name from sysobjects where xtype=P and status> and name<>sp_decrypt
  
  declare @name sysname
  
  open tb
  
  fetch next from tb into @name
  
  while @@fetch_status=
  
  begin
  
  print /*存儲過程 [+@name+] */
  
  exec sp_decrypt @name
  
  fetch next from tb into @name
  
  end
  
  close tb
  
  deallocate tb
  
  */
  
  if exists (select * from dbosysobjects where id = object_id(N[dbo][SP_DECRYPT]) and OBJECTPROPERTY(id NIsProcedure) = )
  
  drop procedure [dbo][SP_DECRYPT]
  
  GO
  
  CREATE PROCEDURE sp_decrypt(@objectName varchar())
  
  AS
  
  begin
  
  set nocount on
  
  破解字節不受限制適用於SQLSERVER存儲過程函數視圖觸發器
  
  修正上一版視圖觸發器不能正確解密錯誤
  
  發現有錯請E_MAIL
  
  begin tran
  
  declare @objectname varchar()@orgvarbin varbinary()
  
  declare @sql nvarchar()@sql varchar()@sql nvarchar()@sql nvarchar()
  
  DECLARE @OrigSpText nvarchar() @OrigSpText nvarchar() @OrigSpText nvarchar() @resultsp nvarchar()
  
  declare @i int@status int@type varchar()@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 intcolid intctext varbinary()encrypted intstatus int)
  
  insert #temp SELECT numbercolidctextencryptedstatus FROM syscomments WHERE id = object_id(@objectName)
  
  select @number=max(number) from #temp
  
  set @k=
  
  while @k<=@number
  
  begin
  
  if exists(select from syscomments where id=object_id(@objectname) and number=@k)
  
  begin
  
  if @type=P
  
  set @sql=(case when @number> 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()@tr_parent_xtype varchar()
  
  select @parent_obj=parent_obj from sysobjects where id=object_id(@objectName)
  
  select @tr_parent_xtype=xtype from sysobjects where id=@parent_obj
  
  if @tr_parent_xtype=V
  
  begin
  
  set @sql=ALTER TRIGGER +@objectname+ ON +OBJECT_NAME(@parentid)+ WITH ENCRYPTION INSTERD OF INSERT AS PRINT
  
  end
  
  else
  
  begin
  
  set @sql=ALTER TRIGGER +@objectname+ ON +OBJECT_NAME(@parentid)+ WITH ENCRYPTION FOR INSERT AS PRINT
  
  end
  
  end
  
  if @type=FN or @type=TF or @type=IF
  
  set @sql=(case @type when TF then
  
  ALTER FUNCTION + @objectName+(@a char()) returns @b table(a varchar()) with encryption as begin insert @b select @a return end
  
  when FN then
  
  ALTER FUNCTION + @objectName+(@a char()) returns char() with encryption as begin return @a end
  
  when IF then
  
  ALTER FUNCTION + @objectName+(@a char()) returns table with encryption as return select @a as a
  
  end)
  
  if @type=V
  
  set @sql=ALTER VIEW +@objectname+ WITH ENCRYPTION AS SELECT as f
  
  set @q=len(@sql)
  
  set @sql=@sql+REPLICATE(@q)
  
  select @sql=REPLICATE()
  
  set @sql=exec(@sql
  
  select @colid=max(colid) from #temp where number=@k
  
  set @n=
  
  while @n<=CEILING(*(@colid)/) and len(@sQL)<=
  
  begin
  
  set @sql=@sql++@
  
  set @n=@n+
  
  end
  
  set @sql=@sql+)
  
  exec sp_executesql @sqlN@sql nvarchar()@ varchar()@sql=@sql@=@sql
  
  end
  
  set @k=@k+
  
  end
  
  set @k=
  
  while @k<=@number
  
  begin
  
  if exists(select from syscomments where id=object_id(@objectname) and number=@k)
  
  begin
  
  select @colid=max(colid) from #temp where number=@k
  
  set @n=
  
  while @n<=@colid
  
  begin
  
  select @OrigSpText=ctext@encrypted=encrypted@status=status FROM #temp WHERE colid=@n and number=@k
  
  SET @OrigSpText=(SELECT ctext FROM syscomments WHERE id=object_id(@objectName) and colid=@n and number=@k)
  
  if @n=
  
  begin
  
  if @type=P
  
  SET @OrigSpText=(case when @number> 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 @OrigSpText=(case @type when TF then
  
  CREATE FUNCTION + @objectName+(@a char()) returns @b table(a varchar()) with encryption as begin insert @b select @a return end
  
  when FN then
  
  CREATE FUNCTION + @objectName+(@a char()) returns char() with encryption as begin return @a end
  
  when IF then
  
  CREATE FUNCTION + @objectName+(@a char()) returns table with encryption as return select @a as a
  
  end)
  
  if @type=TR
  
  begin
  
  if @tr_parent_xtype=V
  
  begin
  
  set @OrigSpText=CREATE TRIGGER +@objectname+ ON +OBJECT_NAME(@parentid)+ WITH ENCRYPTION INSTEAD OF INSERT AS PRINT
  
  end
  
  else
  
  begin
  
  set @OrigSpText=CREATE TRIGGER +@objectname+ ON +OBJECT_NAME(@parentid)+ WITH ENCRYPTION FOR INSERT AS PRINT
  
  end
  
  end
  
  if @type=V
  
  set @OrigSpText=CREATE VIEW +@objectname+ WITH ENCRYPTION AS SELECT as f
  
  set @q=len(@OrigSpText)
  
  set @OrigSpText=@OrigSpText+REPLICATE(@q)
  
  end
  
  else
  
  begin
  
  SET @OrigSpText=REPLICATE( )
  
  end
  
  SET @i=
  
  SET @resultsp = replicate(NA (datalength(@OrigSpText) / ))
  
  WHILE @i<=datalength(@OrigSpText)/
  
  BEGIN
  
  SET @resultsp = stuff(@resultsp @i NCHAR(UNICODE(substring(@OrigSpText @i )) ^

From:http://tw.wingwit.com/Article/program/SQLServer/201311/21965.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.