Вы находитесь на странице: 1из 49

170160116034 7IT-A2

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++)

if(text[i]>='a' && text[i]<='z')

text[i]=text[i]+key;

if(text[i]>'z')

text[i]=text[i]-26;

printf("Encrypted message is: %s",&text);

}
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++)

if(text[i]>='a' && text[i]<='z')

ctext[i]=text[i]-key;

if(ctext[i]<'a')

ctext[i]=ctext[i]+26;

printf("\nDecrypted message is: %s\n",&ctext);

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'};

printf("Enter Plaintext: ");

gets(text);

printf("Enter key: ");

gets(key);

i=0;j=0;

while(text[i]!='\0')

if(text[i]!=' ')

ntext[j]=text[i];

j++;

i++;

printf("Cipher text: ");

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++;

printf("Enter Plaintext: ");

gets(text);

printf("Original Length:%d\n",strlen(text));

printf("Enter Key: ");

gets(key);

while(text[j]!='\0')

if(text[j]>='a' && text[j]<='z')

{
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];

printf("New text: ");

puts(ntext);

printf("New Key: ");

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

printf("Encrypted Message: ");

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++;

printf("Enter Plaintext: ");

gets(text);

printf("Original Length:%d\n",strlen(text));

while(text[j]!='\0')

if(text[j]>='a' && text[j]<='z')

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;

printf("Generated Key: ");

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);

printf("\nEncrypted text: ");

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

void playfair(char ch1,char ch2, char key[MX][MX])

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);

printf("\nEnter the plain text:");

gets(str);

n=strlen(keystr);

for (i=0; i<n; i++)

if(keystr[i]=='j')keystr[i]='i';

else if(keystr[i]=='J')keystr[i]='I';

keystr[i] = toupper(keystr[i]);

for (i=0; i<strlen(str); 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++;

//construct key keymatrix

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");

printf("\n\nEntered text :%s\nCipher Text :",str);

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>

float encrypt[3][1], decrypt[3][1], a[3][3], b[3][3], mes[3][1], c[3][3];

void encryption();

void decryption();

void getKeyMessage();

void inverse();

int main() {

int choice;

//clrscr();

while(1)

printf("Enter Your choice:\n1.Encrypt\t2.Decrypt\t3.Exit\n");

scanf("%d",&choice);

switch(choice)

case 1:

encryption();

break;
170160116034 7IT-A2

case 2:

decryption();

break;

case 3:

break;

default:

printf("Enter a valid choice\n");

if(choice==3)

return 0;

void encryption() {

int i, j, k;

getKeyMessage();

for(i = 0; i < 3; i++)

for(j = 0; j < 1; j++)

for(k = 0; k < 3; k++)

encrypt[i][j] = encrypt[i][j] + a[i][k] * mes[k][j];


170160116034 7IT-A2

printf("\nEncrypted string is: ");

for(i = 0; i < 3; i++)

printf("%c", (char)(fmod(encrypt[i][0], 26) + 97));

printf("\n");

void decryption() {

int i, j, k;

getKeyMessage();

inverse();

for(i = 0; i < 3; i++)

for(j = 0; j < 1; j++)

for(k = 0; k < 3; k++)

decrypt[i][j] = decrypt[i][j] + b[i][k] * encrypt[k][j];

printf("\nDecrypted string is: ");

for(i = 0; i < 3; i++)

printf("%c", (char)(fmod(decrypt[i][0], 26) + 97));

printf("\n");

void getKeyMessage() {

int i, j;

char msg[3];
170160116034 7IT-A2

printf("Enter 3x3 matrix for key (It should be inversible):\n");

for(i = 0; i < 3; i++)

for(j = 0; j < 3; j++) {

scanf("%f", &a[i][j]);

c[i][j] = a[i][j];

printf("\nEnter a 3 letter string: ");

scanf("%s", msg);

for(i = 0; i < 3; i++)

mes[i][0] = msg[i] - 97;

void inverse() {

int i, j, k;

float p, q;

for(i = 0; i < 3; i++)

printf("\n");

for(j = 0; j < 3; j++) {

if(i == j)

b[i][j]=1;
170160116034 7IT-A2

else

b[i][j]=0;

printf("\t %d",b[i][j]);

for(k = 0; k < 3; k++) {

for(i = 0; i < 3; i++) {

p = c[i][k];

q = c[k][k];

for(j = 0; j < 3; j++) {

if(i != k) {

c[i][j] = c[i][j]*q - p*c[k][j];

b[i][j] = b[i][j]*q - p*b[k][j];

for(i = 0; i < 3; i++)

for(j = 0; j < 3; j++)

b[i][j] = b[i][j] / c[i][i];

printf("\n\nInverse Matrix is:\n");

for(i = 0; i < 3; i++) {


170160116034 7IT-A2

for(j = 0; j < 3; j++)

printf("%d ", b[i][j]);

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);

printf("\nPublic value=%d\t Primitive root=%d",q,p);

int Apr,Bpr;

printf("\nEnter Private key for A & B :\n");

scanf("%d %d",&Apr,&Bpr);

printf("\nPrivate Key of A=%d\t Private key of B=%d",Apr,Bpr);

printf("\nCalculating Public values");

int x=1;

for(int i=0;i<Apr;i++)

x=x*p;

x=x%q;

printf("\nA's Public value: %d",x);

int y=1;

for(int j=0;j<Bpr;j++)
170160116034 7IT-A2

y=y*p;

y=y%q;

printf("\nB's Public value: %d",y);

printf("\nkey Exchange");

int t=y;

y=x;

x=t;

printf("\nA receives: %d\t B receives: %d",x,y);

printf("\nSymmetric key Computation");

int kA=1;

for(int i=0;i<Apr;i++)

kA=kA*x;

kA=kA%q;

printf("\nA's Shared Secret key: %d",kA);

int kB=1;

for(int j=0;j<Bpr;j++)

kB=kB*y;

kB=kB%q;

printf("\nB's Shared Secret key: %d",kB);

if(kA==kB)
170160116034 7IT-A2

printf("\nKey Exchanged Succesfully!!");

else

printf("\nKey Exchange failed");

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 modInverse(int a, int m)

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;

printf("Enter a and m:\n");

scanf("%d %d",&a,&m);

printf("Modular multiplicative inverse is %d\n",

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()

printf("Enter two prime no.s:\n");

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;

printf("\nChoose any one value to be e:");


170160116034 7IT-A2

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;

printf("\nEnter Value to be encrypted:\n");

scanf("%d",&m);

printf("Encryption");

int s=1;

for(int t=1;t<=e;t++)

s=s*m;

s=s%n;

printf("\nEncrypted Message for %d is %d",m,s);

printf("\nDecryption");

int r=1;

for(int q=1;q<=d;q++)
170160116034 7IT-A2

r=r*s;

r=r%n;

printf("\nDecrypted Message for %d is %d",s,r);

return 0;

int gcd(int a,int b)

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.

2.Click on Select hash function and select SHA-1.


170160116034 7IT-A2

3.Select Hash Function now and click on Generate Prime Number

4.We would get our desired values and thus the Key is Generated.
170160116034 7IT-A2

5.Generate RSA key


170160116034 7IT-A2
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.

2.Now use any Hash function(used MD5 here) to generate key.


170160116034 7IT-A2

3.Choose Activity to be performed from the toolbar.


170160116034 7IT-A2

4.Enter the key generated and than hit Encryption to Encrypt and Decryption to
Decrypt.
170160116034 7IT-A2
170160116034 7IT-A2

Practical:13

Aim: - Use the Wireshark to study the various network protocols.

1. The Wireshark software window is shown above, and all the


processes on the network are carried within this screen only.
2. Then select Wi-Fi connection.

3.Next Search or select Protocol.


170160116034 7IT-A2

1.First is UDP.

2.Next Protocol is TCP.


170160116077 7IT-A4

3.Next Protocol is HTTP.

4.Next Protocol is ARP.


170160116077 7IT-A4

5.Next Protocol is ICMPV6.

Вам также может понравиться