Sunday 24 May 2015

Polyalphabetic Cipher | Python 3.4.3

The next big changes in cipher development came around with the idea of a Polyalphabetic Cipher.

This type of cipher is much like the previous cipher I interpreted in python, in that letters are moved along the the alphabet a set amount of times that is defined be a pre-set number that both person_1(the sender) and person_2 know.

However instead of a single displacement number to move every character along by in the alphabet, this cipher uses a displacement word to define how many moves a character makes along the alphabet.

For example: If my displacement word where "abcd" then we would take the position of each letter in our displacement word (a=0,b=1,c=2,d=3), and we would apply the first displacement to the first letter of our secret message, then the second displacement to the second letter and so on, until we have gone through every displacement that was generated from our displacement word's characters, at which point we go back to the start of our displacement word and run through it over and over again UNTIL, our secret message is fully encrypted. At which point we send the message to person_2 who can decipher the word, as they know the displacement word too!

This method is called Polyalphabetical due to the fact that "poly" means many in Latin, so we have many different letters to encrypt out message.

The link to the python 3.4.3 code is here, and as always, feel free to ask any questions to me and I will be sure to answer them as soon as I can.

.py in 3.4.3: http://tinyurl.com/kza5r7n

Full code in plain text is below:

import math
import time
import random

encrypt_letter_to_num = {"a":0,"b":1,"c":2,"d":3,"e":4,"f":5,"g":6,"h":7,"i":8,"j":9,"k":10,"l":11,"m":12,"n":13,"o":14,"p":15,"q":16,"r":17,"s":18,"t":19,"u":20,"v":21,"w":22,"x":23,"y":24,"z":25,"'":26,",":27,".":28,"?":29,"/":30," ":31}
encrypt_num_to_letter = {0:"a",1:"b",2:"c",3:"d",4:"e",5:"f",6:"g",7:"h",8:"i",9:"j",10:"k",11:"l",12:"m",13:"n",14:"o",15:"p",16:"q",17:"r",18:"s",19:"t",20:"u",21:"v",22:"w",23:"x",24:"y",25:"z",26:"'",27:",",28:".",29:"?",30:"/",31:" "}

while True:
    en_de = ""
    en_de = input("Would you like to encrypt a message or decrypt a message?[e/d]: ").lower()
 
    if en_de == "e":
        displacement_word = input("Please enter your displacement word: ")
        main_body = (input("Please input your message, all cases will be changed to lower: ")).lower()

        new_word = ""

        displacement_list = []
        for x in displacement_word:
            displacement_list.append(encrypt_letter_to_num[x])

        displacement_list_position_count = 0
     
        for x in main_body:
            if displacement_list_position_count == len(displacement_list):
                displacement_list_position_count = 0
            old_value = encrypt_letter_to_num[x]
            new_value = old_value + displacement_list[displacement_list_position_count]
            while new_value > 31:
                new_value -= 32
            new_word += encrypt_num_to_letter[new_value]
            displacement_list_position_count += 1

        print(new_word)



    if en_de == "d":
        displacement_word = input("Please enter your displacement word: ")
        main_body = (input("Please input your message, all cases will be changed to lower: ")).lower()

        new_word = ""

        displacement_list = []
        for x in displacement_word:
            displacement_list.append(encrypt_letter_to_num[x])

        displacement_list_position_count = 0
     
        for x in main_body:
            if displacement_list_position_count == len(displacement_list):
                displacement_list_position_count = 0
            old_value = encrypt_letter_to_num[x]
            new_value = old_value - displacement_list[displacement_list_position_count]
            while new_value < 0:
                new_value += 32
            new_word += encrypt_num_to_letter[new_value]
            displacement_list_position_count += 1

        print(new_word)

No comments:

Post a Comment