Wednesday, August 5, 2020

PyQT5 and wxPython Implimentation of ROT13

As stated on this wikipedia page, ROT13 ("rotate by 13 places", sometimes hyphenated ROT-13) is a simple letter substitution cipher (a secret or disguised way of writing) that replaces a letter with the 13th letter after it, in the alphabet.

There is an implimentation in javascript at as seen below. Here on this blog we will walk through implimenting it in python desktop GUI (both PyQT5 and wxPython).

Reviewing where rot13 was used in python
The same secret or disguised way of writing is behind the python built-in module "The Zen of Python, by Tim Peters" (import this).

When you run/import the 'this' module for the first time, it display some strings as seen below.

If you access the location of the 'this' module and open it in text editor, you will find a abstract "s" string variable. So where is the beautiful and explicit text above coming from?

s = """Gur Mra bs Clguba, ol Gvz Crgref

Ornhgvshy vf orggre guna htyl.
Rkcyvpvg vf orggre guna vzcyvpvg.
Fvzcyr vf orggre guna pbzcyrk.
Pbzcyrk vf orggre guna pbzcyvpngrq.
Syng vf orggre guna arfgrq.
Fcnefr vf orggre guna qrafr.
Ernqnovyvgl pbhagf.
Fcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf.
Nygubhtu cenpgvpnyvgl orngf chevgl.
Reebef fubhyq arire cnff fvyragyl.
Hayrff rkcyvpvgyl fvyraprq.
Va gur snpr bs nzovthvgl, ershfr gur grzcgngvba gb thrff.
Gurer fubhyq or bar-- naq cersrenoyl bayl bar --boivbhf jnl gb qb vg.
Nygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh'er Qhgpu.
Abj vf orggre guna arire.
Nygubhtu arire vf bsgra orggre guna *evtug* abj.
Vs gur vzcyrzragngvba vf uneq gb rkcynva, vg'f n onq vqrn.
Vs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn.
Anzrfcnprf ner bar ubaxvat terng vqrn -- yrg'f qb zber bs gubfr!"""

d = {}
for c in (65, 97):
    for i in range(26):
        d[chr(i+c)] = chr((i+13) % 26 + c)

print("".join([d.get(c, c) for c in s]))

It turns out that the module uses the rot13 algorithm to display beautiful text on the front end. This is easily verified by copy the string into as shown below.

ROT13 Desktop App
Lets make this a GUI desktop app using python, so we don't have to be online anytime we want to cipher some written text.

First things first, lets get the algorithm to run as a script.

The Rot13 algorithm in python can be written in several ways. For example, in the (import this) module, a nexted 'for loop' was used as seen above.

However, in this guide I will make use of the python string method maketrans() that returns a mapping table for translation usable for translate() method. Read more on this page.

The script should look like this below:-

rot13trans = str.maketrans('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm')

raw_text = 'Hello'
rot13_result = raw_text.translate(rot13trans)

A completely different approach use in the "this module" is seen below:-

s = 'Hello'

d = {}
for c in (65, 97):
    for i in range(26):
        d[chr(i+c)] = chr((i+13) % 26 + c)

print ("".join([d.get(c, c) for c in s]))


You can find the complete source code on this github page.

That is it!

No comments:

Post a Comment