X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=szyfrow%2Frailfence.py;h=741e3e2ad3d7bafddbb7d722c468c8103009cb4f;hb=refs%2Fheads%2Fmain;hp=7d6ac31164cf955dc0b3f87b20e98d23409f2e8a;hpb=a870050db6bc974b1bb0d132001750b6624fb43f;p=szyfrow.git diff --git a/szyfrow/railfence.py b/szyfrow/railfence.py index 7d6ac31..741e3e2 100644 --- a/szyfrow/railfence.py +++ b/szyfrow/railfence.py @@ -1,12 +1,26 @@ +"""[Railfence transposition cipher](https://en.wikipedia.org/wiki/Rail_fence_cipher). + +Works by splitting the text into sections, then reading across them to +generate the rows in the cipher. The rows are then combined to form the +ciphertext. + +Example: the plaintext "hellotherefriends", with a height of four, written +out in the railfence as + h h i + etere* + lorfns + l e d +(with the * showing the one character to finish the last section). +Each 'section' is two columns, but unfolded. In the example, the first +section is 'hellot'. +""" import math from enum import Enum from itertools import starmap, zip_longest -from support.utilities import * -from support.language_models import * +from szyfrow.support.utilities import * +from szyfrow.support.language_models import * -from logger import logger - def railfence_encipher(message, height, fillvalue=''): """Railfence cipher. Works by splitting the text into sections, then reading across them to @@ -130,19 +144,6 @@ def railfence_decipher(message, height, fillvalue=''): return cat(c for r in zip_longest(*(down_rows + up_rows), fillvalue='') for c in r) -def railfence_break(message, max_key_length=20, - fitness=Pletters, chunksize=500): - """Breaks a railfence cipher using a matrix of given rank and letter frequencies - - - """ - - sanitised_message = sanitise(message) - results = starmap(worker, [(sanitised_message, i, fitness) - for i in range(2, max_key_length+1)]) - return max(results, key=lambda k: k[1]) - - def railfence_break(message, max_key_length=20, fitness=Pbigrams, chunksize=500): """Breaks a railfence cipher using a range of lengths and