(收藏)SQL SERVER 存储过程加密和解密 收藏
create PROCEDURE sp_decrypt(@objectname varchar(50))
AS
begin
set nocount on
--CSDN:j9988 copyright:2004.07.15
--V3.2
--破解字节不受限制,适用于SQLSERVER2000存储过程,函数,视图,触发器
--修正上一版"视图触发器"不能正确解密错误
--发现有错,请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), @resultsp nvarchar(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 &#64;k<&#61;&#64;number
begin
if exists(select 1 from syscomments where id&#61;object_id(&#64;objectname) and number&#61;&#64;k)
begin
if &#64;type&#61;&#39;P&#39;
set &#64;sql1&#61;(case when &#64;number>1 then &#39;Alter PROCEDURE &#39;&#43; &#64;objectname &#43;&#39;;&#39;&#43;rtrim(&#64;k)&#43;&#39; WITH ENCRYPTION AS &#39;
else &#39;Alter PROCEDURE &#39;&#43; &#64;objectname&#43;&#39; WITH ENCRYPTION AS &#39;
end)
if &#64;type&#61;&#39;TR&#39;
begin
declare &#64;parent_obj varchar(255),&#64;tr_parent_xtype varchar(10)
select &#64;parent_obj&#61;parent_obj from sysobjects where id&#61;object_id(&#64;objectname)
select &#64;tr_parent_xtype&#61;xtype from sysobjects where id&#61;&#64;parent_obj
if &#64;tr_parent_xtype&#61;&#39;V&#39;
begin
set &#64;sql1&#61;&#39;Alter TRIGGER &#39;&#43;&#64;objectname&#43;&#39; ON &#39;&#43;OBJECT_NAME(&#64;parentid)&#43;&#39; WITH ENCRYPTION INSTERD OF Insert AS PRINT 1 &#39;
end
else
begin
set &#64;sql1&#61;&#39;Alter TRIGGER &#39;&#43;&#64;objectname&#43;&#39; ON &#39;&#43;OBJECT_NAME(&#64;parentid)&#43;&#39; WITH ENCRYPTION FOR Insert AS PRINT 1 &#39;
end
end
if &#64;type&#61;&#39;FN&#39; or &#64;type&#61;&#39;TF&#39; or &#64;type&#61;&#39;IF&#39;
set &#64;sql1&#61;(case &#64;type when &#39;TF&#39; then
&#39;Alter FUNCTION &#39;&#43; &#64;objectname&#43;&#39;(&#64;a char(1)) returns &#64;b table(a varchar(10)) with encryption as begin insert &#64;b select &#64;a return end &#39;
when &#39;FN&#39; then
&#39;Alter FUNCTION &#39;&#43; &#64;objectname&#43;&#39;(&#64;a char(1)) returns char(1) with encryption as begin return &#64;a end&#39;
when &#39;IF&#39; then
&#39;Alter FUNCTION &#39;&#43; &#64;objectname&#43;&#39;(&#64;a char(1)) returns table with encryption as return select &#64;a as a&#39;
end)
if &#64;type&#61;&#39;V&#39;
set &#64;sql1&#61;&#39;Alter VIEW &#39;&#43;&#64;objectname&#43;&#39; WITH ENCRYPTION AS Select 1 as f&#39;
set &#64;q&#61;len(&#64;sql1)
set &#64;sql1&#61;&#64;sql1&#43;REPLICATE(&#39;-&#39;,4000-&#64;q)
select &#64;sql2&#61;REPLICATE(&#39;-&#39;,8000)
set &#64;sql3&#61;&#39;exec(&#64;sql1&#39;
select &#64;colid&#61;max(colid) from #temp where number&#61;&#64;k
set &#64;n&#61;1
while &#64;n<&#61;CEILING(1.0*(&#64;colid-1)/2) and len(&#64;sql3)<&#61;3996
begin
set &#64;sql3&#61;&#64;sql3&#43;&#39;&#43;&#64;&#39;
set &#64;n&#61;&#64;n&#43;1
end
set &#64;sql3&#61;&#64;sql3&#43;&#39;)&#39;
exec sp_executesql &#64;sql3,N&#39;&#64;sql1 nvarchar(4000),&#64; varchar(8000)&#39;,&#64;sql1&#61;&#64;sql1,&#64;&#61;&#64;sql2
end
set &#64;k&#61;&#64;k&#43;1
end
set &#64;k&#61;0
while &#64;k<&#61;&#64;number
begin
if exists(select 1 from syscomments where id&#61;object_id(&#64;objectname) and number&#61;&#64;k)
begin
select &#64;colid&#61;max(colid) from #temp where number&#61;&#64;k
set &#64;n&#61;1
while &#64;n<&#61;&#64;colid
begin
select &#64;OrigSpText1&#61;ctext,&#64;encrypted&#61;encrypted,&#64;status&#61;status FROM #temp Where colid&#61;&#64;n and number&#61;&#64;k
SET &#64;OrigSpText3&#61;(Select ctext FROM syscomments Where id&#61;object_id(&#64;objectname) and colid&#61;&#64;n and number&#61;&#64;k)
if &#64;n&#61;1
begin
if &#64;type&#61;&#39;P&#39;
SET &#64;OrigSpText2&#61;(case when &#64;number>1 then &#39;Create PROCEDURE &#39;&#43; &#64;objectname &#43;&#39;;&#39;&#43;rtrim(&#64;k)&#43;&#39; WITH ENCRYPTION AS &#39;
else &#39;Create PROCEDURE &#39;&#43; &#64;objectname &#43;&#39; WITH ENCRYPTION AS &#39;
end)
if &#64;type&#61;&#39;FN&#39; or &#64;type&#61;&#39;TF&#39; or &#64;type&#61;&#39;IF&#39;
SET &#64;OrigSpText2&#61;(case &#64;type when &#39;TF&#39; then
&#39;Create FUNCTION &#39;&#43; &#64;objectname&#43;&#39;(&#64;a char(1)) returns &#64;b table(a varchar(10)) with encryption as begin insert &#64;b select &#64;a return end &#39;
when &#39;FN&#39; then
&#39;Create FUNCTION &#39;&#43; &#64;objectname&#43;&#39;(&#64;a char(1)) returns char(1) with encryption as begin return &#64;a end&#39;
when &#39;IF&#39; then
&#39;Create FUNCTION &#39;&#43; &#64;objectname&#43;&#39;(&#64;a char(1)) returns table with encryption as return select &#64;a as a&#39;
end)
if &#64;type&#61;&#39;TR&#39;
begin
if &#64;tr_parent_xtype&#61;&#39;V&#39;
begin
set &#64;OrigSpText2&#61;&#39;Create TRIGGER &#39;&#43;&#64;objectname&#43;&#39; ON &#39;&#43;OBJECT_NAME(&#64;parentid)&#43;&#39; WITH ENCRYPTION INSTEAD OF Insert AS PRINT 1 &#39;
end
else
begin
set &#64;OrigSpText2&#61;&#39;Create TRIGGER &#39;&#43;&#64;objectname&#43;&#39; ON &#39;&#43;OBJECT_NAME(&#64;parentid)&#43;&#39; WITH ENCRYPTION FOR Insert AS PRINT 1 &#39;
end
end
if &#64;type&#61;&#39;V&#39;
set &#64;OrigSpText2&#61;&#39;Create VIEW &#39;&#43;&#64;objectname&#43;&#39; WITH ENCRYPTION AS Select 1 as f&#39;
set &#64;q&#61;4000-len(&#64;OrigSpText2)
set &#64;OrigSpText2&#61;&#64;OrigSpText2&#43;REPLICATE(&#39;-&#39;,&#64;q)
end
else
begin
SET &#64;OrigSpText2&#61;REPLICATE(&#39;-&#39;, 4000)
end
SET &#64;i&#61;1
SET &#64;resultsp &#61; replicate(N&#39;A&#39;, (datalength(&#64;OrigSpText1) / 2))
WHILE &#64;i<&#61;datalength(&#64;OrigSpText1)/2
BEGIN
SET &#64;resultsp &#61; stuff(&#64;resultsp, &#64;i, 1, NCHAR(UNICODE(substring(&#64;OrigSpText1, &#64;i, 1)) ^
(UNICODE(substring(&#64;OrigSpText2, &#64;i, 1)) ^
UNICODE(substring(&#64;OrigSpText3, &#64;i, 1)))))
SET &#64;i&#61;&#64;i&#43;1
END
set &#64;orgvarbin&#61;cast(&#64;OrigSpText1 as varbinary(8000))
set &#64;resultsp&#61;(case when &#64;encrypted&#61;1
then &#64;resultsp
else convert(nvarchar(4000),case when &#64;status&2&#61;2 then uncompress(&#64;orgvarbin) else &#64;orgvarbin end)
end)
print &#64;resultsp
set &#64;n&#61;&#64;n&#43;1
end
end
set &#64;k&#61;&#64;k&#43;1
end
drop table #temp
rollback tran
end
go
本文来自CSDN博客&#xff0c;转载请标明出处&#xff1a;http://blog.csdn.net/happyparrot/archive/2008/05/16/2451053.aspx