0% found this document useful (0 votes)
193 views8 pages

Cryptography Fundamentals Lab Assignment - 5: Code

The document describes a C program implementing a man-in-the-middle attack on the Diffie-Hellman key exchange algorithm. It generates random private keys for each user, keys for the attacker to modify the communication, and calculates the final keys between each user that have been altered by the attacker. The program outputs the random private keys generated for each user and the final calculated keys that include the attacker's modifications.

Uploaded by

Surya Vjkumar
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)
193 views8 pages

Cryptography Fundamentals Lab Assignment - 5: Code

The document describes a C program implementing a man-in-the-middle attack on the Diffie-Hellman key exchange algorithm. It generates random private keys for each user, keys for the attacker to modify the communication, and calculates the final keys between each user that have been altered by the attacker. The program outputs the random private keys generated for each user and the final calculated keys that include the attacker's modifications.

Uploaded by

Surya Vjkumar
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/ 8

17BCE0147 VIJAY SURYA

CRYPTOGRAPHY FUNDAMENTALS

LAB ASSIGNMENT - 5

Man in the middle attack in Diffie-Hellman Key Exchange Algorithm.

CODE:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef unsigned long long u_int_64;
u_int_64 QuickMod(u_int_64 privateKey, u_int_64 g, u_int_64
n);
u_int_64 GenerateKey(int person, int personIndex, u_int_64
*privateKey, u_int_64 g, u_int_64
n);
int main(int argc, const char * argv[]) {
int i=0,k=0;
u_int_64 *privateKey, g, n, key,
*mid_att_key,*mak_former,*mak_latter;
int total,mid_att_flag;
srand((unsigned)time(NULL));
printf("Enter g,n and total number of people:\n");
scanf("%lld%lld%d", &g, &n, &total);
privateKey = (u_int_64*)malloc(total * sizeof(u_int_64));
for (i = 0; i < total; i++)
{
privateKey[i] = rand();
}
printf("man_in_the_middle attack mode.\n");

mid_att_key = (u_int_64*)malloc((total) *
sizeof(u_int_64));
for (i = 0; i < total; i++)
{
17BCE0147 VIJAY SURYA
mid_att_key[i] = rand();
}
mak_former = (u_int_64*)malloc((total) *
sizeof(u_int_64));
mak_latter = (u_int_64*)malloc((total) *
sizeof(u_int_64));
for (i = 0; i < total; i++)
{int j=i;
for(k=0;k<total-1;j=(j+1)%total,k++);
mak_former[j] = GenerateKey(total-2 , i,
privateKey, g, n);
}
printf("Final result of the user + attacker:\n");
for(i=0;i<total;i++)
{
u_int_64 final_key;

mak_latter[i]=QuickMod(privateKey[i],mak_former[i],n);

mak_former[i]=QuickMod(mid_att_key[i],mak_former[i],n);

mid_att_key[i]=QuickMod(mid_att_key[i],mak_latter[i],n);

final_key=QuickMod(privateKey[i],mak_former[i],n);
printf("%llu + %llu\n",final_key,mid_att_key[i]);
}
printf("The random private key of each person:\n");
for (i = 0; i < total; i++)
{
printf("%llu \n", privateKey[i]);
}
return 0;

}
u_int_64 QuickMod(u_int_64 privateKey, u_int_64 g, u_int_64
n)
17BCE0147 VIJAY SURYA
{
u_int_64 mod = 1;
while (privateKey)
{
if (privateKey & 0x01)
{
mod = (mod * g) % n;
}
g = (g * g) % n;
privateKey >>= 1;
}
return mod;
}
u_int_64 GenerateKey(int total, int personIndex, u_int_64
*privateKey, u_int_64 g, u_int_64 n)
{
u_int_64 key = g;
int i=0;
int cnt = total;
for (i = (personIndex + 1) % total; cnt!=0; i = (i + 1) %
total)
{
key = QuickMod(privateKey[i], key, n);
cnt--;
}
return key;
}
17BCE0147 VIJAY SURYA

OUTPUT:

17BCE0147 VIJAY SURYA

SHA 1
CODE:

#include "SHA1.h"
#include<stdio.h>
#include<string.h>
#include <malloc/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)
{
int i=0,j=0,m=0;
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);
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(i=0;i < ib;i++)
{
17BCE0147 VIJAY SURYA
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];
for(i=0;i<number_of_chunks;i++)
{
for(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(m=0;m<80;m++)
{
if(m<=19)
{
17BCE0147 VIJAY SURYA
f = (b & c) | ((~b) & d);
k = 0x5A827999;
}
else if(m<=39)
{
f = b ^ c ^ d;
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: %lx %lx %lx %lx %lx",h0, h1, h2, h3, h4);
printf("\n\n");
}
int main()
{
17BCE0147 VIJAY SURYA
char s[100];
printf("Enter string: ");
scanf("%c",&s);
SHA1((unsigned char*)s);
}

Output:

You might also like