SQL stored procedure encryption and decryption

Source: Internet
Author: User
Tags rtrim

It can be used to encrypt SQL stored procedures or triggers.
(This is provided by SQL Server itself, that is, this is Microsoft's encryption algorithm)

Use the with encryption option
The with encryption clause hides the Stored Procedure text from the user. In the following example, create an encryption process and use the sp_helptext system stored procedure to obtain information about the encryption process. Then, try to obtain information about the process directly from the syscomments table.

If exists (Select name from sysobjects
Where name = 'encryption _ this 'and type = 'P ')
Drop procedure encrypt_this
Go
Use pubs
Go
Create procedure encrypt_this
With Encryption
As
Select *
From authors
Go

Exec sp_helptext encrypt_this

The following is the result set:

The object's comments have been encrypted.

Next, select the ID and text of the stored procedure content.

Select C. ID, C. Text
From syscomments C inner join sysobjects o
On C. ID = O. ID
Where o. Name = 'encrypt _ this'

The following is the result set:

The output of the text column is displayed in a single row. This information appears in the same row as the ID column during execution.
Id text
----------------------------------------------------------------------
1413580074 ????????????????????????????????? E ??????????????????????????????????????? ???????????????????????????????????

(1 row (s) affected)

Note:
Create proc name
With Encryption
As
Begin
-- Statement
End

Create trigger trigger_name
On {table | view}
With Encryption
For update
As
Begin
-- Statement
End

SQL stored procedure decryption algorithm cracking Microsoft's Encryption Algorithm
There are also a lot of decryption software for SQL stored procedures on the Internet, but most of them are charged.
The following is an SQL stored procedure decryption algorithm circulating on the Internet. Although it is unofficial, it is absolutely feasible!
It can also be used to decrypt SQL stored procedures or triggers.

Create procedure sp_decrypt (@ objectname varchar (50 ))
As
Begin
Set nocount on
-- Csdn: j9988 copyright: 2004.07.15
-- V3.2
-- Unlimitedly cracked bytes, applicable to sqlserver2000 stored procedures, functions, views, and triggers
-- Fixed the incorrect decryption error of the previous \ "view trigger \" version.
-- Error found. e_mail:
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 <
Begin
If exists (select 1 from syscomments where id = object_id (@ objectname) and)
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
If @ tr_parent_xtype = 'V'
Begin
Set @ sql1 = 'alter trigger on' + object_name (@ parentid) + 'with encryption insterd of insert as print 1'
End
Else
Begin
Set @ sql1 = 'alter trigger 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 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
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 <
Begin

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

While @ n <
Begin
Select @ origsptext1 = ctext, @ encrypted = encrypted, @ status = status from # temp where and

Set @ origsptext3 = (select ctext from syscomments where id = object_id (@ objectname) and)
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 on' + object_name (@ parentid) + 'with encryption instead of insert as print 1'
End
Else
Begin
Set @ origsptext2 = 'create trigger on' + object_name (@ parentid) + 'with encryption for insert as print 1'
End

End

If @ type = 'V'
Set @ origsptext2 = 'create view 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

Go

# Database Technology

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.