-
Notifications
You must be signed in to change notification settings - Fork 368
/
Elgamal.c
75 lines (67 loc) · 1.67 KB
/
Elgamal.c
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
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int gcd(int a, int b) {
if (a < b)
return gcd(b, a);
else if (a % b == 0)
return b;
else
return gcd(b, a % b);
}
int gen_key(int q) {
int key = rand() % q + pow(10, 20);
while (gcd(q, key) != 1) {
key = rand() % q + pow(10, 20);
}
return key;
}
int power(int a, int b, int c) {
int x = 1;
int y = a;
while (b > 0) {
if (b % 2 != 0) {
x = (x * y) % c;
}
y = (y * y) % c;
b = b / 2;
}
return x % c;
}
void encrypt(char* msg, int q, int h, int g, int* en_msg, int size) {
int k = gen_key(q);
int s = power(h, k, q);
int p = power(g, k, q);
printf("g^k used: %d\n", p);
printf("g^ak used: %d\n", s);
for (int i = 0; i < size; i++) {
en_msg[i] = s * msg[i];
}
}
void decrypt(int* en_msg, int p, int key, int q, char* dr_msg, int size) {
int h = power(p, key, q);
for (int i = 0; i < size; i++) {
dr_msg[i] = en_msg[i] / h;
}
}
int main() {
srand(time(0));
char msg[100];
printf("Enter the message: ");
fgets(msg, sizeof(msg), stdin);
printf("Original Message: %s\n", msg);
int q = rand() % (int) pow(10, 50) + pow(10, 20);
int g = rand() % (q - 2) + 2;
int key = gen_key(q);
int h = power(g, key, q);
printf("g used: %d\n", g);
printf("g^a used: %d\n", h);
int size = sizeof(msg) / sizeof(msg[0]);
int en_msg[size];
encrypt(msg, q, h, g, en_msg, size);
char dr_msg[size];
decrypt(en_msg, en_msg[0], key, q, dr_msg, size);
printf("Decrypted Message: %s\n", dr_msg);
return 0;
}