Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Rail fence cipher
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{short description|Type of transposition cipher}} {{redirect|Rail fence|the actual fence|split-rail fence}} {{refimprove|date=January 2017}} [[File:Valla de tablones.jpg|thumb|Rail fence]] The '''rail fence cipher''' (also called a '''zigzag cipher''') is a [[Classical cipher|classical]] type of [[transposition cipher]]. It derives its name from the manner in which encryption is performed, in analogy to a fence built with horizontal rails. ==Encryption== In the rail fence cipher, the plaintext is written downwards diagonally on successive "rails" of an imaginary fence, then moving up when the bottom rail is reached, down again when the top rail is reached, and so on until the whole plaintext is written out. The ciphertext is then read off in rows. For example, to encrypt the message 'WE ARE DISCOVERED. RUN AT ONCE.' with 3 "rails", write the text as: W . . . E . . . C . . . R . . . U . . . O . . . . E . R . D . S . O . E . E . R . N . T . N . E . . A . . . I . . . V . . . D . . . A . . . C . (Spaces and punctuation are omitted.) Then read off the text horizontally to get the ciphertext: WECRUO ERDSOEERNTNE AIVDAC ==Decryption== Let <math>N</math> be the number of rails used during encryption. Observe that as the plaintext is written, the sequence of each letter's vertical position on the rails varies up and down in a repeating cycle. In the above example (where <math>N=3</math>) the vertical position repeats with a period of 4. In general the sequence repeats with a period of <math>2(N-1)</math>. Let <math>L</math> be the length of the string to be decrypted. Suppose for a moment that <math>L</math> is a multiple of <math>2(N-1)</math> and let <math>K = { L \over {2(N-1)}}</math>. One begins by splitting the ciphertext into strings such that the length of the first and last string is <math>K</math> and the length of each intermediate string is <math>2K</math>. For the above example with <math>L=24</math>, we have <math>K=6</math>, so we split the ciphertext as follows: WECRUO ERDSOEERNTNE AIVDAC Write each string on a separate line with spaces after each letter in the first and last line: W E C R U O E R D S O E E R N T N E A I V D A C Then one can read off the plaintext down the first column, diagonally up, down the next column, and so on. If <math>L</math> is not a multiple of <math>2(N-1)</math>, the determination of how to split up the ciphertext is slightly more complicated than as described above, but the basic approach is the same. Alternatively, for simplicity in decrypting, one can pad the plaintext with extra letters to make its length a multiple of <math>2(N-1)</math>. If the ciphertext has not been padded, but you either know or are willing to [[brute-force attack|brute-force]] the number of rails used, you can decrypt it using the following steps. As above, let <math>L</math> be the length of the string to be decrypted and let <math>N</math> be the number of rails used during encryption. We will add two variables, <math>x</math> and <math>y</math>, where <math>x+1</math> = the number of diagonals in the decrypted Rail Fence, and <math>y</math> = the number of empty spaces in the last diagonal. <math>1=\frac{L+y}{N+((N-1)*x)}</math> Next solve for <math>x</math> and <math>y</math> algebraically, where both values are the smallest number possible. This is easily done by incrementing <math>x</math> by 1 until the denominator is larger than <math>L</math>, and then simply solving for <math>y</math>. Consider the example cipher, modified to use 6 rails instead of 3. W.........V.........O .E.......O.E.......T.N ..A.....C...R.....A...C ...R...S.....E...N.....E ....E.I.......D.U....... .....D.........R........ The resulting cipher text is: WVO EOETN ACRAC RSENE EIDU DR We know that <math>L=24</math>, and if we use <math>N=6</math> we can solve the equation above. <math>1=\frac{24+y}{6+(5*x)}</math> <math>1=\frac{18+y}{5*x}</math> Simplify the fraction. <math>1=\frac{18+y}{5*4}</math> Solve for <math>x</math> <math>1=\frac{18+2}{20}</math> Solve for <math>y</math> We now have <math>N=6</math>, <math>x=4</math>, and <math>y=2</math>. Or, '''6 rails, 5 diagonals (4+1), and 2 empty spaces at the end'''. By blocking out the empty spaces at the end of the last diagonal, we can simply fill in the Rail Fence line by line using the ciphertext. _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ X _ _ X W V O E O E T N A C R A C _ _ _ _ _ _ _ _ _ X _ _ X ==Cryptanalysis== The cipher's key is <math>N</math>, the number of rails. If <math>N</math> is known, the ciphertext can be decrypted by using the above algorithm. Values of <math>N</math> equal to or greater than <math>L</math>, the length of the ciphertext, are not usable, since then the ciphertext is the same as the plaintext. Therefore the number of usable keys is low, allowing the brute-force attack of trying all possible keys. As a result, the rail-fence cipher is considered weak.{{cn|date=May 2021}} ==Zigzag cipher== The term ''zigzag cipher'' may refer to the rail fence cipher as described above. However, it may also refer to a different type of cipher described by Fletcher Pratt in ''Secret and Urgent''. It is "written by ruling a sheet of paper in vertical columns, with a letter at the head of each column. A dot is made for each letter of the message in the proper column, reading from top to bottom of the sheet. The letters at the head of the columns are then cut off, the ruling erased and the message of dots sent along to the recipient, who, knowing the width of the columns and the arrangement of the letters at the top, reconstitutes the diagram and reads what it has to say."<ref>{{cite book|author=Pratt, Fletcher|title=Secret and Urgent: The story of codes and ciphers|year=1939|isbn=0-89412-261-4|publisher=[[Aegean Park Press]]|pages=143–144}}</ref> ==See also== * [[Scytale]] ==References== {{reflist}} * [[Helen Fouché Gaines]], ''Cryptanalysis, a study of ciphers and their solution'', Dover, 1956, {{ISBN|0-486-20097-3}} ==External links== * [https://web.archive.org/web/20120105152732/http://cryptogram.org/cdb/aca.info/aca.and.you/chapter_09.pdf#RAILFE American Cryptogram Association] * [https://web.archive.org/web/20110518125721/http://www.simonsingh.net/The_Black_Chamber/railfence.html Black Chamber page for encrypting and decrypting the Rail Fence cipher] {{-}} {{Cryptography navbox | classical}} [[Category:Classical ciphers]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:-
(
edit
)
Template:Cite book
(
edit
)
Template:Clear
(
edit
)
Template:Cn
(
edit
)
Template:Cryptography navbox
(
edit
)
Template:ISBN
(
edit
)
Template:Redirect
(
edit
)
Template:Refimprove
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)