Introduction
One time pad (abbreviated OTP) is an algorithm to encrypt. It encrypts plaintext using a random key. OTP is unbreakable encryption.[1] The reason is because the key is random and when it have been used, it is deleted.[2]
- Creates keys. First it is necessary to create random keys. In this case the keys has specific length.
- Encrypts plaintext. With key and plaintext, encrypts the text with the XOR operator.
- Decrypts ciphertext. With the same operator and same key is possible to find the plaintext.
Functions
- Function createKeysAndSaveInFile(numberKeys, padLength)
Function receives the number of keys that has the file. Pad length is the length of each key. All keys are generated in this function and save in Alice's and Bob's files.
def createKeysAndSaveInFile(numberKeys, padLength): fileAlice = open('Alice.txt','a+') fileBob = open('Bob.txt','a+') for j in range(int(numberKeys)): key = '' for i in range(int(padLength)): aux = random.randint(34,127) while aux == 92: aux = random.randint(34,127) if aux != 92:# ignore character92 '\\' key += chr(random.randint(34,127)) key += chr(aux) fileAlice.write(key) fileAlice.write('\n') fileBob.write(key) fileBob.write('\n') fileAlice.close() fileBob.close()
- Function deleteFiles()
In this function I ensure keys don't save in hard disk when program is finished.def deleteFiles(): os.remove('Alice.txt') os.remove('Bob.txt')
- Function cipherMessage(message, key) [3]
messageCipher += chr(ord(message[i]) ^ ord(key[j]))
Before line is the most important, because it is the line that encrypts or decrypts.
To work with XOR operator is necessary to have in binary the cipher/plain text. Python has some functions help to do this.
For example Python has the function bin(number)[2:], it returns given number in binary. To do the one time pad algorithm follows next steps:
If A and B are the same return false. If A and B are different return true.
It works in this way:
>>> bin(4)[2:]
100
>>> bin(5)[2:]To work with XOR operator is necessary to have in binary the cipher/plain text. Python has some functions help to do this.
For example Python has the function bin(number)[2:], it returns given number in binary. To do the one time pad algorithm follows next steps:
- Get ASCII number of each character (text and key)
- Change number to binary
- Apply XOR operator. Remeber XOR operator works:
A | 0 | 0 | 1 | 1 | |||||
B | 0 | 1 | 0 | 1 | |||||
A^B | 0 | 1 | 1 | 0 |
The code does the same but more efficient. I want to say that I didn't use directly bin function, because exist another function helps to do more fast.
It works in this way:
>>> bin(4)[2:]
100
101
>>> 100 ^ 101
1
>>>chr(100 ^ 101) # an specific character
'\x01'
>>> ord('4')
52
>>> ord('5')
53
>>> ord('4') ^ ord('5')
1
>>>chr(ord('4') ^ ord('5')) # an specific character
'\x01'
So, with before lines we can see:
>>>chr(100 ^ 101) # an specific character
'\x01'
and
>>>chr(ord('4') ^ ord('5')) # an specific character
'\x01'
is the same.
def cipherMessage(message, key): messageCipher = '' j = 0 for i in range(len(message)): messageCipher += chr(ord(message[i]) ^ ord(key[j])) j +=1 if j >= len(key): j = 0 return messageCipher
- Function getKeyFile(document)
Function return key. First gets the first key in Alice's or Bob's file, then deletes it.
def getKeyFile(document): with open(document,'r') as f: key = f.readline() data = f.read().splitlines(True) f = open(document,'r') lines = f.readlines() f.close() f = open(document,'w') for line in lines: if line != key: f.write(line) f.close() return key
Complete code
Implementation
Keys to generate -> 3 Length of keys -> 15 Who is turn? (Write a for Alice or b for Bob) ->b Bob: hello Alice, are you okay? Cipher text: @ R? }4!37 u] Q&E2 ,#k Bob says: hello Alice, are you okay? Who is turn? (Write a for Alice or b for Bob) ->A Alice: NO, IM NOT OK, I was finished my homework but I overwrite it, and I need to do again Cipher text: v#G k!Rs+}=gm C G Z HaIs YG Oz?Xam*W FU I z4 aEd\L" L YD.2RH. k_ Alice says: NO, IM NOT OK, I was finished my homework but I overwrite it, and I need to do again Who is turn? (Write a for Alice or b for Bob) ->b Bob: Sorry Alice, ganbatte. oh! we don't have more keys, bye :/ R o9F .JW O O wn FV \ d" I y OA Bob says: Sorry Alice, ganbatte. oh! we don't have more keys, bye :/ No more keys :(
Figure 1. Example of keys in files. |
Fig. 3 An example of implementation code. |
Bibliography
1. OTP reading
2. More OTP reading
3. Carlos García He explained me how works in python XOR operator with the function chr() and ord().
4. How works chr and ord function in Python
5. How works XOR operator Python
urge aprender usar un syntax highlighter para con embedded source code y también agregar las explicaciones y ejemplos de ejecución antes de que regrese yo aquí a calificar
ReplyDelete+ Control over key length as a parameter: ok
ReplyDelete+ Control over the number of keys as a parameter: ok
+ Automatic key management: present; see comments
+ Functional encryption/decryption: symmetric
+ Adequate use cases for example runs: screenshots of a terminal with just text in it (1/2)
- Spelling: ok
- Grammar: "No more" instead of "Not more"
- Structure and visual design of the report: ok; the screenshots are a bit too small (should have font size comparable to the rest)
- Reference management: see the comments (-1/2)
- Programming style: ok
x English: check
=> 4 out of 5 pts, but as you have extra entries of voluntary homework, you get 5 points.
Why would you delete the files? They should disappear one key at a time…o_0
When erasing used keys, just erase the first one. You might, by small probability, have duplicates that would now get erased, too, with your code, and consequently mess up the synchronization between the two files.
You should place your sources WITHIN the sentences, not outside them. Move the brackets to BEFORE the full stop. Also, a simple link is NOT adequate information for a reference list. Pick a standard reference style to figure out what information and in which order is to be given for each source.