-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
70 lines (57 loc) · 2.04 KB
/
main.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
// This program implements the RSA algorithm and supports unsigned
// integers up to 64 bits. To guarantee this, it must use the __int128
// type, which is not part of the C standard and is ONLY available for
// x64 systems and select compilers. Its range can be expanded by using
// and arbitrary precision arithmetic library such as GMP.
// NOTE! This program is unsafe in both the programmatic sense such as
// potential overflows or memory leaks and the likes, but also in the
// cryptographic sense: small numbers, timing attacks, ...
// I would love it if you'd let me know in case you fix/improve
// this program so that I can learn more as well! - github.com/takakv
#include <stdio.h>
#include "functions.h"
int main()
{
uint64 prime1, prime2;
prime1 = 101;
prime2 = 89;
//prime1 = 18446744073709551557U;
//prime2 = 18446744073709551533U;
bool is_prime = check_prime(prime1);
if (is_prime) is_prime = check_prime(prime2);
if (!is_prime)
{
printf("One of the numbers is not prime!");
return 1;
}
uint128 modulus = (uint128) prime1 * prime2;
uint128 totient = pxp_lambda(prime1, prime2);
uint64 enc_exp = get_ee(totient);
int128 dec_exp, trash1, trash2;
eea(enc_exp, totient, &dec_exp, &trash1, &trash2);
while (dec_exp < 0) dec_exp += totient;
if (modulus <= UINT64_MAX)
{
printf("The public key is: (%lli, %lli).",
(ull) modulus, (ull) enc_exp);
}
else
{
printf("The public key is too large to display ");
printf("using standard functions. As a challenge, ");
printf("try to write a function to print 128bit numbers.");
}
printf("\n");
if (dec_exp <= INT64_MAX)
{
printf("The private key is: %lli. Don't share it with anyone!",
(ull) dec_exp);
}
else
{
printf("The private key is too large to display ");
printf("using standard functions. As a challenge, ");
printf("try to write a function to print 128bit numbers.");
}
return 0;
}