if OBJECT_ID('sp_decrypt_7', 'P') IS NOT NULL
  drop procedure sp_decrypt_7
GO

-- Decrypts SQL Server 7 stored procedures created with 'encryption' option
-- Runs only on SQL Server 2000 due to bigint datatype usage
create procedure sp_decrypt_7
  @enctring varbinary(8000)
as
set nocount on

-- Reversed by jimmers (Martin Rakhmanoff)
-- 12:58 PM 6/26/2002
-- jimmers@yandex.ru
-- http://jimmers.narod.ru

-- This is 'magic' data used for crypting
-- May be found in sqlservr.exe 7/2000 image
declare @magic table (id int identity(0,1) primary key not null, val int)
declare @output table (id int identity(0,1) primary key not null, val int)

insert into @magic values (0x00000000)
insert into @magic values (0x77073096)
insert into @magic values (0x0EE0E612C)
insert into @magic values (0x990951BA)
insert into @magic values (0x76DC419)
insert into @magic values (0x706AF48F)
insert into @magic values (0x0E963A535)
insert into @magic values (0x9E6495A3)
insert into @magic values (0x0EDB8832)
insert into @magic values (0x79DCB8A4)
insert into @magic values (0x0E0D5E91E)
insert into @magic values (0x97D2D988)
insert into @magic values (0x9B64C2B)
insert into @magic values (0x7EB17CBD)
insert into @magic values (0x0E7B82D07)
insert into @magic values (0x90BF1D91)
insert into @magic values (0x1DB71064)
insert into @magic values (0x6AB020F2)
insert into @magic values (0x0F3B97148)
insert into @magic values (0x84BE41DE)
insert into @magic values (0x1ADAD47D)
insert into @magic values (0x6DDDE4EB)
insert into @magic values (0x0F4D4B551)
insert into @magic values (0x83D385C7)
insert into @magic values (0x136C9856)
insert into @magic values (0x646BA8C0)
insert into @magic values (0x0FD62F97A)
insert into @magic values (0x8A65C9EC)
insert into @magic values (0x14015C4F)
insert into @magic values (0x63066CD9)
insert into @magic values (0x0FA0F3D63)
insert into @magic values (0x8D080DF5)
insert into @magic values (0x3B6E20C8)
insert into @magic values (0x4C69105E)
insert into @magic values (0x0D56041E4)
insert into @magic values (0x0A2677172)
insert into @magic values (0x3C03E4D1)
insert into @magic values (0x4B04D447)
insert into @magic values (0x0D20D85FD)
insert into @magic values (0x0A50AB56B)
insert into @magic values (0x35B5A8FA)
insert into @magic values (0x42B2986C)
insert into @magic values (0x0DBBBC9D6)
insert into @magic values (0x0ACBCF940)
insert into @magic values (0x32D86CE3)
insert into @magic values (0x45DF5C75)
insert into @magic values (0x0DCD60DCF)
insert into @magic values (0x0ABD13D59)
insert into @magic values (0x26D930AC)
insert into @magic values (0x51DE003A)
insert into @magic values (0x0C8D75180)
insert into @magic values (0x0BFD06116)
insert into @magic values (0x21B4F4B5)
insert into @magic values (0x56B3C423)
insert into @magic values (0x0CFBA9599)
insert into @magic values (0x0B8BDA50F)
insert into @magic values (0x2802B89E)
insert into @magic values (0x5F058808)
insert into @magic values (0x0C60CD9B2)
insert into @magic values (0x0B10BE924)
insert into @magic values (0x2F6F7C87)
insert into @magic values (0x58684C11)
insert into @magic values (0x0C1611DAB)
insert into @magic values (0x0B6662D3D)
insert into @magic values (0x76DC4190)
insert into @magic values (0x1DB7106)
insert into @magic values (0x98D220BC)
insert into @magic values (0x0EFD5102A)
insert into @magic values (0x71B18589)
insert into @magic values (0x6B6B51F)
insert into @magic values (0x9FBFE4A5)
insert into @magic values (0x0E8B8D433)
insert into @magic values (0x7807C9A2)
insert into @magic values (0x0F00F934)
insert into @magic values (0x9609A88E)
insert into @magic values (0x0E10E9818)
insert into @magic values (0x7F6A0DBB)
insert into @magic values (0x86D3D2D)
insert into @magic values (0x91646C97)
insert into @magic values (0x0E6635C01)
insert into @magic values (0x6B6B51F4)
insert into @magic values (0x1C6C6162)
insert into @magic values (0x856530D8)
insert into @magic values (0x0F262004E)
insert into @magic values (0x6C0695ED)
insert into @magic values (0x1B01A57B)
insert into @magic values (0x8208F4C1)
insert into @magic values (0x0F50FC457)
insert into @magic values (0x65B0D9C6)
insert into @magic values (0x12B7E950)
insert into @magic values (0x8BBEB8EA)
insert into @magic values (0x0FCB9887C)
insert into @magic values (0x62DD1DDF)
insert into @magic values (0x15DA2D49)
insert into @magic values (0x8CD37CF3)
insert into @magic values (0x0FBD44C65)
insert into @magic values (0x4DB26158)
insert into @magic values (0x3AB551CE)
insert into @magic values (0x0A3BC0074)
insert into @magic values (0x0D4BB30E2)
insert into @magic values (0x4ADFA541)
insert into @magic values (0x3DD895D7)
insert into @magic values (0x0A4D1C46D)
insert into @magic values (0x0D3D6F4FB)
insert into @magic values (0x4369E96A)
insert into @magic values (0x346ED9FC)
insert into @magic values (0x0AD678846)
insert into @magic values (0x0DA60B8D0)
insert into @magic values (0x44042D73)
insert into @magic values (0x33031DE5)
insert into @magic values (0x0AA0A4C5F)
insert into @magic values (0x0DD0D7CC9)
insert into @magic values (0x5005713C)
insert into @magic values (0x270241AA)
insert into @magic values (0x0BE0B1010)
insert into @magic values (0x0C90C2086)
insert into @magic values (0x5768B525)
insert into @magic values (0x206F85B3)
insert into @magic values (0x0B966D409)
insert into @magic values (0x0CE61E49F)
insert into @magic values (0x5EDEF90E)
insert into @magic values (0x29D9C998)
insert into @magic values (0x0B0D09822)
insert into @magic values (0x0C7D7A8B4)
insert into @magic values (0x59B33D17)
insert into @magic values (0x2EB40D81)
insert into @magic values (0x0B7BD5C3B)
insert into @magic values (0x0C0BA6CAD)
insert into @magic values (0x0EDB88320)
insert into @magic values (0x9ABFB3B6)
insert into @magic values (0x3B6E20C)
insert into @magic values (0x74B1D29A)
insert into @magic values (0x0EAD54739)
insert into @magic values (0x9DD277AF)
insert into @magic values (0x4DB2615)
insert into @magic values (0x73DC1683)
insert into @magic values (0x0E3630B12)
insert into @magic values (0x94643B84)
insert into @magic values (0x0D6D6A3E)
insert into @magic values (0x7A6A5AA8)
insert into @magic values (0x0E40ECF0B)
insert into @magic values (0x9309FF9D)
insert into @magic values (0x0A00AE27)
insert into @magic values (0x7D079EB1)
insert into @magic values (0x0F00F9344)
insert into @magic values (0x8708A3D2)
insert into @magic values (0x1E01F268)
insert into @magic values (0x6906C2FE)
insert into @magic values (0x0F762575D)
insert into @magic values (0x806567CB)
insert into @magic values (0x196C3671)
insert into @magic values (0x6E6B06E7)
insert into @magic values (0x0FED41B76)
insert into @magic values (0x89D32BE0)
insert into @magic values (0x10DA7A5A)
insert into @magic values (0x67DD4ACC)
insert into @magic values (0x0F9B9DF6F)
insert into @magic values (0x8EBEEFF9)
insert into @magic values (0x17B7BE43)
insert into @magic values (0x60B08ED5)
insert into @magic values (0x0D6D6A3E8)
insert into @magic values (0x0A1D1937E)
insert into @magic values (0x38D8C2C4)
insert into @magic values (0x4FDFF252)
insert into @magic values (0x0D1BB67F1)
insert into @magic values (0x0A6BC5767)
insert into @magic values (0x3FB506DD)
insert into @magic values (0x48B2364B)
insert into @magic values (0x0D80D2BDA)
insert into @magic values (0x0AF0A1B4C)
insert into @magic values (0x36034AF6)
insert into @magic values (0x41047A60)
insert into @magic values (0x0DF60EFC3)
insert into @magic values (0x0A867DF55)
insert into @magic values (0x316E8EEF)
insert into @magic values (0x4669BE79)
insert into @magic values (0x0CB61B38C)
insert into @magic values (0x0BC66831A)
insert into @magic values (0x256FD2A0)
insert into @magic values (0x5268E236)
insert into @magic values (0x0CC0C7795)
insert into @magic values (0x0BB0B4703)
insert into @magic values (0x220216B9)
insert into @magic values (0x5505262F)
insert into @magic values (0x0C5BA3BBE)
insert into @magic values (0x0B2BD0B28)
insert into @magic values (0x2BB45A92)
insert into @magic values (0x5CB36A04)
insert into @magic values (0x0C2D7FFA7)
insert into @magic values (0x0B5D0CF31)
insert into @magic values (0x2CD99E8B)
insert into @magic values (0x5BDEAE1D)
insert into @magic values (0x9B64C2B0)
insert into @magic values (0x0EC63F226)
insert into @magic values (0x756AA39C)
insert into @magic values (0x26D930A)
insert into @magic values (0x9C0906A9)
insert into @magic values (0x0EB0E363F)
insert into @magic values (0x72076785)
insert into @magic values (0x5005713)
insert into @magic values (0x95BF4A82)
insert into @magic values (0x0E2B87A14)
insert into @magic values (0x7BB12BAE)
insert into @magic values (0x0CB61B38)
insert into @magic values (0x92D28E9B)
insert into @magic values (0x0E5D5BE0D)
insert into @magic values (0x7CDCEFB7)
insert into @magic values (0x0BDBDF21)
insert into @magic values (0x86D3D2D4)
insert into @magic values (0x0F1D4E242)
insert into @magic values (0x68DDB3F8)
insert into @magic values (0x1FDA836E)
insert into @magic values (0x81BE16CD)
insert into @magic values (0x0F6B9265B)
insert into @magic values (0x6FB077E1)
insert into @magic values (0x18B74777)
insert into @magic values (0x88085AE6)
insert into @magic values (0x0FF0F6A70)
insert into @magic values (0x66063BCA)
insert into @magic values (0x11010B5C)
insert into @magic values (0x8F659EFF)
insert into @magic values (0x0F862AE69)
insert into @magic values (0x616BFFD3)
insert into @magic values (0x166CCF45)
insert into @magic values (0x0A00AE278)
insert into @magic values (0x0D70DD2EE)
insert into @magic values (0x4E048354)
insert into @magic values (0x3903B3C2)
insert into @magic values (0x0A7672661)
insert into @magic values (0x0D06016F7)
insert into @magic values (0x4969474D)
insert into @magic values (0x3E6E77DB)
insert into @magic values (0x0AED16A4A)
insert into @magic values (0x0D9D65ADC)
insert into @magic values (0x40DF0B66)
insert into @magic values (0x37D83BF0)
insert into @magic values (0x0A9BCAE53)
insert into @magic values (0x0DEBB9EC5)
insert into @magic values (0x47B2CF7F)
insert into @magic values (0x30B5FFE9)
insert into @magic values (0x0BDBDF21C)
insert into @magic values (0x0CABAC28A)
insert into @magic values (0x53B39330)
insert into @magic values (0x24B4A3A6)
insert into @magic values (0x0BAD03605)
insert into @magic values (0x0CDD70693)
insert into @magic values (0x54DE5729)
insert into @magic values (0x23D967BF)
insert into @magic values (0x0B3667A2E)
insert into @magic values (0x0C4614AB8)
insert into @magic values (0x5D681B02)
insert into @magic values (0x2A6F2B94)
insert into @magic values (0x0B40BBE37)
insert into @magic values (0x0C30C8EA1)
insert into @magic values (0x5A05DF1B)
insert into @magic values (0x2D02EF8D)

-- This is 'worker set' - changed every cycle
declare @worker1 bigint, @worker2 bigint, @worker3 bigint
-- Initialize it...
set @worker1 = 0x37C5D22A
set @worker2 = 0x77BE9B9C
set @worker3 = 0x36456178

declare @decstring nvarchar(4000), @length int, @i int, @val bigint, @result
bigint

set @decstring = ''
set @length = len(@enctring)

if NOT (@length>2)
  goto errorhandler

set @i = 1

while (@i<=@length)
begin

  select @val = cast(substring(@enctring, @i, 1) as int)

  set @result = @worker3 & 0xFFFF
  set @result = @result | 0x2
  set @result = @result * (@result ^ 1)
  set @result = @result / 0x100
  set @result = @result ^ @val
  set @result = @result & 0xff

  insert @output values (@result)

  select @val = val from @magic where id = (@worker1 ^ @result) & 0xFF
  set @worker1 = @worker1 / 256
  set @worker1 = @worker1 ^ @val
  set @worker1 = @worker1 & 0xffffffff

  set @worker2 = @worker2 + (@worker1 & 0xFF)
  set @worker2 = (@worker2 * 0x8088405) & 0xffffffff
  set @worker2 = @worker2 + 1

  select @val = val from @magic where id = (@worker3 ^ (@worker2 /16777216)) & 0xFF
  set @worker3 = @worker3 / 256
  set @worker3 = @worker3 ^ @val
  set @worker3 = @worker3 & 0xffffffff

  set @i = @i + 1
end

select @decstring = @decstring + NCHAR(256* o2.val + o1.val)
  from @output o1
 inner join @output o2
    on o1.id = o2.id - 1
 where o1.id % 2 = 0

select @decstring

return 0

errorhandler:
  raiserror 70000 'Expected input in ''hexadecmal'' style, e.g. 0x65B9E12C1B99BB041B66F173F56A'
  return 1
GO


EXEC sp_decrypt_7 0x4CAFD1771AB283A69C1A88839C43BA3B7B4BDB5D512B46EC64983D5A1C0BD5962E4BDF5F6CD1B5E3A170A03E338E0CCDBE1373F0B560769930797B2613F21AE8E25325C16E7CC211EFF12100EB18090079C325A8F321F55856510587C79ED7D45FF3CD5BF5529ECE6C5AF12F38D8DF23EF3D7FEC40A39425E0315ABF0D40
GO