For homework this week in Information security and cryptography class we have to build a cipher using the "One-time pad" method. To do this i used python as language and in this post i will show it code and running.
[image obtained from: "http://www.ranum.com/security/computer_security/papers/otp-faq/otp.jpg" ]
¿What is "One-time Pad"?
Each bit or character from the plaintext is encrypted by a modular addition with a bit or character from a secret random key (or pad) of the same length as the plaintext, resulting in a ciphertext. If the key is truly random, as large as or greater than the plaintext, never reused in whole or part, and kept secret, the ciphertext will be impossible to decrypt or break without knowing the key.
First described by Frank Miller in 1882. It is derived from the Vernam cipher, named after Gilbert Vernam, one of its inventors. Vernam's system was a cipher that combined a message with a key read from a punched tape. In its original form, Vernam's system was vulnerable because the key tape was a loop, which was reused whenever the loop made a full cycle. One-time use came a little later when Joseph Mauborgne recognized that if the key tape were totally random, cryptanalysis would be impossible.
The "pad" part of the name comes from early implementations where the key material was distributed as a pad of paper, so the top sheet could be easily torn off and destroyed after use. For easy concealment, the pad was sometimes reduced to such a small size that a powerful magnifying glass was required to use it.
Code
Now here is my code, i implemented the method using a XOR operator to encode the text. In total it have six functions without counting the main function, two of them serve to read or write the result of encription on files, "binario" function just convert a character to a binary representation using characters too, the "Exor" function is a XOR operator for the binary representation in the string of characters, it recive two strings of the same length and the proper functions of encription and decryption.
#! /usr/bin/python
import random
def binario(n):
if type(n) == type("a"):
res = bin(ord(n))
else:
res = bin(n)
res = res[2:]
for i in range(8-len(res)):
res = '0'+res
return res
def exor(a, b):
res = ""
for i in range(len(a)):
if( a[i] == b[i] ):
res += "0"
else:
res += "1"
return res
def encriptar(m):
chiphertext = ""
k = ""
for i in m:
temp = binario(random.randint(0, 255))
k += temp
chiphertext += exor(binario(i) , temp)
return chiphertext, k
def desencriptar(m, k):
m = exor(m,k)
text = ""
for i in range(len(m)/8):
text += chr(int(m[0+(i*8):8+(i*8)], 2))
return text
def encriptarfl(inputfl, outputfl, keyfl):
inputfl = open(inputfl, "r")
m, k = encriptar( (inputfl.read())[:-1] )
inputfl.close()
outputfl = open(outputfl, "w")
keyfl = open(keyfl, "w")
outputfl.write(m)
keyfl.write(k)
outputfl.close()
keyfl.close()
return m, k
def desencriptarfl(inputfl, outputfl, keyfl):
inputfl = open(inputfl, "r")
keyfl = open(keyfl, "r")
outputfl = open(outputfl, "w")
text = desencriptar( inputfl.read()[:-1], keyfl.read()[:-1])
outputfl.write( text )
outputfl.close()
keyfl.close()
inputfl.close()
def main():
encriptarfl("input.dat", "encriptado.dat", "key.dat")
desencriptarfl("encriptado.dat", "desencriptado.dat", "key.dat")
main()
Now with a input text, i will show the result of the program running. I put the texto to encript in the input text file then i ran the program and it generated a encripted file of the input and a file with the aleatory key, then it used both to decrypt the chipertext.
"input.dat"
As far as we can discern, the sole purpose of human existence is to kindle a light in the darkness of mere being.
"encriptado.dat"
0101011100001100001001101111101011101001010001011111110010111111011001100101100100101100000011111100111011011000011011010000010111101000110100010010111001001101110011010101111111110100100111001111001000101101110011010110000111101000100110011010100111111000001010100011000111101110110000110010001110000100111001010110100010100100110100100001010101011000101000010011001100010101000001010101110000111111001011101001000101001110100011100001110000110111100000001001011001000000010101111111010000011101010011101010011011011000001101010000101110100111000101011100001010110001010100000010100010010111001110001111100110100100111000010101111001101001100101111010010101001111011010010000010100011101110010001010101110110001001110100010010000110011101000110010111101111101011100101110010011010010100010010000011010110101011100111010000000101001001000110110011111011011111100101110000000000001111101100001111000011100
"desencriptado.dat"
As far as we can discern, the sole purpose of human existence is to kindle a light in the darkness of mere being
"key.dat"
0001011001111111000001101001110010001000001101111101110011011110000101010111100101011011011010101110111010111011000011000110101111001000101101010100011100111110101011100011101010000110111100101101111000001101101110010000100110001101101110011101101010010111010001100101010011001110101100110101011011110110100101010000011111010111101101110011010100110111110001110001001101111101011100000011000101011110010000001011000100101011111101100111010101000100111101001111001100101110001101001001000100111101001001111101010111111000010000010110010010000111011111101010101111011111001101000100010011110010000110001001100010000100100011010011011100001110111111111101000101101111000000000110101100111101101111001100001111010100000110100100000001010010110100010100010000010011000101111001011110100001101010010110100111010011010100111100110101001100010100010000001011111011100100001000010101101000100110000111100100110010
Have a nice day.
References:
http://en.wikipedia.org/wiki/One-time_pad
Ehm. Please separate the key generation to a previous phase, generating various keys and storing them into a file. Then, when you encrypt, take a file to obtain the next key. The report being in English, however, let's you get 4 points for this homework.
ResponderBorrarThanks for the above post. I got new information through your article. One time pad is useful in keeping the data secret.But I think that is not used in day to day routine.Now it depends on the user need. You mentioned all the codes for our ease.
ResponderBorrardigital signature software