0% found this document useful (0 votes)
18 views49 pages

Ins Lab Manual

this is lab manual of INS subject in computer engineering

Uploaded by

Aries Gaming
Copyright
© © All Rights Reserved
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)
18 views49 pages

Ins Lab Manual

this is lab manual of INS subject in computer engineering

Uploaded by

Aries Gaming
Copyright
© © All Rights Reserved
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/ 49

PRACTICAL-1

AIM: Implement Caesar cipher encryption-decryption.


Description:
The Caesar Cipher technique is one of the earliest and simplest methods of encryption
technique. It’s simply a type of substitution cipher, i.e., each letter of a given text is replaced
by a letter with a fixed number of positions down the alphabet. For example with a shift of
1, A would be replaced by B, B would become C, and so on. The method is apparently named
after Julius Caesar, who apparently used it to communicate with his officials.
Thus to cipher a given text we need an integer value, known as a shift which indicates the
number of positions each letter of the text has been moved down.
The encryption can be represented using modular arithmetic by first transforming the letters
into numbers, according to the scheme, A = 0, B = 1,…, Z = 25. Encryption of a letter by a
shift n can be described mathematically as.

(Encryption Phase with shift n)

(Decryption Phase with shift n)


Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
{
int i, key;
char text[100],c;
clrscr();

printf("\nCaesar Cipher - Encryption");


printf("\nEnter Message To Encrypt : ");
gets(text);
printf("\nEnter Key : ");
scanf("%d", &key);

for(i=0;text[i]!='\0';++i)
{
c=text[i];
if(c>='a'&&c<= 'z')
{
c=c+key;
if(c>'z')
{
c=c-'z'+'a'-1;

PACIFIC SCHOOL OF ENGINEERING 1


}
text[i]=c;
}
else if(c>='A'&&c<='Z')
{
c=c+key;
if(c>'Z')
{
c=c-'Z'+'A'-1;
}

text[i]=c;
}
}
printf("\nEncrypted Message : %s", text);
getch();
}

Output:

Decryption:
#included<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
{
int i, key;
char text[100],c;
clrscr();

printf("\nCaesar Cipher- Decryption");

PACIFIC SCHOOL OF ENGINEERING 2


printf("\nEnter Message To Decrypt : ");
gets(text);
printf("\nEnter Key : ");
scanf("%d", &key);
for(i = 0; text[i] != '\0'; ++i)
{
c = text[i];
if(c >= 'a' && c <= 'z')
{
c = c - key;
if(c < 'a')
{
c = c + 'z' - 'a' + 1;
}
text[i] = c;
}
else if(c >= 'A' && c <= 'Z')
{
c = c - key;
if(c < 'A')
{
c = c + 'Z' - 'A' + 1;
}
text[i] = c;
}
}
printf("Decrypted text: %s", text);
getch();
}

OUTPUT:

PACIFIC SCHOOL OF ENGINEERING 3


Advantages:

Its benefits are as follows: -

1. It is very easy to implement.


2. This method is the simplest method of cryptography.
3. Only one short key is used in its entire process.
4. If a system does not use complex coding techniques, it is the best method for it.
5. It requires only a few computing resources.

Disadvantages:

Its disadvantages are as follows: -

1. It can be easily hacked. It means the message encrypted by this method can be easily
decrypted.
2. It provides very little security.
3. By looking at the pattern of letters in it, the entire message can be decrypted.

PACIFIC SCHOOL OF ENGINEERING 4


PRACTICAL-2
AIM: Implement Monoalphabetic cipher encryption-decryption.
Description:
A monoalphabetic cipher is any cipher in which the letters of the plain text are mapped
to cipher text letters based on a single alphabetic key. Examples of monoalphabetic
ciphers would include the Caesar-shift cipher, where each letter is shifted based on a
numeric key, and the atbash cipher, where each letter is mapped to the letter symmetric
to it about the center of the alphabet.
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
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]={'Z','Y','X','W','V','U','T','S','R','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A'};
char p[20]={'\0'},c[20]={'\0'},r[20]={'\0'};
int i,j;
printf("\n enter the plain text:");
gets(p);
//converting plain text into cipher text (encryption)
for(i=0;i<strlen(p);i++)
{
for(j=0;j<26;j++)
{
if(pt[j]==p[i])
{
c[i]=ct[j];
}

PACIFIC SCHOOL OF ENGINEERING 5


}
}
printf("\n cipher text is: %s",c);

//converting cipher text into plain text (decryption)


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

Advantages:
1. Simple to understand and implement
2. we have 26! (26*25*24*23*...*1) possible keys. so brute-force attack won't work here.

PACIFIC SCHOOL OF ENGINEERING 6


Disadvantages:
1. Substituting more than one character of ciphertext for each plaintext value, the length
of messages and resulting transmission times are increased.
2. More training and discipline are required to take advantage of the increased security.

PACIFIC SCHOOL OF ENGINEERING 7


PRACTICAL-3
AIM: Implement Playfair cipher encryption-decryption.
Description:
Playfair cipher is an encryption algorithm to encrypt or encode a message. It is the same as a
traditional cipher. The only difference is that it encrypts a digraph (a pair of two letters) instead
of a single letter.

It initially creates a key-table of 5*5 matrix. The matrix contains alphabets that act as the key
for encryption of the plaintext. Note that any alphabet should not be repeated. Another point to
note that there are 26 alphabets and we have only 25 blocks to put a letter inside it. Therefore,
one letter is excess so, a letter will be omitted (usually J) from the matrix. Nevertheless, the
plaintext contains J, then J is replaced by I. It means treat I and J as the same letter, accordingly.

Since Playfair cipher encrypts the message digraph by digraph. Therefore, the Playfair cipher
is an example of a digraph substitution cipher.

Code:
#include<stdio.h>
int check(char table[5][5], char k) {
int i, j;
for (i = 0; i < 5; ++i)
for (j = 0; j < 5; ++j) {
if (table[i][j] == k)
return 0;
}
return 1;
}
void main() {
int i, j, key_len;
char table[5][5];
for (i = 0; i < 5; ++i)
for (j = 0; j < 5; ++j)
table[i][j] = '0';

printf("**********Playfair Cipher************\n\n");

PACIFIC SCHOOL OF ENGINEERING 8


printf("Enter the length of the Key. ");
scanf("%d", &key_len);

char key[key_len];

printf("Enter the Key. ");


for (i = -1; i < key_len; ++i) {
scanf("%c", &key[i]);
if (key[i] == 'j')
key[i] = 'i';
}

int flag;
int count = 0;

// inserting the key into the table


for (i = 0; i < 5; ++i) {
for (j = 0; j < 5; ++j) {
flag = 0;
while (flag != 1) {
if (count > key_len)
goto l1;

flag = check(table, key[count]);


++count;
}// end of while
table[i][j] = key[(count - 1)];
}// end of inner for
}// end of outer for

PACIFIC SCHOOL OF ENGINEERING 9


printf("\n");
int val = 97;
//inserting other alphabets
for (i = 0; i < 5; ++i) {
for (j = 0; j < 5; ++j) {
if (table[i][j] >= 97 && table[i][j] <= 123) {
} else {
flag = 0;
while (flag != 1) {
if ('j' == (char) val)
++val;
flag = check(table, (char) val);
++val;
}// end of while
table[i][j] = (char) (val - 1);
}//end of else
}// end of inner for
}// end of outer for

printf("The table is as follows:\n");


for (i = 0; i < 5; ++i) {
for (j = 0; j < 5; ++j) {
printf("%c ", table[i][j]);
}
printf("\n");
}

int l = 0;

PACIFIC SCHOOL OF ENGINEERING 10


printf("\nEnter the length length of plain text.(without spaces) ");
scanf("%d", &l);

printf("\nEnter the Plain text. ");


char p[l];
for (i = -1; i < l; ++i) {
scanf("%c", &p[i]);
}

for (i = -1; i < l; ++i) {


if (p[i] == 'j')
p[i] = 'i';
}

printf("\nThe replaced text(j with i)");


for (i = -1; i < l; ++i)
printf("%c ", p[i]);

count = 0;
for (i = -1; i < l; ++i) {
if (p[i] == p[i + 1])
count = count + 1;
}

printf("\nThe cipher has to enter %d bogus char.It is either 'x' or 'z'\n",


count);

int length = 0;
if ((l + count) % 2 != 0)
length = (l + count + 1);

PACIFIC SCHOOL OF ENGINEERING 11


else
length = (l + count);

printf("\nValue of length is %d.\n", length);


char p1[length];

//inserting bogus characters.


char temp1;
int count1 = 0;
for (i = -1; i < l; ++i) {
p1[count1] = p[i];
if (p[i] == p[i + 1]) {
count1 = count1 + 1;
if (p[i] == 'x')
p1[count1] = 'z';
else
p1[count1] = 'x';
}
count1 = count1 + 1;
}

//checking for length

char bogus;
if ((l + count) % 2 != 0) {
if (p1[length - 1] == 'x')
p1[length] = 'z';
else
p1[length] = 'x';
}

PACIFIC SCHOOL OF ENGINEERING 12


printf("The final text is:");
for (i = 0; i <= length; ++i)
printf("%c ", p1[i]);

char cipher_text[length];
int r1, r2, c1, c2;
int k1;

for (k1 = 1; k1 <= length; ++k1) {


for (i = 0; i < 5; ++i) {
for (j = 0; j < 5; ++j) {
if (table[i][j] == p1[k1]) {
r1 = i;
c1 = j;
} else if (table[i][j] == p1[k1 + 1]) {
r2 = i;
c2 = j;
}
}//end of for with j
}//end of for with i

if (r1 == r2) {
cipher_text[k1] = table[r1][(c1 + 1) % 5];
cipher_text[k1 + 1] = table[r1][(c2 + 1) % 5];
}

else if (c1 == c2) {


cipher_text[k1] = table[(r1 + 1) % 5][c1];
cipher_text[k1 + 1] = table[(r2 + 1) % 5][c1];

PACIFIC SCHOOL OF ENGINEERING 13


} else {
cipher_text[k1] = table[r1][c2];
cipher_text[k1 + 1] = table[r2][c1];
}

k1 = k1 + 1;
}//end of for with k1

printf("\n\nThe Cipher text is:\n ");


for (i = 1; i <= length; ++i)
printf("%c ", cipher_text[i]);

}
OUTPUT:

PACIFIC SCHOOL OF ENGINEERING 14


Advantages:

1. Diverse ciphertext if we scrutinize the Algorithm, we can notice at every Stage we are
getting diverse ciphertext, thus more trouble to cryptanalyst.
2. Brute force attack does not affect it.
3. Cryptanalyze (the process of decoding cipher without knowing key) is not possible.
4. Overcomes the limitation of simple Playfair square cipher.
5. Easy to perform the substitution.

Disadvantages:

1. Only 25 alphabets are supported.


2. It does not support numeric characters.
3. Only either upper cases or lower cases are supported.
4. The use of special characters (such as blank space, newline, punctuations, etc.) is
prohibited.
5. It does not support other languages, except English.
6. Encryption of media files is also not supported.

PACIFIC SCHOOL OF ENGINEERING 15


PRACTICAL- 4
AIM: Implement Polyalphabetic cipher encryption-decryption.
Description:
A polyalphabetic cipher is any cipher based on substitution, using multiple
substitution alphabets. The Vigenère cipher is probably the best-known example of a
polyalphabetic cipher, though it is a simplified special case.
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);

PACIFIC SCHOOL OF ENGINEERING 16


//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);
getch();
}
OUTPUT:

PACIFIC SCHOOL OF ENGINEERING 17


Advantages:
Under different alphabets, the same plain text character is thus encrypted to different cipher
text characters, preventing simple frequency analysis as per monoalphabetic substitution.
Therefore, polyalphabetic cipher techniques make the message more secure as compared to
various other techniques.
Disadvantages:
if the key length is smaller than the plaintext length, then the key will be repeated, because it
most likely will produce the same ciphertext as long as the same plaintext

PACIFIC SCHOOL OF ENGINEERING 18


PRACTICAL-5
AIM: Implement Hill cipher encryption-decryption.
Description:
Hill cipher is a polygraphic substitution cipher based on linear algebra.Each letter is
represented by a number modulo 26. Often the simple scheme A = 0, B = 1, …, Z = 25 is
used, but this is not an essential feature of the cipher. To encrypt a message, each block of n
letters (considered as an n-component vector) is multiplied by an invertible n × n matrix,
against modulus 26. To decrypt the message, each block is multiplied by the inverse of the
matrix used for encryption. The matrix used for encryption is the cipher key, and it should
be chosen randomly from the set of invertible n × n matrices (modulo 26).
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++)

PACIFIC SCHOOL OF ENGINEERING 19


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

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

PACIFIC SCHOOL OF ENGINEERING 20


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

PACIFIC SCHOOL OF ENGINEERING 21


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]);
printf("\n");
}
}
OUTPUT:

Advantages:
1. It perfectly conceals single-letter frequencies
2. 3×3 Hill Ciphers are extremely effective when it comes to hiding both single-letter and
two-letter frequency information.
3. It is highly strong against attacks made on ciphertext except if the attack is through a
known plaintext.
Disadvantages:
1. Noninvertible key over is the main disadvantages of hill cipher, because few of the
matrices have inverses over. This means that the encrypted text can’t be decrypted.

PACIFIC SCHOOL OF ENGINEERING 22


PRACTICAL-6
AIM: To implement Simple DES or AES.
Description:
DES Cipher-
The Data Encryption Standard, often known as DES, is a symmetric key block cypher
developed by IBM in 1977.
 Plaintext is divided into two halves in DES encryption, and then DES uses a 64-bit
plaintext and a 56-bit key to generate a 64-bit ciphertext, which is an encrypted
representation of the data.
 The key length used for encryption in DES is 56 bits, although the block size is 64 bits
(the remaining 8 bits are check bits only; they are not used by the encryption algorithm).
DES entails 16 rounds of identical procedures, regardless of key length.
 Because the amount of operations in DES is fixed and no permutation combinations are
permitted, it is easier to break the encryption, making it less secure than AES.
DES is a symmetric key algorithm used to encrypt digital data. Its short key length of 56 bits
makes it too weak to secure most current applications that is based on encryption.

AES Cipher-

Advanced Encryption Standard, or AES, is a symmetric key block cipher developed by


Vincent Rijmen and Joan Daemen in 2001. AES is implemented worldwide, both in hardware
and software, to encrypt sensitive data. AES is widely used while transmitting data over
computer networks, particularly in wireless networks.
 AES uses a 128-bit plaintext and a 128-bit secret key to create a 128-bit block, which is
then processed to produce 16 bytes (128-bit) ciphertext.
 In the case of AES, the key length might be 128 bits, 192 bits, or 256 bits, with 10
rounds (128 bits), 12 rounds (192 bits), or 14 rounds (256 bits).
 AES, on the other hand, is more secure than DES encryption and has become the de
facto international standard.
The encryption process of Advanced Encryption Standard is based upon substitution and
permutation operations in iterative manner. The 16 bytes of data are arranged in a matrix of
four columns and four rows. On this matrix, AES performs rounds of substitution-permutation
operations.
Each of these rounds uses a different cipher key, which is calculated from the original AES
key. The number of rounds of operations depends upon the size of the key in the following
manner –
 For 128-bit cipher key, 10 rounds
 For 192-bit cipher key, 12 rounds
 For 256-bit cipher key, 14 rounds

PACIFIC SCHOOL OF ENGINEERING 23


Code:
#include<stdio.h>
int main()
{
int i, cnt=0, p8[8]={6,7,8,9,1,2,3,4};
int p10[10]={6,7,8,9,10,1,2,3,4,5};

char input[11], k1[10], k2[10], temp[11];


char LS1[5], LS2[5];
//k1, k2 are for storing interim keys
//p8 and p10 are for storing permutation key

//Read 10 bits from user...


printf("Enter 10 bits input:");
scanf("%s",input);
input[10]='\0';

//Applying p10...
for(i=0; i<10; i++)
{
cnt = p10[i];
temp[i] = input[cnt-1];
}
temp[i]='\0';
printf("\nYour p10 key is :");
for(i=0; i<10; i++)
{ printf("%d,",p10[i]); }

printf("\nBits after p10 :");


puts(temp);

PACIFIC SCHOOL OF ENGINEERING 24


//Performing LS-1 on first half of temp
for(i=0; i<5; i++)
{
if(i==4)
temp[i]=temp[0];
else
temp[i]=temp[i+1];
}
//Performing LS-1 on second half of temp
for(i=5; i<10; i++)
{
if(i==9)
temp[i]=temp[5];
else
temp[i]=temp[i+1];
}
printf("Output after LS-1 :");
puts(temp);

printf("\nYour p8 key is :");


for(i=0; i<8; i++)
{ printf("%d,",p8[i]); }

//Applying p8...
for(i=0; i<8; i++)
{
cnt = p8[i];
k1[i] = temp[cnt-1];
}
printf("\nYour key k1 is :");

PACIFIC SCHOOL OF ENGINEERING 25


puts(k1);
}
OUTPUT:

Advantages and disadvantages of DES:


Advantages:
1. DES has been around a long time (since 1977), even now no real weaknesses
have been found: the most efficient attack is still brute force.
2. DES is also an ANSI and ISO standard - anybody can learn the details and
implement it.
3. Since DES was designed to run on 1977 hardware, it is fast in hardware and
relatively fast in software.

Disadvantages:
1. The 56-bit key size is the biggest defect of DES. Chips to perform one
million of DES encrypt or decrypt operations a second are available (in 1993).
A $1 million DES cracking machine can search the entire key space in about 7
hours.
2. Hardware implementations of DES are very fast; DES was not designed for
software and hence runs relatively slowly.
3. As the technology is improving lot more day by day so there is a possibility
to break the encrypted code, so AES is preferred than DES.
4. As we know in DES only one private key is used for encryption as well as for
decryption because it is symmetric encryption technique so if we lost that key to
decrypt the data then we cannot get the readable data at the receiving end.
Advantages and disadvantages of AES:
Advantages:
1. As it is implemented in both hardware and software, it is most robust security protocol.

2. It uses higher length key sizes such as 128, 192 and 256 bits for encryption. Hence it
makes AES algorithm more robust against hacking.
3. It is most common security protocol used for wide various of applications such as
wireless communication, financial transactions, e-business, encrypted data storage etc.

PACIFIC SCHOOL OF ENGINEERING 26


4. It is one of the most spread commercial and open source solutions used all over the
world.
5. No one can hack your personal information.
6. For 128 bit, about 2128 attempts are needed to break. This makes it very difficult to hack
it as a result it is very safe protocol.
Disadvantages:

1. It uses too simple algebraic structure.


2. Every block is always encrypted in the same way.
3. Hard to implement with software.
4. AES in counter mode is complex to implement in software taking both performance
and security into considerations

PACIFIC SCHOOL OF ENGINEERING 27


PRACTICAL-7
AIM: Implement Diffi-Hellmen Key exchange Method.
Description:
The Diffie Hellman key exchange method was first used to develop and exchange keys over
an insecure channel safely. It set a milestone in cryptography and is still used today in various
applications where encryption is required.
Let’s understand the mechanism with the following example,
You want to communicate with a spy from an allied nation who is not known to you. There is
no secure channel to talk to them. Messages sent without encryption can make any undesirable
person read the contents. If the encryption is done on the message, no one will be able to read
it.
This issue can be handled easily with the Diffie Hallman key exchange, which makes this
method unique. The algorithm provisions safely create a shared key on a secure channel and
even over an insecure channel tracked by the adversaries.

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 = 23;
printf("The value of P : %lld\n", P);

G = 9;

PACIFIC SCHOOL OF ENGINEERING 28


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

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

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

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

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


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

return 0;
}
OUTPUT:

PACIFIC SCHOOL OF ENGINEERING 29


Advantages:

1. The sender and receiver don’t need any prior knowledge of each other.
2. Once the keys are exchanged, the communication of data can be done through an
insecure channel.
3. The sharing of the secret key is safe.

Disadvantages:

1. The algorithm can not be sued for any asymmetric key exchange.
2. Similarly, it can not be used for signing digital signatures.
3. Since it doesn’t authenticate any party in the transmission, the Diffie Hellman key
exchange is susceptible to a man-in-the-middle attack.

PACIFIC SCHOOL OF ENGINEERING 30


PRACTICAL-8
AIM: Implement RSA encryption-decryption algorithm.
Description:
The RSA algorithm is a public-key signature algorithm developed by Ron Rivest, Adi Shamir,
and Leonard Adleman. Their paper was first published in 1977, and the algorithm uses
logarithmic functions to keep the working complex enough to withstand brute force and
streamlined enough to be fast post-deployment. The image below shows it verifies the digital
signatures using RSA methodology.

RSA can also encrypt and decrypt general information to securely exchange data along with
handling digital signature verification. The image above shows the entire procedure of the RSA
algorithm.

Code:

#include<stdio.h>
#include<math.h>
int gcd(int a, int h)
{
int temp;
while (1)
{
temp = a%h;
if (temp == 0)
return h;
a = h;
h = temp;

PACIFIC SCHOOL OF ENGINEERING 31


}
}
int main()
{
double p = 3;
double q = 7;

// First part of public key:


double n = p*q;

double e = 2;
double phi = (p-1)*(q-1);
while (e < phi)
{
if (gcd(e, phi)==1)
break;
else
e++;
}
int k = 2;
double d = (1 + (k*phi))/e;
// Message to be encrypted
double msg = 20;
printf("Message data = %lf", msg);
// Encryption c = (msg ^ e) % n
double c = pow(msg, e);
c = fmod(c, n);
printf("\nEncrypted data = %lf", c);
// Decryption m = (c ^ d) % n
double m = pow(c, d);

PACIFIC SCHOOL OF ENGINEERING 32


m = fmod(m, n);
printf("\nOriginal Message Sent = %lf", m);

return 0;
}
OUTPUT:

Advantages:

1. It is very easy to implement RSA algorithm.


2. RSA algorithm is safe and secure for transmitting confidential data.
3. Cracking RSA algorithm is very difficult as it involves complex mathematics.
4. Sharing public key to users is easy.

Disadvantages:

1. It may fail sometimes because for complete encryption both symmetric and
asymmetric encryption is required and RSA uses asymmetric encryption only.

2. It has slow data transfer rate due to large numbers involved.

3. It requires third party to verify the reliability of public keys sometimes.

4. High processing is required at receiver’s end for decryption.

5. RSA can’t be used for public data encryption like election voting.

PACIFIC SCHOOL OF ENGINEERING 33


PRACTICAL-9
AIM: Write a program to generate SHA-1 hash.
Description:
SHA-1 or Secure Hash Algorithm 1 is a cryptographic hash function which takes an input and
produces a 160-bit (20-byte) hash value. This hash value is known as a message digest. This
message digest is usually then rendered as a hexadecimal number which is 40 digits long.
Code:
#include<stdio.h>
#include<string.h>
#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)))

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;


str = (unsigned char *)malloc(strlen((const char *)str1)+100);
strcpy((char *)str,(const char *)str1);

int current_length = strlen((const char *)str);

PACIFIC SCHOOL OF ENGINEERING 34


int original_length = current_length;
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';
int i,j;
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';

PACIFIC SCHOOL OF ENGINEERING 35


int number_of_chunks = current_length/64;
unsigned long int word[80];
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;

PACIFIC SCHOOL OF ENGINEERING 36


k = 0x6ED9EBA1;
}
else if(m<=59)
{
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");

PACIFIC SCHOOL OF ENGINEERING 37


}
void main()
{
SHA1((unsigned char *)"The quick brown fox jumps over the lazy dog");
}
OUTPUT:

Advantages:
1. It’s a slow algorithm. This characteristic made it useful for storing password hashes as
it slows down brute force attacks.
2. It can be used to compare files or codes to identify unintentional only corruptions.
3. Can replace SHA-2 in case of interoperability issues with legacy codes.
Disadvantages:
1. Slower than other algorithms, therefore unsuitable for many purposes other than
password storage (e.g., when establishing secure connections to websites or comparing
files).
2. Less secure with many vulnerabilities found during the years.
3. Collisions are easy and cheap to find.
4. Key length too short to resist to attacks.

PACIFIC SCHOOL OF ENGINEERING 38


PRACTICAL-10
AIM: To study about digital signature algorithm.
Description:

A digital signature is an electronic analogue of a written signature to provide assurance that the
claimed signatory signed the information. In addition, a digital signature may be used to detect
whether or not the information was modified after it was signed (i.e., to detect the integrity of
the signed data).

The digital signature process can be divided into 2 parts:

1. Signature Generation:

 Generating a pair of public key and provide key by the sender of the message.

 Generating the message digest from the message using a hash function.

 Generating the digital signature from the message digest with the private key.

 Sending the message, the digital signature, and the public key to receiver.

2. Signature Verification:

 Generating the message digest from the message using the same hash function.

 Verifying the digital signature with message digest using the public key.

Here is a diagram showing the digital signature process:

Digital Signature Generation and Verification Processes

PACIFIC SCHOOL OF ENGINEERING 39


There are 2 popular algorithms that are used to generate and verify digital signature using
public keys:

 RSA (Rivest, Shamir and Adleman) algorithm developed by Ronald L. Rivest, Adi
Shamir, and Leonard M. Adleman in 1976.

 DSA (Digital Signature Algorithm) algorithm developed by US government in 1991.

Advantages:

1. A digital signature provides better security in the transaction. Any unauthorized person
cannot do fraudulence in transactions.
2. You can easily track the status of the documents on which the digital signature is
applied.
3. High speed up document delivery.
4. It is 100% legal it is issued by the government authorized certifying authority.
5. If you have signed a document digitally, then you cannot deny it.
6. In this signature, When a document is get signed, date and time are automatically
stamped on it.
7. It is not possible to copy or change the document signed digitally.
8. Identification of the person that signs.
9. Elimination of the possibility of committing fraud by an imposter.

Disadvantages:

1. You need to troubleshoot all the compatibility problems. In there are a lot of
compatibility settings like an updated version of driver and software.
2. Software is one of the main issues while using a digital signature certificate.
3. If you are belonging to the corporate world and running an export-import organization,
you need to produce a digital signature for E-ticketing.
4. In this signature, Lost or theft of keys and the use of vulnerable storage facilities.
5. There is a stronger need for a standard through which these different methods can
interact.
6. In this era of fast technological advancement, many of these tech products have a short
shelf life.
7. In order to effectively use a digital signature, both senders and recipients may have to
buy digital certificates.
8. To work with digital certificates, the sender and recipients have to buy verification
software at a cost.

PACIFIC SCHOOL OF ENGINEERING 40


PRACTICAL-11
AIM: Perform various encryption-decryption techniques with cryptool.
Description:
Cryptool is an open-source and freeware program that can be used in various aspects of
cryptographic and cryptanalytic concepts. There are no other programs like it available over
the internet where you can analyze the encryption and decryption of various algorithms. This
tools provides graphical interface, better documentation to achieve the encryption and
decryption, bundles of analytic tools, and several algorithms.
What is Cryptool?

 A freeware program with graphical user interface (GUI).


 A tool for applying and analyzing cryptographic algorithms.
 With extensive online help, it's understandable without deep crypto knowledge.
 Contains nearly all state-of-the-art crypto algorithms.
 “Playful” introduction to modern and classical cryptography.
 Not a “hacker" tool.

Encryption and Decryption of Caesar Cipher

The Caesar Cipher involves replacing each letter of the alphabet with a letter – placed down
or up according to the key given.

To start with the process you have to move to the Encrypt/Decrypt tab of the program. There,
you will find Symmetric (Classic) tab - Choose Caesar Cipher.

Figure1: Encrypt/Decrypt of Cryptool

In encryption, we are replacing the plaintext letter with the 3rd letter of the alphabet that is if
“A” is our plaintext character, then the Ciphertext will be “D”.

PACIFIC SCHOOL OF ENGINEERING 41


Figure2: Caesar Cipher

So, if I give “Monarchy” as plaintext in Caesar Cipher, it will show me the encryption, a shown
in the below image.

Figure3: Caesar Cipher Encryption

Encryption and Decryption of Playfair

Again, we have to move to Encrypt/Decrypt - Symmetric - Playfair Cipher and perform the
encryption part. We are putting the same plaintext – MONARCHY.

PACIFIC SCHOOL OF ENGINEERING 42


Figure4: Playfair Cipher

So, when we press the encrypt button, we will get the Ciphertext – “ONARMDYB”.

Figure5: Playfair Encryption

Encryption and Decryption of Hill Cipher

Again, we have to move to Encrypt/Decrypt - Symmetric - Hill Cipher and perform the
encryption part. We are putting the plaintext as – DRGREERROCKS and assuming that the
program gives us the Ciphertext as – FZIFTOTBXGPO.

PACIFIC SCHOOL OF ENGINEERING 43


Figure6: Hill Cipher

So, when we press the encrypt button, we will get the Ciphertext – “FZIFTOTBXGPO”.

Figure7: Hill Cipher Encryption

Encryption and Decryption of Vigener Cipher

Again, we have to move to Encrypt/Decrypt - Symmetric - Vigener Cipher and perform the
encryption part. We are putting the plaintext as –
MICHIGANTECHNOLOGICALUNIVERSITY and assuming that the program gives us the
Ciphertext as – TWWNPZOAAS…..,with the help of key as – HOUGHTON.

PACIFIC SCHOOL OF ENGINEERING 44


Figure8: Vigener Cipher

So, when we press the encrypt button, we will get the Ciphertext somewhat like –
“TWWNPZOAASWNUHZBNWWGSNBVCSLYPMM”.

Figure9: Vigener Cipher Encryption

PACIFIC SCHOOL OF ENGINEERING 45


PRACTICAL-12
AIM: Study and use the Wireshark for the various network protocols.
Description:
What is Wireshark?
Wires hark is a network packet analyzer. A network packet analyzer will try to capture Network
Packets and tries to display that packet data as detailed as possible. You could think of a
network packet analyzer as a measuring device used to examine what’s going on inside a
network cable, just like a voltmeter is used by an electrician to examine what’s going on inside
an electric cable (but at a higher level, of course). In the past, such tools were either very
expensive, proprietary, or both. However, with the advent of wire shark, all that has changed.
Wires hark is perhaps one of the best open source packet analyzers available today.
Features
The following are some of the many features wire shark provides:
 Available for Unix and windows
 Capture live data packets
 Filter packet on many Criteria
 Save packet data capture
 Export some or all packets in a number of capture file format
 Display packets with very detailed protocol information.
 Colorized packet display based on filter
 Create various statistics
1) How to Capture Data Packets
To begin capturing packets, first select one or more of these networks by clicking on your
choice(s) and using the Shift or Ctrl keys if you'd like to record data from multiple networks
simultaneously. Once a connection type is selected for capturing purposes, its background will
be shaded in either blue or gray. Click on Capture from the main menu, located towards the top
of the wires hark interface. When the drop-down menu appears, select the Start option.

PACIFIC SCHOOL OF ENGINEERING 46


To begin capturing packets, first select one or more of these networks by clicking on your
choice(s) and using the Shift or Ctrl keys if you'd like to record data from multiple networks
simultaneously. Once a connection type is selected for capturing purposes, its background will
be shaded in either blue or gray. Click on Capture from the main menu, located towards the top
of the wire shark interface. When the drop-down menu appears, select the Start option.
2) Viewing and Analyzing Packet Contents

Packet List
The packet list pane, located at the top of the window, shows all packets found in the active
capture file. Each packet has its own row and corresponding number assigned to it, along

PACIFIC SCHOOL OF ENGINEERING 47


with each of these data points.
 Time
 Source
 Destination
 Protocol
 Length
 Info
When a packet is selected in the top pane, you may notice one or more symbols appear in the
first column. Open and/or closed brackets, as well as a straight horizontal line, can indicate
whether or not a packet or group of packets are all part of the same back-and-forth conversation
on the network. A broken horizontal line signifies that a packet is not part of said conversation.

The details pane, found in the middle, presents the protocols and protocol fields of the selected
packet in a collapsible format. In addition to expanding each selection, you can also apply
individual wires hark filters based on specific details as well as follow streams of data based
on protocol type via the details context menu – accessible by right-clicking your mouse on the
desired item within this pane.
3) Using wire shark filters

One of the most important feature sets in wires hark is its filter capabilities, especially when

PACIFIC SCHOOL OF ENGINEERING 48


you're dealing with files that are significant in size. Capture filters can be set before the fact,
instructing wire shark to only record those packets that meet your specified criteria. Filters can
also be applied to a capture file that has already been created so that only certain packets are
shown. These are referred to as display filters. Wires hark provides a large number of
predefined filters by default, letting you narrow down the number of visible packets with just
a few keystrokes or mouse clicks.
4) Coloring Rules

While Wire shark’s capture and display filters allow you to limit which packets are recorded
or shown on the screen, its colorization functionality takes things a step further by making it
easy to distinguish between different packets types based on their individual hue. This handy
feature lets you quickly locate certain packets within a saved set by their row's color scheme in
the packet list pane.

PACIFIC SCHOOL OF ENGINEERING 49

You might also like