class Railfence: def __railfence_id(self, tam, key): ''' (Railfence, int, int) -> list of int Retorna um lista de inteiros com a posicao da linha que o caracter do texto ira ocupar, variando de 0 ate key - 1. ''' j = 0 inc = 0 idx = [] for i in range(tam): if j == key - 1: inc = -1 elif j == 0: inc = 1 idx.append(j) j += inc return idx def encrypt(self, texto, key): ''' (Railfence, str, int) -> str Retorna o texto plano cifrado na cifra rail fence com a chave key. ''' texto = texto.replace(' ', '') tam = len(texto) idx = self.__railfence_id(tam, key) cifrado = '' for i in range(key): for z in range(tam): if idx[z] == i: cifrado += texto[z] return cifrado.upper() def decrypt(self, texto, key): ''' (Railfence, str, int) -> str Retorna o texto plano para um texto cifrado com a cifra rail fence com a chave key. ''' texto = texto.replace(' ', '') tam = len(texto) idx = self.__railfence_id(tam, key) idx_sorted = sorted(idx) texto_plano = '' for i in range(tam): for j in range(tam): if idx[i] == idx_sorted[j] and idx[i] > -1: texto_plano += texto[j] idx[i] = -1 idx_sorted[j] = -1 return texto_plano.lower()