PROF.
DEVANSHI DAVE
PRACTICAL: 5
5. Write a Program to implement Vigenere Cipher.
AIM: To implement the Vigenere Cipher substitution technique using C program.
DESCRIPTION:
To encrypt, a table of alphabets can be used, termed a tabula recta, Vigenère square,
or Vigenère table. It consists o
f the alphabet written out 26 times in differ ent rows, each
alphabet shifted cyclically to the left compared to the previous alphabet, corresponding to the
26 possible Caesar ciphers. At different points in the encryption process, the cipher uses a
different alphabet from one of the rows. The alphabet used at each point depends on a
repeating keyword.
Each row starts with a key letter. The remainder of the row holds the letters A to Z.
Although there are 26 key rows shown, you will only use as many keys as there are unique
letters in the key string, here just 5 keys, {L, E, M, O, N}. For successive letters of the
message, we are going to take successive letters of the key string, and encipher each message
letter using its corresponding key row. Choose the next letter of the key, go al ong that row to
find the column heading that matches the message character; the letter at the intersection of
[key-row, msg-col] is the enciphered letter.
PROF.DEVANSHI DAVE
ALGORITHM:
STEP-1: Arrange the alphabets in row and column of a 26*26 matrix.
STEP-2: Circulate the alphabets in each row to position left such that the first letter
is attached to last.
STEP-3: Repeat this process for all 26 rows and construct the final key matrix.
STEP-4: The keyword and the plain text is read from the user.
STEP-5: The characters in the keyword are repeated sequentially so as to
match with that of the plain text.
STEP-6: Pick the first letter of the plain text and that of the keyword as the row
indices and column indices respectively.
STEP-7: The junction character where these two meet forms the cipher character.
STEP-8: Repeat the above steps to generate the entire cipher text.
PROGRAM: (Vigenere Cipher)
#include <stdio.h>
#include<conio.h>
#include <ctype.h>
#include <string.h>
void encipher();
void decipher();
void main()
{
int choice;
clrscr();
while(1)
{
printf("\n1. Encrypt Text");
printf("\t2. Decrypt Text");
printf("\t3. Exit"); printf("\n\
nEnter Your Choice : ");
scanf("%d",&choice);
if(choice == 3)
exit(0);
else if(choice == 1)
encipher();
PROF.DEVANSHI DAVE
else if(choice == 2)
decipher();
else
printf("Please Enter Valid Option.");
void encipher()
unsigned int i,j;
char input[50],key[10]; printf("\
n\nEnter Plain Text: ");
PROF.DEVANSHI DAVE
scanf("%s",input); printf("\
nEnter Key Value: ");
scanf("%s",key);
printf("\nResultant Cipher Text: ");
for(i=0,j=0;i<strlen(input);i++,j++)
if(j>=strlen(key))
{ j=0;
printf("%c",65+(((toupper(input[i])-65)+(toupper(key[j])- 65))
%26));
}}
void decipher()
unsigned int i,j;
char input[50],key[10];
int value;
printf("\n\nEnter Cipher Text: ");
scanf("%s",input);
printf("\n\nEnter the key value: ");
scanf("%s",key);
for(i=0,j=0;i<strlen(input);i++,j++)
{
if(j>=strlen(key))
{ j=0; }
value = (toupper(input[i])-64)-(toupper(key[j])-64);
if( value < 0)
{ value = value * -1;
printf("%c",65 + (value % 26));
}}
PROF.DEVANSHI DAVE
OUTPUT:
RESULT: Thus the Vigenere Cipher substitution technique had been implemented successfully.
PROF.DEVANSHI DAVE
PRACTICAL: 6
6. Write a Program to implement the rail fence transposition technique.
DESCRIPTION:
In the rail fence cipher, the plain text is written downwards and diagonally on
successive "rails" of an imaginary fence, then moving up when we reach the
bottom rail. When we reach the top rail, the message is written downwards again until
the whole plaintext is written out. The message is then read off in rows.
EXAMPLE:
ALGORITHM:
STEP-1: Read the Plain text.
STEP-2: Arrange the plain text in row columnar matrix format.
STEP-3: Now read the keyword depending on the number of columns of the plain
text.
STEP-4: Arrange the characters of the keyword in sorted order and the
corresponding columns of the plain text.
STEP-5: Read the characters row wise or column wise in the former order to get
the cipher text.
PROF.DEVANSHI DAVE
PROGRAM: (Rail Fence)
#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("\n\t\t RAIL FENCE
TECHNIQUE"); printf("\n\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
PROF.DEVANSHI DAVE
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();
}
PROF.DEVANSHI DAVE
OUTPUT:
RESULT:
Thus the rail fence algorithm had been executed successfully