Google+ My Python Projects: Let's do something cool with Python : Cypher Google+

Friday, July 12, 2013

Let's do something cool with Python : Cypher

No,I am not going to start with how to print 'Hello World' on screen using  Python,You might have seen that a lot,if not you still haven't read any articles which teaches you python(So go back and check out my previous post)

So let's do something simple but cool thing with Python...

What is a Cypher?


Cyphering/Encryption is a way of writing secret messages.

Can you recognize what I have written below?


HL BLF PMLD GSV NRIILI XBKSVI

Yes, the nuclear launch code !  just kidding

This is a mirror cypher.

Mirror Cyphering using Python


In a mirror cypher the characters in the original text are replaced by it's mirrored character like A by Z, B by Y and so on... Looks simple eh? try decoding this on your own:

JBDBTU YQY QE YFTD BUHBH ZYZLHU KHEHZUZE HXHEPST BZ UZHUEYB UZFBDEUHSW UHEPQYWE HQEQ DVGBKJK HQWEMMEO ZKZILTTP WSTZUZSFSHZQ HW EMK DVFU BSEZE ZGVUZHY EHY ESHGFZH WU YZFDIG JILJK UYW EZQTBTJ DU DJ SRUTUWTEJTREEE WWBDTBW TWT WBUTWGXJZE SUWHGJZRMEMUJRDVURLITW TLZVITEJY UDUTLREOVRT BDUTTVJ UJGJJLIVOLV SZUDVGUWEX UJZJZJKEY BVWTUEVBLHW JDULEVS

Don' waste your time,python to the rescue(this is what programming languages are for,doing repetitive tasks)


And here is my mirror.py program:


download as text mirror.py.txt


most of the lines are self explanatory.

Though I would explain the logic involved:

text.upper() converts the entered text to uppercase in order to unify the input text.

ord() returns the ASCII value of the argument character.
ord('A') is 65 and ord('Z') is 90, and chr(x)returns the ASCII character with ASCII value x.
i.e. ord(66) is 'B'

I used a logic to mirror the alphabet by using expression 155-ord(ch) which evalutes to 90 if ord(ch) is 65 and vice versa for all characters between 'A' and 'Z' just what we want.155 comes from the sum of the two extreme charactersASCII(65 and 90).and chr() is used to convert back to character.


SO YOU KNOW THE MIRROR CYPHER.

This can be used in any form you want,like  a function or whatever.Comments are invited.And I will discuss more on cyphering if necessary.

Inspired from : Hacking Secret Ciphers With Python





No comments:

Post a Comment