0% found this document useful (0 votes)
66 views

Implementation of Rail Fence Ciphers: BY: M.SATYA HARSHA (19BCE7026) P.Radha Viswanath (19Bcn7110)

The document discusses the implementation of Rail Fence ciphers. It begins with an introduction to Rail Fence ciphers, which involve writing plaintext down and diagonally across successive "rails" of an imaginary fence. It then provides code to encrypt and decrypt messages using Rail Fence ciphers and details how to solve a Rail Fence cipher through determining the height and cycle. It concludes that Rail Fence ciphers are not very strong due to a small number of practical keys and that frequency analysis can be used to break them on messages of sufficient length.

Uploaded by

Radha Viswanath
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Implementation of Rail Fence Ciphers: BY: M.SATYA HARSHA (19BCE7026) P.Radha Viswanath (19Bcn7110)

The document discusses the implementation of Rail Fence ciphers. It begins with an introduction to Rail Fence ciphers, which involve writing plaintext down and diagonally across successive "rails" of an imaginary fence. It then provides code to encrypt and decrypt messages using Rail Fence ciphers and details how to solve a Rail Fence cipher through determining the height and cycle. It concludes that Rail Fence ciphers are not very strong due to a small number of practical keys and that frequency analysis can be used to break them on messages of sufficient length.

Uploaded by

Radha Viswanath
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

IMPLEMENTATION OF RAIL FENCE CIPHERS

BY:
M.SATYA HARSHA(19BCE7026)
P.RADHA VISWANATH(19BCN7110)
Abstract:

In today’s world, internet is one of the biggest platforms where huge amount of information had been
exchanged to fulfil the individual’s objective or purpose. Due to this overall scenario, internet has really called
for vital security . Before the mode of transfer, there are a number of algorithms available for encrypting data
and transportation ciphers. Rail fence ciphers are widely used along with other ciphers to increase the
encryption complexity in order to form a product cipher and by using these ciphers we can decrease time
complexity.
Introduction:

• Rail Fence Ciphers:


The rail fence cipher (also called a zigzag cipher) is a form of transposition cipher. It derives its name from the
way in which it is encoded.
• Method:
In the rail fence cipher, the plain text is written downwards and diagonally on successive "rails" of an
imaginary fence, then moving up when the bottom rail is reached. When the top rail is reached, the message is
written downwards again until the whole plaintext is written out. The message is then read off in rows. For
example, if 3 "rails" and the message 'WE ARE DISCOVERED. FLEE AT ONCE' is used, the cipher writes out:
W...E...C...R...L...T...E
.E.R.D.S.O.E.E.F.E.A.O.C.
..A...I...V...D...E...N..

Then reads off to get the ciphertext:

WECRLTEERDSOEEFEAOCAIVDEN

Note that this particular example does NOT use spaces separating the words. The decipherer will need to add
them based on context. If spaces are shown in the ciphertext, then they must be included in the count of
letters to determine the width of the solution grid.
Keys can also be used in this cipher e.g. In this example shown above have Key=3 which means there are three
rails, or three lines of text.

SOLUTION:
This solution does not work on all Rail Fence Ciphers. Here is another example to see how to actually solve a
Rail Fence cipher. We'll use a 3-rail fence to encode a new phrase and include spacing in between the words.
Our ciphertext comes out as IA_EZS_ELYLK_UZERLIPL. Note that
our ciphertext has a total of 21 units (letters + spaces). This will be important later on as we try to decipher it.
To solve the cipher, you must know the height and cycle of the puzzle. The height is simply the number of fence
rails used to create it. In this example, we said that 3 fence rails were used, so the height is 3. The height will
always be higher than 2 and no more than the number of letters in the ciphertext (in this case 21) or else the
phrase will not be properly encoded; the height can thus be discovered by process of elimination if not known.
To determine the puzzle width, which will tell us how many total units will be in each row, you must determine
the "cycle" of letters. A "cycle" of letters runs from the top row, down through each subsequent row, and then
up again, but stopping before reaching the top row again. (The next letter on the top row will actually begin the
next cycle.) So a 2-rail puzzle has a "cycle" of 2 letters; a 3-rail puzzle has a "cycle" of 4 letters; a 4-rail puzzle
has a "cycle" of 6 letters; etc. (See below.) The math equation for this is: "Cycle" = ([# of rails] x 2) - 2 (since the
top and bottom rows have half as many units per cycle as any middle row(s)).

1.1.1.1.11...1...1...1...11.....1.....1.....1.....1
.2.2.2.2..2.4.2.4.2.4.2.4..2...6.2...6.2...6.2...6.
..3...3...3...3....3.5...3.5...3.5...3.5..
...4.....4.....4.....4...

Our 3-rail fence example has a "cycle" of 4 units. So divide the total units (letters + spaces) by the cycle number
and round down to the next whole number. There are 21 units in the example, so our "base puzzle width" is 5
(21 / 4 = 5.25, which rounds down to 5). It is important to realize that there are 5 "full cycles" plus a "partial
cycle" of 1 more letter (5 x 4 = 20 and 20 + 1 = 21 units). Therefore, the top row has 6 units in it (5 "full cycles" +
the 1 extra letter that is starting off the 6th cycle all by itself). The middle row has 10 units (5 "full cycles" x 2
units for each cycle). The bottom row has 5 units (5 "full cycles" x 1 unit for each cycle since it is the bottom-
most row).
Take the first 6 units from our ciphertext and write them across the top row, leaving much space between the
units: [IA_EZS]_ELYLK_UZERLIPL.
I...A..._...E...Z...S
The middle row takes the next 10 units and adds 1 unit just after and 1 unit just before each unit in the top
row: IA_EZS[_ELYLK_UZE]RLIPL.
I...A..._...E...Z...S
._.E.L.Y.L.K._.U.Z.E.
The bottom row gets the final 5 units written below and in between the pairs of units in the middle row:
IA_EZS_ELYLK_UZE[RLIPL].
I...A..._...E...Z...S
._.E.L.Y.L.K._.U.Z.E.
..R...L...I...P...L..
Now just follow the down-up-down-up pattern to determine the original message: I_REALLY_LIKE_PUZZLES!
Implementation details:
Code for Railfence cipher:
import java.util.*;
class RailFenceBasic{
int depth;
String Encryption(String plainText,int depth)throws Exception
{
int r=depth,len=plainText.length();
int c=len/depth;
char mat[][]=new char[r][c];
int k=0;
String cipherText="";
for(int i=0;i< c;i++)
{
for(int j=0;j< r;j++)
{ for(int j=0;j< c;j++)
{ cipherText+=mat[i][j];
} } return cipherText;
}
String Decryption(String cipherText,int depth)
throws Exception { int r=depth,len=cipherText.length();
int c=len/depth;
char mat[][]=new char[r][c];
int k=0;
String plainText="";
for(int i=0;i< r;i++)
{ for(int j=0;j< c;j++) {
mat[i][j]=cipherText.charAt(k++);
}}
for(int i=0;i< c;i++)
{ for(int j=0;j< r;j++)
{ plainText+=mat[j][i];
}}
return plainText;
}}
class Main{ public static void main(String args[])
throws Exception { RailFenceBasic rf=new RailFenceBasic();
Scanner scn=new Scanner(System.in);
int depth;
String plainText,cipherText,decryptedText;
System.out.println("Enter plain text:");
plainText=scn.nextLine();
System.out.println("Enter depth for Encryption:"); depth=scn.nextInt();
cipherText=rf.Encryption(plainText,depth);
System.out.println("Encrypted text is:\n"+cipherText);
decryptedText=rf.Decryption(cipherText, depth);
System.out.println("Decrypted text is:\n"+decryptedText); } }

Output:
Enter plain text:
we are discovered,flee at once
Enter depth for Encryption: 3
Encrypted text is: wa svefetnerdcedl c eior,eaoe
Decrypted text is: we are discovered,flee at once
Problems with the rail fence cipher:

The rail fence cipher is not very strong; the number of practical keys (the number of rails) is small enough that a
cryptanalyst can try them all by hand.
Existing or Related work if any:
The rail fence cipher is a very old encryption scheme, pre-dating the Middle Ages. It was used as a field cipher
by both sides in the US Civil War.

Conclusion:
Rail fence ciphers permute the letters of the plaintext, the plaintext and ciphertext will share the same
frequency distribution of characters. Because the frequencies of characters in English
(and other languages) are well-known, with messages of sufficient length a transposition cipher can be
identified with reasonable confidence using frequency analysis.

You might also like