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