Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import string
- from sys import argv
- #It's a global variable that we have to use, like a module
- modulo = len(dict.fromkeys(string.printable, 0)) + 1
- #We generate two pad keys for the two users
- #They are generate in a random way in a range between 1 and the module
- def genera(cantidad, largo):
- global modulo
- output1 = open('key1.txt','w')
- output2 = open('key2.txt','w')
- for i in range(cantidad):
- clave = ''
- for j in range(largo):
- clave += str(random.randrange(1, modulo)) + '.'
- print>>output1, '%s' % clave
- print>>output2, '%s' % clave
- output1.close()
- output2.close()
- return
- #This function erase the key line that the user used to encrypt o decrypt
- def borra(clave):
- Input = open(clave, 'r')
- keys = Input.readlines()
- Input.close()
- del keys[0]
- Output = open(clave, 'w')
- for k in keys:
- print >>Output, '%s' % k.strip()
- Output.close()
- return
- #This function open 2 files, the pad key and the message to encrypt
- #where we read the line that we have to read, then we create a dictionary
- #and inside that we have a alphabet with a sequence, 1 by 1.
- #after that I split the key by a point '.', then we initialize a
- #boolean variable and string variable those we'll need forward.
- #Now I iterate all the lines of the message until we get an empty line,
- # inside of the for we cypher adding the value of the alphabet
- #and the value of the pad key and we use the module of that product
- # if we don't have enough keys, we fall in an exception and print a warning.
- # Atfer all we erase the key line.
- def cifra(mensaje, clave):
- global modulo
- Input = open(clave,'r')
- Mensaje = open(mensaje, 'r')
- llave = Input.readline()
- Input.close()
- alfabeto = dict.fromkeys(string.printable, 0)
- aux = 1
- for a in alfabeto:
- alfabeto[a] = aux
- aux+=1
- k = llave.split('.')
- ok = True
- cifrado = ''
- for l in Mensaje.readlines():
- l = l.strip()
- if len(l) > 0:
- for m in l:
- try:
- cifrado += str(((alfabeto[m] + int(k.pop(0)))) % modulo) + '.'
- except:
- print 'Mensaje demasiado largo.'
- ok = False
- break
- Mensaje.close()
- while len(k) > 0:
- cifrado += k.pop(0) + '.'
- Output = open('%s.cifrado' % mensaje,'w')
- print >>Output, cifrado
- Output.close()
- if ok:
- borra(clave)
- return
- #We create a file called like the message plus '.original', then we open
- #the pad key and read the line #that we need, we create an alphabet inside
- #a dictionary and we put in this way " a[5] = 'a', a[7] = #'b' " then we
- #read every line of message and we decrypt them find it in the dictionary
- #using the letter of the message that is a number and the key value
- #that is a key too, substracting #those and make a module of that
- #we got our message, after all we erase the key line of the pad.
- def decifra(mensaje, clave):
- global modulo
- Output = open('%s.original' % mensaje,'w')
- Input = open(clave,'r')
- Mensaje = open(mensaje, 'r')
- llave = Input.readline()
- alfabeto = dict.fromkeys(string.printable, 0)
- aux = 1
- reverso = dict()
- reverso[0] = ' '
- for a in alfabeto:
- reverso[aux] = a
- aux+=1
- k = llave.split('.')
- ok = True
- decifrado = ''
- for l in Mensaje.readlines():
- l = l.strip()
- for m in l.split('.'):
- if len(m) > 0:
- cl = int(k.pop(0))
- recuperado = reverso[((int(m) - cl) % modulo)]
- decifrado += recuperado
- print >>Output, decifrado
- Output.close()
- Mensaje.close()
- Input.close()
- borra(clave)
- return
- def main():
- opcion = argv[1]
- if opcion == 'genera':
- genera(int(argv[2]), int(argv[3]))
- elif opcion == 'cifra':
- cifra(argv[2], argv[3])
- elif opcion == 'decifra':
- decifra(argv[2], argv[3])
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement