-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRSA.pp
80 lines (73 loc) · 2.88 KB
/
RSA.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
72
73
74
75
76
77
78
79
80
Program RSAEncryptAndDecrypt_SignAndVerify;
{$H+}
Uses FGInt, FGIntPrimeGeneration, FGIntRSA;
Var
n, e, d, dp, dq, p, q, phi, one, two, gcd, temp, nilgint : TFGInt;
test, signature : String;
ok : boolean;
Begin
// Enter a random number to generate a prime, i.e.
// incremental search starting from that number
writeln('Searching for the first prime...');
Base10StringToFGInt('102336547456161301', p);
PrimeSearch(p);
writeln('Searching for the second prime...');
Base256StringToFGInt('AEFAFGhdhsgoi!ç"ty!a', q);
PrimeSearch(q);
// Compute the modulus
FGIntMul(p, q, n);
// Compute p-1, q-1 by adjusting the last digit of the GInt
p.Number[1] := p.Number[1] - 1;
q.Number[1] := q.Number[1] - 1;
// Compute phi(n)
FGIntMul(p, q, phi);
// Choose a public exponent e such that GCD(e,phi)=1
// common values are 3, 65537 but if these aren 't coprime
// to phi, use the following code
Base10StringToFGInt('65537', e); // just an odd starting point
Base10StringToFGInt('1', one);
Base10StringToFGInt('2', two);
writeln('Searching for a public exponent...');
FGIntGCD(phi, e, gcd);
While FGIntCompareAbs(gcd, one) <> Eq Do
Begin
FGIntadd(e, two, temp);
FGIntCopy(temp, e);
FGIntGCD(phi, e, gcd);
End;
FGIntDestroy(two);
FGIntDestroy(one);
FGIntDestroy(gcd);
// Compute the modular (multiplicative) inverse of e, i.e. the secret exponent (key)
writeln('Computing secret key...');
FGIntModInv(e, phi, d);
FGIntModInv(e, p, dp);
FGIntModInv(e, q, dq);
p.Number[1] := p.Number[1] + 1;
q.Number[1] := q.Number[1] + 1;
FGIntDestroy(phi);
FGIntDestroy(nilgint);
// Now everything is set up to start Encrypting/Decrypting, Signing/Verifying
test := 'Suppose you were an idiot. And suppose you were a member of Congress. But I repeat myself.';
writeln('Encrypting... :"Suppose you were an idiot. And suppose you were a member of Congress. But I repeat myself"');
RSAEncrypt(test, e, n, test);
writeln('Decrypting...');
RSADecrypt(test, d, n, Nilgint, Nilgint, Nilgint, Nilgint, test);
// this Is faster : RSADecrypt(test, nilGInt, n, dp, dq, p, q, test);
If test = 'Suppose you were an idiot. And suppose you were a member of Congress. But I repeat myself.' Then writeln('Encryption test successfull') Else writeln('Decryption failed');
test := 'Drugs may lead to nowhere, but at least it''s the scenic route.';
writeln('Signing...:"Drugs may lead to nowhere, but at least it''s the scenic route."');
RSASign(test, d, n, Nilgint, Nilgint, Nilgint, Nilgint, signature);
// this Is faster : RSASign(test, nilgint, n, dp, dq, p, q, signature);
writeln('Verifying...');
RSAVerify(test, signature, e, n, ok);
If ok Then writeln('Signature test successfull') Else writeln('Signature verification failed');
FGIntDestroy(p);
FGIntDestroy(q);
FGIntDestroy(dp);
FGIntDestroy(dq);
FGIntDestroy(e);
FGIntDestroy(d);
FGIntDestroy(n);
readln
End.