PYTHON   16
cifra cesar
Guest on 16th March 2023 12:03:41 AM


  1. #!/usr/bin/python
  2. import string
  3. import sys
  4.  
  5. class cifra_cesar(object):
  6.         alfabeto = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
  7.         "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]
  8.        
  9.         def __init__(self):
  10.                 self.menu()
  11.  
  12.         def menu(self):
  13.                 while(1):
  14.                         print "1. Criptografar\n2. Descriptografar\n3. Sair\n: \r"
  15.                         try:
  16.                                 opcao = int(raw_input())
  17.                                 if opcao > 3:
  18.                                         continue
  19.                                 elif opcao == 3:
  20.                                         break
  21.                                         return
  22.                                 break
  23.                         except:
  24.                                 continue
  25.  
  26.                 print "Digite o texto: "
  27.                 texto = string.lower(raw_input())
  28.                 while(1):
  29.                         print "Digite o k: "
  30.                         try:                   
  31.                                 k = int(raw_input())
  32.                                 break
  33.                         except:
  34.                                 continue
  35.                 if opcao == 1:
  36.                         print self.crypt_descrypt("c", texto, k)
  37.                 elif opcao == 2:
  38.                         print self.crypt_descrypt("d", texto, k)
  39.  
  40.  
  41.         def crypt_descrypt(self, opcao, texto, k):     
  42.                 nova_frase = ""
  43.                 for letra in texto:
  44.                         j = 0  
  45.                         for carac in self.alfabeto:
  46.                                 j += 1
  47.                                 if letra == " ":
  48.                                         nova_frase += " "
  49.                                         break
  50.                                 elif letra == carac:
  51.                                         if opcao == "c":
  52.                                                 posicao = j+k-1
  53.                                                 while(1):
  54.                                                         if posicao >= self.alfabeto.__len__():
  55.                                                                 posicao = posicao - int(self.alfabeto.__len__())
  56.                                                         else:
  57.                                                                 break
  58.                                                 nova_frase += str(self.alfabeto[posicao])
  59.                                         else:
  60.                                                 posicao = j-k-1
  61.                                                 while(1):
  62.                                                         if posicao >= self.alfabeto.__len__():
  63.                                                                 posicao = posicao + int(self.alfabeto.__len__())
  64.                                                         else:
  65.                                                                 break
  66.                                                 nova_frase += str(self.alfabeto[posicao])
  67.                                         break
  68.                                 elif carac == self.alfabeto[int(self.alfabeto.__len__()) -1]:
  69.                                         nova_frase += letra
  70.                 return nova_frase[::-1]
  71.  
  72. def main():
  73.         c = cifra_cesar()
  74.         exit
  75.  
  76. if __name__ == "__main__":
  77.         main()

Raw Paste

Login or Register to edit or fork this paste. It's free.