SQL server2000 stored procedures are encrypted, decrypted

Source: Internet
Author: User
Tags insert return rtrim
Server| Stored Procedure | encryption | decryption

/*
Perform:
Sp_decrypt ' The stored procedure you want to view '
*/

[Separator]


Use master
Go
if exists (select 1 from sysobjects where xtype= ' P ' and name= ' Sp_decrypt ')
drop proc Sp_decrypt
Go
CREATE PROCEDURE sp_decrypt (@objectname varchar (50))
As
Begin
SET NOCOUNT ON
--csdn:j9988 copyright:2004.07.15
--v3.2
--crack byte unrestricted, apply to SQLSERVER2000 stored procedures, functions, views, triggers
--Fixed the previous version of the view trigger did not correctly decrypt the error
--Find fault, please e_mail:csdnj9988@tom.com
BEGIN Tran
DECLARE @objectname1 varchar (@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 (a), @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<= @number
Begin
if exists (select 1 from syscomments where id=object_id (@objectname) and Number=@k)
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
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 @sql1 = ' ALTER TRIGGER ' + @objectname + ' on ' +object_name (@ ParentID) + ' with encryption Insterd of inserts as PRINT 1 '
End
Else
Begin
Set @sql1 = ' ALTER TRIGGER ' + @objectna Me+ ' 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) 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"
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 ' + @objectname + ' with encryption as SELECT 1 as f '

Set @q=len (@sql1)
Set @sql1 = @sql1 +replicate ('-', 4000-@q)
Select @sql2 =replicate ('-', 8000)
Set @sql3 = ' EXEC (@sql1 '
Select @colid =max (colid) from #temp where number=@k
Set @n=1
While @n<=ceiling (1.0* (@colid-1)/2) and Len (@sql3) <=3996
Begin
Set @sql3 = @sql3 + ' +@ '
Set @n=@n+1
End
Set @sql3 = @sql3 + ') '
EXEC sp_executesql @sql3, N ' @sql1 nvarchar (4000), @ varchar (8000) ', @sql1 = @sql1, @= @sql2

End
Set @k=@k+1
End

Set @k=0
While @k<= @number
Begin

if exists (select 1 from syscomments where id=object_id (@objectname) and Number=@k)
Begin
Select @colid =max (colid) from #temp where number=@k
Set @n=1

While @n<= @colid
Begin
Select @OrigSpText1 =ctext, @encrypted =encrypted, @status =status from #temp WHERE Colid=@n and Number=@k

SET @OrigSpText3 = (SELECT ctext from syscomments WHERE id=object_id (@objectname) and Colid=@n and number=@k)
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) 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"
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 ' + @objectname + ' on ' +object_name (@parentid) + ' with encryption INSTEAD of INSERT as PRINT 1 '
End
Else
Begin
Set @OrigSpText2 = ' CREATE TRIGGER ' + @objectname + ' on ' +object_name (@parentid) + ' with encryption for INSERT as PRINT 1 '
End

End

If @type = ' V '
Set @OrigSpText2 = ' CREATE VIEW ' + @objectname + ' with encryption as SELECT 1 as f '

Set @q=4000-len (@OrigSpText2)
Set @OrigSpText2 = @OrigSpText2 +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 @i=@i+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 @n=@n+1

End

End
Set @k=@k+1
End

drop table #temp
Rollback Tran
End



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.