martes, 28 de agosto de 2012

One time pad

One Time Pad

Hi everyone, this is the homework of one time pad. Firstly one time pad is a concept  of cryptography, is a type of encryption algorithms by which the plain text is combined with a random key same length as the plain text and used only once. It was invented in 1917. If the key is truly random, never reused and, of course, is kept secret, it can be shown that the method of the one-time pad is unbreakable. 

So, for this homework i created a one time pad encryption using Python, i'll resume all my work in few steps. 

1 - I created the text files ("keys1.txt", and "keys2.txt")






These are all the keys generated(in both files: "keys1.txt", and "keys2.txt"). 10 different keys, but it doesn't matter because each message crypted and decrypted will generate different keys. The length of each key is 7  because this is the length of ASCII values (in binary).





2 - I requested to user for a string 




3 - Each character of the string gave by the user goes to the encrypt function.






4 - Then each character goes to the decrypt function





5 - Finally i printed out the binary value of the character, key used,  xor result, xor decrypted (using xor method again), ASCII value and the character decrypted.





Now, this is all my code...

#!/usr/bin/python
#Carlos Triana
import random
from numpy import *
def createKeys():
file1 = open('keys1.txt', 'w') #Creating file1
file2 = open('keys2.txt', 'w') #Creating file2
i = 0
j = 0
while i<10:
while j<7:
number = random.randint(0,2)
file1.write(str(number)) #Filling file texts with
file2.write(str(number)) #random numbers(binary)
j = j +1
file1.write("\n")
file2.write("\n")
j = 0
i = i + 1
file1.close()
#crypt()
def crypt(binary, size, i):
st = ""
st2 = ""
xor = []
file1 = open('keys1.txt', 'r') #Reading key
st = file1.readline()
key = list(st)
j = 0
while j < 7:
if(binary[j] == key[j]): #Crypting character
xor.append(0)
else:
xor.append(1)
j = j+1
decrypt(xor,binary, i, size)
file1.close()
if(i == size-1):
createKeys()
def decrypt(xor,binary, i, size):
st2 = ""
dec = []
file2 = open('keys2.txt', 'r') #Reading keys
st2 = file2.readline()
key2 = list(st2)
k = 0
PreDecrypt = []
while k < 7:
number = 4
number = int(key2[k])
if(xor[k] == number): #Decrypting character(xor)
dec.append(0)
else:
dec.append(1)
k = k + 1
file2.close()
print "binary = " + str(binary) #This is the binary of the character
print "key used = " + str(key2) #This is the key saved in the "keys2.txt" file
print "xor or crypted = "+str(xor) #This is the key crypted (by xor)
print "decrypted(binary) = "+str(dec)#This is the binary decrypted
numero = "bla"
numero = str(dec).strip('[]')
numero = numero[::3] #Removing characters("," and " ")
baseDiez = int(numero, 2)
print "ASCII decrypted = "+str(baseDiez) #This is the ASCII value of the binary(character)
decoder = chr(baseDiez)
if (decoder == "@"): #For the "backspace" values
decoder = " "
if (baseDiez == 16): #For the "backspace" values
decoder = " "
print "Character decrypted = " + str(decoder) #This is the character decrypted
print
if(i == size-1): #If the character is the last one,
createKeys() #create keys again(one time pad)
def string2Binary():
myInput = "" #Requesting a string
myInput = raw_input('Get in a string to crypt it: ')
newInput = list(myInput)
size = len(myInput)
binary = []
i = 0
while i < size:
if (newInput[i] == " "):
st = "001000000"
else:
st = bin(ord(newInput[i])) #Converting to binary
st = st[2:] #I dont care first 2 values("0b")
binary.append(st)
crypt(binary[i], size, i) #Crypting the binary(previously character)
i = i+1
def main():
createKeys()
while True:
number = input('Get in 1 to continue or 2 to exit: ') #1 to crypt/decrypt
if (number == 1): #2 to exit
string2Binary()
else:
break
main()
view raw OneTimePad.py hosted with ❤ by GitHub


Finally this is mi program running (The message is: its)


If you want to write more messages you must to type 1 (if you type 2, program will close), and new messages will be crypted with other new key.



Leave your comment if you have any doubt


Thank you

No hay comentarios:

Publicar un comentario