Encryption and decryption of SQL Server Stored Procedures and stored Functions

Source: Internet
Author: User
Tags rtrim

Encryption and decryption of SQL Server Stored Procedures and stored Functions

StorageStoredProcess and storageStorageFunction ENCRYPTION: WITH ENCRYPTION
<! -- [If! SupportLineBreakNewLine] -->
<! -- [Endif] -->

 

 

 

CREATE procedure dbo. sp_XML_main

 

 

@ Table_name nvarchar (260) = '',

 

 

@ Dirname nvarchar (20) =''

 

 

WITH ENCRYPTION

 

 

As

 

 

Begin

 

 

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

 

 

End

 

 

Go

 

StorageStoredProcess and storageStorageFunction decryption (The following is the code written by a peerless expert)

 

 

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

 

 

 

 

/* -- Crack functions, processes, triggers, and views. Limited to SQLSERVER2000

 

 

 

 

-- Author: J9988 --*/

 

 

/* -- Call example

 

 

 

 

-- Decrypts a specified Stored Procedure

 

 

Exec sp_decrypt 'encrypt P _ Test'

 

 

 

 

-- Decrypt all stored procedures

 

 

Declare tb cursor

 

 

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 '/* ------- stored procedure [' + @ name + '] ----------- */'

 

 

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

 

 

-- Unlimitedly cracked bytes, applicable to SQLSERVER2000 stored procedures, functions, views, and triggers

 

 

-- Fixed the incorrect decryption error of the previous view trigger.

 

 

-- Found error, please E_MAIL: CSDNj9988@tom.com

 

 

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), @ resulnvtsp archar (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 <= @ 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'

 

 

Else 'alter PROCEDURE '+ @ objectName +' with encryption'

 

 

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 id = @ parent_obj

 

 

If @ tr_parent_xtype = 'V'

 

 

Begin

 

 

Set @ sql1 = 'alter trigger' + @ objectname + 'on' + OBJECT_NAME (@ parentid) + 'with encryption insterd of insert as print 1'

 

 

End

 

 

Else

 

 

Begin

 

 

Set @ sql1 = 'alter trigger' + @ objectname + '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'

 

 

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'

 

 

Else 'create PROCEDURE '+ @ objectName +' with encryption'

 

 

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'

 

 

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

 

Related Article

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.