0% found this document useful (0 votes)
14 views15 pages

Cryptography DA3

The document contains a lab assessment for cryptography and network security, featuring three main tasks: implementing an RSA cipher scheme, designing a Diffie-Hellman key exchange protocol with a MITM attack demonstration, and developing an MD5 hash algorithm. Each task includes code snippets and explanations for generating keys, encrypting messages, and computing hash values. The assessment is intended for a student named Sneh Gandhi under the supervision of faculty member Nivethitha K.

Uploaded by

snehg02102003
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)
14 views15 pages

Cryptography DA3

The document contains a lab assessment for cryptography and network security, featuring three main tasks: implementing an RSA cipher scheme, designing a Diffie-Hellman key exchange protocol with a MITM attack demonstration, and developing an MD5 hash algorithm. Each task includes code snippets and explanations for generating keys, encrypting messages, and computing hash values. The assessment is intended for a student named Sneh Gandhi under the supervision of faculty member Nivethitha K.

Uploaded by

snehg02102003
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/ 15

Cryptography and Network Security Lab

LAB ASSESSMENT 3

Name : Sneh Gandhi

Reg No.: 22BCI0120

Slot No.: L41+L42

Faculty Name: NIVETHITHA K Ma’am

Class No.: 5027


Q1. Develop a cipher scheme using RSA algorithm.
Code:
#include <bits/stdc++.h>
using namespace std;

int power(int base, int expo, int m) {


int res = 1;
base = base % m;
while (expo > 0) {
if (expo & 1)
res = (res * 1LL * base) % m;
base = (base * 1LL * base) % m;
expo = expo / 2;
}
return res;
}

int modInverse(int e, int phi) {


for (int d = 2; d < phi; d++) {
if ((e * d) % phi == 1)
return d;
}
return -1;
}

void generateKeys(int &e, int &d, int &n) {


int p, q;
cout << "Enter prime number p: ";
cin >> p;
cout << "Enter prime number q: ";
cin >> q;

n = p * q;
int phi = (p - 1) * (q - 1);

for (e = 2; e < phi; e++) {


if (__gcd(e, phi) == 1)
break;
}

d = modInverse(e, phi);
}

int encrypt(int m, int e, int n) {


return power(m, e, n);
}

int decrypt(int c, int d, int n) {


return power(c, d, n);
}

int main() {
int e, d, n;

generateKeys(e, d, n);

cout << "Public Key (e, n): (" << e << ", " << n << ")\n";
cout << "Private Key (d, n): (" << d << ", " << n << ")\n";

int M;
cout << "Enter message to encrypt: ";
cin >> M;

int C = encrypt(M, e, n);


cout << "Encrypted Message: " << C << endl;

int decrypted = decrypt(C, d, n);


cout << "Decrypted Message: " << decrypted << endl;

return 0;
}
Snapshot:
Output:

Q2. Design Diffie Hellman key exchange protocol and perform MITM
attack.
Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int power(int base, int expo, int m) {


int res = 1;
base = base % m;
while (expo > 0) {
if (expo & 1)
res = (res * 1LL * base) % m;
base = (base * 1LL * base) % m;
expo = expo / 2;
}
return res;
}

class A {
public:
int n;
A(int p) {
cout << "Enter Alice's private key (1 to " << p - 1 << "): ";
cin >> n;
}
int publish(int g, int p) {
return power(g, n, p);
}

int compute_secret(int gb, int p) {


return power(gb, n, p);
}
};

class B {
public:
int a, b;
B(int p) {
cout << "Enter Bob's private key (1 to " << p - 1 << "): ";
cin >> b;
cout << "Enter Eve's private key for Alice (1 to " << p - 1 << "): ";
cin >> a;
}

int publish(int g, int p, bool forAlice) {


if (forAlice) {
return power(g, a, p);
} else {
return power(g, b, p);
}
}

int compute_secret(int ga, bool forAlice, int p) {


if (forAlice) {
return power(ga, a, p);
} else {
return power(ga, b, p);
}
}
};

int main() {
srand(time(0));

int p, g;
cout << "Enter a prime number p: ";
cin >> p;
cout << "Enter a primitive root g: ";
cin >> g;

A alice(p);
B bob(p);

cout << "Eve selected private number for Alice (c): " << bob.a << endl;
cout << "Eve selected private number for Bob (d): " << bob.b << endl;

int ga = alice.publish(g, p);


int gb = bob.publish(g, p, false);
int gea = bob.publish(g, p, true);
int geb = bob.publish(g, p, false);

cout << "Alice published (ga): " << ga << endl;


cout << "Bob published (gb): " << gb << endl;
cout << "Eve published value for Alice (gc): " << gea << endl;
cout << "Eve published value for Bob (gd): " << geb << endl;

int sa = alice.compute_secret(gea, p);


int sea = bob.compute_secret(ga, true, p);
int sb = bob.compute_secret(geb, false, p);
int seb = bob.compute_secret(gb, false, p);

cout << "Alice computed secret key (S1): " << sa << endl;
cout << "Eve computed key for Alice (S1): " << sea << endl;
cout << "Bob computed secret key (S2): " << sb << endl;
cout << "Eve computed key for Bob (S2): " << seb << endl;

return 0;
}
Snapshot:
Output:

Q3. Develop a MD5 hash algorithm to generate hash code.


Code:
#include <iostream>
#include <string>
#include <cmath>
#include <cstring>

typedef union uwb {


unsigned w;
unsigned char b[4];
} MD5union;

typedef unsigned DigestArray[4];

static unsigned func0(unsigned abcd[]) {


return (abcd[1] & abcd[2]) | (~abcd[1] & abcd[3]);
}
static unsigned func1(unsigned abcd[]) {
return (abcd[3] & abcd[1]) | (~abcd[3] & abcd[2]);
}

static unsigned func2(unsigned abcd[]) {


return abcd[1] ^ abcd[2] ^ abcd[3];
}

static unsigned func3(unsigned abcd[]) {


return abcd[2] ^ (abcd[1] | ~abcd[3]);
}

typedef unsigned(*DgstFctn)(unsigned a[]);

static unsigned* calctable(unsigned* k) {


double s, pwr;
int i;

pwr = pow(2.0, 32);


for (i = 0; i < 64; i++) {
s = fabs(sin(1.0 + i));
k[i] = (unsigned)(s * pwr);
}
return k;
}

static unsigned rol(unsigned r, short N) {


unsigned mask1 = (1 << N) - 1;
return ((r >> (32 - N)) & mask1) | ((r << N) & ~mask1);
}

static unsigned* MD5Hash(std::string msg) {


int mlen = msg.length();
static DigestArray h0 = { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476 };
static DgstFctn ff[] = { &func0, &func1, &func2, &func3 };
static short M[] = { 1, 5, 3, 7 };
static short O[] = { 0, 1, 5, 0 };
static short rot0[] = { 7, 12, 17, 22 };
static short rot1[] = { 5, 9, 14, 20 };
static short rot2[] = { 4, 11, 16, 23 };
static short rot3[] = { 6, 10, 15, 21 };
static short* rots[] = { rot0, rot1, rot2, rot3 };
static unsigned kspace[64];
static unsigned* k;

static DigestArray h;
DigestArray abcd;
DgstFctn fctn;
short m, o, g;
unsigned f;
short* rotn;
union {
unsigned w[16];
char b[64];
} mm;
int os = 0;
int grp, grps, q, p;
unsigned char* msg2;

if (k == NULL) k = calctable(kspace);

for (q = 0; q < 4; q++) h[q] = h0[q];

{
grps = 1 + (mlen + 8) / 64;
msg2 = (unsigned char*)malloc(64 * grps);
memcpy(msg2, msg.c_str(), mlen);
msg2[mlen] = (unsigned char)0x80;
q = mlen + 1;
while (q < 64 * grps) { msg2[q] = 0; q++; }
{
MD5union u;
u.w = 8 * mlen;
q -= 8;
memcpy(msg2 + q, &u.w, 4);
}
}

for (grp = 0; grp < grps; grp++) {


memcpy(mm.b, msg2 + os, 64);
for (q = 0; q < 4; q++) abcd[q] = h[q];
for (p = 0; p < 4; p++) {
fctn = ff[p];
rotn = rots[p];
m = M[p]; o = O[p];
for (q = 0; q < 16; q++) {
g = (m * q + o) % 16;
f = abcd[1] + rol(abcd[0] + fctn(abcd) + k[q + 16 * p] + mm.w[g], rotn[q % 4]);

abcd[0] = abcd[3];
abcd[3] = abcd[2];
abcd[2] = abcd[1];
abcd[1] = f;
}
}
for (p = 0; p < 4; p++)
h[p] += abcd[p];
os += 64;
}

return h;
}

static std::string GetMD5String(std::string msg) {


std::string str;
int j;
unsigned* d = MD5Hash(msg);
MD5union u;
for (j = 0; j < 4; j++) {
u.w = d[j];
char s[9];
sprintf(s, "%02x%02x%02x%02x", u.b[0], u.b[1], u.b[2], u.b[3]);
str += s;
}
return str;
}

int main() {
std::string input;
std::cout << "Enter a string to hash with MD5: ";
std::getline(std::cin, input);

std::string md5Hash = GetMD5String(input);


std::cout << "MD5 hash of the input is: " << md5Hash << std::endl;

return 0;
}

Snapshots:
Output:

You might also like