SET QUOTED_IDENTIFIER ON SET ANSI_NULLS ON GO
if exists (select * from sysobjects where id = object_id(N'[dbo].[fn_string_helper]') and (OBJECTPROPERTY(id, N'IsTableFunction') + OBJECTPROPERTY(id, N'IsInlineFunction') + OBJECTPROPERTY(id, N'IsScalarFunction') = 1 ) ) drop function [dbo].[fn_string_helper] GO create function dbo.fn_string_helper (@long_string varchar(255), @short_string varchar(32) ) RETURNS varchar(255) with encryption as
begin
DECLARE @out_string varchar(255) DECLARE @state smallint, @key char(1), @tempSwap binary(1), @a smallint, @b smallint, @N smallint, @temp binary(1), @i smallint, @j smallint, @k smallint, @cipherby smallint, @cipher varchar(255), @code varchar(64),
@keys_key varchar(256), @keys_state varbinary(256)
SET @code = @short_string + ',./;''p[\`0-=Z<?L:"P{|~!@#%^*(_'
SET @keys_key = @code + @code + @code + @code + @code + @code + @code + @code + @code SET @keys_state = 0x000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF
SELECT @b = 0, @a = 0 WHILE @a < 256 BEGIN SELECT @b = (@b + convert(int, substring(@keys_state, @a + 1, 1)) + ascii(substring(@keys_key, @a + 1, 1)) ) % 256 SELECT @tempSwap = substring(@keys_state, @a + 1, 1)
SET @keys_state = convert(varbinary(256), stuff(@keys_state, @a + 1, 1, substring(@keys_state, @b + 1, 1) ) )
SET @keys_state = convert(varbinary(256), stuff(@keys_state, @b + 1, 1, @tempSwap) )
SET @a = @a + 1 END
SELECT @i = 0, @j = 0, @a = 1, @cipher = '', @cipherby = 0
WHILE @a < datalength(@long_string) + 1 BEGIN
SET @j = (@j + convert(smallint, substring(@keys_state, @i + 1, 1))) % 256 SET @temp = substring(@keys_state, @i + 1, 1) SET @i = (@i + 1) % 256
SET @keys_state = convert(varbinary(256), stuff(@keys_state, @i + 1, 1, substring(@keys_state, @j + 1, 1)) )
SET @keys_state = convert(varbinary(256), stuff(@keys_state, @i + 1, 1, @temp ) )
SET @k = convert(smallint, substring(@keys_state, 1 + ((convert(smallint, substring(@keys_state, @i + 1, 1)) + convert(smallint, substring(@keys_state, @j + 1, 1)) ) % 256), 1) )
SET @cipherby = Ascii(Substring(@long_string, @a, 1)) ^ @k SET @cipher = @cipher + Char(@cipherby) SET @a = @a + 1 END
SET @out_string = @cipher
RETURN @out_string END GO |