How can I decrypt a SQL Server stored-procedure?

A. Here is the code for SQL 6.5/***********************************************// REVISED REC'D 5/21/98 // sp_decrypt_object Tom Sager 01/26/98 // // Decrypts objects (views, procedures & trigs) // created with the WITH ENCRYPTION option. // // Uses the encrypt() built-in function to find // a plaintext string that encrypts to the same */

Neil Pike

December 23, 1999

1 Min Read
ITPro Today logo

A. Here is the code for SQL 6.5

/************************************************//* REVISED REC'D 5/21/98 *//* sp_decrypt_object Tom Sager 01/26/98 *//* *//* Decrypts objects (views, procedures & trigs) *//* created with the WITH ENCRYPTION option. *//* *//* Uses the encrypt() built-in function to find *//* a plaintext string that encrypts to the same *//* value as stored in the text column of the *//* syscomments table. *//* *//************************************************/create proc sp_decrypt_object(@objname varchar(30))WITH ENCRYPTIONasSET NOCOUNT ONdeclare @errmsg varchar(80)declare @encrtext varchar(255)declare @decrtext varchar(255)declare @testtext varchar(255)declare @printline varchar(255)declare @textlen intdeclare @lup intdeclare @match char(1)declare @testchar smallintdeclare @begblk smallintdeclare @endblk smallintif (select count(*)from sysobjectswhere name = @objname) = 0beginselect @errmsg = 'Object '+@objname+' not found in database '+DB_NAME()print @errmsgreturn 1endif (select count(*) from sysobjects t1,syscomments t2where t1.name = @objnameand t1.id = t2.idand t2.texttype & 4 != 0) = 0beginselect @errmsg = 'Object '+@objname+' is not encrypted in database '+DB_NAME()print @errmsgreturn 1endDECLARE comments_cursor CURSOR forselect t1.textfrom syscomments t1,sysobjects t2where t1.id = t2.idand t2.name = @objnameorder by t1.colidOPEN comments_cursorFETCH NEXT FROM comments_cursorINTO @encrtextWHILE (@@fetch_status <> -1)BEGINIF (@@fetch_status <> -2)BEGINselect @decrtext = REPLICATE(' ', 255)select @textlen = DATALENGTH(@encrtext)select @lup = 1select @match = 'n'while (@lup <= @textlen)beginselect @testchar = 0select @match = 'n'while (@match = 'n')beginselect @decrtext =STUFF(@decrtext,@lup,1,CHAR(@testchar))select @testtext = encrypt(@decrtext)if ASCII(SUBSTRING(@testtext,@lup,1)) =ASCII(SUBSTRING(@encrtext,@lup,1))beginselect @match = 'y'endselect @testchar = @testchar + 1if (@testchar > 255)beginprint 'Error...no match found'return 1endendselect @lup = @lup + 1endselect @begblk = 1select @endblk = 1while (@endblk <= @textlen)beginif (substring(@decrtext,@endblk,1) = 0x0a)beginselect @printline = @printline +SUBSTRING(@decrtext,@begblk,@endblk-@begblk+1)print @printlineselect @begblk = @endblk + 1select @endblk = @begblkselect @printline = NULLendselect @endblk = @endblk + 1endselect @printline = @printline +SUBSTRING(@decrtext,@begblk,@endblk-@begblk+1)ENDFETCH NEXT FROM comments_cursor INTO @encrtextENDprint @printlineCLOSE comments_cursorDEALLOCATE comments_cursorGO

 

 

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like