Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2012
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. #!/usr/bin/python
  2. import random
  3.  
  4. def get_randomkey(text):
  5.  
  6.     """ genera una llave numerica pseudoaleatoria
  7.    de la misma longitud del texto a cifrar  """
  8.  
  9.     start = '1'
  10.     end = '9'
  11.  
  12.     for i in range(0, len(text) - 1):
  13.         start += '0'
  14.         end += '9'
  15.  
  16.     #print "start: ", start
  17.     #print "end: ", end
  18.  
  19.     key = random.randint(int(start), int(end))
  20.  
  21.     return str(key)
  22.  
  23. def crypt(text, key):
  24.     ''' cifra/descifra un texto utilizando operacion XOR'''
  25.     new_text = ""
  26.  
  27.     for i, c in enumerate(text):
  28.         code = ord(c)
  29.         xor = code ^ ord(key[i])
  30.         new_text += chr(xor)
  31.  
  32.     return new_text
  33.  
  34. if __name__ == '__main__':
  35.     print "cifrado de vernam (XOR)"
  36.     print "( 1 ) Cifrar"
  37.     print "( 2 ) Descifrar"
  38.     option = 0
  39.  
  40.     while option <> "1" and option <> "2":
  41.         option =  raw_input("teclee una opcion valida: ")
  42.  
  43.     if option == "1":
  44.         text = raw_input("Texto a cifrar: ")
  45.         key = get_randomkey(text)
  46.         text = crypt(text, key)
  47.         print "texto cifrado: ", text
  48.         print "clave para descifrar: ", key
  49.  
  50.     elif option == "2":
  51.         text = raw_input("Texto a descifrar: ")
  52.         key = raw_input("clave: ")
  53.         text = crypt(text, key)
  54.         print "texto descifrado: ", text
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement