CNS 41 (File)
CNS 41 (File)
Practical – 1
Source Code:
// encryption
#include<stdio.h>
int main()
{
char message[100], ch;
int i, key;
printf("Enter a message to encrypt: ");
gets(message);
printf("Enter key: ");
scanf("%d", &key);
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch + key;
if(ch > 'z'){
ch = ch - 'z' + 'a' - 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch + key;
if(ch > 'Z'){
ch = ch - 'Z' + 'A' - 1;
}
message[i] = ch;
}
}
printf("Encrypted message: %s", message);
return 0;
}
//DECRYPTION
#include<stdio.h>
int main()
{
char message[100], ch;
int i, key;
printf("Enter a message to decrypt: ");
gets(message);
printf("Enter key: ");
scanf("%d", &key);
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch - key;
if(ch < 'a'){
ch = ch + 'z' - 'a' + 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch - key;
if(ch < 'A'){
ch = ch + 'Z' - 'A' + 1;
}
message[i] = ch;
}
}
printf("Decrypted message: %s", message);
return 0;
}
Output:
Practical – 2
Aim: Implement monoalphabetic cipher encryption-decryption.
Source Code:
char
pt[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
ct[26]={'Q','W','E','R','T','Y','U','I','O','P','A','S','D','F','G','H','J','K','L','Z','X','C','V
','B','N','M'};
char p[20]={'\0'},c[20]={'\0'},r[20]={'\0'};
int i,j;
for(j=0;j<26;j++)
{
if(ct[j]==c[i])
{
r[i]=pt[j];
}
}
}
printf("\n \n plain text is: %s",r);
getch();
}
Output:
Practical – 3
Source Code:
//implement polyalphabetic cipher encryption-decryption.
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char msg[30],key[30],k[20],ct[20],pt[20];
int lenm,lenk,i,j;
Output:
Practical – 4
Aim: Implement playfair cipher encryption-decryption.
Source Code:
gets(key);
//converting entered key to Capital letters
for(i=0,j=0;i<strlen(key);i++)
{
if(key[i]!=' ')
{
key[j]=toupper(key[i]);
j++;
}
}
key[j]='\0';
printf("%s\n",key);
//Storing key in terms of ascii
k=0;
for(i=0;i<strlen(key)+26;i++)
{
if(i<strlen(key))
{
if(key[i]=='J')
{
flag=8;
printf("%d",flag);
}
numkey[i]=key[i]-'A';
}
else
{
if(k!=9 && k!=flag)//Considering I=J and taking I in place of J except
when J is there in key ignoring I
{
numkey[i]=k;
}
k++;
}
}
templen=i;
lenkey=removerepeated(templen,numkey);
printf("Entered key converted according to Play Fair Cipher rule\n");
for(i=0;i<lenkey;i++)
{
printf("%c",numkey[i]+'A');
}
printf("\n");
//Arranging the key in 5x5 grid
k=0;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
cipherkey[i][j]=numkey[k];
k++;
}
}
printf("Arranged key\n");
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
printf("%c ",cipherkey[i][j]+'A');
}
printf("\n");
}
//Message Processing
for(i=0;i<lennumstr;i+=2)
{
if(numstr[i]==numstr[i+1])
{
insertelementat(i+1,numstr,lennumstr);
lennumstr++;
}
}
if(lennumstr%2!=0)
{
insertelementat(lennumstr,numstr,lennumstr);
lennumstr++;
}
printf("Entered String/Message After Processing according to Play fair
cipher rule\n");
for(i=0;i<lennumstr;i++)
{
printf("%c",numstr[i]+'A');
}
for(k=0;k<lennumstr;k+=2)
{
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(numstr[k]==cipherkey[i][j])
{
row1=i;
col1=j;
}
if(numstr[k+1]==cipherkey[i][j])
{
row2=i;
col2=j;
}
}
}
//Only change between Ecryption to decryption is changing + to -
//If negative add 5 to that row or column
if(row1==row2)
{
col1=(col1-1)%5;
col2=(col2-1)%5;
if(col1<0)
{
col1=5+col1;
}
if(col2<0)
{
col2=5+col2;
}
numcipher[k]=cipherkey[row1][col1];
numcipher[k+1]=cipherkey[row2][col2];
}
if(col1==col2)
{
row1=(row1-1)%5;
row2=(row2-1)%5;
if(row1<0)
{
row1=5+row1;
}
if(row2<0)
{
row2=5+row2;
}
numcipher[k]=cipherkey[row1][col1];
numcipher[k+1]=cipherkey[row2][col2];
}
if(row1!=row2&&col1!=col2)
{
numcipher[k]=cipherkey[row1][col2];
numcipher[k+1]=cipherkey[row2][col1];
}
}
printf("\nCipher Text is\n");
for(i=0;i<lennumstr;i++)
{
if((numcipher[i]+'A')!='X')//Should remove extra 'X' which were created
during Encryption
printf("%c",numcipher[i]+'A');
}
printf("\n");
}
int removerepeated(int size,int a[])
{
int i,j,k;
for(i=0;i<size;i++)
{
for(j=i+1;j<size;)
{
if(a[i]==a[j])
{
for(k=j;k<size;k++)
{
a[k]=a[k+1];
}
size--;
}
else
{
j++;
}
}
}
return(size);
}
int insertelementat(int position,int a[],int size)
{
int i,insitem=23,temp[size+1];
for(i=0;i<=size;i++)
{
if(i<position)
{
temp[i]=a[i];
}
if(i>position)
{
temp[i]=a[i-1];
}
if(i==position)
{
temp[i]=insitem;
}
}
for(i=0;i<=size;i++)
{
a[i]=temp[i];
}
}
Output:
Practical – 5
Aim: Implement Hill cipher encryption-decryption.
Source Code:
}
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
b[i][j] = b[i][j]/c[i][i];
Output:
Practical – 6
Aim: Implement Rail Fence and transposition cipher. Both are permutation
cipher. Analyze strength of the cipher in terms of cryptanalysis.
Source Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main()
{
int i,j,len,rails,count,code[100][1000];
char str[1000];
printf("Enter a Secret Message\n");
gets(str);
len=strlen(str);
printf("Enter number of rails\n");
scanf("%d",&rails);
for(i=0;i<rails;i++)
{
for(j=0;j<len;j++)
{
code[i][j]=0;
}
}
count=0;
j=0;
while(j<len)
{
if(count%2==0)
{
for(i=0;i<rails;i++)
{
//strcpy(code[i][j],str[j]);
code[i][j]=(int)str[j];
j++;
}
else
{
for(i=rails-2;i>0;i--)
{
code[i][j]=(int)str[j];
j++;
}
}
count++;
}
for(i=0;i<rails;i++)
{
for(j=0;j<len;j++)
{
if(code[i][j]!=0)
printf("%c",code[i][j]);
}
}
printf("\n");
}
Output:
Practical – 7
Source Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
m[i]=msg[i];
n=p*q;
t=(p-1)*(q-1);
ce();
printf("\nPOSSIBLE VALUES OF e AND d ARE\n");
for(i=0;i<j-1;i++)
printf("\n%ld\t%ld",e[i],d[i]);
encrypt();
decrypt();
return 0;
}
void ce()
{
int k;
k=0;
for(i=2;i<t;i++)
{
if(t%i==0)
continue;
flag=prime(i);
if(flag==1&&i!=p&&i!=q)
{
e[k]=i; flag=cd(e[k]);
if(flag>0)
{
d[k]=flag;
k++;
}
if(k==99)
break;
}
}
}
void encrypt()
{
long int pt,ct,key=e[0],k,len;
i=0;
len=strlen(msg);
while(i!=len)
{
pt=m[i];
pt=pt-96;
k=1;
for(j=0;j<key;j++)
{
k=k*pt;
k=k%n;
}
temp[i]=k;
ct=k+96;
en[i]=ct;
i++;
}
en[i]=-1;
printf("\nTHE ENCRYPTED MESSAGE IS\n");
for(i=0;en[i]!=-1;i++)
printf("%c",en[i]);
}
void decrypt()
{
long int pt,ct,key=d[0],k;
i=0;
while(en[i]!=-1)
{
ct=temp[i];
k=1;
for(j=0;j<key;j++)
{
k=k*ct;
k=k%n;
}
pt=k+96;
m[i]=pt;
i++;
}
m[i]=-1;
printf("\nTHE DECRYPTED MESSAGE IS\n");
for(i=0;m[i]!=-1;i++)
printf("%c",m[i]);
}
Output:-
Practical – 8
Aim: Implement Euclid Algorithm to find GCD.
Source code:-
#include <stdio.h>
int main() {
int m, n; /* given numbers */
Output:-
Practical – 9
Aim: Generate random number of 32 bits. Use different random
number generation algorithms. Which method gives the best?
Random number must pass 3 tests (1) Uniformity (2) Scalability (3)
Consistency. First method Linear congruential generator Xn+1=
(aXn+c) mod m. m, a, c, X0 are integers. Second method: Blum Blum
shub generator.
Source code:-
#include<stdio.h>
#include<stdint.h>
int linearCong(int xo, int m, int a, int c, long randNums[], int n)
{
int i;
randNums[0] = xo;
for(i=1; i<n; i++)
randNums[i] = ((randNums[i-1] * a) + c) % m;
return 0;
}
int printIn32Bit(long n, int bit)
{
if(bit >= 8 * sizeof(n))
return 0;
printIn32Bit(n/2, bit+1);
printf("%ld", n%2);
if(bit%8 == 0)
printf(" ");
if(bit == 0)
printf("\n");
}
int main()
{
int xo, m, c, a, n, i;
printf("\nSeed Value \t\t :");
scanf("%d", &xo);
printf("Modoulo value \t\t :");
scanf("%d", &m);
printf("Multiplier term \t :");
scanf("%d", &a);
long randNums[n];
linearCong(xo, m, a, c, randNums, n);
printf("\n\nRandom 32 Bit integers using Linear Congruential
Generator\n\n");
for(i=0; i<n; i++)
{
uint32_t x1 = randNums[i];
printf("%5d. %5u :",i+1, x1);
printIn32Bit(x1, 0);
}
printf("\n\n");
return 0;
}
Output:-
Practical-10
Aim: Implement Diffi-Hellman Key Exchange Method.
Source code:-
#include<stdio.h>
#include<math.h>
long long int power(long long int a, long long int b,long long int P)
{
if (b == 1)
return a;
else
return (((long long int)pow(a, b)) % P);
}
int main()
{
long long int P, G, x, a, y, b, ka, kb;
// Both the persons will be agreed upon the public keys G and P
P = 23; // A prime number P is taken
printf("The value of P : %lld\n", P);
return 0;
}
Output:-
Practical-11
Aim: Implement a digital signature algorithm.
Source code:-
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <stdlib.h>
long int findGCD(long int, long int);
long int multipicative_inverse(long int, long int);
void key_pair(long int e, long int n, long int d)
{
printf("\nEncryption key (Private Key Pair) (e,n)\t :(%ld, %ld)", d, n);
printf("\nDecryption key (Public Key Pair) (d,n)\t : (%ld, %ld)\n", e, n);
}
long int multipicative_inverse(long int m, long int b)
{
long int a1 = 1, a2 = 0, a3 = m, b1 = 0, b2 = 1, b3 = b, t1, t2, t3, q;
top:
if (b3 == 0)
return 0;
else if (b3 == 1)
{
if (b2 < 0)
return (b2 + m);
else
return b2;
}
q = a3 / b3;
t1 = b1;
t2 = b2;
t3 = b3;
b1 = a1 - (q * b1);
b2 = a2 - (q * b2);
b3 = a3 - (q * b3);
a1 = t1;
a2 = t2;
a3 = t3;
// printf("\nb2=%ld\n",b2);
goto top;
}
long int findGCD(long int e, long int fn)
{
long int c = e, d = fn;
long int r;
while (d != 0)
{
r = c % d;
c = d;
d = r;
}
return c;
}
int main()
{
long int pt, p, q, n, fn, e = 2, temp, d, ct;
printf("\n\nGenerating Signature\n\n");
printf("\nEnter your signature to encrypt\t\t :");
scanf("%d", &pt);
printf("\nEnter two prime numbers p and q\t\t :");
scanf("%ld %ld", &p, &q);
n = p * q;
fn = (p - 1) * (q - 1);
temp = findGCD(e, fn);
while (temp != 1)
{
e++;
temp = findGCD(e, fn);
}
d = multipicative_inverse(fn, e);
if (d == 0)
{
printf("\nKey generation is not possible for given p and q.\n\n");
exit(0);
}
ct = fmod(pow(pt, d), n);
key_pair(e, n, d);
printf("\n\nGenerated Encrypted signature text\t :%ld", ct);
printf("\n\n\nVerifying Signature\n\n");
printf("Encrypted Signature\t\t\t :%ld", ct);
printf("\nEnter Public Key Pair (e,n)\t\t : (%ld, %ld)", e, n);
pt = fmod(pow(ct, e), n);
printf("\n\n\nDecrypted signature\t\t\t : %ld", pt);
printf("\n\nVERIFIED\n\n");
return 0;
}
Output:-
Practical-12
Aim: Read traffic going on network. Analyze the traffic, Connect to
internet and read what is going on internet. Hint: Use Wireshark.
Steps:-
Select Packet. And then we can see all the details of packet.