Decryption Stored Procedure

Source: Internet
Author: User
Create procedure [DBO]. [SP _ windbi $ decrypt]
(@ Procedure sysname = NULL, @ revfl Int = 1)
As
/*
Translated by Wang Chenghui. Please refer to www.windbi.com, the Microsoft Bi pioneer.
Currently, this stored procedure can only decrypt stored procedures. As for the stored procedures of decryption functions, triggers, and views, this website will pay more attention to the following calling methods:
Exec DBO. SP _ windbi $ decrypt @ procedure, 0
If the second parameter uses 1, some prompts about the stored procedure are displayed.
-- Release 2.0
*/
Set nocount on
If @ revfl = 1
Begin
Print 'Warning: This stored procedure will delete and recreate the original stored procedure. '
Print 'make sure your database has a backup before running this stored procedure. '
Print 'this stored procedure should generally run in a backup non-product environment in the product environment. '
Print 'to run this stored procedure, change the value of @ refl to 0. '
Return 0
End
Declare @ intprocspace bigint, @ t bigint, @ maxcolid smallint, @ intencrypted
Tinyint, @ procnamelength int
Select @ maxcolid = max (subobjid), @ intencrypted = imageval from
SYS. sysobjvalues where objid = object_id (@ procedure)
Group by imageval
-- Select @ maxcolid as 'rows in SYS. sysobjvalues'
Select @ procnamelength = datalength (@ procedure) + 29
Declare @ real_01 nvarchar (max)
Declare @ fake_01 nvarchar (max)
Declare @ fake_encrypt_01 nvarchar (max)
Declare @ real_decrypt_01 nvarchar (max), @ real_decrypt_01a nvarchar (max)
Declare @ objtype varchar (2), @ parentname nvarchar (max)
Select @ real_decrypt_01a =''
-- Extract the object type, for example, stored procedure or function. If it is a trigger, obtain the name of its parent object.
Select @ objtype = type, @ parentname = object_name (parent_object_id)
From SYS. objects where [object_id] = object_id (@ procedure)
-- Extract the encrypted imageval record from SYS. sysobjvalues
Set @ real_01 = (select top 1 imageval from SYS. sysobjvalues where objid =
Object_id (@ procedure) and valclass = 1 order by subobjid)
-- Create a temporary table
Create Table # output ([ident] [int] identity (1, 1) not null,
[Real_decrypt] nvarchar (max ))
-- Start a transaction and roll back later
Begin tran
-- Change the original stored procedure and replace it with a hyphen
If @ objtype = 'P'
Set @ fake_01 = 'alter procedure '+ @ procedure +' with encryption
'+ Replicate ('-', 40003-@ procnamelength)
Else if @ objtype = 'fn'
Set @ fake_01 = 'alter function' + @ procedure + '() returns int with encryption as begin return 1
/* '+ Replicate (' * ', datalength (@ real_01)/2-@ procnamelength) +' */end'
Else if @ objtype = 'V'
Set @ fake_01 = 'alter view' + @ procedure + 'with encryption as select 1 as COL
/* '+ Replicate (' * ', datalength (@ real_01)/2-@ procnamelength) + '*/'
Else if @ objtype = 'tr'
Set @ fake_01 = 'alter trigger' + @ procedure + 'on' + @ parentname + 'with encryption after insert as raiserror (''n)
/* '+ Replicate (' * ', datalength (@ real_01)/2-@ procnamelength) + '*/'
Execute (@ fake_01)
-- Extract encrypted false data from SYS. sysobjvalues
Set @ fake_encrypt_01 = (select top 1 imageval from SYS. sysobjvalues where objid =
Object_id (@ procedure) and valclass = 1 order by subobjid)
If @ objtype = 'P'
Set @ fake_01 = 'create processure '+ @ procedure +' with encryption
'+ Replicate ('-', 40003-@ procnamelength)
Else if @ objtype = 'fn'
Set @ fake_01 = 'create function' + @ procedure + '() returns int with encryption as begin return 1
/* '+ Replicate (' * ', datalength (@ real_01)/2-@ procnamelength) +' */end'
Else if @ objtype = 'V'
Set @ fake_01 = 'create view' + @ procedure + 'with encryption as select 1 as COL
/* '+ Replicate (' * ', datalength (@ real_01)/2-@ procnamelength) + '*/'
Else if @ objtype = 'tr'
Set @ fake_01 = 'create trigger' + @ procedure + 'on' + @ parentname + 'with encryption after insert as raiserror (''n)
/* '+ Replicate (' * ', datalength (@ real_01)/2-@ procnamelength) + '*/'
-- Start counting
Set @ intprocspace = 1
-- Fill temporary variables with characters
Set @ real_decrypt_01 = replicate (N 'A', (datalength (@ real_01)/2 ))
-- Set every variable in a loop to create a real Variable
-- One byte each time
Set @ intprocspace = 1
-- Traverse each @ real_xx variable and decrypt it if necessary
While @ intprocspace <= (datalength (@ real_01)/2)
Begin
-- The real and encrypted false operations are different or handled.
Set @ real_decrypt_01 = stuff (@ real_decrypt_01, @ intprocspace, 1,
Nchar (Unicode (substring (@ real_01, @ intprocspace, 1) ^
(Unicode (substring (@ fake_01, @ intprocspace, 1) ^
Unicode (substring (@ fake_encrypt_01, @ intprocspace, 1 )))))
Set @ intprocspace = @ intprocspace + 1
End
-- Insert a variable to table # output using the sp_helptext Logic
Insert # output (real_decrypt) Select @ real_decrypt_01
-- Select real_decrypt as '# output Chek' from # output -- Test
---------------------------------------
-- Start extracting from sp_helptext
---------------------------------------
Declare @ dbname sysname
, @ Blankspaceadded int
, @ Basepos int
, @ Currentpos int
, @ Textlength int
, @ Lineid int
, @ Addonlen int
, @ Lfcr int -- enter the length of the line feed
, @ Definedlength int
, @ Syscomtext nvarchar (4000)
, @ Line nvarchar (255)
Select @ definedlength = 255
Select @ blankspaceadded = 0 -- space at the end of the trace row. Note that the Len function ignores unnecessary spaces.
Create Table # commenttext
(Lineid int
, Text nvarchar (255) Collate database_default)
-- Replace SYS. sysobjvalues with # output
Declare ms_crs_syscom cursor local
For select real_decrypt from # output
Order by ident
For read only
-- Get Text
Select @ lfcr = 2
Select @ lineid = 1
Open ms_crs_syscom
Fetch next from ms_crs_syscom into @ syscomtext
While @ fetch_status> = 0
Begin
Select @ basepos = 1
Select @ currentpos = 1
Select @ textlength = Len (@ syscomtext)
While @ currentpos! = 0
Begin
-- Press enter to find the end of the row
Select @ currentpos = charindex (char (13) + char (10), @ syscomtext,
@ Basepos)
-- If you find the carriage return
If @ currentpos! = 0
Begin
-- If the new value of @ lines length is greater than the value set, insert the current content of @ lines and continue
While (isnull (LEN (@ line), 0) + @ blankspaceadded +
@ Currentpos-@ basepos + @ lfcr)> @ definedlength
Begin
Select @ addonlen = @ definedlength-(isnull (LEN (@ line), 0) +
@ Blankspaceadded)
Insert # commenttext values
(@ Lineid,
Isnull (@ line, n'') + isnull (substring (@ syscomtext,
@ Basepos, @ addonlen), n ''))
Select @ line = NULL, @ lineid = @ lineid + 1,
@ Basepos = @ basepos + @ addonlen, @ blankspaceadded = 0
End
Select @ line = isnull (@ line, n'') +
Isnull (substring (@ syscomtext, @ basepos, @ currentpos-@ basepos + @ lfcr), n '')
Select @ basepos = @ currentpos + 2
Insert # commenttext values (@ lineid, @ line)
Select @ lineid = @ lineid + 1
Select @ line = NULL
End
Else
-- If the carriage return cannot be found
Begin
If @ basepos <= @ textlength
Begin
-- If the new value of @ lines length is greater than the defined length
While (isnull (LEN (@ line), 0) + @ blankspaceadded +
@ Textlength-@ basepos + 1)> @ definedlength
Begin
Select @ addonlen = @ definedlength-
(Isnull (LEN (@ line), 0) + @ blankspaceadded)
Insert # commenttext values
(@ Lineid,
Isnull (@ line, n'') + isnull (substring (@ syscomtext,
@ Basepos, @ addonlen), n ''))
Select @ line = NULL, @ lineid = @ lineid + 1,
@ Basepos = @ basepos + @ addonlen, @ blankspaceadded =
0
End
Select @ line = isnull (@ line, n'') +
Isnull (substring (@ syscomtext, @ basepos, @ textlength-@ basepos + 1), n '')
If Len (@ line) <@ definedlength and charindex ('',
@ Syscomtext, @ textlength + 1)> 0
Begin
Select @ line = @ line + '', @ blankspaceadded = 1
End
End
End
End
Fetch next from ms_crs_syscom into @ syscomtext
End
If @ line is not null
Insert # commenttext values (@ lineid, @ line)
Select text from # commenttext order by lineid
Close ms_crs_syscom
Deallocate ms_crs_syscom
Drop table # commenttext
---------------------------------------
-- End extraction from sp_helptext
---------------------------------------
-- Delete the stored procedure created with a dash and recreate the original Stored Procedure
Rollback tran
Drop table # output

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.