Pna lbh ernq guvf zrffntr?

Some fun with ROT13 in Python and T-SQL

Simone Rigoni
3 min readJul 23, 2023
https://en.wikipedia.org/wiki/ROT13#/media/File:ROT13_table_with_example.svg

Just for fun I wanted write all this article encoded with ROT13 but then i thought it was a bit too much.

In the realm of cryptography, ROT13 (Rotate by 13 places) is a classic and straightforward encryption technique. It is a simple implementation of the Caesar cipher, which involves shifting each letter in the alphabet by a fixed number of positions.

In Python we can use ROT13 leveraging the codecs library:

import codecs

message = 'Can you read this message?'

message_encoded = codecs.encode(message, 'rot_13')
print(message_encoded)

message_decoded = codecs.decode(message_encoded, 'rot_13')
print(message_decoded)

# Applying ROT13 to a string encoded with ROT13 we obtain the original string
message_encoded_encoded = codecs.encode(message_encoded, 'rot_13')
print(message_encoded_encoded)
Python ROT 13 examples execution

Just for fun let’s implement it from scratch:

def encode_rotation_13_cipher(message):
'''
Encode a message in ROT13

Arguments:
message (str): message to encode
Returns:
message_encoded (str): message encoded
'''
alphabet = string.ascii_lowercase
message_encoded = ''

for character in message:
character_lower = character.lower()
if character_lower in alphabet:
character_encoded = alphabet[(alphabet.find(character_lower) + 13) % (13 * 2)]

if character.isupper():
message_encoded = message_encoded + character_encoded.upper()
else:
message_encoded = message_encoded + character_encoded
else:
message_encoded = message_encoded + character

return message_encoded


def decode_rotation_13_cipher(message_encoded):
'''
Decode a message in ROT13. Applying ROT13 to a string encoded with ROT13 we obtain the original string

Arguments:
message_encoded (str): message to decode
Returns:
message_decoded (str): message decoded
'''
return encode_rotation_13_cipher(message_encoded)


message = 'This is funny stuff'

message_encoded = encode_rotation_13_cipher(message)
print(message_encoded)

print(decode_rotation_13_cipher(message_encoded))
Python ROT 13 examples execution

Now let’s do it in T-SQL:

DROP FUNCTION IF EXISTS  dbo.ufn_ROT13;
GO

CREATE FUNCTION dbo.ufn_ROT13 (@message VARCHAR(1000))
RETURNS VARCHAR(1000)
AS
BEGIN
DECLARE @message_encoded VARCHAR(1000) = '';
DECLARE @length INT = LEN(@message);
DECLARE @i INT = 1;

WHILE @i <= @length
BEGIN
DECLARE @char CHAR(1) = SUBSTRING(@message, @i, 1);
DECLARE @char_ascii INT = ASCII(@char);

IF @char_ascii BETWEEN ASCII('a') AND ASCII('z')
SET @char = CHAR(((@char_ascii - ASCII('a') + 13) % (13 * 2)) + ASCII('a'));
ELSE IF @char_ascii BETWEEN ASCII('A') AND ASCII('Z')
SET @char = CHAR(((@char_ascii - ASCII('A') + 13) % (13 * 2)) + ASCII('A'));

SET @message_encoded += @char;
SET @i += 1;
END

RETURN @message_encoded;
END
;
GO

DECLARE @message VARCHAR(1000) = 'What is the Answer to Life, the Universe and Everything?';

DECLARE @message_encoded VARCHAR(1000) = dbo.ufn_ROT13(@message);
SELECT @message_encoded AS encoded_message;

DECLARE @message_encoded_encoded VARCHAR(1000) = dbo.ufn_ROT13(@message_encoded);
SELECT @message_encoded_encoded AS encoded_encoded_message;
T-SQL ROT 13 examples execution

Of course the answer is 42.

In conclusion ROT13 is a simple Caesar cipher variant. While it may not provide high-level security, it remains a fascinating historical encryption technique with various applications, particularly in text obfuscation. It is very interesting the fact that ROT13 is self-inverse, meaning that applying the ROT13 algorithm to an already ROT13-encoded text will decrypt it back to the original text.

Outro

I hope the story was interesting and thank you for taking the time to read it. On my Blogspot you can find the same post in Italian. Let me know if you have any question and if you like the content that I create feel free to buy me a coffee.

--

--

Simone Rigoni
Simone Rigoni

No responses yet