-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelgamal_crypto.c
More file actions
64 lines (51 loc) · 1.8 KB
/
elgamal_crypto.c
File metadata and controls
64 lines (51 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Modular exponentiation: (base^exponent) % modulus
long long mod_pow(long long base_val, long long exp, long long mod) {
long long result = 1;
base_val = base_val % mod;
while (exp > 0) {
if (exp % 2 == 1)
result = (result * base_val) % mod;
exp = exp / 2;
base_val = (base_val * base_val) % mod;
}
return result;
}
// Generate random number between 2 and max - 1
long long get_random(long long max) {
return rand() % (max - 2) + 2;
}
int main() {
srand(time(NULL));
long long p, g;
// Input prime and generator
printf("Enter a prime number p (e.g., 467): ");
scanf("%lld", &p);
printf("Enter a generator g (less than p): ");
scanf("%lld", &g);
// Party A's keys
long long xA = get_random(p); // Private key
long long yA = mod_pow(g, xA, p); // Public key
// Party B's keys
long long xB = get_random(p); // Private key
long long yB = mod_pow(g, xB, p); // Public key
printf("\nParty A: Private Key = %lld, Public Key = %lld\n", xA, yA);
printf("Party B: Private Key = %lld, Public Key = %lld\n", xB, yB);
// Ephemeral key and shared secret (from A)
long long k = get_random(p);
long long c1 = mod_pow(g, k, p);
long long sharedA = mod_pow(yB, k, p);
printf("\nParty A sends c1 = %lld to Party B\n", c1);
printf("Party A computed shared secret = %lld\n", sharedA);
// Party B computes shared secret
long long sharedB = mod_pow(c1, xB, p);
printf("Party B computed shared secret = %lld\n", sharedB);
// Verify match
if (sharedA == sharedB)
printf("\n✅ Shared secrets match! Secure key exchange successful.\n");
else
printf("\n❌ Error: Shared secrets do NOT match!\n");
return 0;
}