Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 23

Ex No. 1.

CAESAR CIPHER IMPLEMENTATION

AIM
To implement Caesar cipher technique
THEORY
The Caesar cipher is named after the Roman military and political leader Gaius
Julius Caesar (100 BC 44 BC).1Caesar used this relatively simple form of ciphering
to encode military messages.The Caesar cipher is one of the earliest known and
simplest ciphers. It is a type of substitution cipher in which each letter in the plaintext
is 'shifted' a certain number of places down the alphabet. For example, with a shift of
1, A would be replaced by B, B would become C, and so on. The method is named
after Julius Caesar, who apparently used it to communicate with his generals.
MATHEMATICAL DESCRIPTION
First we translate all of our characters to numbers, 'a'=0, 'b'=1, 'c'=2, ... , 'z'=25.
We can now represent the caesar cipher encryption function, e(x), where x is the
character we are encrypting, as:

Where k is the key (the shift) applied to each letter. After applying this function the
result is a number which must then be translated back into a letter. The decryption
function is :

CAESAR CIPHER:
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
int key,i;
char data[200];
clrscr();
printf("\nEnter the alphabetical plain text in lowercase to be encrypted:\n");
gets(data); //plain text.
printf("\nEnter the key value,e.g.:3,4,5,etc. : ");
scanf("%d",&key); //key.

if(key>26)
key=key % 26; //0<=key<26.
for(i=0;i<strlen(data);i++)
{
if(data[i]==' ')
{
continue;
}
else
{
if(data[i]>= 'x')
{
data[i]=data[i]-26;
}
data[i]=data[i]+key;
}
}
printf("Your cipher text is: %s\n",data); //cipher text.
getch();
}
OUTPUT:

RESULT:
Thus a program to implement Caesar Cipher technique was implemented and
output was verified.

EX.NO:1B
AIM:

PLAYFAIR CIPHER IMPLEMENTATION

To implement Play fair cipher technique


THEORY
The Playfair cipher was the first practical digraph substitution cipher. The
scheme was invented in 1854 by Charles Wheatstone, but was named after Lord
Playfair who promoted the use of the cipher. The technique encrypts pairs of letters
(digraphs), instead of single letters as in the simple substitution cipher. The Playfair is
significantly harder to break since the frequency analysis used for simple substitution
ciphers does not work with it. Frequency analysis can still be undertaken, but on the
25*25=625 possible digraphs rather than the 25 possible monographs. Frequency
analysis thus requires much more ciphertext in order to work.
ALGORITHM
The 'key' for a playfair cipher is generally a word, for the sake of example we will
choose 'monarchy'. This is then used to generate a 'key square', e.g.
monar
chybd
efgik
lpqst
uvwxz
Any sequence of 25 letters can be used as a key, so long as all letters are in it and there
are no repeats. Note that there is no 'j', it is combined with 'i'. We now apply the
encryption rules to encrypt the plaintext.
1.

Remove any punctuation or characters that are not present in the key square
(this may
mean spelling out numbers, punctuation etc.).

2.

Identify any double letters in the plaintext and replace the second occurence
with an

'x' e.g. 'hammer' -> 'hamxer'.


3.

If the plaintext has an odd number of characters, append an 'x' to the end to
make it
even.

4.

Break the plaintext into pairs of letters, e.g. 'hamxer' -> 'ha mx er'

5.

The algorithm now works on each of the letter pairs.

6.

Locate the letters in the key square, (the examples given are using the key
square
above)
o If the letters are in different rows and columns, replace the pair with the
letters on the same row respectively but at the other pair of corners of the
rectangle defined by the original pair. The order is important the first
encrypted letter of the pair is the one that lies on the same row as the first
plaintext letter.'ha' -> 'bo', 'es' -> 'il'
o If the letters appear on the same row of the table, replace them with the
letters to their immediate right respectively (wrapping around to the left
side of the row if a letter in the original pair was on the right side of the
row). 'ma' -> 'or', 'lp' -> 'pq'
o If the letters appear on the same column of the table, replace them with
the letters immediately below respectively (wrapping around to the top
side of the column if a letter in the original pair was on the bottom side
of the column). 'rk' -> 'dt', 'pv' -> 'vo'

Program:
#include <stdio.h>
#include<conio.h>
#define siz 5
void encrypt(int *i, int *j)
{
(*i)++,(*j)++;
if((*i)==siz) *i=0;
else if((*j)==siz) *j=0;
}
void playfair(char ch1,char ch2, char mat[siz][siz])
{
int j,m,n,p,q,c,k;
for(j=0,c=0;(c<2)||(j<siz);j++)
for(k=0;k<siz;k++)
if(mat[j][k] == ch1)
m=j,n=k,c++;

else if(mat[j][k] == ch2)


p=j,q=k,c++;
if(m==p)
encrypt(&n,&q);
else if(n==q)
encrypt(&m,&p);
else
n+=q,q=n-q,n-=q;
printf("%c%c",mat[m][n],mat[p][q]);
}
void main()
{
char mat[siz][siz],key[10],str[25]={0};
int m,n,i,j;
char temp;
clrscr();
printf("Enter Key:");
gets(key);
m=n=0;
for(i=0;key[i]!='\0';i++)
{
for(j=0;j<i;j++)
if(key[j] == key[i]) break;
if(key[i]=='j') key[i]='i';
if(j>=i)
{
mat[m][n++] = key[i];
if(n==siz)
n=0,m++;
}
}
for(i=97;i<=122;i++)
{
for(j=0;key[j]!='\0';j++)
if(key[j] == i)
break;
else if(i=='j')
break;
if(key[j]=='\0')
{
mat[m][n++] = i;
if(n==siz) n=0,m++;
}
}
printf("Enter input String:");

gets(str);
printf("\n\nMatrix :\n");
for(i=0;i<siz;i++)
{
for(j=0;j<siz;j++)
printf("%c\t",mat[i][j]);
printf("\n");
}
printf("\n\nEntered text :%s\nCipher Text :",str);
for(i=0;str[i]!='\0';i++)
{
temp = str[i++];
if(temp == 'j') temp='i';
if(str[i]=='\0')
playfair(temp,'x',mat);
else
{
if(str[i]=='j') str[i]='i';
if(temp == str[i])
{
playfair(temp,'x',mat);
playfair('x',str[i],mat);
}
else
playfair(temp,str[i],mat);
}
}
getch();}
OUTPUT:

RESULT:
Thus a program to implement Playfair Cipher technique was implemented and
output was verified.
EX.NO:1C
HILL CIPHER IMPLEMENTATION
AIM:
To implement Hill cipher technique
THEORY:
Invented by Lester S. Hill in 1929, the Hill cipher is a polygraphic substitution
cipher based on linear algebra. Hill used matrices and matrix multiplication to mix up
the plaintext.To counter charges that his system was too complicated for day to day
use, Hill constructed a cipher machine for his system using a series of geared wheels
and chains. However, the machine never really sold.Hill's major contribution was the
use of mathematics to design and analyse cryptosystems. It is important to note that
the analysis of this algorithm requires a branch of mathematics known as number
theory. Many elementary number theory text books deal with the theory behind the
Hill cipher, with several talking about the cipher in detail (e.g. Elementary Number
Theory and its applications, Rosen, 2000). It is advisable to get access to a book such
as this, and to try to learn a bit if you want to understand this algorithm in depth.
EXAMPLE

This example will rely on some linear algebra and some number theory.
The key for a hill cipher is a matrix e.g.

In the above case, we have taken the size to be 33, however it can be any size (as
long as it is square). Assume we want to encipher the message ATTACK AT DAWN.
To encipher this, we need to break the message into chunks of 3. We now take the first
3 characters from our plaintext, ATT and create a vector that corresponds to the letters
(replace A with 0, B with 1 ... Z with 25 etc.) to get: [0 19 19] (this is ['A' 'T' 'T']).
To get our ciphertext we perform a matrix multiplication (you may need to
revise matrix multiplication if this doesn't make sense):

This process is performed for all 3 letter blocks in the plaintext. The plaintext may
have to be padded with some extra letters to make sure that there is a whole number of
blocks.
Now for the tricky part, the decryption. We need to find an inverse matrix modulo 26
to use as our 'decryption key'. i.e. we want something that will take 'PFO' back to
'ATT'. If our 3 by 3 key matrix is calledK, our decryption key will be the 3 by 3
matrix K-1 , which is the inverse of K.

To find K-1 we have to use a bit of maths. It turns out that K-1 above can be calculated
from our key. A lengthy discussion will not be included here, but we will give a short

example. The important things to know are inverses (mod m), determinants of
matrices, and matrix adjugates.
Let K be the key matrix. Let d be the determinant of K. We wish to find K1

(the inverse of K), such that K K-1 = I (mod 26), where I is the identity matrix. The

following formula tells us how to find K-1 given K:

where d d-1 = 1(mod 26), and adj(K) is the adjugate matrix of K.


d (the determinant) is calculated normally for K (for the example above, it is 489 = 21
(mod 26)). The inverse, d-1, is found by finding a number such that d d-1 = 1 (mod
26) (this is 5 for the example above since 5*21 = 105 = 1 (mod 26)). The simplest way
of doing this is to loop through the numbers 1..25 and find the one such that the
equation is satisfied. There is no solution (i.e. choose a different key) if gcd(d,26)
1 (this means d and 26 share factors, if this is the case K can not be inverted, this
means the key you have chosen will not work, so choose another one).That is it.
Once K-1 is found, decryption can be performed.
HILL CIPHER.C
#include<stdio.h>
#include<conio.h>
#include<string.h>
int i,j,l;
char mssg[100];
int k[][3] = { {17,17,5},
{21,18,21},
{2,2,19}
};
int ki[][3] = { {4,9,15},
{15,17,6},
{24,0,17}
};
int c[3];

int p[3];
void get_mssg()
{
printf("\n Enter the Plaintext (in caps): ");
gets(mssg);
}
void multiply()
{
for(i=0;i<3;i++)
{
p[i] = 0;
for(j=0;j<3;j++)
p[i] += c[j]*k[i][j];
p[i] %= 26;
}
}
void multiply2()
{
for(i=0;i<3;i++)
{
c[i] = 0;
for(j=0;j<3;j++)
c[i] += p[j]*ki[i][j];
c[i] %= 26;
}
}
void en_mssg()
{
int i = 0;
while(i < strlen(mssg))
{
c[0] = mssg[i]-65;
if(mssg[i+1] == '\0')
c[1] = c[2] = 0;
else
{
c[1] = mssg[i+1]-65;
if(mssg[i+2] == '\0')
c[2] = 0;
else
c[2] = mssg[i+2]-65;
}

multiply();
l =0;
while(i<strlen(mssg) && l<3)
mssg[i++] = p[l++]+65;
}
printf("\n The Ciphertext is : ");
puts(mssg);
}
void de_mssg()
{
int i = 0;
while(i < strlen(mssg))
{
p[0] = mssg[i]-65;
if(mssg[i+1] == '\0')
p[1] = p[2] = 0;
else
{
p[1] = mssg[i+1]-65;
if(mssg[i+2] == '\0')
p[2] = 0;
else
p[2] = mssg[i+2]-65;
}
multiply2();
l =0;
while(i<strlen(mssg) && l<3)
mssg[i++] = c[l++]+65;
}
printf("\n The Decrypted Plaintext is : ");
puts(mssg);
}
int main()
{
get_mssg();
en_mssg();
de_mssg();
getch();
return 0;
}
Output:

RESULT:
Thus a program to implement Hill Cipher technique was implemented and
output was verified.

EX.NO:1D
AIM

VIGENERE CIPHER IMPLEMENTATION


To implement Vigenere cipher technique

THEORY

A Polyalphabetic Cipher One of the main problems with simple substitution


ciphers is that they are so vulnerable to frequency analysis. Given a sufficiently large
ciphertext, it can easily be broken by mapping the frequency of its letters to the know
frequencies of, say, English text. Therefore, to make ciphers more secure,
cryptographers have long been interested in developing enciphering techniques that are
immune to frequency analysis. One of the most common approaches is to suppress the
normal frequency data by using more than one alphabet to encrypt the message.
A polyalphabetic substitution cipher involves the use of two or more cipher alphabets.
Instead of there being a one-to-one relationship between each letter and its substitute,
there is a one-to-many relationship between each letter and its substitutes.
THE VIGENERE TABLE
The Vigenere Cipher , proposed by Blaise de Vigenere from the court of Henry
III of France in the sixteenth century, is a polyalphabetic substitution based on the
following table
AB C D E F G H I J K L M N O P Q R S T U V W X YZ
A AB C D E F G H I J K LM N O PQ R S TU VW X YZ
B BCDEFG HIJKLM NOPQ RSTUVWXYZA
C C D E F G H I J K LM N O PQ R S T U VWX YZ AB
D D E F G H I J K LM N O PQ R S T U VWX YZ AB C
E E F G H I J K LM N O PQ R S T U VWX YZ AB C D
F F G H I J K LM N O PQ R S T U VWX YZ AB C D E
G G H I J K L M N O P Q R S T U V W X YZ AB C D E F
H H I J K LM N O PQ R S TU VW X YZ AB C D E F G
I I J K LM N O PQ R S TU VW X YZ AB C D E F G H
J J K L M N O P Q R S T U V W X YZ AB C D E F G H I
K K LM N O PQ R S T U VWX YZ AB C D E F G H I J
L LM N O PQ R S TU VW X YZ AB C D E F G H I J K
M M N O PQ R S TU VW X YZ AB C D E F G H I J K L
N N O PQ R S TU VW X YZ AB C D E F G H I J K LM
O O PQ R S T U VWX YZ AB C D E F G H I J K LM N
P PQ R S TU VW X YZ AB C D E F G H I J K LM N O
Q Q R S T U VW X YZ AB C D E F G H I J K L M N O P
R R S T U VWX YZAB C D E F G H I J K LM N O PQ
S S TU VW X YZAB C D E F G H I J K LM N O PQ R
T TU VW X YZAB C D E F G H I J K LM N O PQ R S
U U VWX YZAB C D E F G H I J K LM N O PQ R S T
V VW X YZAB C D E F G H I J K LM N O PQ R S TU
W W X YZAB C D E F G H I J K LM N O PQ R S TU V
X X YZAB C D E F G H I J K LM N O PQ R S TU VW

Y YZAB C D E F G H I J K LM N O PQ R S TU VW X
Z Z AB C D E F G H I J K L M N O P Q R S T U V W X Y
Note that each row of the table corresponds to a Caesar Cipher. The first row is a shift
of 0; the second is a shift of 1; and the last is a shift of 25.
The Vigenere cipher uses this table together with a keyword to encipher a
message. For example, suppose we wish to encipher the plaintext message:
TO BE OR NOT TO BE THAT IS THE QUESTION
using the keyword RELATIONS. We begin by writing the keyword, repeated as many
times as necessary, above the plaintext message. To derive the ciphertext using the
tableau, for each letter in the plaintext, one finds the intersection of the row given by
the corresponding keyword letter and the column given by the plaintext letter itself to
pick out the ciphertext letter.
Keyword:

RELAT IONSR ELATI ONSRE LATIO NSREL

Plaintext:

TOBEO RNOTT OBETH ATIST HEQUE STION

Ciphertext:

KSMEH ZBBLK SMEMP OGAJX SEJCS FLZSY

Decipherment of an encrypted message is equally straightforward. One writes the


keyword repeatedly above the message:
Keyword:

RELAT IONSR ELATI ONSRE LATIO NSREL

Ciphertext:

KSMEH ZBBLK SMEMP OGAJX SEJCS FLZSY

Plaintext:

TOBEO RNOTT OBETH ATIST HEQUE STION

This time one uses the keyword letter to pick a column of the table and then traces
down the column to the row containing the ciphertext letter. The index of that row is
the plaintext letter.
The strength of the Vigenere cipher against frequency analysis can be seen by
examining the above ciphertext. Note that there are 7 'T's in the plaintext message and
that they have been encrypted by 'H,' 'L,' 'K,' 'M,' 'G,' 'X,' and 'L' respectively. This
successfully masks the frequency characteristics of the English 'T.' One way of looking
at this is to notice that each letter of our keyword RELATIONS picks out 1 of the 26
possible substitution alphabets given in the Vigenere tableau. Thus, any message
encrypted by a Vigenere cipher is a collection of as many simple substitution ciphers
as there are letters in the keyword.

Although the Vigenere cipher has all the features of a useful field cipher -- i.e.,
easily transportable key and tableau, requires no special apparatus, easy to apply, etc.
-- it did not catch on its day. A variation of it, known as the Gronsfeld cipher, did catch
on in Germany and was widely used in Central Europe. The Gronsfeld variant used the
digits of a keynumber instead of a the letters of keyword, but remained unchanged in
all other respects. So in fact the Gronsfeld is a weaker technique than Vigenere since it
only uses 10 substitute alphabets (one per digit 0..9) instead of the 26 used by
Vigenere.
SOLVING THE VIGENERE CIPHER : THE KASISKI/KERCKHOFF
METHOD
Vigenere-like substitution ciphers were regarded by many as practically
unbreakable for 300 years. In 1863, a Prussian major named Kasiski proposed a
method for breaking a Vigenere cipher that consisted of finding the length of the
keyword and then dividing the message into that many simple substitution
cryptograms. Frequency analysis could then be used to solve the resulting simple
substitutions.
Kasiski's technique for finding the length of the keyword was based on measuring the
distance between repeated bigrams in the ciphertext. Note that in the above
cryptogram the plaintext bigram 'TO' occurs twice in the message at position 0 and 9
and in both cases it lines up perfectly with the first two letters of the keyword. Because
of this it produces the same ciphertext bigram, 'KS.' The same can be said of plaintext
'BE' which occurs twice starting at positions 2 and 11, and also is encrypted with the
same ciphertext bigram, 'ME.' In fact, any message encrypted with a Vigenere cipher
will produce many such repeated bigrams. Although not every repeated bigram will be
the result of the encryption of the same plaintext bigram, many will, and this provides
the basis for breaking the cipher. By measuring and factoring the distances between
recurring bigrams -- in this case the distance is 9 -- Kasiski was able to guess the
length of the keyword. For this example,
Location

01234 56789 01234 56789 01234 56789

Keyword:

RELAT IONSR ELATI ONSRE LATIO NSREL

Plaintext:

TOBEO RNOTT OBETH ATIST HEQUE STION

Ciphertext:

KSMEH ZBBLK SMEMP OGAJX SEJCS FLZSY

the Kasiski method would create something like the following list:
Repeated

Location Distance Factors


Bigram
KS
9
9
3, 9
SM
10
9
3, 9
ME
11
9
3, 9
...
Factoring the distances between repeated bigrams is a way of identifying possible
keyword lengths, with those factors that occur most frequently being the best
candidates for the length of the keyword. Note that in this example since 3 is also a
factor of 9 (and any of its multiples) both 3 and 9 would be reasonable candidates for
keyword length. Although in this example we don't have a clear favorite, we've
narrowed down the possibilities to a very small list. Note also that if a longer
ciphertext were encrypted with the same keyword ('RELATIONS'), we would expect
to find repeated bigrams at multiples of 9 -- i.e., 18, 27, 81, etc. These would also have
3 as a factor. Kasiski's important contribution is to note this phenomenon of repeated
bigrams and propose a method -- factoring of distances -- to analyze it.
Once the length of the keyword is known, the ciphertext can be broken up into
that many simple substitution cryptograms. That is, for a keyword of length 9, every 9th letter in the ciphertext was encrypted with the same keyword letter. Given the
structure of the Vigenere tableau, this is equivalent to using 9 distinct simple
substitution ciphers, each of which was derived from 1 of the 26 possible Caesar shifts
given in the tableau. The pure Kasiski - proceeds by analyzing these simple
substitution cryptograms using frequency analysis and the other standard techniques. A
variant of this method, proposed by the French cryptographer Kerckhoff, is based on
discovering the keyword itself and then using it to decipher the cryptogram. In
Kerckhoff's method, after the message has been separated into several columns,
corresponding to the simple substitution cryptograms, one tallies the frequencies in
each column and then uses frequency and logical analysis to construct the key. For

example, suppose the most frequent letter in the first column is 'K'. We would
hypothesize that 'K' corresponds to the English 'E'. If we consult the Vigenere tableau
at this point, we can see that if English 'E' were enciphered into 'K' then row G of the
table must have been the alphabet used for the first letter of the keyword. This implies
that the first letter of the keyword is 'G'.
The problem with this "manual" approach is that for short messages there are
often several good candidates for English 'E' in each column. This requires the testing
of multiple hypotheses, which can get quite tedious and involved. Therefore we need a
more sensitive test to discover the alphabet used by each letter of the keyword.
Recalling that each row of the Vigenere tableau is one of the 26 Caesar shifts, we can
use the chi-square test to determine which of the 26 possible shifts was used for each
letter of the keyword. This modern day version of the Kerckhoff method turns out to
be very effective.
VIGENER CIPHER
# include <stdio.h>
# include <conio.h>
# include <ctype.h>
# include <string.h>
void vigenereChiper(char *text, char *key){
unsigned int i, j;
printf("Encrypted text
: ");
for(i=0,j=0;i<strlen(text);i++, j++)
{
if(j>=strlen(key))
{
j = 0; // j back to nol value when j >= length of string(keyword)
}
if(text[i] >= 'a' && text[i] <= 'z')
{
// mod 26, because to adapt of total alphabet
printf("%c",97+(((toupper(text[i])-65)+(toupper(key[j])-65))%26));
}
else if(text[i] >= 'A' && text[i] <= 'Z')
{
printf("%c",65+(((toupper(text[i])-65)+(toupper(key[j])-97))%26));

}
}
}
void main(){
char text[255], keyword[30];
printf("Enter the Text you want to Encrypt : ");
gets(text);
printf("Enter the Keyword : ");
gets(keyword);
vigenereChiper(text, keyword);
getch();
}
Output:

RESULT:
Thus a program to implement Vigenere Cipher technique was implemented and
output was verified.

EX.NO:1 E

RAIL FENCE ROW & COLUMN TRANSFORMATION


IMPLEMENTATION

AIM
To implement Rail Fence cipher technique
THEORY
The railfence cipher is a very simple, easy to crack cipher. It is a transposition
cipher that follows a simple rule for mixing up the characters in the plaintext to form
the ciphertext. The railfence cipher offers essentially no communication security, and it

will be shown that it can be easily broken even by hand.Although weak on its own, it
can be combined with other ciphers, such as a substitution cipher, the combination of
which is more difficult to break than either cipher on it's own.Many websites claim
that the rail-fence cipher is a simpler "write down the columns, read along the rows"
cipher. This is equivalent to using an un-keyed columnar transposition cipher.
EXAMPLE
The railfence cipher is an easy to apply transposition cipher that jumbles up the order
of the letters of a message in a quick convenient way. It also has the security of a key
to make it a little bit harder to break. The Rail Fence cipher works by writing your
message on alternate lines across the page, and then reading off each line in turn. For
example, the plaintext "defend the east wall" is written as shown below, with all spaces
removed.

The simplest Rail Fence Cipher, where each letter is written in a zigzag pattern across
the page. The ciphertext is then read off by writing the top row first, followed by the
bottom row, to get "DFNTEATALEEDHESWL".
ENCRYPTION
To encrypt a message using the Rail Fence Cipher, you have to write your
message in zigzag lines across the page, and then read off each row. Firstly, you need
to have a key, which for this cipher is the number of rows you are going to have. You
then start writing the letters of the plaintext diagonally down to the right until you
reach the number of rows specified by the key. You then bounce back up diagonally
until you hit the first row again. This continues until the end of the plaintext.
For the plaintext we used above, "defend the east wall", with a key of 3, we get the
encryption process shown below.

The Rail Fence Cipher with a key of 3. Notice the nulls added at the end of the
message to make it the right length. Note that at the end of the message we have
inserted two "X"s. These are called nulls, and act as placeholders. We do this to make
the message fit neatly in to the grid (so that there are the same number of letters on the
top row, as on the bottom row. Although not necessary, it makes the decryption process
a lot easier if the message has this layout. The ciphertext is read off row by row to get
"DNETLEEDHESWLXFTAAX".
DECRYPTION
The decryption process for the Rail Fence Cipher involves reconstructing the
diagonal grid used to encrypt the message. We start writing the message, but leaving a
dash in place of the spaces yet to be occupied. Gradually, you can replace all the
dashes with the corresponding letters, and read off the plaintext from the table.
We start by making a grid with as many rows as the key is, and as many columns as
the length of the ciphertext. We then place the first letter in the top left square, and
dashes diagonally downwards where the letters will be. When we get back to the top
row, we place the next letter in the ciphertext. Continue like this across the row, and
start the next row when you reach the end. For example, if you receive the cipher text
"TEKOOHRACIRMNREATANFTETYTGHH", encrypted with a key of 4, you start
by placing the "T" in the first square. You then dash the diagonal down spaces until
you get back to the top row, and place the "E" here. Continuing to fill the top row you
get the pattern below.

The first row of the decryption process for the Rail Fence Cipher. We have a table with
4 rows because the key is 4, and 28 columns as the ciphertext has length 28.
Continuing this row-by-row, we get the successive stages shown below.

The second stage in the decryption process.

The third stage in the decryption process.

The fourth and final stage in the decryption process. From this we can now read the
plaintext off following the diagonals to get "they are attacking from the north".
DISCUSSION
The Rail Fence Cipher is a very easy to apply transposition cipher. However, it is not
particularly secure, since there are a limited number of usable keys, especially for
short messages (for there to be enough movement of letters, the length of the message
needs to be at lease twice the key, but preferably 3 times the key). You could process
these quite quickly by hand, and even more quickly with a computer.
The use of nulls can also have a detrimental effect on the security of the cipher, as an
interceptor can use them to identify where the end of the line is, and so have a sensible
guess at the key. This can be averted by using a more common letter, such as "E", to
fill the null spaces, as it will still be clear to the recipient that these are not part of the
message as they will appear at the end of the plaintext. The Rail Fence Cipher can also
be utilised without the use of nulls.
One way to also make the encryption a little bit more secure, is to keep the spaces as
characters, and include them in the encryption table. They are treated in exactly the
same way as any other letter. For example, using the plaintext "defend the east wall"
with a key of 3 again, but this time including spaces we get the table below.

The Rail Fence Cipher with spaces left in the message. Colour is used to emphasise
where spaces are, against the blank squares of the table. So the ciphertext would read
"DNHAWXEEDTEES ALF TL".
RAIL FENCE METHOD:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j,k,l;
char a[20],c[20],d[20];
clrscr();
printf("\nEnter the input string : ");
gets(a);
l=strlen(a);
/*Ciphering*/
for(i=0,j=0;i<l;i++)
{
if(i%2==0)
c[j++]=a[i];
}
for(i=0;i<l;i++)
{
if(i%2==1)
c[j++]=a[i];
}
c[j]='\0';
printf("\nCipher text after applying rail fence :");
printf("\n%s",c);
/*Deciphering*/
if(l%2==0)
k=l/2;
else
k=(l/2)+1;
for(i=0,j=0;i<k;i++)
{
d[j]=c[i];

j=j+2;
}
for(i=k,j=1;i<l;i++)
{
d[j]=c[i];
j=j+2;
}
d[l]='\0';
printf("\nText after decryption : ");
printf("%s",d);
getch();
}
OUTPUT:

RESULT:
Thus a program to implement Rail fence Cipher technique was implemented
and output was verified.

You might also like