【草原和大树】解密MsSql存储过程
上一篇 / 下一篇 2008-07-23 13:51:17 / 个人分类:数据库技术
create PROCEDURE sp_decrypt(@objectname varchar(50))
zN/POs
s0 AS
2}"n/\Zts;S[0 begin ITPUB个人空间sG}/T7{i
qt5j
set nocount on ITPUB个人空间Z.M+d4oJ8K2}
--CSDN:j9988 copyright:2004.07.15
+tm4Y6@1M,w(J-^0C0 --V3.2 ITPUB个人空间 dr/n1{JC
LS%AM
--破解字节不受限制,适用于SQLSERVER2000
v$a6m"U3Dm0 --修正上一版"视图触发器"不能正确解密错误
o\D'V;r Y3?4fH0 --发现有错,请E_MAIL:CSDNj9988@tom.com ITPUB个人空间2?ksrP/ZBd;N
begin tran ITPUB个人空间B(J!dT;@.H
declare @objectname1 varchar(100),@orgvarbin varbinary(8000) ITPUB个人空间 W:lAw#uaS"J7Ln
declare @sql1 nvarchar(4000),@sql2 varchar(8000),@sql3 nvarchar(4000),@sql4 nvarchar(4000) ITPUB个人空间6U+`1v4h K;pR
DECLARE @OrigSpText1 nvarchar(4000), @OrigSpText2 nvarchar(4000) , @OrigSpText3 nvarchar(4000), @resultsp nvarchar(4000) ITPUB个人空间f!`#q\d5y2{8H2T'G0W/v
declare @i int,@status int,@type varchar(10),@parentid int
*\kCSXY0 declare @colid int,@n int,@q int,@j int,@k int,@encrypted int,@number int
DS?8dq0 select @type=xtype,@parentid=parent_obj from sysobjects where id=object_id(@objectname) ITPUB个人空间#Xa\_u}.XRCg
H J0R
p?x/S/N3lT'J0 create table #temp(number int,colid int,ctext varbinary(8000),encrypted int,status int)
MdQm
m2Z
R0 insert #temp SELECT number,colid,ctext,encrypted,status FROM syscomments WHERE id = object_id(@objectname) ITPUB个人空间9x#D
Z6s.i
u5h
select @number=max(number) from #temp ITPUB个人空间Ns'U3Y7j
k-m
set @k=0 ITPUB个人空间F3iNQ.K(j)Wl;g.k
?A+N;Q.c!O0 while @k<=@number ITPUB个人空间~L`zW
begin ITPUB个人空间H@k2kFz
o5d0]h
if exists(select 1 from syscomments where id=object_id(@objectname) and number=@k) ITPUB个人空间^&}(|
B/c W0X
begin
J.]#}^Eb0 if @type='P'
"} n/}7e
@0 set @sql1=(case when @number>1 then 'ALTER PROCEDURE '+ @objectname +';'+rtrim(@k)+' WITH ENCRYPTION AS ' ITPUB个人空间uDQ'}1ed/tj$hC
else 'ALTER PROCEDURE '+ @objectname+' WITH ENCRYPTION AS '
(\%t9@)Y
W6pf/G ]3E0 end)
4fW K.c(uA,RI0 ITPUB个人空间'OhD`O]1T2AL
if @type='TR'
3r'Y }8g%P&U6V,|Fx7E0 begin ITPUB个人空间v%d%Ec/^N!K/R9P8q
declare @parent_obj varchar(255),@tr_parent_xtype varchar(10)
2u8}b$G+A"V8oY!pp0 select @parent_obj=parent_obj from sysobjects where id=object_id(@objectname) ITPUB个人空间bn A4S0x!N~
select @tr_parent_xtype=xtype from sysobjects where id=@parent_obj
(b}Fk1IL0 if @tr_parent_xtype='V' ITPUB个人空间e4_0^4Q
Z T
`j
begin ITPUB个人空间
z@ Am$_$NZ1E&O'y
set @sql1='ALTER TRIGGER '+@objectname+' ON '+OBJECT_NAME(@parentid)+' WITH ENCRYPTION INSTERD OF INSERT AS PRINT 1 '
UdR nE0 end ITPUB个人空间!R*v9{EN
else ITPUB个人空间j,ew#?%G0mgyS
begin
9M)Xv)M!X[5z}0 set @sql1='ALTER TRIGGER '+@objectname+' ON '+OBJECT_NAME(@parentid)+' WITH ENCRYPTION FOR INSERT AS PRINT 1 '
o\`#td/x&a9JM'qs0 end
sslf;zrK4[tq0
9rv0`,^8IjU0 end ITPUB个人空间 G^|o9t']'R
if @type='FN' or @type='TF' or @type='IF' ITPUB个人空间'Syq
}%[1A
~4l
set @sql1=(case @type when 'TF' then
2CL3r!]:na&Sd0 'ALTER FUNCTION '+ @objectname+'(@a char(1)) returns @b table(a varchar(10)) with encryption as begin insert @b select @a return end ' ITPUB个人空间qd+e"E/cnu!i
when 'FN' then ITPUB个人空间&kr0p*~8NJK
'ALTER FUNCTION '+ @objectname+'(@a char(1)) returns char(1) with encryption as begin return @a end'
YC^0c%@L:T`Q(w0 when 'IF' then
+nxD [LeT|%S5p0 'ALTER FUNCTION '+ @objectname+'(@a char(1)) returns table with encryption as return select @a as a' ITPUB个人空间 A+_xUkcf
end) ITPUB个人空间(N:^.U%_f9N"I~0F ]
E*l*io*A&bk0 if @type='V' ITPUB个人空间v8q I3d/M,vp)Lzd
set @sql1='ALTER VIEW '+@objectname+' WITH ENCRYPTION AS SELECT 1 as f' ITPUB个人空间?7kZCi2^C
)HK.T,iFY0 set @q=len(@sql1)
lu ?\
`0u)n5OkC0 set @sql1=@sql1+REPLICATE('-',4000-@q) ITPUB个人空间WvNaq&c9{&Ki5t
select @sql2=REPLICATE('-',8000) ITPUB个人空间.pJ"x+j6O V M
set @sql3='exec(@sql1'
b7JD*L/W}ssX(N0 select @colid=max(colid) from #temp where number=@k ITPUB个人空间?uU#FD7l7W;E8J4p*V
set @n=1
&~sU"TY
Z(Hl0 while @n<=CEILING(1.0*(@colid-1)/2) and len(@sql3)<=3996 ITPUB个人空间7wfpK3F4W&?
begin ITPUB个人空间0n`Z
Z*jj'J
set @sql3=@sql3+'+@' ITPUB个人空间9HrGwa
set @n=@n+1
%]){_)@s7r-|0 end ITPUB个人空间
Y!W%G+T$L
set @sql3=@sql3+')'
\sH1I7M*z0 exec sp_executesql @sql3,N'@sql1 nvarchar(4000),@ varchar(8000)',@sql1=@sql1,@=@sql2 ITPUB个人空间0^xu^P$r0F
E.dE
ITPUB个人空间k.b/K$U2]]
end ITPUB个人空间6GL$^Ju1y*W"^
set @k=@k+1
SR)Q5]C"o1}0 end ITPUB个人空间 w~7g#[u
Jb:y+A0n*F
os1u0 set @k=0
h5v:|b.WL6xQ0 while @k<=@number ITPUB个人空间\Y [Y0V3R
r
begin
)}HMM5S.]#N-V0
bev/I%cQ,yho:nI&t0 if exists(select 1 from syscomments where id=object_id(@objectname) and number=@k) ITPUB个人空间 ~TI$z!GY#}sC
begin
1r7r7g,V_&uF&y0 select @colid=max(colid) from #temp where number=@k ITPUB个人空间*[?,h;JZ6J
set @n=1
S,W5Gu
bN0
"Tk~\qL(J0 while @n<=@colid ITPUB个人空间,vi
qYa
begin
3JCT1W'Q!aA0 select @OrigSpText1=ctext,@encrypted=encrypted,@status=status FROM #temp WHERE colid=@n and number=@k ITPUB个人空间o3C"XJ`tf;?
ZK1V&JT/_+A0 SET @OrigSpText3=(SELECT ctext FROM syscomments WHERE id=object_id(@objectname) and colid=@n and number=@k) ITPUB个人空间1MU}%h}-lU*CW
if @n=1 ITPUB个人空间`lH_jw2b(sf
begin
#xJ%F3A?1F$A1N5@ x|0 if @type='P' ITPUB个人空间9kK+B_.r}b4MZ
SET @OrigSpText2=(case when @number>1 then 'CREATE PROCEDURE '+ @objectname +';'+rtrim(@k)+' WITH ENCRYPTION AS ' ITPUB个人空间.nY%|CWacWsb
else 'CREATE PROCEDURE '+ @objectname +' WITH ENCRYPTION AS '
q? TmZes0 end) ITPUB个人空间UVxJA3NWO2j
r3M"zb'~ r0
*I\~U JS&B N:n0 if @type='FN' or @type='TF' or @type='IF' ITPUB个人空间a:}R'N5x8Ly
SET @OrigSpText2=(case @type when 'TF' then ITPUB个人空间M'U \_e/wn
'CREATE FUNCTION '+ @objectname+'(@a char(1)) returns @b table(a varchar(10)) with encryption as begin insert @b select @a return end '
Y[lEiK0 when 'FN' then
]oAB&q