-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathElGamal.pp
71 lines (62 loc) · 2.36 KB
/
ElGamal.pp
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
Program ElGamalSignAndVerify_EncryptAndDecrypt;
{$H+}
Uses FGInt, FGIntPrimeGeneration, FGIntElGamal;
Var
p, phi, g, x, y, k, one, two, temp, gcd : TFGInt;
test, a, b : String;
ok : boolean;
Begin
// Enter a random number to generate a prime, i.e.
// incremental search starting from that number
writeln('Searching for a prime...');
Base10StringToFGInt('10233654741234567890123456789056161301', p);
PrimeSearch(p);
// Compute phi(p)
FGIntCopy(p, phi);
phi.Number[1] := phi.Number[1] - 1;
// x is your secret key
Base10StringToFGInt('11212312341234512345612345671203', x);
// g is any number
Base10StringToFGInt('21316465461203', g);
// k a random value, such that GCD(k,phi)=1, NEVER use the same k twice
Base10StringToFGInt('1131', k);
Base10StringToFGInt('1', one);
Base10StringToFGInt('2', two);
writeln('Searching for the random parameter...');
FGIntGCD(phi, k, gcd);
While FGIntCompareAbs(gcd, one) <> Eq Do
Begin
FGIntDestroy(gcd);
FGIntadd(k, two, temp);
FGIntCopy(temp, k);
FGIntGCD(phi, k, gcd);
End;
FGIntDestroy(two);
FGIntDestroy(one);
FGIntDestroy(gcd);
// Now everything is set up to sign and verify
test := 'eagles may soar high, but weasles do not get sucked into jet engines';
writeln('Signing... : "eagles may soar high, but weasles do not get sucked into jet engines"');
ElGamalSign(test, p, g, x, k, a, b);
// a and b form the signature
// compute a public key from the secret key: g^x = mod p
FGIntModExp(g, x, p, y);
writeln('Verifying...');
ElGamalVerify(g, y, p, a, b, test, ok);
If ok Then writeln('Signature test successfull') Else writeln('Signature verification failed');
// k a random number, never use the same k twice
Base10StringToFGInt('106511234567890123123412345', k);
// Now everything is set up to start encrypting and decrypting
test := 'A conscience is what hurts when all your other parts feel so good.';
writeln('Encrypting... : "A conscience is what hurts when all your other parts feel so good."');
ElGamalEncrypt(test, g, y, k, p, test);
writeln('Decrypting...');
ElGamalDecrypt(test, x, p, test);
If test = 'A conscience is what hurts when all your other parts feel so good.' Then writeln('Encryption test successfull') Else writeln('Decryption failed');
FGIntDestroy(p);
FGIntDestroy(g);
FGIntDestroy(x);
FGIntDestroy(y);
FGIntDestroy(k);
readln
End.