The SQL Hammer

When You're A Hammer, Everything Looks Like A Nail

<November 2008>
SuMoTuWeThFrSa
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456


Navigation

Subscriptions



ROT13 function

I've been reading Neal Stephenson's Cryptonomicon which has been pretty cool since it uses a pen and paper cipher using a deck of cards.  An appendix from Bruce Schneier discusses how he came up with the algorithm for the book.  It's explained pretty well here.

Since people of every have contributed algorithms in virtually every permutation of code, including K (WTF? -- actually, it looks kinda cool, but don't go Torquamada on me just because I think it's a novel approach), I thought I'd have a go at it in T-SQL. Well, five hours later, I have something in the code that isn't quite right, but I wanted to get something on the blog anyway. So, my best 10 minute effort is a ROT13 encoding/decoding function.

Stop laughing.

Besides, a cursory look at Google didn't reveal anything that someone else has been done before. Hey, just because it isn't entirely practical doesn't mean that somebody shouldn't do it. Like this example.

Anyway, since this wouldn't be considered "real security" (whereby the oppenent knows what your using the encode your data, but doesn't have the means to decode it) and would rather be "security through obfuscation" (i.e. if your opponent knows how you encoded your data, they can break the code), this could be somewhat practical in cases where you wouldn't want a casual look over your shoulder to reveal the data.

Maybe you have SQL authenticated passwords in a text file? We tend to change out passwords used by web applications every few months. Sure, the password file is in a secured location, but this goes a little further in helping obscure the data without being too limiting or imposing.

Whatever.

Here's the code, and in respects to the passing of one of the greatest comedians of all time yesterday:

V pna'g trg ab erfcrpg!

----------------- code below -----------

CREATE FUNCTION ufn_Rot13 (@phrase varchar(8000))
 RETURNS varchar(8000)
AS
BEGIN
 DECLARE @j int,      --// loop iterator
   @phraseOUT varchar(8000),  --// function output
   @letter int     --// ascii letter
  
  --// initializing variables
 SET @j = 1
 SET @phraseOUT = ''

  --// main iterator through each letter of input phrase
 WHILE @j <= len(@phrase) BEGIN
   --// pick out letter
  SET @letter = ASCII(SUBSTRING(@phrase, @j, 1))

   -- // rotate letter 13 places in either upper or lower case ascii range and add to output variable
  SELECT @phraseOUT = @phraseOUT + CASE
        WHEN @letter BETWEEN 97 AND 122 THEN CASE
                 WHEN @letter - 13 < 97 THEN char((@letter - 13) + 122 - 96)
                 ELSE char(@letter - 13) END
        WHEN @letter BETWEEN 65 AND 90 THEN CASE
                 WHEN @letter - 13 < 65 THEN char((@letter - 13) + 90 - 64)
                 ELSE char(@letter - 13) END
        ELSE CHAR(@letter)
        END 
  SET @j = @j + 1
 END
 RETURN @phraseOUT
END
 
go

posted on Wednesday, October 06, 2004 5:42 AM by aegix


# re:ROT13 function @ Monday, April 11, 2005 5:35 AM

^_^,Pretty Good!

aegix

# re:ROT13 function @ Monday, April 11, 2005 9:44 PM

^_^,Pretty Good!

aegix

# re:ROT13 function @ Tuesday, April 12, 2005 9:37 PM

^_^,Pretty Good!

aegix

# re:ROT13 function @ Friday, April 15, 2005 11:22 AM

^_^,Pretty Good!

aegix

# re:ROT13 function @ Tuesday, May 17, 2005 1:13 AM

^_~,pretty good!csharpsseeoo

aegix

# re:ROT13 function @ Tuesday, May 17, 2005 1:33 AM

^_~,pretty good!csharpsseeoo

aegix




Powered by Dot Net Junkies, by Telligent Systems