eatklion.blogg.se

Rail fence cipher
Rail fence cipher










Private static String splitIntoLines(int numLines, String message, boolean encrypted) Lines = lines.substring(1) ĭirection *= lineCount = 0 || lineCount = key - 1 ? -1 : 1 String letter = String.valueOf(lines.charAt(0)) * Add first letter to result by removing it from the line */ * condense zig-zag array into normal string */įor (int i = 0 i < message.length() ++i) Lines = lines.replaceFirst("\\?", letter) String letter = String.valueOf(message.charAt(charCount)) String lines = splitIntoLines(key, message, true) Public static String decrypt(int key, String message) String lines = splitIntoLines(key, message, false) Public static String encrypt(int key, String message)

rail fence cipher

This was ultra fun! Here's my Java solution: public class RailFenceCipher _ -> putStrLn "Usage: (enc|dec) number string" In reduce is (y:l) (overi i xs (const ys)) Where build (x:xs) l (i:is) = build xs (overi i l (x:)) is $ reverse build s (replicate n "") (sort $ take (length s) $ zigzag n) Decided against golfing that and make it actually readable Zigzag n = cycle $ ++ ĭecode n s = reverse $ reduce (zigzag n) "" ScrewGHC = error "'cause GHC likes to complain, but `is' is infinite" The basic idea was rather simple, but it turned out that it wasn't possible to use Haskell's zipWith or a fold with it, so it didn't exactly turn out as I wanted. Kinda unelegant but working Haskell solution. Unsigned int key = stoi(input.substr(input.find_first_of(' '), input.find_last_of(' '))) String message = input.substr(input.find_last_of(' ') + 1) String command = input.substr(0, input.find_first_of(' ')) String::const_iterator cipherIter = cipherText.begin() įor (int index = row index < result.size() index += (isTrough ? troughSkips : peakSkips)) String decode(const string& cipherText, const unsigned int key) String encode(const string& message, const unsigned int key)įor (int index = row index < message.size() index += (isTrough ? troughSkips : peakSkips))Įlse if (row = key - 1) isTrough = false This took me much longer than i feel like it should have taken. Output, pos, i = text, pos+1, i+step2Įdit: also implemented /u/jetRink's great idea for using the same mapping for encryption and decryption. Then, read along the fence-line (zig-zag) and you're done! Lastly the final line has 6 spaces so take the remaining 6 characters ( DCDYGM) and fill them in. The next line has 12 spaces, so take 12 more characters ( EDTORALPORME) and fill them in. The first line has 7 spaces, so take the first 7 characters ( RIMIRAR) and fill them in. This involves putting the zig-zag shape in beforehand and filling it in along the lines. You can also decrypt (it would be pretty useless if you couldn't!). Putting those together gives you RIMIRAREDTORALPORMEDCDYGM, which is the ciphertext. The top line has RIMIRAR, the second line has EDTORALPORME and the last line has DCDYGM. See how it goes up and down? Now, to get the ciphertext, instead of reading with the zigzag, just read along the lines instead.

rail fence cipher

We would write our message like this: R I M I R A R This involved choosing a number (we'll choose 3) and writing our message as a zig-zag with that height (in this case, 3 lines high.) Let's say our message is REDDITCOMRDAILYPROGRAMMER. One such method was the rail-fence cipher.

rail fence cipher rail fence cipher

This means the methods of encryption were usually much simpler as they had to be done reliably by a person, possibly in wartime scenarios. Before the days of computerised encryption, cryptography was done manually by hand.












Rail fence cipher