Cryptography DA3
Cryptography DA3
LAB ASSESSMENT 3
n = p * q;
int phi = (p - 1) * (q - 1);
d = modInverse(e, phi);
}
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;
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;
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);
}
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 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;
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:
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);
{
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);
}
}
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;
}
int main() {
std::string input;
std::cout << "Enter a string to hash with MD5: ";
std::getline(std::cin, input);
return 0;
}
Snapshots:
Output: