先把下面的存储过程在要解密的存储过程的数据库上创建
然后新建查询 再右击连接->更改连接,然后在服务器名称输入admin:127.0.0.1,点击连接
输入以下代码更改要解密的数据库
exec dbo.sp__procedure$decrypt ‘数据库.存储过程’,1
看完提示后保证都做好了备份,再把1改成0再次运行
2000版本的直接创建存储,再进行解密就行了
exec sp_decrypt ‘数据库.存储过程’
由于版本不一样,请选择对应的存储过程,不然会出现错误
2008的解密存储过程
CREATE PROCEDURE [dbo].[sp__procedure$decrypt]
(@procedure sysname = NULL, @revfl int = 1)
AS
SET NOCOUNT ON
IF @revfl = 1
BEGIN
PRINT ‘注意:此过程删除并重新构建原始存储过程。’
PRINT ‘在运行此过程之前备份数据库。’
PRINT ‘理想情况下,此过程应该在该过程的非生产副本上运行。’
PRINT ‘要运行此过程,请将@revfl参数更改为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 @real_02 nvarchar(max)
DECLARE @real_03 nvarchar(max)
DECLARE @real_04 nvarchar(max)
DECLARE @real_05 nvarchar(max)
DECLARE @fake_01 nvarchar(max)
,@fake_02 nvarchar(max)
,@fake_03 nvarchar(max)
,@fake_04 nvarchar(max)
,@fake_05 nvarchar(max)
DECLARE @fake_encrypt_01 nvarchar(max)
DECLARE @fake_encrypt_02 nvarchar(max)
DECLARE @fake_encrypt_03 nvarchar(max)
DECLARE @fake_encrypt_04 nvarchar(max)
DECLARE @fake_encrypt_05 nvarchar(max)
DECLARE @real_decrypt_01 nvarchar(max)
,@real_decrypt_01a nvarchar(max)
,@real_decrypt_02 nvarchar(max)
,@real_decrypt_02a nvarchar(max)
,@real_decrypt_03 nvarchar(max)
,@real_decrypt_03a nvarchar(max)
,@real_decrypt_04 nvarchar(max)
,@real_decrypt_04a nvarchar(max)
,@real_decrypt_05 nvarchar(max)
,@real_decrypt_05a nvarchar(max)
select @real_decrypt_01a = ‘’
,@real_decrypt_02a = ‘’
,@real_decrypt_03a = ‘’
,@real_decrypt_04a = ‘’
,@real_decrypt_05a = ‘’
– extract the encrypted imageval rows from sys.sysobjvalues
SELECT @real_01=substring(imageval,1,8000)
,@real_02=substring(imageval,8001,16000)
,@real_03=substring(imageval,16001,24000)
,@real_04=substring(imageval,24001,32000)
,@real_05=substring(imageval,32001,40000)
FROM sys.sysobjvalues
WHERE objid = object_id(@procedure) and valclass = 1 and subobjid = 1
– create this table for later use
create table #output ( [ident] [int] IDENTITY (1, 1) NOT NULL ,
[real_decrypt] NVARCHAR(MAX)
)
– We’ll begin the transaction and roll it back later
BEGIN TRAN
– alter the original procedure, replacing with dashes
SET @fake_01=‘ALTER PROCEDURE ‘+ @procedure +’ WITH ENCRYPTION AS
‘+REPLICATE(’-’, 40003 – @procNameLength)
EXECUTE (@fake_01)
– extract the encrypted fake imageval rows from sys.sysobjvalues
SELECT @fake_encrypt_01=substring(imageval,1,8000)
,@fake_encrypt_02=substring(imageval,8001,16000)
,@fake_encrypt_03=substring(imageval,16001,24000)
,@fake_encrypt_04=substring(imageval,24001,32000)
,@fake_encrypt_05=substring(imageval,32001,40000)
FROM sys.sysobjvalues
WHERE objid = object_id(@procedure) and valclass = 1 and subobjid = 1
SET @fake_01=‘CREATE PROCEDURE ‘+ @procedure +’ WITH ENCRYPTION AS ’
+ REPLICATE(’-’, 40003 – @procNameLength)
–start counter
SET @intProcSpace=1
–fill temporary variable with with a filler character
SET @real_decrypt_01 = replicate(N’A’, (datalength(@real_01) /2 ))
–loop through each of the variables sets of variables, building the real variable
–one byte at a time.
SET @intProcSpace=1
– Go through each @real_xx variable and decrypt it, as necessary
WHILE @intProcSpace<=(datalength(@real_01)/2)
BEGIN
–xor real & fake & fake encrypted
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
–one byte at a time.
SET @intProcSpace=1
– Go through each @real_xx variable and decrypt it, as necessary
WHILE @intProcSpace<=(datalength(@real_02)/2)
BEGIN
–xor real & fake & fake encrypted
SET @real_decrypt_02 = stuff(@real_decrypt_02, @intProcSpace, 1,
NCHAR(UNICODE(substring(@real_02, @intProcSpace, 1)) ^
(UNICODE(substring(@fake_02, @intProcSpace, 1)) ^
UNICODE(substring(@fake_encrypt_02, @intProcSpace, 1)))))
SET @intProcSpace=@intProcSpace+1
END
–one byte at a time.
SET @intProcSpace=1
– Go through each @real_xx variable and decrypt it, as necessary
WHILE @intProcSpace<=(datalength(@real_03)/2)
BEGIN
–xor real & fake & fake encrypted
SET @real_decrypt_03 = stuff(@real_decrypt_03, @intProcSpace, 1,
NCHAR(UNICODE(substring(@real_03, @intProcSpace, 1)) ^
(UNICODE(substring(@fake_03, @intProcSpace, 1)) ^
UNICODE(substring(@fake_encrypt_03, @intProcSpace, 1)))))
SET @intProcSpace=@intProcSpace+1
END
–one byte at a time.
SET @intProcSpace=1
– Go through each @real_xx variable and decrypt it, as necessary
WHILE @intProcSpace<=(datalength(@real_04)/2)
BEGIN
–xor real & fake & fake encrypted
SET @real_decrypt_04 = stuff(@real_decrypt_04, @intProcSpace, 1,
NCHAR(UNICODE(substring(@real_04, @intProcSpace, 1)) ^
(UNICODE(substring(@fake_04, @intProcSpace, 1)) ^
UNICODE(substring(@fake_encrypt_04, @intProcSpace, 1)))))
SET @intProcSpace=@intProcSpace+1
END
–one byte at a time.
SET @intProcSpace=1
– Go through each @real_xx variable and decrypt it, as necessary
WHILE @intProcSpace<=(datalength(@real_05)/2)
BEGIN
–xor real & fake & fake encrypted
SET @real_decrypt_05 = stuff(@real_decrypt_05, @intProcSpace, 1,
NCHAR(UNICODE(substring(@real_05, @intProcSpace, 1)) ^
(UNICODE(substring(@fake_05, @intProcSpace, 1)) ^
UNICODE(substring(@fake_encrypt_05, @intProcSpace, 1)))))
SET @intProcSpace=@intProcSpace+1
END
– Load the variables into #output for handling by sp_helptext logic
INSERT INTO #output (real_decrypt)
SELECT @real_decrypt_01
UNION ALL
SELECT @real_decrypt_02
UNION ALL
SELECT @real_decrypt_03
UNION ALL
SELECT @real_decrypt_04
UNION ALL
SELECT @real_decrypt_05
– select real_decrypt AS ‘#output chek’ from #output – Testing
– Beginning of extract from sp_helptext
declare @dbname sysname
,@BlankSpaceAdded int
,@BasePos int
,@CurrentPos int
,@TextLength int
,@LineId int
,@AddOnLen int
,@LFCR int –lengths of line feed carriage return
,@DefinedLength int
,@SyscomText nvarchar(4000)
,@Line nvarchar(255)
Select @DefinedLength = 255
SELECT @BlankSpaceAdded = 0 –Keeps track of blank spaces at end of lines. Note Len function ignores trailing blank spaces
CREATE TABLE #CommentText
(LineId int
,Text nvarchar(255) collate database_default)
– use #output instead of sys.sysobjvalues
DECLARE ms_crs_syscom CURSOR LOCAL
FOR SELECT real_decrypt
from #output
ORDER BY ident
FOR READ ONLY
– Else get the 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
–Looking for end of line followed by carriage return
SELECT @CurrentPos = CHARINDEX(char(13)+char(10), @SyscomText,
@BasePos)
–If carriage return found
IF @CurrentPos != 0
BEGIN
–If new value for @Lines length will be > then the
–set length then insert current contents of @line
–and proceed.
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
–else carriage return not found
BEGIN
IF @BasePos <= @TextLength
BEGIN
–If new value for @Lines length will be > then 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 of extract from sp_helptext
– Drop the procedure that was setup with dashes and rebuild it with the good stuff
– Version 1.1 mod; makes rebuilding hte proc unnecessary
ROLLBACK TRAN
DROP TABLE #output
执行结果如下:
2000版本的存储过程
CREATE PROCEDURE sp_decrypt(@objectName varchar(50))
AS
begin
begin tran
declare @objectname1 varchar(100),@orgvarbin varbinary(8000)
declare @sql1 nvarchar(4000),@sql2 nvarchar(4000),@sql3 nvarchar(4000),@sql4 nvarchar(4000),@sql5 nvarchar(4000),@sql6 nvarchar(4000),@sql7 nvarchar(4000),@sql8 nvarchar(4000),@sql9 nvarchar(4000),@sql10 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 @k<=@number
begin
if exists(select 1 from syscomments where id=object_id(@objectname) and number=@k)
begin
if @type=‘P’
set @sql1=(case when @number>1 then ‘ALTER PROCEDURE ‘+ @objectName +’;’+rtrim(@k)+’ WITH ENCRYPTION AS ’
else ‘ALTER PROCEDURE ‘+ @objectName+’ WITH ENCRYPTION AS ’
end)
if @type=‘TR’
set @sql1=’ALTER TRIGGER ‘+@objectname+’ ON ‘+OBJECT_NAME(@parentid)+’ WITH ENCRYPTION FOR INSERT AS PRINT 1 ’
if @type=‘FN’ or @type=‘TF’ or @type=‘IF’
set @sql1=(case @type when ‘TF’ then
‘ALTER FUNCTION ‘+ @objectName+’(@a char(1)) returns @b table(a varchar(10)) with encryption as begin insert @b select @a return end ’
when ‘FN’ then
‘ALTER FUNCTION ‘+ @objectName+’(@a char(1)) returns char(1) with encryption as begin return @a end’
when ‘IF’ then
‘ALTER FUNCTION ‘+ @objectName+’(@a char(1)) returns table with encryption as return select @a as a’
end)
if @type=‘V’
set @sql1=’ALTER VIEW ‘+@objectname+’ WITH ENCRYPTION AS SELECT 1 ’
set @q=len(@sql1)
set @sql1=@sql1+REPLICATE(’-’,4000-@q)
select @sql2=REPLICATE(’-’,4000),@sql3=REPLICATE(’-’,4000),@sql4=REPLICATE(’-’,4000),@sql5=REPLICATE(’-’,4000),@sql6=REPLICATE(’-’,4000),@sql7=REPLICATE(’-’,4000),@sql8=REPLICATE(’-’,4000),@sql9=REPLICATE(’-’,4000),@sql10=REPLICATE(’-’,4000)
exec(@sql1+@sql2+@sql3+@sql4+@sql5+@sql6+@sql7+@sql8+@sql9+@sql10)
end
set @k=@k+1
end
set @k=0
while @k<=@number
begin
if exists(select 1 from syscomments where id=object_id(@objectname) and number=@k)
begin
select @colid=max(colid) from #temp where number=@k
set @n=1
while @n<=@colid
begin
select @OrigSpText1=ctext,@encrypted=encrypted,@status=status FROM #temp WHERE colid=@n and number=@k
SET @OrigSpText3=(SELECT ctext FROM syscomments WHERE id=object_id(@objectName) and colid=@n and number=@k)
if @n=1
begin
if @type=‘P’
SET @OrigSpText2=(case when @number>1 then ‘CREATE PROCEDURE ‘+ @objectName +’;’+rtrim(@k)+’ WITH ENCRYPTION AS ’
else ‘CREATE PROCEDURE ‘+ @objectName +’ WITH ENCRYPTION AS ’
end)
if @type=‘FN’ or @type=‘TF’ or @type=‘IF’–刚才有错改一下
SET @OrigSpText2=(case @type when ‘TF’ then
‘CREATE FUNCTION ‘+ @objectName+’(@a char(1)) returns @b table(a varchar(10)) with encryption as begin insert @b select @a return end ’
when ‘FN’ then
‘CREATE FUNCTION ‘+ @objectName+’(@a char(1)) returns char(1) with encryption as begin return @a end’
when ‘IF’ then
‘CREATE FUNCTION ‘+ @objectName+’(@a char(1)) returns table with encryption as return select @a as a’
end)
if @type=‘TR’
set @OrigSpText2=’CREATE TRIGGER ‘+@objectname+’ ON ‘+OBJECT_NAME(@parentid)+’ WITH ENCRYPTION FOR INSERT AS PRINT 1 ’
if @type=‘V’
set @OrigSpText2=’CREATE VIEW ‘+@objectname+’ WITH ENCRYPTION AS SELECT 1 ’
set @q=4000-len(@OrigSpText2)
set @OrigSpText2=@OrigSpText2+REPLICATE(’-’,@q)
end
else
begin
SET @OrigSpText2=REPLICATE(’-’, 4000)
end
–start counter
SET @i=1
–fill temporary variable
SET @resultsp = replicate(N’A’, (datalength(@OrigSpText1) / 2))
–loop
WHILE @i<=datalength(@OrigSpText1)/2
BEGIN
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
set @orgvarbin=cast(@OrigSpText1 as varbinary(8000))
set @resultsp=(case when @encrypted=1
then @resultsp
else convert(nvarchar(4000),case when @status&2=2 then uncompress(@orgvarbin) else @orgvarbin end)
end)
print @resultsp
–execute( @resultsp)
set @n=@n+1
end
end
set @k=@k+1
end
drop table #temp
rollback tran
end
GO