Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 34

HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

PRACTICAL NO. 01
IMPLEMENTATION OF CAESAR CIPHER

AIM: To implement the simple substitution technique named Caesar cipher using C language.
DESCRIPTION:
To encrypt a message with a Caesar cipher, each letter in the message is changed using a simple rule:
shift by three. Each letter is replaced by the letter three letters ahead in the alphabet. A becomes D, B
becomes E, and so on. For the last letters, we can think of the alphabet as a circle and "wrap around".
W becomes Z, X becomes A, Y bec mes B, and Z becomes C. To change a message back, each letter
is replaced by the one three before it.

EXAMPLE:

ALGORITHM:
STEP-1: Read the plain text from the user.
STEP-2: Read the key value from the user.
STEP-3: If the key is positive then encrypt the text by adding the key with each
character in the plain text.
STEP-4: Else subtract the key from the plain text.
STEP-5: Display the cipher text obtained above.

PROGRAM: (Caesar Cipher)


#include<stdio.h>
#include<ctype.h>
int main() {
char text[500], ch;

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

int key;
// Taking user input.
printf("Enter a message to encrypt: ");
scanf("%s", text);
printf("Enter the key: ");
scanf("%d", & key);
// Visiting character by character.
for (int i = 0; text[i] != '\0'; ++i) {
ch = text[i];
// Check for valid characters.
if (isalnum(ch)) {
//Lowercase characters.
if (islower(ch)) {
ch = (ch - 'a' + key) % 26 + 'a';
}
// Uppercase characters.
if (isupper(ch)) {
ch = (ch - 'A' + key) % 26 + 'A';
}
// Numbers.
if (isdigit(ch)) {
ch = (ch - '0' + key) % 10 + '0';
}
}
// Invalid character.
else {
printf("Invalid Message");
}
// Adding encoded answer.
text[i] = ch;
}
printf("Encrypted message: %s", text);

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

return 0;
}

OUTPUT:

RESULT: Thus the implementation of Caesar cipher had been execute

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

PRACTICAL NO. 02
IMPLEMENTATION OF PLAYFAIR CIPHER

AIM: To write a C program to implement the Playfair Substitution technique.


DESCRIPTION:
The Playfair cipher starts with creating a key table. The key table is a 5×5 grid of letters that will act
as the key for encrypting your plaintext. Each of the 25 letters must bemunique and one letter of the
alphabet is omitted from the table (as there are 25 spots and 26 letters in the alphabet).
To encrypt a message, one would break the message into digrams (groups of 2 letters) such that, for
example, "HelloWorld" becomes "HE LL OW OR LD", and map them out on the key table. The two
letters of the diagram are considered as the opposite corners of a rectangle in the key table. Note the
relative position of the corners of this rectangle. Then apply the following 4 rules, in order, to each
pair of letters in the plaintext:
1. If both letters are the same (or only one letter is left), add an "X" after the first letter.
2. If the letters appear on the same row of your table, replace them with the letters to
their immediate right respectively.
3. If the letters appear on the same column of your table, replace them with the letters
immediately below respectively.
4. If the letters are not on the same row or column, replace them with the letters on the
same row respectively but at the other pair of corners of the rectangle defined by the
original pair.

EXAMPLE:
Example1:Plaintext: INFORMATION TECHONOLOGY Key: IDEAL Ciphertext:?
Grouped text: IN FO RM AT IO NT EC HO NO LO GY
Ciphertext: EK GN MC YO AK OS DF GP MN AP AT

I/J D E A L
B C F G H
K M N O P
Q R S T U
V W X Y Z

ALGORITHM:
STEP-1: Read the plain text from the user.
STEP-2: Read the keyword from the user.

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

STEP-3: Arrange the keyword without duplicates in a 5*5 matrix in the row order and
fill the remaining cells with missed out letters in alphabetical order. Note that
‘i’ and ‘j’ takes the same cell.
STEP-4: Group the plain text in pairs and match the corresponding corner letters by
forming a rectangular grid.
STEP-5: Display the obtained cipher text.

PROGRAM: (Playfair Cipher)


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 30

void change_to_lowercase(char plain[], int ps)


{
int i;
for (i = 0; i < ps; i++) {
if (plain[i] > 64 && plain[i] < 91)
plain[i] += 32;
}
}
int remove_all_spaces(char* plain, int ps)
{
int i, count = 0;
for (i = 0; i < ps; i++)
if (plain[i] != ' ')
plain[count++] = plain[i];
plain[count] = '\0';
return count;
}
void generate_key(char key[], int ks, char keyT[5][5])
{

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

int i, j, k, flag = 0, *dicty;

dicty = (int*)calloc(26, sizeof(int));

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


if (key[i] != 'j')
dicty[key[i] - 97] = 2;
}
dicty['j' - 97] = 1;
i = 0;
j = 0;
for (k = 0; k < ks; k++) {
if (dicty[key[k] - 97] == 2) {
dicty[key[k] - 97] -= 1;
keyT[i][j] = key[k];
j++;
if (j == 5) {
i++;
j = 0;
}
}
}
for (k = 0; k < 26; k++) {
if (dicty[k] == 0) {
keyT[i][j] = (char)(k + 97);
j++;
if (j == 5) {
i++;
j = 0;
}
}
}

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

}
void searching(char keyT[5][5], char a, char b, int arr[])
{
int i, j;
if (a == 'j')
a = 'i';
else if (b == 'j')
b = 'i';
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
if (keyT[i][j] == a) {
arr[0] = i;
arr[1] = j;
}
else if (keyT[i][j] == b) {
arr[2] = i;
arr[3] = j;
}
}
}
}
int mod5(int a)
{
if (a < 0)
a += 5;
return (a % 5);
}
void decrypt(char str[], char keyT[5][5], int ps)
{
int i, a[4];
for (i = 0; i < ps; i += 2) {
searching(keyT, str[i], str[i + 1], a);

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

if (a[0] == a[2]) {
str[i] = keyT[a[0]][mod5(a[1] - 1)];
str[i + 1] = keyT[a[0]][mod5(a[3] - 1)];
}
else if (a[1] == a[3]) {
str[i] = keyT[mod5(a[0] - 1)][a[1]];
str[i + 1] = keyT[mod5(a[2] - 1)][a[1]];
}
else {
str[i] = keyT[a[0]][a[3]];
str[i + 1] = keyT[a[2]][a[1]];
}
}
}
void decrypt_by_playfair_cipher(char str[], char key[])
{
char ps, ks, keyT[5][5];
// Key text
ks = strlen(key);
ks = remove_all_spaces(key, ks);
change_to_lowercase(key, ks);
// ciphertext
ps = strlen(str);
change_to_lowercase(str, ps);
ps = remove_all_spaces(str, ps);
generate_key(key, ks, keyT);
decrypt(str, keyT, ps);
}

int main()
{
char str[SIZE], key[SIZE];

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

strcpy(key, " IDEAL");


printf("Key text: %s\n", key);
strcpy(str, " INFORMATION TECHONOLOGY");
printf("Encrypted text: %s\n", str);
decrypt_by_playfair_cipher(str, key);
printf("Ciphered text: %s\n", str);
return 0;
}

OUTPUT:

RESULT: Thus the Playfair cipher substitution technique had been implemented successfully.

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

PRACTICAL NO.03
IMPLEMENTATION OF HILL CIPHER

AIM: IMPLEMENTATION OF HILL CIPHER To write a C program to implement the hill cipher
substitution techniques.

DESCRIPTION:
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 is
multiplied by an invertible n × n matrix, against modulus 26. To decrypt the message, each block is
multiplied by the inverse of the m trix used for encryption. The matrix used for encryption is the
cipher key, and it sho ld be chosen randomly from the set of invertible n × n matrices (modulo 26).
EXAMPLE:
Plaintext: DOG

6 24 1 3 22
13 16 10 14 ( mod 26 ) = 11 = ‘ WLY ’
20 17 15 6 24

ALGORITHM:
STEP-1: Read the plain text and key from the user.
STEP-2: Split the plain text into groups of length three.
STEP-3: Arrange the keyword in a 3*3 matrix.
STEP-4: Multiply the two matrices to obtain the cipher text of length three.
STEP-5: Combine all these groups to get the complete cipher text.

PROGRAM: (Hill Cipher)


#include<stdio.h>
#include<string.h>
int main() {
unsigned int a[3][3] = { { 6, 24, 1 }, { 13, 16, 10 }, { 20, 17, 15 } };
unsigned int b[3][3] = { { 8, 5, 10 }, { 21, 8, 21 }, { 21, 12, 8 } };
int i, j;
unsigned int c[20], d[20];
char msg[20];

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

int determinant = 0, t = 0;
;
printf("Enter plain text\n ");
scanf("%s", msg);
for (i = 0; i < 3; i++) {
c[i] = msg[i] - 65;
printf("%d ", c[i]);
}
for (i = 0; i < 3; i++) {
t = 0;
for (j = 0; j < 3; j++) {
t = t + (a[i][j] * c[j]);
}
d[i] = t % 26;
}
printf("\nEncrypted Cipher Text :");
for (i = 0; i < 3; i++)
printf(" %c", d[i] + 65);
for (i = 0; i < 3; i++) {
t = 0;
for (j = 0; j < 3; j++) {
t = t + (b[i][j] * d[j]);
}
c[i] = t % 26;
}
printf("\nDecrypted Cipher Text :");
for (i = 0; i < 3; i++)
printf(" %c", c[i] + 65);
return 0;
}

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

OUTPUT:

RESULT: Thus the hill cipher substitution technique had been implemented successfully in C.

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

PRACTICAL NO.04
IMPLEMENTATION OF VIGENERE CIPHER

AIM: To implement the Vigenere Cipher substitution technique using C program.


DESCRIPTION:
To encrypt, a table of alphabets can be used, termed a tabula recta, Vigenère square, or Vigenère table.
It consists f the alphabet written out 26 times in differ nt rows, each alphabet shifted cyclically to the
left compared to the previous alphabet, corresponding to the 26 possible Caesar ciphers. At different
points in the encryption process, the cipher uses a different alphabet from one of the rows. The
alphabet used at each point depends on repeating keyword.
Each row starts with a key letter. The remainder of the row holds the letters A to Z. Although there are
26 key rows shown, you will only use as many keys as there are unique letters in the key string, here
just 5 keys, {L, E, M, O, N}. For successive letters of the message, we are going to take successive
letters of the key string, and encipher each message letter using its corresponding key row. Choose the
next letter of the key, go al ng that row to find the column heading that atches the message character;
the letter at the intersection of [key-row, msg-col] is the enciphered letter.

EXAMPLE:
Plaintext: Key: GOOD MORNING Key: DAY

To generate a new key, the given key is repeated in a circular manner, as long as the length of the
plain text does not equal to the new key.

G O O D M O R N I N G
D A Y D A Y D A Y D A

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

Encryption

The first letter of the plaintext is combined with the first letter of the key. The column of plain text
"G" and row of key "D" intersects the alphabet of "J" in the vigenere table, so the first letter of
ciphertext is "J".

Similarly, the second letter of the plaintext is combined with the second letter of the key. The column
of plain text "O" and row of key "A" intersects the alphabet of "O" in the vigenere table, so the second
letter of ciphertext is "O".

This process continues continuously until the plaintext is finished.

Ciphertext = JOMGMMUNGQG

Decryption

Decryption is done by the row of keys in the vigenere table. First, select the row of the key letter, find
the ciphertext letter's position in that row, and then select the column label of the corresponding
ciphertext as the plaintext.

J O M G M M U N G Q G
D A Y D A Y D A Y D A

For example, in the row of the key is "D" and the ciphertext is "J" and this ciphertext letter appears in
the column "G", that means the first plaintext letter is "G".

Next, in the row of the key is "A" and the ciphertext is "O" and this ciphertext letter appears in the
column "O", that means the second plaintext letter is "O".

This process continues continuously until the ciphertext is finished.

Plaintext = GOODMORNING

ALGORITHM:

STEP-1: Arrange the alphabets in row and column of a 26*26 matrix.

STEP-2: Circulate the alphabets in each row to position left such that the first letter is

attached to last.

STEP-3: Repeat this process for all 26 rows and construct the final key matrix.

STEP-4: The keyword and the plain text is read from the user.

STEP-5: The characters in the keyword are repeated sequentially so as to match with

that of the plain text.

STEP-6: Pick the first letter of the plain text and that of the keyword as the row indices

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

and column indices respectively.

STEP-7: The junction character where these two meet forms the cipher character.

STEP-8: Repeat the above steps to generate the entire cipher text.

PROGRAM: (Vegenere Cipher)


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void upper_case(char *src) {
while (*src != '\0') {
if (islower(*src))
*src &= ~0x20;
src++;
}
}
char* encipher(const char *src, char *key, int is_encode) {
int i, klen, slen;
char *dest;
dest = strdup(src);
upper_case(dest);
upper_case(key);
/* strip out non-letters */
for (i = 0, slen = 0; dest[slen] != '\0'; slen++)
if (isupper(dest[slen]))
dest[i++] = dest[slen];
dest[slen = i] = '\0'; /* null pad it, make it safe to use */
klen = strlen(key);
for (i = 0; i < slen; i++) {
if (!isupper(dest[i]))
continue;

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

dest[i] = 'A' + (is_encode ? dest[i] - 'A' + key[i % klen] - 'A'


: dest[i] - key[i % klen] + 26) % 26;
}
return dest;
}
int main() {
const char *str = "GOOD MORNING";
const char *cod, *dec;
char key[] = "DAY";
printf("Text: %s\n", str);
printf("key: %s\n", key);
cod = encipher(str, key, 1);
printf("Encrypted Cipher Text: %s\n", cod);
dec = encipher(cod, key, 0);
printf("Decrypted Cipher Text: %s\n", dec);
/* free(dec); free(cod); *//* nah */
return 0;
}

OUTPUT:

RESULT: Thus the Vigenere Cipher substitution technique had been implemented successfully.

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

PRACTICAL NO.05
IMPLEMENTATION OF RAIL FENCE CIPHER

AIM: To write a C program to implement the rail fence transposition technique.


DESCRIPTION:
In the rail fence cipher, the plain text is written downwards and diagonally on successive "rails" of an
imaginary fence, then moving up when we reach the bottom rail. When we reach the top rail, the
message is written downwards again until the whole plaintext is written out. The message is then read
off in rows.

EXAMPLE:

Ciphertext: ACHTAKTET M

ALGORITHM:
STEP-1: Read the Plain text.
STEP-2: Arrange the plain text in row columnar matrix format.
STEP-3: Now read the keyword depending on the number of columns of the plain text.

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

STEP-4: Arrange the characters of the keyword in sorted order and the corresponding
columns of the plain text.
STEP-5: Read the characters row wise or column wise in the former order to get the
cipher text.

PROGRAM: (Rail Fence Cipher)


#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 plaintext\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++)

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

{
//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");
}

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

OUTPUT:

RESULT: Thus the rail fence algorithm had been executed successfully

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

PRACTICAL NO.06
IMPLEMENTATION OF RSA
AIM: To write a C program to implement the RSA encryption algorithm.
DESCRIPTION:
RSA is an algorithm used by modern computers to encrypt and decrypt messages. It is an asymmetric
cryptographic algorithm. Asymmetric means that there are two different keys. This is also called
public key cryptography, because one of them can be given to everyone. A basic principle behind RSA
is the observation that it is practical to find three very large positive integers e, d and n such that with
modular exponentiation for all integer m:

The public key is represented by the integers n and e; and, the private key, by the integer d. m
represents the message. RSA involves a public key and a private key. The public key can be known by
everyone and is used for encrypting messages. The intention is that messages encrypted with the
public key can only be decrypted in a reasonable amount of time using the private key.

EXAMPLE:

ALGORITHM:
STEP-1: Select two co-prime numbers as p and q.
STEP-2: Compute n as the product of p and q.
STEP-3: Compute (p-1)*(q-1) and store it in z.

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

STEP-4: Select a random prime number e that is less than that of z.


STEP-5: Compute the private key, d as e * mod-1 (z).
STEP-6: The cipher text is computed as message * mod n.
STEP-7: Decryption is done as cipherdmod n.

PROGRAM: (RSA)
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
long int p, q, n, t, flag, e[100], d[100], temp[100], j, m[100], en[100], i;
char msg[100];
int prime(long int);
void ce();
long int cd(long int);
void encrypt();
void decrypt();
int main()
{
printf("ENTER FIRST PRIME NUMBER: ");
scanf("%ld", &p);
flag = prime(p);
if (flag == 0 || p == 1)
{
printf("WRONG INPUT\n");
exit(1);
}
printf("ENTER ANOTHER PRIME NUMBER: ");
scanf("%ld", &q);
flag = prime(q);
if (flag == 0 || q == 1 || p == q)
{

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

printf("WRONG INPUT\n");
exit(1);
}
printf("ENTER MESSAGE: ");
scanf(" %[^\n]s", msg);
for (i = 0; i < strlen(msg); i++)
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("%ld\t%ld\n", e[i], d[i]);
encrypt();
decrypt();
return 0;
}
int prime(long int pr)
{
int i;
if (pr == 1)
return 0;
for (i = 2; i <= sqrt(pr); i++)
{
if (pr % i == 0)
return 0;
}
return 1;
}
void ce()
{
int k;

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

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;
}
}
}
long int cd(long int x)
{
long int k = 1;
while (1)
{
k = k + t;
if (k % x == 0)
return (k / x);
}
}
void encrypt()
{

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

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", (char)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++)

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

{
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", (char)m[i]);
}

OUTPUT:

RESULT: Thus the C program to implement RSA encryption technique had been implemented
successfully.

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

PRACTICAL NO.07
IMPLEMENTATION OF DIFFIE HELLMAN KEY EXCHANGE
ALGORITHM
AIM: To implement the Diffie-Hellman Key Exchange algorithm using C language.
DESCRIPTION:
The Diffie-Hellman algorithm is being used to establish a shared secret that can be used for secret
communications while exchanging data over a public network using the elliptic curve to generate
points and get the secret key using the parameters.

 For the sake of simplicity and practical implementation of the algorithm, we will consider
only 4 variables, one prime P and G (a primitive root of P) and two private values a and b.
 P and G are both publicly available numbers. Users (say Alice and Bob) pick private values a
and b and they generate a key and exchange it publicly. The opposite person receives the key
and that generates a secret key, after which they have the same secret key to encrypt.

EXAMPLE:
RAM SHYAM
Public Keys available = P, G Public Keys available = P, G
Private Key Selected = a Private Key Selected = b
Key Generated as = Key Generated as=

Exchange of generated keys takes place

Key received = y Key received = x

Generated Secret Key = Generated Secret Key =

Algebraically, it can be shown that

Users now have a symmetric secret key to encrypt

Step 1: Ram and Shyam get public numbers P = 23, G = 9


Step 2: Ram selected a private key a = 4 Shyam selected a private key b = 3
Step 3: Alice and Bob compute public values
Ram:- x =(9^4 mod 23) = (6561 mod 23) = 6
Shyam:-y = (9^3 mod 23) = (729 mod 23) = 16
Step 4: Ram and Shyam exchange public numbers

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

Step 5: Ram receives public key y =16 and


Shyam receives public key x = 6
Step 6: Ram and Shyam compute symmetric keys
Ram: ka = y^a mod p = 65536 mod 23 = 9
Shyam: kb = x^b mod p = 216 mod 23 = 9
Step 7: 9 is the shared secret.

ALGORITHM:
STEP-1: Both Ram and Shyam shares the same public keys P and G.
STEP-2: Ram selects a random public key a.
STEP-3: Ramcomputes his secret key A as P a mod p.
STEP-4: Then Ram sends A to Shaym.
STEP-5: Similarly Shaym also selects a public key b and computes his secret key as B and sends the
same back to Ram.
STEP-6: Now both of them compute their common secret key as the other one’s secret key power of
a
mod p.

PROGRAM: (Diffie Hellman Key Exchange)


#include <math.h>
#include <stdio.h>
// Power function to return value of a ^ b mod P
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);
}
// Driver program

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

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

G = 9; // A primitive root for P, G is taken


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

// Alice will choose the private key a


a = 4; // a is the chosen private key
printf("The private key a for Ram : %lld\n", a);
x = power(G, a, P); // gets the generated key

// Bob will choose the private key b


b = 3; // b is the chosen private key
printf("The private key b for Shyam : %lld\n\n", b);
y = power(G, b, P); // gets the generated key

// Generating the secret key after the exchange


// of keys
ka = power(y, a, P); // Secret key for Ram
kb = power(x, b, P); // Secret key for Shyam

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


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

return 0;
}

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

OUTPUT:

RESULT: Thus the Diffie-Hellman key exchange algorithm had been successfully implement
using C.

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

PRACTICAL NO.08
IMPLEMENTATION OF SHA-1
Aim: To implement the SHA-I hashing technique using C program.

DESCRIPTION:
In cryptography, SHA-1 (Secure Hash Algorithm 1) is a cryptographic hash function. SHA-1
produces a 160-bit hash value known as a message digest. The way this algorithm
works is that for a message of size < 264 bits it computes a 160-bit condensed output called a message
digest. The SHA-1 algorithm is designed so that it is practically infeasible to find two input messages
that hash to the same output message. A hash function such as SHA-1 is used to calculate an
alphanumeric string that serves as the cryptographic representation of a file or a piece of data. This is
called a digest and can serve as a digital signature. It is supposed to be unique and non-reversible.

EXAMPLE:

ALGORITHM:
STEP-1: Read the 256-bit key values.
STEP-2: Divide into five equal-sized blocks named A, B, C, D and E.
STEP -3: The blocks B, C and D are passed to the function F.
STEP-4: The resultant value is permuted with block E.
STEP-5: The block A is shifted right by ‘s’ times and permuted with the result of step-4.
STEP-6: Then it is permuted with a weight value and then with some other key pair and taken as the
first block.STEP-7: Block A is taken as the second block and the block B is shifted by ‘s’ times and
taken as the third block.
STEP-8: The blocks C and D are taken as the block D and E for the final output.

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

PROGRAM: (Secure Hash Algorithm)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/sha.h>

void bytesToHex(unsigned char *bytes, char *hexString, int hexStringLength);

int main() {
EVP_MD_CTX *mdctx;
const EVP_MD *md;
unsigned char md_value[EVP_MAX_MD_SIZE];
char hexString[(EVP_MAX_MD_SIZE * 2) + 1]; // +1 for the null terminator

OpenSSL_add_all_digests();

md = EVP_get_digestbyname("sha1");

if (!md) {
printf("SHA-1 not found, exiting.\n");
exit(1);
}

mdctx = EVP_MD_CTX_create();

printf("Message digest object info:\n");


printf("Algorithm = %s\n", EVP_MD_name(md));
printf("Provider = %s\n", EVP_MD_pkey_type(md));
printf("ToString = %p\n", (void *)md);

const char *input = "";

EVP_DigestInit_ex(mdctx, md, NULL);


EVP_DigestUpdate(mdctx, input, strlen(input));
EVP_DigestFinal(mdctx, md_value, NULL);

bytesToHex(md_value, hexString, EVP_MD_size(md));


printf("\nSHA1(\"%s\") = %s\n", input, hexString);

input = "abc";

EVP_DigestInit_ex(mdctx, md, NULL);


EVP_DigestUpdate(mdctx, input, strlen(input));
EVP_DigestFinal(mdctx, md_value, NULL);

bytesToHex(md_value, hexString, EVP_MD_size(md));


printf("\nSHA1(\"%s\") = %s\n", input, hexString);

input = "abcdefghijklmnopqrstuvwxyz";

EVP_DigestInit_ex(mdctx, md, NULL);

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

EVP_DigestUpdate(mdctx, input, strlen(input));


EVP_DigestFinal(mdctx, md_value, NULL);

bytesToHex(md_value, hexString, EVP_MD_size(md));


printf("\nSHA1(\"%s\") = %s\n\n", input, hexString);

EVP_MD_CTX_destroy(mdctx);
EVP_cleanup();

return 0;
}

void bytesToHex(unsigned char *bytes, char *hexString, int hexStringLength) {


int i;
for (i = 0; i < hexStringLength; i++) {
sprintf(&hexString[i * 2], "%02x", bytes[i]);
}
hexString[hexStringLength * 2] = '\0';
}

Output:-

RESULT: Thus the SHA-1 hashing technique had been implemented successfully.

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19
HVPM COET SUB: INFORMATION SECURITY SYSTEM LAB

SUBMITTED BY: KRUTIKA WANKHADE


ROLL NO.: 19

You might also like