Decrypt encrypted stored procedures (转)

Decrypt encrypted stored procedures (转)[@more@]

http://searchdatabase.techtarget.com/tip/1,289483,sid13_gci837799,00.html


This SP will decrypt Stored Procedures, Views or Triggers that were encrypted using "with encryption". It is adapted from a script by .NET-source-code.com/vb/scripts/ShowCode.ASP?txtCodeId=505&lngWId=5">Joseph Gama and ShoeBoy. There are two versions: one for SP's only and the other one for SP's, triggers and views. For version 1, the input is object name (stored procedure, view or trigger), and for version 2, the input is object name (stored procedure, view or trigger), object type ('T'-trigger, 'P'-stored procedure or 'V'-view). From PlanetSourceCode.com.

create PROCEDURE sp_decrypt_sp (@objectName varchar(50)) AS DECLARE @OrigSpText1 nvarchar(4000), @OrigSpText2 nvarchar(4000) , @OrigSpText3 nvarchar(4000), @resultsp nvarchar(4000) declare @i int , @t bigint --get encrypted data SET @OrigSpText1=(select ctext FROM sySCOmments WHERE id = object_id(@objectName)) SET @OrigSpText2='ALTER PROCEDURE '+ @objectName +' WITH ENCRYPTION AS '+REPLICATE('-', 3938) EXECUTE (@OrigSpText2) SET @OrigSpText3=(SELECT ctext FROM syscomments WHERE id = object_id(@objectName)) SET @OrigSpText2='CREATE PROCEDURE '+ @objectName +' WITH ENCRYPTION AS '+REPLICATE('-', 4000-62) --start counter SET @i=1 --fill temporary variable SET @resultsp = replicate(N'A', (datalength(@OrigSpText1) / 2)) --loop WHILE @i<=datalength(@OrigSpText1)/2 BEGIN --reverse encryption (XOR original+bogus+bogus encrypted) 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 --drop original SP EXECUTE ('drop PROCEDURE '+ @objectName) --remove encryption --preserve case SET @resultsp=REPLACE((@resultsp),'WITH ENCRYPTION', '') SET @resultsp=REPLACE((@resultsp),'With Encryption', '') SET @resultsp=REPLACE((@resultsp),'with encryption', '') IF CHARINDEX('WITH ENCRYPTION',UPPER(@resultsp) )>0 SET @resultsp=REPLACE(UPPER(@resultsp),'WITH ENCRYPTION', '') --replace Stored procedure without enryption execute( @resultsp) GO

 
 

Reader Feedback

Joakim M. writes: I tried this script with mixed results. It works for some encrypted procedures, but for others I get error meassages like:

Server: Msg 512, Level 16, State 1, Procedure sp_decrypt_sp, Line 7. Subquery returned more than 1 value. This is not peRmitted when the subquery follows =, !=, , >= or when the subquery is used as an expression.

 
 

Karl C writes: I got the same message as Joakim M. but upon further investigation I found that this happens only when stored procedures exceed 4000 characters. When this happens, sql SERVER stores the procedure across multiple rows so you get the error 'subquery returne more than 1 row'. To get around that you can change the statement

SELECT ctext FROM syscomments WHERE id = object_id(@objectName to SELECT top 1 ctext FROM syscomments WHERE id = object_id(@objectName order by colid

 
 

That will get you the first part of the stored procedure, which can't be created because it is missing the end part and is not a valid syntax but you can print @resultsp out to see it.

For More Information

  • Mailto:tdichiara@techtarget.com" rel="nofollow">Feedback: E-mail the editor with your thoughts about this tip.
  • tipsIndex/0,289482,sid13_tax286331,00.html" rel="nofollow">More tips: Hundreds of free SQL SERVER tips and scripts.
  • Tip contest: Have a SQL Server tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize -- submit your tip today!
  • webLinks/0,289521,sid13_tax282894,00.html">Best Web Links: SQL Server tips, tutorials, scripts, and more.
  • Forums: Ask your technical SQL Server questions--or help out your peers by answering them--in our active forums.
  • Ask the Experts: Our SQL, Database Design, ORACLE, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-992926/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752043/viewspace-992926/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值