Advertisement
Guest User

Vigenère Chiper v2 - By Nafta

a guest
Jul 6th, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import string
  2. import time
  3. import os
  4. import sys
  5.  
  6. def cls():
  7.     os.system(['clear','cls'][os.name == 'nt'])
  8. cls()
  9.  
  10. def restart_program():
  11.     python = sys.executable
  12.    
  13.     os.execl(python, python, * sys.argv)
  14.  
  15. class Cifrario:
  16.     Stringa = ""
  17.     ListaTesto = []
  18.     ListaChiave = []
  19.     Alfabeto = string.ascii_uppercase
  20.     def RiempiListaNumeri(self, stringa):
  21.        
  22.         lista = []
  23.         for i in self.Stringa:
  24.             numero = self.Alfabeto.index(i)
  25.             lista.append(numero)
  26.         self.Lista = lista
  27.  
  28.     def Crypt(self):
  29.         crypt = []
  30.         crypted = []
  31.         for i in range(0, len(testo.Filtered)):
  32.             crypt.append((self.ListaTesto[i]+self.ListaChiave[i])%26)
  33.         for N in crypt:
  34.             crypted.append(self.Alfabeto[N])
  35.         stampa = ''.join(crypted)
  36.         self.Crypted = stampa
  37.         print self.Crypted
  38.  
  39.     def Decrypt(self):
  40.         decrypt = []
  41.         decrypted = []
  42.         for i in range(0, len(testo.Filtered)):
  43.             decrypt.append((self.ListaTesto[i]-self.ListaChiave[i])%26)
  44.         for N in decrypt:
  45.             decrypted.append(self.Alfabeto[N])
  46.         stampa = ''.join(decrypted)
  47.         self.Decrypted = stampa
  48.         print self.Decrypted
  49.        
  50. class FiltraStringhe:
  51.     DaFiltrare = ""
  52.    
  53.     def Filtra(self, stringa):
  54.         applica_filtro1 = ''.join(e for e in self.DaFiltrare if e.isalnum())
  55.         applica_filtro2 = applica_filtro1.upper()
  56.         self.Filtered = applica_filtro2
  57.  
  58.     def AdattaChiave(self):
  59.         resto = len(testo.Filtered)%len(chiave.Filtered)
  60.         divisione = len(testo.Filtered)/len(chiave.Filtered)
  61.         chiave_var = chiave.Filtered
  62.         if resto > 0:
  63.             giuntura = chiave_var[0:resto]
  64.             chiave_var = chiave_var*divisione+giuntura
  65.         elif resto <= 0:
  66.             chiave_var = chiave_var*divisione
  67.         self.Adattata = chiave_var
  68.  
  69. print ( '   ___________________________________________ ' )
  70. print ( '  |         _   _        __ _                 |' )      
  71. print ( '  |        | \ | |      / _| |                |' )      
  72. print ( '  |        |  \| | __ _| |_| |_ __ _          |' )  
  73. print ( '  |        |     |/ _  |  _| __/ _  |         |' )
  74. print ( '  |        | |\ | (_| | | | || (_| |         |' )
  75. print ( '  |  ______|_| \_|\__|_|_|  \__\__|_|_______  |' )
  76. print ( '  | |_______________________________________| |' )
  77. print ( '  |                                           |' )
  78. print ( '  |           Cifrario di Vigenere            |' )
  79. print ( '  |                                           |' )
  80. print ( '  |            ZaIXBlNFYtNHFOEsUS             |' )
  81. print ( '  |___________________________________________|' )
  82. print ( '' )
  83. print ( '' )
  84. print ( '     Scegliere tra le opzioni disponibili.')
  85. print ( '' )
  86. print ( '   1) Crypta.' )
  87. print ( '   2) Decrypta. ' )
  88. print ( '   3) Esci.' )
  89. print ( '' )
  90. opzione = int(input ( "     Opzione: "))
  91. print ""
  92. if opzione == 3:
  93.     quit()
  94. if (opzione <= 0) or (opzione > 3):
  95.     cls()
  96.     print ( '' )
  97.     print ( '     Scelta non valida' )
  98.     print ( '' )
  99.     print ( '     Hehehe VsSHTFoMTrQCMhOFeFguZbJKfEoRHNrTtMHEEJ!!!' )      
  100.     time.sleep(2)
  101.     cls()
  102.     restart_program()
  103.     main()
  104.    
  105. if (opzione == 1) or (opzione == 2):
  106.     # input testo e chiave
  107.     testo_immesso = raw_input ( "Testo da cifrare: " )
  108.     print ""
  109.     chiave_immessa = raw_input ( "Chiave cifratura: " )
  110.     print ""
  111.         # ripulisce testo e chiave da caratteri speciali e li muta in maiuscolo
  112.     testo = FiltraStringhe()
  113.     testo.DaFiltrare = testo_immesso
  114.     testo.Filtra(testo.DaFiltrare)
  115.     chiave = FiltraStringhe()
  116.     chiave.DaFiltrare = chiave_immessa
  117.     chiave.Filtra(chiave.DaFiltrare)
  118.     # tronca o allunga la chiave ripetendola in base al rapporto
  119.     # tra lunghezza chiave e lunghezza testo
  120.     chiave.AdattaChiave()
  121.    
  122.     # converte i caratteri delle stringhe contenenti chiave e testo
  123.     # da alfabetiche a numeriche, inserendoli poi in due liste distinte
  124.     num_testo = Cifrario()
  125.     num_testo.Stringa = testo.Filtered
  126.     num_testo.RiempiListaNumeri(num_testo.Stringa)
  127.    
  128.     num_chiave = Cifrario()
  129.     num_chiave.Stringa = chiave.Adattata
  130.     num_chiave.RiempiListaNumeri(num_testo.Stringa)
  131.    
  132.     # istanzia un oggetto  della classe cifrario per poterne usare i metodi
  133.     Cifra = Cifrario()
  134.     Cifra.ListaTesto = num_testo.Lista
  135.     Cifra.ListaChiave = num_chiave.Lista
  136.    
  137.     # usa il metodo crypta o il metodo decrypta della classe cifrario
  138.     if opzione == 1:
  139.         Cifra.Crypt()
  140.        
  141.     if opzione == 2:
  142.         Cifra.Decrypt()
  143.    
  144.     cls()
  145.     print 'Testo cifrato: '
  146.     print ( '' )
  147.     if opzione == 1:
  148.         print ( ' "' ), Cifra.Crypted , ( '" ' )
  149.     if opzione == 2:
  150.         print ( ' "' ), Cifra.Decrypted, ( '" ' )
  151.     print ( '' )
  152.     raw_input( 'Premere invio per continuare...' )
  153.     restart_program()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement