renix1

cifra cesar with multiprocessing python 2.x

Dec 19th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.25 KB | None | 0 0
  1. # coding: utf-8
  2.  
  3. """ FAZER CIFRA DE CÉSAR COM MULTIPROCESSING
  4.     Verificador False == ñ cifrado
  5.     Verificador True == cifrado"""
  6.  
  7. import sys, multiprocessing as mp
  8.  
  9. def criptografar(q):
  10.     rot, msg, verificador = q.get(), q.get(), q.get()
  11.     chars = ""
  12.     nums = []
  13.     if len(msg) >= 1:
  14.         print "Criptografando..."
  15.         if verificador == False:
  16.             for char in msg:
  17.                 num = ord(char)+rot
  18.                 nums.append(num)
  19.             else:
  20.                 for num in nums:
  21.                     char = chr(num)
  22.                     chars += char
  23.                 else:
  24.                     print "A mensagem é: %s" % (chars)
  25.  
  26. def descriptografar(q):
  27.     rot, msg, verificador = q.get(), q.get(), q.get()
  28.     chars = ""
  29.     nums = []
  30.     if len(msg) >= 1:
  31.         print "Descriptografando..."
  32.         if verificador == True:
  33.             for char in msg:
  34.                 num = ord(char)-rot
  35.                 nums.append(num)
  36.             else:
  37.                 for num in nums:
  38.                     char = chr(num)
  39.                     chars += char
  40.                 else:
  41.                     print "A mensagem é: %s" % (chars)
  42.  
  43. def menu():
  44.     print "\t\t\t[1] - Criptografar\n\t\t\t[2] - Descriptografar\n\t\t\t[3] - Sair\n\t\t\tDigite um número: "
  45.  
  46. def main(q):
  47.     try:
  48.         while True:
  49.             menu()
  50.             escolha_menu = int(raw_input('\t\t\t\t\t'))
  51.             if escolha_menu == 1:
  52.                 texto_puro = raw_input("Digite um(a) texto/frase qualquer: ")
  53.                 rot = int(raw_input("Digite a rotação: "))
  54.                 if rot >= 26:
  55.                     print "Tem que ser menos de 26"
  56.                 else:
  57.                     if len(texto_puro) >= 1:
  58.                         queue.put(rot)
  59.                         queue.put(texto_puro)
  60.                         queue.put(False)
  61.                         p = mp.Process(target=criptografar, args=(queue,))
  62.                         p.daemon = True
  63.                         p.start()
  64.                         p.join()
  65.  
  66.             elif escolha_menu == 2:
  67.                 texto_cifrado = raw_input("Digite um(a) texto/frase cifrado(a): ")
  68.                 rot = int(raw_input("Digite a rotação: "))
  69.                 if rot >= 26:
  70.                     print "Tem que ser menos de 26"
  71.                 else:
  72.                     if len(texto_cifrado) >= 1:
  73.                         queue.put(rot)
  74.                         queue.put(texto_cifrado)
  75.                         queue.put(True)
  76.                         p = mp.Process(target=descriptografar, args=(queue,))
  77.                         p.daemon = True
  78.                         p.start()
  79.                         p.join()
  80.             else:
  81.                 print "Parece que o senhor quer sair...\nCumpriremos isto!"
  82.                 sys.exit(0)
  83.                
  84.     except KeyboardInterrupt:
  85.         print "Saindo...\n"
  86.         sys.exit(0)
  87.     except e:
  88.         print "exception: ", e
  89.         sys.exit(0)
  90.  
  91. if __name__ == '__main__':
  92.     queue = mp.Queue()
  93.     main(queue)
Advertisement
Add Comment
Please, Sign In to add comment