#!/usr/bin/python
import random
def get_randomkey(text):
""" genera una llave numerica pseudoaleatoria
de la misma longitud del texto a cifrar """
start = \'1\'
end = \'9\'
for i in range(0, len(text) - 1):
start += \'0\'
end += \'9\'
#print "start: ", start
#print "end: ", end
key = random.randint(int(start), int(end))
return str(key)
def crypt(text, key):
\'\'\' cifra/descifra un texto utilizando operacion XOR\'\'\'
new_text = ""
for i, c in enumerate(text):
code = ord(c)
xor = code ^ ord(key[i])
new_text += chr(xor)
return new_text
if __name__ == \'__main__\':
print "cifrado de vernam (XOR)"
print "( 1 ) Cifrar"
print "( 2 ) Descifrar"
option = 0
while option <> "1" and option <> "2":
option = raw_input("teclee una opcion valida: ")
if option == "1":
text = raw_input("Texto a cifrar: ")
key = get_randomkey(text)
text = crypt(text, key)
print "texto cifrado: ", text
print "clave para descifrar: ", key
elif option == "2":
text = raw_input("Texto a descifrar: ")
key = raw_input("clave: ")
text = crypt(text, key)
print "texto descifrado: ", text