-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSA.c
138 lines (112 loc) · 1.77 KB
/
RSA.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "RSA.h"
int isPrime(unsigned long long num)
{
if (num <= 1)
{
return 0;
}
if (num % 2 == 0)
{
return num == 2 ? 0 : 1;
}
for (int i = 3; i <= sqrt(num); i += 2)
{
if (num % i == 0)
{
return 0;
}
}
return 1;
}
unsigned long long power(int bot, int index)
{
if (index == 0)
{
return 1;
}
return power(bot, index - 1) * bot;
}
unsigned long long gcd(unsigned long long a, long long b)
{
if (b == 0)
{
return a;
}
else
{
return gcd(b, a % b);
}
}
unsigned long long inverse(int a, unsigned long long b)
{
long long r1, r2, q, r, t, t1, t2;
r1 = a;
r2 = b;
t1 = 0; t2 = 1;
while (r1 != 1)
{
q = r2 / r1;
r = r2 - r1 * q;
t = t1 - t2 * q;
r2 = r1;
r1 = r;
t1 = t2;
t2 = t;
}
if (t2 < 0)
{
t2 = t2 + b;
}
return t2;
}
unsigned long long SquandMulti(long long bottom, unsigned long long e, unsigned long long n)
{
Binary* Bin = (Binary*)malloc(sizeof(Binary));
if (Bin == NULL)
{
printf("Error occurs\n");
return -1;
}
Bin = binaryConvert(e);
unsigned long long fin = 1;
int i;
for (i = Bin->num; i >= 0; i--)
{
fin = power(fin, 2) % n;
if (i == 1)
{
fin = (fin * bottom) % n;
}
return fin;
}
}
Binary* binaryConvert(unsigned long long num)
{
Binary* myBin = (Binary*)malloc(sizeof(Binary));
if (myBin == NULL)
{
printf("Error occurs\n");
return -1;
}
myBin->bin = (char*)malloc(sizeof(char));
myBin->num = 0;
int i = 0;
while (num > 0)
{
if (num % 2 == 1)
{
myBin->bin[i] = 1;
}
else
{
myBin->bin[i] = 0;
}
i++;
num = num / 2;
}
myBin->num = i;
return myBin;
}