Advertisement
sau1

One-time pad

Aug 23rd, 2012
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.03 KB | None | 0 0
  1. import random
  2. import string
  3. from sys import argv
  4. #It's a global variable that we have to use, like a module
  5. modulo = len(dict.fromkeys(string.printable, 0)) + 1
  6. #We generate two pad keys for the two users
  7. #They are generate in a random way in a range between 1 and the module
  8. def genera(cantidad, largo):
  9.     global modulo
  10.     output1 = open('key1.txt','w')
  11.     output2 = open('key2.txt','w')
  12.     for i in range(cantidad):
  13.         clave = ''
  14.         for j in range(largo):
  15.             clave += str(random.randrange(1, modulo)) + '.'
  16.         print>>output1, '%s' % clave
  17.         print>>output2, '%s' % clave
  18.     output1.close()
  19.     output2.close()
  20.     return
  21. #This function erase the key line that the user used to encrypt o decrypt
  22. def borra(clave):
  23.     Input = open(clave, 'r')
  24.     keys = Input.readlines()
  25.     Input.close()
  26.     del keys[0]
  27.     Output = open(clave, 'w')
  28.     for k in keys:
  29.         print >>Output, '%s' % k.strip()
  30.     Output.close()
  31.     return
  32. #This function open 2 files, the pad key and the message to encrypt
  33. #where we read the line that we have to read, then we create a dictionary
  34. #and inside that we have a alphabet with a sequence, 1 by 1.
  35. #after that I split the key by a point '.', then we initialize a
  36. #boolean variable and string variable those we'll need forward.
  37. #Now I iterate all the lines of the message until we get an empty line,
  38. # inside of the for we cypher adding the value of the alphabet
  39. #and the value of the pad key and we use the module of that product
  40. # if we don't have enough keys, we fall in an exception and print a warning.
  41. # Atfer all we erase the key line.
  42.  
  43. def cifra(mensaje, clave):
  44.     global modulo
  45.     Input = open(clave,'r')
  46.     Mensaje = open(mensaje, 'r')
  47.     llave = Input.readline()
  48.     Input.close()
  49.     alfabeto = dict.fromkeys(string.printable, 0)
  50.     aux = 1
  51.     for a in alfabeto:
  52.         alfabeto[a] = aux
  53.         aux+=1
  54.     k = llave.split('.')
  55.     ok = True
  56.     cifrado = ''
  57.     for l in Mensaje.readlines():
  58.         l = l.strip()
  59.         if len(l) > 0:
  60.             for m in l:
  61.                 try:
  62.                     cifrado += str(((alfabeto[m] + int(k.pop(0)))) % modulo) + '.'
  63.                 except:
  64.                     print 'Mensaje demasiado largo.'
  65.                     ok = False
  66.                     break
  67.     Mensaje.close()
  68.     while len(k) > 0:
  69.         cifrado += k.pop(0) + '.'
  70.     Output = open('%s.cifrado' % mensaje,'w')
  71.     print >>Output, cifrado
  72.     Output.close()
  73.     if ok:
  74.         borra(clave)
  75.     return
  76. #We create a file called like the message plus '.original', then we open
  77. #the pad key and read the line #that we need, we create an alphabet inside
  78. #a dictionary and we put in this way " a[5] = 'a', a[7] = #'b' " then we
  79. #read every line of message and we decrypt them find it in the dictionary
  80. #using the letter of the message that is a number and the key value
  81. #that is a key too, substracting #those and make a module of that
  82. #we got our message, after all we erase the key line of the pad.
  83. def decifra(mensaje, clave):
  84.     global modulo
  85.     Output = open('%s.original' % mensaje,'w')
  86.     Input = open(clave,'r')
  87.     Mensaje = open(mensaje, 'r')
  88.     llave = Input.readline()
  89.     alfabeto = dict.fromkeys(string.printable, 0)
  90.     aux = 1
  91.     reverso = dict()
  92.     reverso[0] = ' '
  93.     for a in alfabeto:
  94.         reverso[aux] = a
  95.         aux+=1
  96.     k = llave.split('.')
  97.     ok = True
  98.     decifrado = ''
  99.     for l in Mensaje.readlines():
  100.         l = l.strip()
  101.         for m in l.split('.'):
  102.             if len(m) > 0:
  103.                 cl = int(k.pop(0))
  104.                 recuperado = reverso[((int(m) - cl) % modulo)]
  105.                 decifrado += recuperado
  106.     print >>Output, decifrado
  107.     Output.close()
  108.     Mensaje.close()
  109.     Input.close()
  110.     borra(clave)
  111.     return
  112.  
  113. def main():
  114.     opcion = argv[1]
  115.     if opcion == 'genera':
  116.         genera(int(argv[2]), int(argv[3]))
  117.     elif opcion == 'cifra':
  118.         cifra(argv[2], argv[3])
  119.     elif opcion == 'decifra':
  120.         decifra(argv[2], argv[3])
  121.  
  122. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement