Sunday 31 May 2015

One-Time Pad Cipher | Python 3.4.3

The One-Time Pad Cipher is a very popular cipher as it is nearly impossible to break. It would take thousands of years and is therefore "practically" unbreakable, as oppose to "completely" unbreakable, as we will be long dead before our information is leaked.

The One-Time Pad works just like the previous cipher that I coded into python, in that it uses a sequence of numbers that both person_1 and person_2 know. However the difference with this cipher is that every single time it is called upon, it will generate a completely random, non-repeating, list of numbers that is the same length as the amount of characters in the secret message to be transmitted.

With no sequence by which to detect, computers will find it impossible to find any kind of frequency fingerprint in the encrypted message, and therefore can only brute-force that message open, which would take decades to centuries. (As you may notice, the code required for this cipher is almost twice of that required for the previous two)

I will be creating code soon on how to break some of the ciphers I have written, and along with more projects both cipher and non-cipher related!

Download link for my One-Time Pad Cipher in python 3.4.3 is here, feel free to ask any questions and I will endeavour to get back to you as soon as possible, as per:

http://tinyurl.com/ocb9axc

And here is the python 3.4.3 code in plain text for you:

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":
        main_body = (input("Please input your message, all cases will be changed to lower: ")).lower()
        new_word = ""

        displacement_list = []
        for x in range(len(main_body)):
            displacement_list.append(random.randint(0,31))                          

        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("Your encrypted word is: ")
        print("")
        print("")
        print(new_word)
        print("")
        print("")

        print("Your displacement pass is: ")
        print("")
        print("")
        for x in displacement_list:
            if len(str(x)) == 2:
                print (x,end="")
            if len(str(x)) == 1:
                print ("0" + str(x),end="")
        print("")
        print("")
        print("")
       

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

        new_word = ""

        split_count_start = 0
        split_count_finish = 2
        displacement_list = [displacement_pass[i:i+2] for i in range(0, len(displacement_pass), 2)]
        displacement_list = [int(i) for i in displacement_list]
                                   

        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("Your message translates as: ")
        print("")
        print("")
        print(new_word)
        print("")
        print("")

1 comment:

  1. Top 10 best edge titanium - Titanium-Arts
    Tithrium-Arts. There are lots of the best and best edge titanium urban titanium metallic in the world today. titanium pans You'll titanium nose stud never titanium hair trimmer as seen on tv find a lot of them in titanium welder stores,

    ReplyDelete