-
Notifications
You must be signed in to change notification settings - Fork 2
/
Program.cs
248 lines (200 loc) · 6.66 KB
/
Program.cs
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Windows.Forms;
namespace RSound_A
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new PrimaryForm());
}
}
}
namespace Encryption
{
public class RSA
{
public static BigInteger privateKey;
public static List<BigInteger> publickey;
public BigInteger GetPrivateKey() { return privateKey; }
public List<BigInteger> GetPublickey() { return publickey; }
public RSA()
{
GenerateKey();
}
public BigInteger Crypt(BigInteger toCrypt, bool isUsedToCryptImage = false)
{
return PowerModulo(toCrypt, publickey[1], publickey[0]);
}
public BigInteger Decrypt(BigInteger toDecrypt,bool isUsedToCryptImage = false)
{
return PowerModulo(toDecrypt, privateKey, publickey[0]);
}
public BigInteger Decrypt(BigInteger toDecrypt, BigInteger privateK, BigInteger firstPublicKey)
{
return PowerModulo(toDecrypt, privateK, firstPublicKey) ;
}
public void GenerateKey()
{
RSAKey RSAKey = new RSAKey();
publickey = RSAKey.GetPublicKey();
privateKey = RSAKey.GetPrivateKey();
}
public static BigInteger PowerModulo(BigInteger M, BigInteger e, BigInteger mod)
{
/*
Calcul de c = M^e [mod]. Fonctionne seulement pour des "petits" nombres (int)
int c = M;
for (int a = 1; a < e; ++a)
c = (M * c) % mod;
return c ;
*/
return BigInteger.ModPow(M, e, mod);
}
internal void SetPublicKey(List<BigInteger> publicKey)
{
publickey = publicKey;
}
internal void SetPrivateKey(BigInteger newPrivateKey)
{
privateKey = newPrivateKey;
}
}
}
public class RSAKey
{
private static List<int> PrimaryNumbers = new List<int>{
74891, 361223, 229283, 217829, 486023, 302759,
440773, 4049, 76379, 68501, 250799, 7009547, 265957, 559673, 210823, 7009603, 291979,
184997, 387007, 588229, 171427, 290627, 205537, 569809, 318347,
116993, 499027, 225509,
};
private static BigInteger Q;
private static BigInteger P;
private static BigInteger N;
private static BigInteger E;
private static BigInteger Phi;
public RSAKey()
{
while (P == Q) {
Q = GetRandomPrimaryNumber();
P = GetRandomPrimaryNumber();
}
N = (Q * P);
Phi = (P - 1) * (Q - 1);
E = GetEncryptionExponent();
}
private static BigInteger GetRandomPrimaryNumber()
{
return PrimaryNumbers[new Random().Next(0, PrimaryNumbers.Count)];
}
private static BigInteger PGCD(BigInteger a, BigInteger b)
{
BigInteger modulo = a % b;
if (modulo == 0) return b;
return PGCD(b, modulo);
}
public static BigInteger RandomizeBigInt(BigInteger maxValue)
{
Random random = new Random();
byte[] maxValue_array = maxValue.ToByteArray();
byte[] randomValue_array = new byte[maxValue_array.Count()];
bool on_limit = true;
for (int generate_byte = maxValue_array.Count() - 1; generate_byte >= 0; generate_byte--) {
byte random_byte = 0;
if (on_limit) {
random_byte = (byte)random.Next(maxValue_array[generate_byte]);
if (random_byte != (byte)random.Next(maxValue_array[generate_byte])) {
on_limit = false;
}
} else {
random_byte = (byte)random.Next(256);
}
randomValue_array[generate_byte] = random_byte;
}
return new BigInteger(randomValue_array);
}
private static BigInteger GetEncryptionExponent()
{
while (true) {
BigInteger e = RandomizeBigInt(Phi);
if ((PGCD(e, Phi) == 1)) {
return e;
}
}
}
public List<BigInteger> GetPublicKey()
{
return new List<BigInteger> { N, E };
}
public BigInteger GetPrivateKey()
{
return ModInverse(E, Phi);
}
public static BigInteger ModInverse(BigInteger X, BigInteger Y)
{
List<List<BigInteger>> MatriceOperation = new List<List<BigInteger>>();
BigInteger a, b, q, r;
a = X;
b = Y;
r = a % b;
q = a / b;
while (true) {
List<BigInteger> futureLigne = new List<BigInteger>();
a = b;
b = r;
r = a % b;
q = a / b;
futureLigne.Add(a);
futureLigne.Add(q);
futureLigne.Add(b);
futureLigne.Add(r);
MatriceOperation.Add(futureLigne);
if (r == 0) break;
}
int nombreEtape = MatriceOperation.Count();
BigInteger A, B, U, V = 0;
for (int i = 0; i < nombreEtape - 1; ++i) {
a = MatriceOperation[i][0];
q = MatriceOperation[i][1];
b = MatriceOperation[i][2];
r = MatriceOperation[i][3];
}
/*
a[i] q[i] b[i] r[i]
0 1000 = 3 × 257 + 229
1 257 = 1 × 229 + 28
2 229 = 8 × 28 + 5
3 28 = 5 × 5 + 3
4 5 = 1 × 3 + 2
5 3 = 1 × 2 + 1
6 2 = 2 × 1 + 0
*/
V = 1;
A = MatriceOperation[nombreEtape - 2][2];
U = -(MatriceOperation[nombreEtape - 2][1]);
B = MatriceOperation[nombreEtape - 2][0];
int cpt = nombreEtape - 1;
while (cpt != 1) {
--cpt;
BigInteger tmpV = V;
BigInteger tmpB = B;
BigInteger tmpU = U;
B = MatriceOperation[cpt - 1][0];
V = tmpU;
A = tmpB;
U = tmpV - (MatriceOperation[cpt - 1][1]) * U;
}
if (U < 0) {
for (int k = 0; U < 0; ++k)
U = U + k * B;
}
return U;
}
}