Академический Документы
Профессиональный Документы
Культура Документы
Practical-1 & 2
AIM: Implement Caesar cipher for encryption-decryption.
#include<stdio.h>
void encryption()
int i,key;
char text[100];
printf("Enter string\n");
gets(text);
printf("Enter Key\n");
scanf("%d",&key);
for(i=0;i<strlen(text);i++)
text[i]=text[i]+key;
if(text[i]>'z')
text[i]=text[i]-26;
}
170160116034 7IT-A2
void decryption()
int i,key;
char text[100],ctext[100];
printf("\nDecryption\n");
strcpy(ctext,text);
for(key=0;key<26;key++)
for(i=0;i<strlen(text);i++)
ctext[i]=text[i]-key;
if(ctext[i]<'a')
ctext[i]=ctext[i]+26;
void main()
encryption();
decryption();
170160116034 7IT-A2
Output:
Practical-3
AIM: Implement Monoalphabetic cipher for encryption-decryption.
#include <stdio.h>
#include <stdlib.h>
int main()
{ int i,j,m,n;
char text[100],key[26],ntext[100],cipher[100];
char
170160116034 7IT-A2
alpha[]={'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'};
gets(text);
gets(key);
i=0;j=0;
while(text[i]!='\0')
if(text[i]!=' ')
ntext[j]=text[i];
j++;
i++;
for(m=0;m<strlen(ntext);m++)
for(n=0;n<strlen(alpha);n++)
if(ntext[m]==alpha[n])
cipher[m]=key[n];
printf("%c",cipher[m]);
}
170160116034 7IT-A2
return 0;
Output:
170160116034 7IT-A2
Practical-4
AIM: Implement Polyalphabetic(Vigener) cipher for encryption-decryption.
#include <stdio.h>
#include <stdlib.h
#include <string.h>
int main()
int n=0,i,j=0,k=0,x,y,m,p,q,r,l,g,h,d,e;
int N[26],T[100],K[100],C[100];
char text[100],ntext[100],key[10],nkey[100],cipher[100];
char
alpha[]={'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'};
for(i=0;i<26;i++)
N[i]=n;
n++;
gets(text);
printf("Original Length:%d\n",strlen(text));
gets(key);
while(text[j]!='\0')
{
170160116034 7IT-A2
ntext[k]=text[j];
k++;
j++;
printf("New Length:%d\n",strlen(ntext));
for(x=0,y=0;x<strlen(ntext);x++,y++)
if(y==strlen(key))
y=0;
nkey[x]=key[y];
for(m=0;m<strlen(alpha);m++)
for(p=0;p<strlen(ntext);p++)
if(alpha[m]==ntext[p])
T[p]=N[m];
for(q=0;q<strlen(alpha);q++)
{
170160116034 7IT-A2
for(r=0;r<strlen(nkey);r++)
if(alpha[q]==nkey[r])
K[r]=N[q];
puts(ntext);
puts(nkey);
for(l=0;l<strlen(ntext);l++)
C[l]=(T[l]+K[l])%26;
for(g=0;g<strlen(alpha);g++)
for(h=0;h<strlen(ntext);h++)
if(N[g]==C[h])
cipher[h]=alpha[g];
}
170160116034 7IT-A2
puts(cipher);
return 0;
Output:
170160116034 7IT-A2
Practical-5
AIM: Implement One Time Pad (Vernam cipher) for encryption-decryption.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
char text[100],ntext[100],key[100],cipher[100];
int i,n=0,j=0,k=0,m,q,r,p,g,h,c,d,e;
int T[100],K[100],N[26],C[100];
char
alpha[]={'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'};
for(i=0;i<26;i++)
N[i]=n;
n++;
gets(text);
printf("Original Length:%d\n",strlen(text));
while(text[j]!='\0')
ntext[k]=text[j];
k++;
170160116034 7IT-A2
j++;
for(g=0;g<strlen(alpha);g++)
for(h=0;h<strlen(ntext);h++)
if(alpha[g]==ntext[h])
T[h]=N[g];
printf("New Length:%d\n",strlen(ntext));
for(m=0;m<strlen(ntext);m++)
K[m]=rand()%26;
for(q=0;q<strlen(alpha);q++)
for(r=0;r<strlen(ntext);r++)
if(N[q]==K[r])
key[r]=alpha[q];
170160116034 7IT-A2
puts(key);
for(c=0;c<strlen(ntext);c++)
C[c]=(T[c]+K[c])%26;
for(d=0;d<strlen(alpha);d++)
for(e=0;e<strlen(ntext);e++)
if(N[d]==C[e])
cipher[e]=alpha[d];
puts(cipher);
return 0;
Output:
170160116034 7IT-A2
170160116034 7IT-A2
Practical-6
AIM:Implement Playfair cipher encryption-decryption.
C Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#define MX 5
int i,j,w,x,y,z;
FILE *out;
if((out=fopen("cipher.txt","a+"))==NULL)
printf("File Currupted.");
for(i=0;i<MX;i++)
for(j=0;j<MX;j++)
if(ch1==key[i][j])
w=i;
x=j;
170160116034 7IT-A2
else if(ch2==key[i][j])
y=i;
z=j;
//printf("%d%d %d%d",w,x,y,z);
if(w==y)
x=(x+1)%5;z=(z+1)%5;
printf("%c%c",key[w][x],key[y][z]);
fprintf(out, "%c%c",key[w][x],key[y][z]);
else if(x==z)
w=(w+1)%5;y=(y+1)%5;
printf("%c%c",key[w][x],key[y][z]);
fprintf(out, "%c%c",key[w][x],key[y][z]);
else
printf("%c%c",key[w][z],key[y][x]);
fprintf(out, "%c%c",key[w][z],key[y][x]);
}
170160116034 7IT-A2
fclose(out);
void main()
int i,j,k=0,l,m=0,n;
char key[MX][MX],keyminus[25],keystr[10],str[25]={0};
char
alpa[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'};
printf("\nEnter key:");
gets(keystr);
gets(str);
n=strlen(keystr);
if(keystr[i]=='j')keystr[i]='i';
else if(keystr[i]=='J')keystr[i]='I';
keystr[i] = toupper(keystr[i]);
{
170160116034 7IT-A2
if(str[i]=='j')str[i]='i';
else if(str[i]=='J')str[i]='I';
str[i] = toupper(str[i]);
j=0;
for(i=0;i<26;i++)
for(k=0;k<n;k++)
if(keystr[k]==alpa[i])
break;
else if(alpa[i]=='J')
break;
if(k==n)
keyminus[j]=alpa[i];j++;
k=0;
170160116034 7IT-A2
for(i=0;i<MX;i++)
for(j=0;j<MX;j++)
if(k<n)
key[i][j]=keystr[k];
k++;}
else
key[i][j]=keyminus[m];m++;
printf("%c ",key[i][j]);
printf("\n");
for(i=0;i<strlen(str);i++)
if(str[i]=='J')str[i]='I';
if(str[i+1]=='\0')
playfair(str[i],'X',key);
else
if(str[i+1]=='J')str[i+1]='I';
if(str[i]==str[i+1])
170160116034 7IT-A2
playfair(str[i],'X',key);
else
playfair(str[i],str[i+1],key);i++;
getch();
Output:
170160116034 7IT-A2
PRACTICAL: 7
AIM: Implement Hill Cipher Encryption-Decryption.
#include<stdio.h>
#include<math.h>
void encryption();
void decryption();
void getKeyMessage();
void inverse();
int main() {
int choice;
//clrscr();
while(1)
scanf("%d",&choice);
switch(choice)
case 1:
encryption();
break;
170160116034 7IT-A2
case 2:
decryption();
break;
case 3:
break;
default:
if(choice==3)
return 0;
void encryption() {
int i, j, k;
getKeyMessage();
printf("\n");
void decryption() {
int i, j, k;
getKeyMessage();
inverse();
printf("\n");
void getKeyMessage() {
int i, j;
char msg[3];
170160116034 7IT-A2
scanf("%f", &a[i][j]);
c[i][j] = a[i][j];
scanf("%s", msg);
void inverse() {
int i, j, k;
float p, q;
printf("\n");
if(i == j)
b[i][j]=1;
170160116034 7IT-A2
else
b[i][j]=0;
printf("\t %d",b[i][j]);
p = c[i][k];
q = c[k][k];
if(i != k) {
printf("\n");
Output:
170160116034 7IT-A2
170160116034 7IT-A2
Practical-8
AIM: Implement Diffie-Hellmen key exchange method.
C Code:
#include <stdio.h>
#include <stdlib.h>
int main()
int q,p;
printf("Enter Public values :\n");//As 2nd no. is primitive root of 1st no.
scanf("%d %d",&q,&p);
int Apr,Bpr;
scanf("%d %d",&Apr,&Bpr);
int x=1;
for(int i=0;i<Apr;i++)
x=x*p;
x=x%q;
int y=1;
for(int j=0;j<Bpr;j++)
170160116034 7IT-A2
y=y*p;
y=y%q;
printf("\nkey Exchange");
int t=y;
y=x;
x=t;
int kA=1;
for(int i=0;i<Apr;i++)
kA=kA*x;
kA=kA%q;
int kB=1;
for(int j=0;j<Bpr;j++)
kB=kB*y;
kB=kB%q;
if(kA==kB)
170160116034 7IT-A2
else
return 0;
Output:
170160116034 7IT-A2
PRACTICAL:9
AIM: Implement Extended Euclidean algorithm for Finding multiplicative
inverse in GF(2n).
#include <stdio.h>
#include <conio.h>
int m0 = m;
int y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1)
int q = a / m;
int t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;
if (x < 0)
x += m0;
170160116034 7IT-A2
return x;
int main()
int a,m;
scanf("%d %d",&a,&m);
modInverse(a, m));
getch();
return 0;
Output:
170160116034 7IT-A2
Practical-10
AIM: Implement RSA encryption-decryption algorithm.
C Code
#include <stdio.h>
#include <stdlib.h>
int main()
int p,q;
scanf("%d\t%d",&p,&q);
int n=p*q;
int fi=(p-1)*(q-1);
printf("\nn=%d\t fi=%d\n",n,fi);
for(int i=2;i<fi;i++)
int E[fi],j=0;
if(gcd(i,fi)==1)
E[j]=i;
printf("\ne=%d",E[j]);
j++;
int e;
scanf("\n%d",&e);
int d;
for(int k=1;k<fi;k++)
if((k*e%fi)==1)
d=k;
printf("\nd=%d",d);
break;
int m;
scanf("%d",&m);
printf("Encryption");
int s=1;
for(int t=1;t<=e;t++)
s=s*m;
s=s%n;
printf("\nDecryption");
int r=1;
for(int q=1;q<=d;q++)
170160116034 7IT-A2
r=r*s;
r=r%n;
return 0;
while(b!=0)
int t=b;
b=a%b;
a=t;
return a;
Output:
170160116034 7IT-A2
170160116034 7IT-A2
Practical-11
AIM: Perform techniques SHA-1hash and digital signature algorithm with
Cryptool.
Steps:
1.Click on Digital Signature/PKI, which would lead you to the windows shown.
4.We would get our desired values and thus the Key is Generated.
170160116034 7IT-A2
6.After Completing all steps pop-up will show that Digital signature is generated
using SHA-1.
170160116034 7IT-A2
170160116034 7IT-A2
Practical-12
AIM: Perform AES or DES encryption-decryption techniques with Cryptool.
Steps:
1.On one file write Plain text and use other for Password.
4.Enter the key generated and than hit Encryption to Encrypt and Decryption to
Decrypt.
170160116034 7IT-A2
170160116034 7IT-A2
Practical:13
1.First is UDP.