What I have to do?
- First, I have to create x number of keys, this keys have to be binary numbers
- Second, the program asks the user for a message to encrypt. This program writes the key that use and the cypher text in a file
- Third, the program using another function opens the file with the cypher text and the key and decrypt the cypher text.
For this program I use the xor function to encrypt the message, so the program takes each value of the binary text and each value of the key and applies them the xor function
An example could be if the binary message is 0100101 and the key 0001101, the cypher text would be:
message: 0100101
key: 0001101
cypher: 0101000
Here is my program:
(Note: due to time I couldn't make the function to decrypt the message, I just only implement the function to encrypt the message)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import random | |
from numpy import * | |
def gen_keys(): | |
i = 0 | |
j = 0 | |
keys = open("notebook.txt","w")#file for the keys | |
while i < 10: | |
while j < 7: | |
rand = random.randint(0,2)#random integer beetween 0 and 2 | |
keys.write(str(rand))#write the random integer in the file | |
j = j+1 | |
keys.write("\n")#newline | |
j=0 | |
i = i+1 | |
keys.close() | |
def cipher(): | |
key = open("notebook.txt","r")#open file just for reading it | |
msg = raw_input("Mensaje: ")#reads a message from the user | |
size = len(msg)#size of the message | |
y = 0 | |
cipher_text = [] | |
x = 0 | |
while x < 1: | |
dato = key.readline()#read just the first line, that is the key tha we are going to use | |
x = x+1 | |
cipher_key = str(dato[:7]) | |
while y < size: | |
letter = ord(msg[y])#convert into a integer number each character of the message | |
binary = bin(letter)#convert into binary value each integer number | |
aux = binary[2:]#bin() function adds two values that we dont need at the beginning | |
str(aux)#convert into string the binary value | |
print "binary: ", aux | |
print "key: ", cipher_key | |
i=0 | |
while i < len(aux): | |
bit = int(aux[i]) ^ int(cipher_key[i])#xor function between the key and the binary message | |
cipher_text = cipher_text + [str(bit)]#concatenate all the values | |
i = i+1 | |
y = y + 1 | |
cipher_text=display_bits(cipher_text)#display as string | |
#int(cipher_text) | |
print len(cipher_text) | |
key.close()#close the file | |
bob = open("cipher.txt","w")#file for the key and cipher text | |
bob.write(cipher_key)#writes the key into the file | |
bob.write("\n") | |
bob.write(cipher_text)#writes the cypher text into de file | |
bob.close() | |
#function to display the bits as string | |
def display_bits(b): | |
return ''.join(str(i) for i in b) | |
#not working. This is going to be to display the each character of the cypher text as text not as binary | |
def algorithm(bit): | |
assert len(bit) == 7 | |
ascii = 0 | |
for e in bit: | |
ascii = (ascii * 2) + int(e) | |
print unichr(ascii) | |
f = open("cypher.txt", "a") | |
f.write(unichr(ascii)) | |
f.write(" ") | |
f.close | |
return str(ascii) | |
#function to display the cipher text into normal text | |
def bits_to_string(bits): | |
return ' '.join([algorithm(bits[i:i + 7]) | |
for i in range(0, len(bits), 7)]) | |
gen_keys() | |
cipher() |
Here are some images of the program running:
- In this image we can see each character of the message being encrypted
- This is the file where I have the keys:
- Here is the file where I have the key and the cypher text
Well, you get 2 points for what you managed to complete. Note that the key should be as long as the message. Repeating the key makes the system much more vulnerable.
ReplyDelete