
We use "active_roundnum" so that the round 5 is actually "round 1 of the 2nd loop decryption" (roundnum 5 - num_rounds_1 (4) = 1). So, after 4 rounds we're on round 5 and must undo your 2nd while loop (after i=1). This is 8 letters in length, which means the first loop is responsible for handling the first half (4 letters). In your example of 'abcdefgh' it becomes 'acegbdfh'. Once you go through the first half (your first loop) of decryption, we change the modifier to instead be -1 instead of -2. G: round 4 * 2 = 8 and -2 = 6 (position once decrypted) So, this is how I got the logic for multipling the round number by 2 and then subtracting 2:Ī: round 1 * 2 = 2 and -2 = 0 (position once decrypted)Ĭ: round 2 * 2 = 4 and -2 = 2 (position once decrypted)Į: round 3 * 2 = 6 and -2 = 4 (position once decrypted) I figured this out by basically saying: okay in round 1 we have the letter 'a' and this needs to be in position 0 (first letter), and in round 2 we have 'c' which must be in position 2, and in 3 we have 'e' which must be in position 4. We subtract 2 to undo the first loop you have, and subtract 1 for the second loop you have. It works by figuring out how many rounds there are in the encryption, and doing a modification of the multipling the round number by 2, and then subtracting either 2, or 1. This took me longer than expected, but here is the working decryption. This simplistic method of cryptanalysis (checking every single possible key) only works on very simple ciphers such as this cipher, even slightly more complex ciphers can have far too many keys to check all of them.If you test this, you will note that the output is correct. So the method used is to take the ciphertext, try decrypting it with each key, then see which decryption looks the best. Of course, the more ciphertext you have, the more likely this is to be true (this is the case for all statistical measures, including the frequency approaches above). The key that results in a decryption with the highest likelyhood of being english text is most probably the correct key. For automated methods of determining how 'english like' a piece of text is, check out the Classical Cryptanalysis section, in particular Quadgrams as a fitness measure.


In other words it should look just like this: English Letter Frequenciesįor a method that works well on computers, we need a way of figuring out which of the keys results in the most english like plaintext after decryption. Anagramming is another very powerful method that can be used with any transposition cipher, that consists of taking chunks of ciphertext and guessing what the plaintext would be.Ī peculiarity of transposition ciphers is that the frequency distribution of the characters will be identical to that of natural text (since no substitutions have been performed, it is just the order that has been mixed up).

It is very easy to find a key if you know some of the plaintext, or can guess some of it. A cryptanalyst (code breaker) simply has to try several keys until the correct one is found. The railfence cipher is a very easy cipher to break. This is a JavaScript implementation of the Railfence Cipher.Ĭryptanalysis is the art of breaking codes and ciphers. The ciphertext is again read off along the rows: dttfsedhswotatfneaalhcleelee JavaScript Example § The ciphertext is read off along the rows: dnetlhseedheswloteateftaafcl We write it out in a special way on a number of rails (the key here is 3)ĭ.

The key for the railfence cipher is just the number of rails. This is equivalent to using an un-keyed columnar transposition cipher. Many websites claim that the rail-fence cipher is a simpler "write down the columns, read along the rows" cipher. The railfence cipher offers essentially no communication security, and it will be shown that it can be easily broken even by hand.Īlthough weak on its own, it can be combined with other ciphers, such as a substitutionĬipher, the combination of which is more difficult to break than either cipher on it's own. It is a transposition cipher that follows a simple rule for mixing up the characters in the plaintext to form the ciphertext.
#RAIL FENCE CIPHER PROGRAM CODE CRACK#
The railfence cipher is a very simple, easy to crack cipher.
