Subject: The Corporal's Cypher - decoder program

Posted on: October 01 2011 @ 09:15 AM
By: wvf

Content:

Hello, my comrades in arms! (Especially you mutants!) I've developed a small program, written in Python, to help decode the cyphers we've been seeing between Corporal Punishment and Sgt. Stern.

For instance, given

PHP Formatted Code
SEFUB ZRVD BQGZSKBDBH PBFFEJB KBDB,
CVU OBEWB LQ UKB TVRUEULRQ PEDNF.
and the partial cypher
PHP Formatted Code
ZEBR SC FGHIJKLMNOPQTUVWXY
we get back
PHP Formatted Code
Missing characters in cipher: AD
PeSTE YOUR ENCYPHEREh MESSeGE HERE,
BUT LEeVE IN THE QUOTeTION MeRKS.

The capitals are ciphered output, the lower-case letters are unciphered output. Can you guess where the missing letters go? Razz

Here is the python file, cipher.py. It runs on python 2. Enjoy. Post questions here.
PHP Formatted Code
#cipher.py translates incomplete ciphers. For python 2. (Released under gnu gpl by David D. Roberts on 2011. Contact: email at robertsdavidddr@gmail.com)
#run command in console: "python cipher.py" (Without the quotation marks.)
src =   "ZEBR SC FGHIJKLMNOPQTUVWXY" #You put the normal alphabet on the top to encipher. Reverse to decipher. Leave spaces in src for unknown letters. src is what the message is currently in, dst is what you want it to be.
dst =   "ABCDEFGHIJKLMNOPQRSTUVWXYZ" #These two strings must be equal length.

message = """

SEFUB ZRVD BQGZSKBDBH PBFFEJB KBDB,
CVU OBEWB LQ UKB TVRUEULRQ PEDNF.

"
"" #This, above, is what gets translated.

lacking = ''
for character in dst:
        if not character.upper() in src.upper():
                lacking += character
if len(lacking) > 0:
        print "Missing characters in cipher:", lacking, "\n"

def cipher(char):
        if not char.isalpha(): #string not cypherable
                return char
        letter = dst.upper().find(char.upper()) #String not in destination. (incomplete alphabet)
        if letter == -1:
                return '*'
        letter = src[letter:letter+1]
        if letter == ' ':
                return char.lower()
        return letter

deciphered = ''
for character in message:
        deciphered += cipher(character)
print deciphered
 


(Yes, I expect there's a website out there that will happily decipher the messages for me, and possibly even figure out the cipher, but that would be... cheating. Somehow. And this isn't. Rolling Eyes )



Replies:

The Improbable Island Enquirer - Forum
http://enquirer.improbableisland.com/forum/viewtopic.php?showtopic=25782