Nothing Special   »   [go: up one dir, main page]

201040107059IS Lab Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 39

Enrollment No:201040107059

LABORATORY MANUAL
Information Security (3170720)
COMPUTER ENGINEERING
B.E. 4th Year, SEM. VII (COMPUTER), Year 2023-24 (Odd Sem.)

DEPARTMENT OF COMPUTER ENGINEERING


GUJARAT POWER ENGINEERING AND RESEARCH INSTITUTE, MEHSANA

(APPROVED BY AICTE)

(Affiliated to Gujarat Technological University)

Near Toll Booth, Ahmedabad-Mehsana Express Way, Village-Mevad

District Mehsana-Gujarat

Contact No. 02762-292262/63


GUJARAT POWER ENGINEERING AND RESEARCH
INSTITUTE, MEHSANA
(A constituent of Gujarat Technological
University)

This is certify that the work carried out by Mr./Ms. Rupam Biswas
Enrollment No. 201040107059 student of Computer Engineering, semester V of session
2023-2024 has satisfactorily completed at Gujarat PowerEngineering and Research Institute,
Mehsana. This manual is record of his/her own work of subject Information Security carried
out under competent supervision and guidance.

Date of Submission: 01-11-2023

Faculty In-charge Head Of Department

Prof. Maitree Desai Prof. Avani Raval


3170720 201040107059

INDEX
Sr. Page
Experiment Title Date Sign
No. No.
1 Implement Ceaser Cipher Encryption-Decryption. 1 11-08-2023

Implement Monoalphabetic Cipher Encryption-Decryption. 4 18-08-2023


2
Implement Playfair Cipher Encryption-Decryption. 9 25-08-2023
3
4 Implement Polyalphabetic Cipher Encryption-Decryption. 14 01-09-2023

5 Implement Hill Cipher Encryption-Decryption. 16 08-09-2023

6 Implement Simple AES & DES. 20 22-09-2023

7 Implement Diffi-Hellman Key Exchange Method. 24 29-09-2023

8 Implement RSA Encryption-Decryption Algorithm. 26 06-10-2023

9 Write A Program To Generate SHA-1. 30 12-102023

10 Implement A Digital Signature Algorithm. 35 20-20-2023


3170720 201040107059

Practical:-1
Aim:- Implement Caesar cipher encryption-decryption. include<string.h>

Code(Encryption):-
#include <stdio.h>
#include <conio.h>
void main()
{
char msg[100], ch;
int key, i;
printf("Enter a message to encrypt:");
gets(msg);
printf("Enter key:");
scanf("%d", &key);
for (i = 0; msg[i] != '\0'; ++i)
{
ch = msg[i];
if (ch >= 'a' && ch <= 'z')
{
ch = ch + key;
if (ch > 'z')
{
ch = ch - 'z' + 'a' - 1;
}
msg[i] = ch;
}
else if (ch >= 'A' && ch <= 'Z')
{
ch = ch + key;

1
3170720 201040107059

if (ch > 'Z')


{
ch = ch - 'Z' + 'A' - 1;
}
msg[i] = ch;
}
}
printf("Encrypted Message is: %s", msg);
getch();
}

Output:-

Code(Deceryption):-
#include <stdio.h>
#include <conio.h>
void main()
{
char msg[100], ch;
int key, i;
printf("Enter a message to decrypt: ");
gets(msg);
printf("Enter key: ");
scanf("%d", &key);
for (i = 0; msg[i] != '\0'; ++i)
{

2
3170720 201040107059

ch = msg[i];
if (ch >= 'a' && ch <= 'z')
{
ch = ch - key;
if (ch < 'a')
{
ch = ch + 'z' - 'a' + 1;
}
msg[i] = ch;
}
else if (ch >= 'A' && ch <= 'Z')
{
ch = ch - key;
if (ch < 'A')
{
ch = ch + 'Z' - 'A' + 1;
}
msg[i] = ch;
}
}
printf("Decrypted Message is: %s", msg);
getch();
}

Output:-

3
3170720 201040107059

Practical:-2
Aim:- Implement Monoalphabetic cipher encryption-decryption.

Code:-
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
char keyl[26] = {'k', 'd', 'j', 'i', 'l', 'w', 'y', 'o', 'r', 't', 'a', 'c', 'e', 'h', 'b', 'u', 'x', 'v', 'p', 'z', 'n',
'q', 'f', 's', 'm', 'g'};
char keyu[26] = {'K', 'D', 'J', 'I', 'L', 'W', 'Y', 'O', 'R', 'T', 'A', 'C', 'E', 'H', 'B', 'U', 'X', 'V', 'P',
'Z', 'N', 'Q', 'F', 'S', 'M', 'G'};
void take_input(char *text)
{
int i = 0;
char c;
while (1)
{
c = getch();
if (c == 13)
{
text[i] = '\0';
break;
}
else if (c == 8)
{
putch('\b');
putch(' ');
putch('\b');

4
3170720 201040107059

i--;
}
else if (c != 32)
{
if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122))
{
printf("%c", c);
text[i] = c;
i++;
}
}
}
}
int position(char c)
{
int i;
for (i = 0; i < 26; i++)
{
if (c == keyl[i] || c == keyu[i])
{
return i;
}
}
}
void encrypt()
{
int i = 0, len;
char ct[200], pt[200], tmp;
printf("Enter Message to Encrypt:");
take_input(pt);

5
3170720 201040107059

len = strlen(pt);
while (i < len)
{
tmp = pt[i];
if (isupper(tmp))
{
ct[i] = keyu[pt[i] - 65];
}
else
{
ct[i] = keyl[pt[i] - 97];
}
i++;
}
ct[i] = '\0';
printf("\n Encrypted Message: %s \n", ct);
}
void decrypt()
{
int i = 0, len;
char tmp, ct[200], pt[200];
printf("Enter Message to Decrypt:\n");
take_input(ct);
len = strlen(ct);
while (i < len)
{
tmp = ct[i];
if (isupper(tmp))
{
pt[i] = position(tmp) + 65;

6
3170720 201040107059

}
else
{
pt[i] = position(tmp) + 97;
}
i++;
}
pt[i] = '\0';
printf("\n Decrypted Message: %s \n", pt);
}
void main()
{
int choice;
while (1)
{
printf("Enter Your Choice: \n 1. Encryption \n 2. Decryption \n 3. Exit \n");
scanf("%d", &choice);
switch (choice)
{
case 1:
{
encrypt();
break;
}
case 2:
{
decrypt();
break;
}
case 3:

7
3170720 201040107059

break;
default:
{
printf("Enter a Valid Choice");
}
}
if (choice == 3)
{
break;
}
}
}

Output:-

8
3170720 201040107059

Practical:-3
Aim:- Implement Playfair cipher encryption-decryption.

Code(Encryption):-
#include <stdio.h>
#include <conio.h>
void main()
{
char arr[5][5] = {"MONAR", "CHYBD", "EFGIK", "LPQST", "UVWXZ"};
char pt[10];
int i, j, r1 = 0, r2 = 0, c1 = 0, c2 = 0;
printf(" PLAYFAIR CIPHER KEYMATRIX: ");
printf(" \n ");
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
printf(" %c ", arr[i][j]);
}
printf(" \n ");
}
printf(" \n Enter Your Plain Text:");
scanf("%s", &pt);
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
if (arr[i][j] == pt[0])
{

9
3170720 201040107059

r1 = i;
c1 = j;
}
if (arr[i][j] == pt[1])
{
r2 = i;
c2 = j;
}
}
}
if (r1 == r2)
{
if (c2 == 4)
printf("Cipher Text: %c%c \n", arr[r1][c1 + 1], arr[r2][0]);
else
printf("Cipher Text: %c%c \n", arr[r1][c1 + 1], arr[r2][c2 + 1]);
}
if (c1 == c2)
{
if (r2 == 4)
printf("Cipher Text: %c%c \n", arr[r1 + 1][c1], arr[0][c2]);
else
printf("Cipher Text: %c%c \n", arr[r1 + 1][c1], arr[r2 + 1][c2]);
}
if (r1 != r2 && c1 != c2)
{
printf("\n Cipher Text: %c%c \n", arr[r1][c2], arr[r2][c1]);
}
}

10
3170720 201040107059

Output:-

Code(Decryption):-
#include <stdio.h>
#include <conio.h>
void main()
{
char arr[5][5] = {"MONAR", "CHYBD", "EFGIK", "LPQST", "UVWXZ"};
char ct[10];
int i, j, r1 = 0, r2 = 0, c1 = 0, c2 = 0;
printf(" PLAYFAIR CIPHER KEYMATRIX: ");
printf(" \n ");
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
printf(" %c ", arr[i][j]);
}
printf(" \n ");
}
printf(" \n Enter Your Cipher Text:");
scanf("%s", &ct);
for (i = 0; i < 5; i++)
{

11
3170720 201040107059

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


{
if (arr[i][j] == ct[0])
{
r1 = i;
c1 = j;
}
if (arr[i][j] == ct[1])
{
r2 = i;
c2 = j;
}
}
}
if (r1 == r2)
{
if (c2 == 0)
printf("Plain Text: %c%c \n", arr[r1][c1 - 1], arr[r2][4]);
else
printf("Plain Text: %c%c \n", arr[r1][c1 - 1], arr[r2][c2 - 1]);
}
if (c1 == c2)
{
if (r2 == 0)
printf("Plain Text: %c%c \n", arr[r1 - 1][c1], arr[4][c2]);
else
printf("Plain Text: %c%c \n", arr[r1 - 1][c1],arr[r2 - 1][c2]);
}
if (r1 != r2 && c1 != c2)
{

12
3170720 201040107059

printf("\n Plain Text: %c%c \n", arr[r1][c2], arr[r2][c1]);


}
}
Output:-

13
3170720 201040107059

Practical:-4
Aim:- Implement Polyalphabetic cipher encryption-decryption.

Code:-
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char pt[20] = {'\0'}, ct[20] = {'\0'}, key[20] = {'\0'}, rt[20] = {'\0'};
int i, j;

printf("\n enter the plain text:");


scanf("%s", pt);
printf("\n enter the key:");
scanf("%s", key);

//length of plaintext equal to length of key


j = 0;
for (i = strlen(key); i < strlen(pt); i++)
{
if (j == strlen(key))
{
j = 0;
}
key[i] = key[j];
j++;
}
printf("\n new key is:%s", key);

14
3170720 201040107059

//converting plain text to cipher text (encryption)


for (i = 0; i < strlen(pt); i++)
{
ct[i] = (((pt[i] - 97) + (key[i] - 97)) % 26) + 97;
}
printf("\n \n cipher text is:%s", ct);

//converting cipher text to plain text (decryption)


for (i = 0; i < strlen(ct); i++)
{
if (ct[i] < key[i])
{
rt[i] = 26 + ((ct[i] - 97) - (key[i] - 97)) + 97;
}
else
rt[i] = (((ct[i] - 97) - (key[i] - 97)) % 26) + 97;
}
printf("\n \n plain text is:%s", rt);
}

Output:-

15
3170720 201040107059

Practical:-5
Aim :- Implement Hill cipher encryption-decryption.

Code:-
#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(); //encrypts the message
void decryption(); //decrypts the message
void getKeyMessage(); //gets key and message from user
void inverse(); //finds inverse of key matrix

void main()
{ getKeyMessage
();encryption();
decryption();
}
void encryption()
{int i, j, k;
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];
printf("\nEncrypted string is: ");
for(i = 0; i < 3; i++)
printf("%c", (char)(fmod(encrypt[i][0], 26) + 97));

16
3170720 201040107059

}
void decryption()
{int i, j, k;
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];
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;

17
3170720 201040107059

}
void inverse()
{int i, j, k;
float p, q;
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
{if(i == j)
b[i][j]=1;
else
b[i][j]=0;
}
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++) {
for(j = 0; j < 3; j++)
printf("%d ", b[i][j]);

18
3170720 201040107059

printf("\n");
}
}

Output:-

19
3170720 201040107059

Practical:-6
Aim:- To implement Simple DES or AES.

Code:-
#include <stdio.h>
#include <conio.h>

void main()
{
int p10[10]={3,5,2,7,4,10,1,9,8,6},p8[8]={6,3,7,4,8,5,10,9};
int i,key1[10],key[10],ls1[5],ls2[5],temp,p1[10],j,temp1;
printf("\nEnter Key Value : ");
for(i=0;i<10;i++)
{
scanf("%d",&key[i]);
}
printf("\nP10 value : ");
for(i=0;i<10;i++)
{
printf("%d ",p10[i]);
}
printf("\nP8 value : ");
for(i=0;i<8;i++)
{
printf("%d ",p8[i]);
}
for(i=0;i<10;i++)
{
temp=p10[i];

20
3170720 201040107059

for(j=0;j<temp;j++)
{
temp1=key[j];

}
p1[i]=temp1;
}
printf("\nAfter Applying Permutation p10 : ");
for(i=0;i<10;i++)
{
printf("%d ",p1[i]);
}
for(i=0;i<10;i++)
{
temp1=p1[i];
if(i<5)
{
ls1[i]=temp1;
}
else
{
ls2[i-5]=temp1;
}
}
ls1[5]=ls1[0];
for(i=0;i<5;i++)
{
ls1[i]=ls1[i+1];
}
ls1[4]=ls1[5];

21
3170720 201040107059

printf("\nAfter Left Shifting By 1 \n");


printf("\nFirst 5 bits : ");
for(i=0;i<5;i++)
{
printf("%d ",ls1[i]);
}
ls2[5]=ls2[0];
for(i=0;i<5;i++)
{
ls2[i]=ls2[i+1];
}
ls2[4]=ls2[5];
printf("\nSecond 5 bits : ");
for(i=0;i<5;i++)
{
printf("%d ",ls2[i]);
}
for(i=0;i<10;i++)
{
p1[i]=ls1[i];
if(i>5)
{
p1[i]=ls2[i-5];
}
}
for(i=0;i<10;i++)
{
temp=p8[i];
for(j=0;j<temp;j++)
{

22
3170720 201040107059

temp1=p1[j];
}
key1[i]=temp1;
}
printf("\nKey k1 is : ");
for(i=0;i<8;i++)
{
printf("%d ",key1[i]);
}

getch();
}

Output:-

23
3170720 201040107059

Practical:-7
Aim:- Implement Diffi-Hellmen Key exchange Method.

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;

P = 12;
printf("The value of P : %lld\n", P);

G = 17;
printf("The value of G : %lld\n\n", G);

24
3170720 201040107059

a = 14;
printf("The private key a for BOB : %lld\n", a);
x = power(G, a, P);

b = 15;
printf("The private key b for CARRY : %lld\n\n", b);
y = power(G, b, P);

ka = power(y, a, P);
kb = power(x, b, P);

printf("Secret key for the BOB is : %lld\n", ka);


printf("Secret Key for the CARRY is : %lld\n", kb);

return 0;
}

Output:-

25
3170720 201040107059

Practical:-8
Aim:- Implement RSA encryption-decryption algorithm.

Code:-
#include<stdio.h>
#include<math.h>
void encrypt();
void decrypt();
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(e,n):%ld\t%ld\n",e,n);
printf("\nDecryption key(d,n):%ld\t%ld\n",d,n);
}
void encrypt(){
long int pt,p,q,n,fn,e=2,temp,d,ct;
printf("Enter your message to encrypt:\n");
scanf("%d",&pt);
printf("Enter two prime numbers p and q:\n");
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);
//printf("e=%ld\ttemp=%d\t",e,temp);
}
//printf("\ne=%ld\n",e);

26
3170720 201040107059

d=multipicative_inverse(fn,e);
if(d==0) {
printf("\nrsa is not possible for given p and q.\n");
exit(0);
}
ct=fmod(pow(pt,e),n);
key_pair(e,n,d);
printf("\nencrypted text:\t%ld\n",ct);

}
void
decrypt(){ long
int pt,d,ct,n;
printf("Enter message to decrypt:\n");
scanf("%ld",&ct);
printf("Enter key pair(d,n):\n");
scanf("%ld %ld",&d,&n);
pt=fmod(pow(ct,d),n);
printf("Decrypted message:\t%ld",pt);
}
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;

27
3170720 201040107059

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(){ in
t choice;
while(1){
printf("\nEnter your choice:\t1.Encrypt\t2.Decrypt\t3.Exit\n");
scanf("%d",&choice);
switch(choice)
{case 1: {
encrypt();
break;
}

28
3170720 201040107059

case 2:
{ decrypt
(); break;
}
case 3:
break;
default:
printf("\nInvalid choice,Enter valid choice.\n");
}
if(choice==3)
break;
}
return 0;
}

Output:-

29
3170720 201040107059

Practical:-9
Aim :- Write a program to generate SHA-1 hash.

Code:-
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<malloc.h>
#include<math.h>
#include<stdlib.h>
#define rotateleft(x,n) ((x<<n) | (x>>(32-n)))
#define rotateright(x,n) ((x>>n) | (x<<(32-n)))
using namespace std;
void SHA1(unsigned char * str1)
{
unsigned long int h0,h1,h2,h3,h4,a,b,c,d,e,f,k,temp;
h0 = 0x67452301;
h1 = 0xEFCDAB89;
h2 = 0x98BADCFE;
h3 = 0x10325476;
h4 = 0xC3D2E1F0;
unsigned char * str;
int i,j;
str = (unsigned char *)malloc(strlen((const char *)str1)+100);
strcpy((char *)str,(const char *)str1);
int current_length = strlen((const char *)str);
int original_length = current_length;

30
3170720 201040107059

str[current_length] = 0x80;
str[current_length + 1] = '\0';
char ic = str[current_length];
current_length++;
int ib = current_length % 64;
if(ib<56)
ib = 56-ib;
else
ib = 120 - ib;
for(int i=0;i < ib;i++)
{
str[current_length]=0x00;
current_length++;
}
str[current_length + 1]='\0';
for(i=0;i<6;i++)
{

str[current_length]=0x0;
current_length++;
}
str[current_length] = (original_length * 8) / 0x100 ;
current_length++;
str[current_length] = (original_length * 8) % 0x100;
current_length++;
str[current_length+i]='\0';
int number_of_chunks = current_length/64;
unsigned long int word[80];

31
3170720 201040107059

for(i=0;i<number_of_chunks;i++)
{
for(int j=0;j<16;j++)
{
word[j] = str[i*64 + j*4 + 0] * 0x1000000 + str[i*64 + j*4 + 1] * 0x10000 + str[i*64 + j*4 + 2] *
0x100 + str[i*64 + j*4 + 3];
}
for(j=16;j<80;j++)
word[j] = rotateleft((word[j-3] ^ word[j-8] ^ word[j-14] ^ word[j-16]),1);
a = h0;
b = h1;
c = h2;
d = h3;
e = h4;
for(int m=0;m<80;m++)
{
if(m<=19)
{
f = (b & c) | ((~b) & d);
k = 0x5A827999;
}
else if(m<=39)
{
f = b ^ c ^ d;
k = 0x6ED9EBA1;
}
else if(m<=59)

32
3170720 201040107059

{
f = (b & c) | (b & d) | (c & d);
k = 0x8F1BBCDC;
}
else
{
f = b ^ c ^ d;
k = 0xCA62C1D6;
}
temp = (rotateleft(a,5) + f + e + k + word[m]) & 0xFFFFFFFF;
e = d;
d = c;
c = rotateleft(b,30);
b = a;
a = temp;
}
h0 = h0 + a;
h1 = h1 + b;
h2 = h2 + c;
h3 = h3 + d;
h4 = h4 + e;
}
printf("\n\n");
printf("Hash: %x %x %x %x %x",h0, h1, h2, h3, h4);
printf("\n\n");
}
int main()

33
3170720 201040107059

{
char str[100];
cout<<"Enter the String: ";
cin>>str;
SHA1((unsigned char *)str);
return 0;
}

Output:-

34
3170720 201040107059

Practical:-10
Aim:- Implement a Digital Signature Algorithm.

Code:-

import java.security.*;
import java.util.Base64;

public class DigitalSignatureExample {


public static void main(String[] args) {
try {
KeyPair keyPair = generateKeyPair();
String message = "This is a ketu message.";
byte[] digitalSignature = sign(message, keyPair.getPrivate());
boolean isVerified = verify(message, digitalSignature, keyPair.getPublic());
System.out.println("Original message: " + message);
System.out.println("Digital Signature: " +
Base64.getEncoder().encodeToString(digitalSignature));
System.out.println("Signature verified: " + isVerified);
} catch (Exception e) { e.printStackTrace();
}
}
public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
keyPairGenerator.initialize(2048); // Key size

return keyPairGenerator.generateKeyPair();

Signature signature = Signature.getInstance("SHA256withRSA");


signature.update(message.getBytes());
return signature.sign();
signature.initSign(privateKey);

public static boolean verify(String message, byte[] signature, PublicKey publicKey) throws
Exception {

Signature verifier = Signature.getInstance("SHA256withRSA");


verifier.initVerify(publicKey); verifier.update(message.getBytes());
return verifier.verify(signature);
}
}

35
3170720 201040107059

OUTPUT:

36

You might also like