[Type text]
PROGRAM NO: 1
AIM : Write a program to do the CEASER CIPHER encoding.
SOURCE CODE :
#include<stdio.h>
# include<conio.h>
#include<string.h>
void main()
{
//to hold plain text fed in by the user
char PT[50];
//to hold the cipher text
char CT[50];
char ch;
int len,i;
clrscr();
//read the plain text from the user
printf("Enter the plain text : ");
gets(PT);
len=strlen(PT);
//code to do the CEASER CIPHER
for(i=0;i<len;i++)
{
ch=PT[i]+3;
//to wrap up to the other side if range exceeds
if(ch>'Z')
CT[i]=ch-26;
else
CT[i]=ch;
if(ch>'z')
CT[i]=ch-26;
[Type text]
else
CT[i]=ch;
}
printf(("The CIPHER TEXT is as : ");
printf("%s",CT);
getch();
OUTPUT :
Enter the plain text : ABCxyz
The CIPHER TEXT is as : DEFabc
[Type text]
PROGRAM NO: 2
AIM : Write a program to do the RAIL FENCING encoding.
SOURCE CODE :
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
//to hold plain text fed in by the user
char PT[50];
//to hold the cipher text
char CT1[50];
char CT2[50];
//other variables declared
int len , i,j,k;
j=k=0;
clrscr();
//read the plain text from the user
printf("Enter the plain text : ");
gets(PT);
len=strlen(PT);
//code to do the RAIL FENCING encoding
for(i=0;i<len;i++)
{
//check if index is even
if(i%2 == 0)
{
CT1[j]=PT[i];
j++;
}
[Type text]
//executed if index is odd
else
{
CT2[k]=PT[i];
k++;
printf("The CIPHER TEXT IS AS FOLLOWS : ");
printf("%s",CT1);
printf("%s",CT2);
getch( );
OUTPUT :
Enter the plain text : railfencing
The CIPHER TEXT IS AS FOLLOWS : rifnigalecn
[Type text]
PROGRAM NO: 3
AIM : Write a program to do the HILL CIPHER encoding.
SOURCE CODE :
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
//to hold plain text fed in by the user
char PT[3];
// to hold cipher text generated
char CT[3];
//to hold the converted character array into corresponding integer matrix
int inp[3][1];
//a matrix holding random elements
int mat1[3][3]={6,24,1,13,16,10,20,17,15};
//to hold the integer result
int res[3][1];
//other variables declared
int i,j,k;
clrscr();
//to read the input plain text
printf("Enter the plain text(of three alphabets):");
gets(PT);
//code to do the HILL CIPHER
//loop to convert the character array into corresponding integer matrix
for(i=0;i<3;i++)
{
if(PT[i]>='A' && PT[i]<='Z')
[Type text]
inp[i][0]=PT[i]-65;
//multiply the random matrix with the above integer matrix
for(i=0;i<3;i++)
{
for(j=0;j<1;j++)
{
res[i][j]=0;
for(k=0;k<3;k++)
{
res[i][j]+=mat1[i][k]*inp[k][j];
}
}
printf("The CIPHER TEXT IS AS FOLLOWS : ");
//to convert the integer matrix into corresponding character - CIPHER TEXT
for(i=0;i<3;i++)
{
CT[i]=(res[i][0]%26)+65;
printf("%c",CT[i]);
}
getch();
}
OUTPUT :
Enter the plain text(of three alphabets):CAT
The CIPHER TEXT IS AS FOLLOWS : FIN
[Type text]
PROGRAM NO: 4
AIM : Write a program to do the SIMPLE COLOUMN TRANSPOSITION encoding.
SOURCE CODE :
#include<stdio.h>
#include<conio.h>
# include<string.h>
void main()
{
//to hold plain text fed in by the user
char PT[50];
//to hold the cipher text
char CT[80];
//other variables declared
int i,j,len;
j=0;
clrscr();
//initialize character array
for(i=0;i<80;i++)
CT[i]=' ';
//read the plain text from the user
printf("Enter the plain text : \n");
gets(PT);
len=strlen(PT);
// code to do the SIMPLE COLOUMN TRANSPOSITION
//to divide the input according to index
for(i=0;i<len;i++)
{
//if index is 1
[Type text]
if(i%4==1)
{
CT[j]=PT[i];
j++;
}
}
for(i=0;i<len;i++)
{
//if index is 3
if(i%4==3)
{
CT[j]=PT[i];
j++;
}
}
for(i=0;i<len;i++)
{
//if index is 2
if(i%4==2)
{
CT[j]=PT[i];
j++;
}
}
for(i=0;i<len;i++)
{
//if index is 4
if(i%4==0)
{
CT[j]=PT[i];
j++;
}
}
printf("The CIPHER TEXT is as follows : " );
//display the cipher text
for(i=0;i<len;i++)
printf("%c",CT[i]);
getch();
}
[Type text]
OUTPUT :
Enter the plain text :
mysimpleculoumntransposition
he CIPHER TEXT is as follows : ypumaoiieotsinsllnnsommcurpt