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

Is Print Ready

The document contains details of 7 practical assignments related to implementing different ciphers in C programming language. The 7 ciphers are: 1) Caesar cipher 2) Monoalphabetic cipher 3) Playfair cipher 4) Hill cipher 5) Railfence cipher 6) Columnar cipher 7) RSA cipher. For each cipher, the document provides the code implementation along with sample input, output and explanation.

Uploaded by

Milan Manwar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Is Print Ready

The document contains details of 7 practical assignments related to implementing different ciphers in C programming language. The 7 ciphers are: 1) Caesar cipher 2) Monoalphabetic cipher 3) Playfair cipher 4) Hill cipher 5) Railfence cipher 6) Columnar cipher 7) RSA cipher. For each cipher, the document provides the code implementation along with sample input, output and explanation.

Uploaded by

Milan Manwar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

1|100020107064

INTERNET SECURITY
>>INDEX<<

Sr. No.

Practical Name

Write a program to implement Ceaser cipher in C.

Write a program to implement monoalphabetic cipher in C.

Write a program to implement playfair cipher in C.

Write a program to implement Hill cipher in C.

Write a program to implement railfence cipher in C.

Write a program to implement columner cipher in C.

Write a program to implement RSA in C.

Signature

2|100020107064

Practical No. 1
AIM:- Write a program to implement Ceaser
cipher in C.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j,shift,k,temp;
char pt[100],ency[100];
char value(int); clrscr();
printf("Please Enter The PlainText=\n");
gets(pt);
printf("Please Enter The shift value=\n");
scanf("%d",&shift);
printf("\n \n The Encrypted Text is=\n");
for (i=0;i<strlen(pt);i++)
{
if (pt[i]==' ')
{
ency[i]=pt[i];
//printf("%c",pt[i]);
}
Else
{
if (pt[i]=='.')
{
ency[i]=pt[i];
//printf("%c",pt[i]);
}
else
{
temp=pt[i]+shift;
ency[i]=temp;
//printf("%c",temp);
}
}
}
puts(ency);
printf("\n \n The Decrypted Text is=\n");
for (i=0;i<strlen(pt);i++)
{
if (ency[i]==' ')
{
printf("%c",ency[i]);
}

3|100020107064

else
{
if (ency[i]=='.')
{
printf("%c",pt[i]);
}
else
{
temp=ency[i]-shift;
printf("%c",temp);
}
}
}
ency[i]='\0';
getch();
}

4|100020107064

Output:
Pleas Enter the Planitext=
Milan
Pleas Enter The Shift Value=
2
The Encrypted text is=
Oknco
The Decrypted Text is=
milan

5|100020107064

Practical No. 2
AIM:-Write a program to implement MONOALPHABETIC cipher in C.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,length,temp;
char a[100],encrypt[100],decrypt[100];
char cipher(char,int);
clrscr();
printf("Enter The PlainText=\n");
gets(a);
length=strlen(a);
temp=0;
printf("The Encrypted Message=\n");
for (i=0;i<length;i++)
{
encrypt[i]=cipher(a[i],temp); printf("%c",encrypt[i]);
}
encrypt[i]='\0'; temp=1;
printf("\nThe Decrypted Message=\n"); for (i=0;i<strlen(encrypt);i++)
{
decrypt[i]=cipher(encrypt[i],tem)
printf("%c",decrypt[i]);
}
getch();
}
char cipher(char ch,int i)
{
if (i==0)
{
if (ch!=' ')
{
if (ch=='.')
{
return(ch);
}
else
{
switch (ch)
{
case 'a':
return ('b');
case 'b':
return ('c');
case 'c':
return ('d');

6|100020107064

case 'd':
return ('e');
case 'e':
return ('f');
case 'f':
return ('g');
case 'g':
return ('h');
case 'h':
return ('i');
case 'i':
return ('j');
case 'j':
return ('k');
case 'k':
return ('l');
case 'l':
return('m');
case 'm':
return ('n');
case 'n':
return ('o');
case 'o':
return ('p');
case 'p':
return ('q');
case 'q':
return ('r');
case 'r':
return ('s');
case 's':
return ('t');
case 't':
return ('u');
case 'u':
return ('v');
case 'v':
return (w');
case 'w':
return ('x');
case 'x':
return ('y');
case 'y':
return ('z');
case 'z':
return ('a');
}
}}
else{return(' '); }
}
}

7|100020107064

Output:
Enter The Plaintext=
Milan
The Encrypted Message=
njmbo
The Decrypted Message=
milan

8|100020107064

Practical No. 3
AIM:-Write a program to implement Playfair cipher in C.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char key[10],a[5][5],pt[100],ct[100];
int row=0,col=0,x,y,i,j,flag=0;
clrscr();
printf(" enter key\n ");
gets(key);
for(i=0;i<strlen(key);i++)
{
flag=1;
for(j=0;j<i;j++)
{
if(key[i]==key[j])
{
//continue; flag=0;
}
}
if(flag==1)
{
a[row][col]=key[i
printf("
%c",a[row][col]); col++;
if(col==5)
{
printf("\n");
col=0;
row=row+1;
}
}
}
for(i=0;i<26;i++)
{ flag=1;
for(j=0;j<strlen(key);j++)
{

9|100020107064

if(key[j]==i+97)
{
flag=0;
}
}
if(flag==1 && i!=9)
{
a[row][col]=i+97;
printf(" %c",a[row][col]); col++;
if(col==5)
{
row=row+1;
col=0;
printf("\n");
}
}
}
printf("\n ");
printf("enter the plaintext:"); gets(pt);
j=1;
for(i=0;i<strlen(pt)-1;)
{
int r1=0,c1=0,r2=0,c2=0,p=0,q=0; x=pt[i];
y=pt[j];
if(x=='j') pt[i]='i';
if(y=='j') pt[j]='i';
for(p=0;p<5;p++)
{
for(q=0;q<5;q++)
{
if(a[p][q]==x)

10 | 1 0 0 0 2 0 1 0 7 0 6 4

{
r1=p;
c1=q;
}
if(a[p][q]==y)
{
r2=p;
c2=q;
}
}
}
if(r1==r2)
{
ct[i]=a[r1][(c1+1)%5]; ct[j]=a[r2][(c2+1)%5];
printf("%c%c",ct[i],ct[j]);
}
else if(c1==c2)
{
ct[i]=a[(r1+1)%5][c1];
ct[j]=a[(r2+1)%5][c2];
printf("%c%c",ct[i],ct[j]);
}
else
{
ct[i]=a[r1][c2];
ct[j]=a[r2][c1];
printf("%c%c",ct[i],ct[j]);
}
i+=2;
j+=2;
}
getch();
}

11 | 1 0 0 0 2 0 1 0 7 0 6 4

Output:
Enter Key:
Monkey
M

i/j

Enter The Plaintext : Milan


kfgd

12 | 1 0 0 0 2 0 1 0 7 0 6 4

Practical No.4
AIM:-Write a program to implement HILL CIPHER in C.
#include<stdio.h
>
#include<conio.
h> void main()
{
char
s[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char p[100],ency[100],decy[100];
int
i,j,a[10][10]={{6,24,1},{13,16,10},{20,17,15}},length,num[100],c[100],b[10][10]={{8,5,10},{21,8,21},{2
1,12,8}}; clrscr();
printf(" The Key is=\n
"); for (i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("
%d
",a[i][j]); printf("\n");
}
printf("Enter
The
plaintext=\n"); scanf("%s",p);
length=strlen(p);
for
(i=0;i<length;i++)
{
for (j=0;j<26;j++)
{
if

(s[j]==p[i])
num[i]
=j;

}
}
for (i=0;i<3;i++)
{
c[i]=0;
for
(j=0;j<3;j++)
c[i]=c[i]+(a[i][j]*num
[j]);
}
c[i]='\0';
printf("The Encryptrd
is=\n"); for (i=0;i<3;i++)
ency[i]=s[c[i]%

Message

13 | 1 0 0 0 2 0 1 0 7 0 6 4

26]; ency[i]='\0';
puts(ency);
printf(" The Inverse Key
is=\n "); for (i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("
%d
",b[i][j]); printf("\n");
}
for (i=0;i<length;i++)
{
for (j=0;j<26;j++)
{
if (s[j]==ency[i]) num[i]=j;
}
}
for (i=0;i<3;i++)
{
c[i]=0;
for (j=0;j<3;j++) c[i]=c[i]+(b[i][j]*num[j]);
}
c[i]='\0';
printf("The Decryptrd Message is=\n"); for (i=0;i<3;i++)
decy[i]=s[c[i]%26]; decy[i]='\0';
puts(decy); getch();
}

14 | 1 0 0 0 2 0 1 0 7 0 6 4

Output:
The Key is=
6

24

13

16

10

20

17

17

Enter The Plaintext=


Milan
The encrypted Message is=
Vep
The Inverse Key is=
8

10

21

21

21

12

The Decrypted Message is=


milan

15 | 1 0 0 0 2 0 1 0 7 0 6 4

Practical No.5
AIM:-Write a program to implement RAILFENCE cipher in C.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,length,j,k,l,len1,len2,temp1,temp2,q;
char a[100],text1[100],text2[100],
encrypt[100],decrypttext1[100],decrypttext2[100],decrypt[100]
clrscr();
printf("Enter The PlainText=\n");
gets(a);
length=strlen(a); j=0;
k=0;
for (i=0;i<length;i++)
{
if (i%2==0)
{
text1[j]=a[i];
j++;
}
else
{
text2[k]=a[i];
k++;
}
}
for (i=0;i<j;i++)
{
encrypt[i]=text1[i];
}

16 | 1 0 0 0 2 0 1 0 7 0 6 4

len1=i;
for (l=0;l<k;l++)
{
encrypt[i]=text2[l]; i++;
}
len2=l;
encrypt[i]='\0';
printf("The Encrypted Message is=\n");
printf("%s\n",encrypt);
for (i=0;i<len1;i++)
{
decrypttext1[i]=encrypt[i];
}
decrypttext1[i]='\0';
for (j=0;j<len2;j++)
{
decrypttext2[j]=encrypt[i];
i++;
}
decrypttext2[j]='\0';
q=0;
printf("Decrypted Message is=\n");
for (i=0;i<strlen(encrypt);i++)
{
if (i<=len1)
{
printf("%c",decrypttext1[i]);
}
if (i<=len2)
{
printf("%c",decrypttext2[i]);
}
}
decrypt[q]='\0';
getch();
}

17 | 1 0 0 0 2 0 1 0 7 0 6 4

Output:
Enter the plaintext=
Milan
The encrypted message is=
Mlnia
Decrypted message is=
milan

18 | 1 0 0 0 2 0 1 0 7 0 6 4

Practical No.6
AIM:-Write a program to implement COLUMNER cipher in C.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[100],b[100][100]={' '},ency[100]={' '},decy[100][100]={' '},temp[100][100]={' '};
int pattern[10]={7,0,9,5,1,6,3,4,2,8};
int length,i,j,k,l,m,condi,distance,p,length12,l1,l2;
clrscr();
printf("Enter The Message=\n");
gets(a);
length=strlen(a); condi=length;
k=0;
l1=0;
l2=0;
printf("The Encryption Message is=\n");
if (length%10==0)
{
for (i=0;i<(length/10);i++)
{
for (j=0;j<10;j++)
{
b[i][j]=a[k];
k++;
}
}
b[i][j]='\0';
l=0;
for (i=0;i<(length/10);i++)
{
l1=0;
for (j=0;j<10;j++)
{
m=pattern[j];
ency[l]=b[i][m];
temp[i][l1]=b[i][m];
printf("%c",temp[i][l1]);
l++;
l1++;
l2++;
}
}
temp[i][l1]='\0';
}
else if (length%10!=0)
{
distance=length%10;
distance=10-distance;

19 | 1 0 0 0 2 0 1 0 7 0 6 4

distance=distance+length;
for (i=length;i<distance;i++)
{
a[i]='X';
}
a[i]='\0';
length=strlen(a);
for (i=0;i<(length/10);i++)
{
for (j=0;j<10;j++)
{
b[i][j]=a[k];
k++
}
}
b[i][j]='\0';
for (i=0;i<(length/10);i++)
{
l1=0;
for (j=0;j<10;j++)
{
m=pattern[j];
ency[l]=b[i][m];
temp[i][l1]=b[i][m];
printf("%c",temp[i][l1]);
l++;
l1++;
l2++;
}
}
temp[i][l1]='\0';
}
printf("\nThe Decryption Message=\n");
printf("%d\n",length);
for (i=0;i<(l2/10);i++)
{
for (j=0;j<10;j++)
{
m=pattern[j];
decy[i][m]=temp[i][j];
}
}
p=0;
for (i=0;i<(l2/10);i++)
{
for (j=0;j<10;j++)
if (condi!=p)
{
printf("%c",decy[i][j]);
p++;
}
}
getch();
}

20 | 1 0 0 0 2 0 1 0 7 0 6 4

Output:
Enter the message=
Milan
The encryption message is=
Xmxxixanlx
The decryption message is=
milan

21 | 1 0 0 0 2 0 1 0 7 0 6 4

Practical No.7
AIM:-Write a program to implement RSA in C.
#include<stdio.h>
#include<conio.h>
void main()
{
int p,q,n,e,i;
int temp,temp1,d,pt,c,m;
clrscr();
printf("Enter The first Prime number=\n"); scanf("%d",&p);
printf("Enter The second Prime number=\n"); scanf("%d",&q);
n=p*q; q=(p-1)*(q-1); e=2;
while (e>1)
{
if (q%e!=0)
goto end;
else
e++;
}
end :
d=1;
temp=e-2;
d=((temp*q)+1)/e;
last:
printf("\n%d",d);
printf("\nThe Public Key is e={%d} and n={%d}\n",e,n);
printf("The Private Key is d={%d} and n={%d}\n",d,n);
printf("Enter The Plain Text=\n");
scanf("%d",&pt);
c=1;
printf("The cipher text is=\n");
for (i=0;i<e;i++)
c=c*pt%n;
c=c%n;
printf("%d",c); m=1;
printf("\nThe Decryeted text is=\n");
for (i=0;i<e;i++)
m=m*c%n;
m=m%n;
printf("%d\n",m);
getch();
}

22 | 1 0 0 0 2 0 1 0 7 0 6 4

Output:
Enter the first prime number=
13
Enter the second prime number=
17
115
The public key is e={5} and n={221}
The private key is d={115} and n={221}
Enter the plain text=
Milan
The cipher text is=
138
The decrypted text is=
115

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy