Cifra Scytale em Python


SUBMITTED BY: Guest

DATE: July 5, 2013, 7:34 p.m.

FORMAT: Python

SIZE: 1.1 kB

HITS: 1771

  1. class Scytale:
  2. def encrypt(self, texto, key):
  3. ''' (Scytale, str, int) -> str
  4. Cifra o texto com a cifra scytale utilizando a chave key.
  5. '''
  6. cifrado = ''
  7. texto = texto.replace(' ', '')
  8. # calcula a qtd de caracteres e a qtd de colunas
  9. qtd_ch = len(texto)
  10. col = qtd_ch // key
  11. if qtd_ch % key > 0:
  12. col += 1
  13. i = ord('A')
  14. while len(texto) < key * col:
  15. texto += chr(i)
  16. i += 1
  17. for i in range(col):
  18. for j in range(0, qtd_ch, col):
  19. cifrado += texto[i + j]
  20. return cifrado.upper()
  21. def decrypt(self, texto, key):
  22. ''' (Scytale, str, int) -> str
  23. Decifra o texto cifrado com a cifra Scytale usando a chave key.
  24. '''
  25. texto_plano = ''
  26. texto = texto.replace(' ', '')
  27. for i in range(key):
  28. for j in range(0, len(texto), key):
  29. texto_plano += texto[i + j]
  30. return texto_plano.lower()

comments powered by Disqus