-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSA.cs
255 lines (214 loc) · 7.94 KB
/
RSA.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
249
250
251
252
253
254
255
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Numerics;
using static System.Net.Mime.MediaTypeNames;
using System.Drawing;
using System.Drawing.Imaging;
using Image = System.Drawing.Image;
namespace _17743_Mihajlo_Marjanovic_ZI_Projekat
{
class RSA
{
private BigInteger _e;
private BigInteger _n;
private BigInteger _d;
private BigInteger _phi;
public RSA(int keySize)
{
// Generate prime numbers p and q
BigInteger p = GeneratePrime(keySize / 2);
BigInteger q = GeneratePrime(keySize / 2);
// Calculate n = p * q
_n = BigInteger.Multiply(p, q);
// Euklidov phi je broj integera manjih ili jednakih sa N koji ne dele nijedan zajednički faktor sa N
// Npr: phi(8) = 4 // [1,3,5,7]
// phi od prostog broja P je P-1
// Pa je zato lako da se izračuna phi(n) koga čini proizvod dva prosta broja p i q, na sledeći način:
// phi = (p - 1) * (q - 1)
_phi = BigInteger.Multiply(BigInteger.Subtract(p, 1), BigInteger.Subtract(q, 1));
// Generate a public key e such that gcd(e, phi) = 1
// Neparan broj koji ne deli zajednički faktor sa phi i n
_e = GeneratePublicKey(_phi);
// Calculate d such that d * e = 1 mod phi
// d= e^-1 mod phi(n)
_d = ModInverse(_e, _phi);
}
public BigInteger PublicKey => _e;
public BigInteger PrivateKey => _d;
public BigInteger Modulus => _n;
public BigInteger Phi => _phi;
public RSA(BigInteger e, BigInteger n, BigInteger d, BigInteger phi)
{
// Validate the keys
if (e <= 1 || e >= phi)
{
throw new Exception("Invalid public key");
}
if (d <= 0 || d >= phi)
{
throw new Exception("Invalid private key");
}
if (BigInteger.GreatestCommonDivisor(e, phi) != 1)
{
throw new Exception("Invalid public key");
}
if (d * e % phi != 1)
{
throw new Exception("Invalid private key");
}
_e = e;
_n = n;
_d = d;
_phi = phi;
}
private BigInteger GeneratePublicKey(BigInteger phi)
{
// E je neparan broj koji ne deli zajednički faktor sa phi i n
// Generate a random number in the range [2, phi - 1]
BigInteger e = GenerateRandomInteger(2, phi - 1);
// Keep generating random numbers until a number is found such that gcd(e, phi) = 1
while (BigInteger.GreatestCommonDivisor(e, phi) != 1)
{
e = GenerateRandomInteger(2, phi - 1);
}
return e;
}
private BigInteger GeneratePrime(int keySize)
{
// Generate a random number of the specified key size
BigInteger number = GenerateRandomInteger(keySize);
// If the number is even, add 1 to make it odd
if (number % 2 == 0)
{
number += 1;
}
// Keep generating random numbers until a prime is found
while (!IsPrime(number))
{
number += 2;
}
return number;
}
private BigInteger GenerateRandomInteger(int keySize)
{
// Generate a random byte array of the specified key size
var randomBytes = new byte[keySize / 8];
//RNGCryptoServiceProvider is an implementation of a random number generator.
using (var rng = new System.Security.Cryptography.RNGCryptoServiceProvider())
{
rng.GetBytes(randomBytes); // The array is now filled with cryptographically strong random bytes.
}
// Convert the byte array to a big integer
BigInteger number = new BigInteger(randomBytes);
// If the number is negative, negate it
if (number < 0)
{
number = -number;
}
return number;
}
private BigInteger GenerateRandomInteger(BigInteger min, BigInteger max)
{
// Generate a random byte array of the same size as min and max
var randomBytes = new byte[Math.Max(min.ToByteArray().Length, max.ToByteArray().Length)];
using (var rng = new System.Security.Cryptography.RNGCryptoServiceProvider())
{
rng.GetBytes(randomBytes);
}
// Convert the byte array to a big integer
BigInteger number = new BigInteger(randomBytes);
// If the number is negative, negate it
if (number < 0)
{
number = -number;
}
// If the number is less than min, add min to it
if (number < min)
{
number += min;
}
// If the number is greater than max, subtract max from it
if (number > max)
{
number -= max;
}
return number;
}
private BigInteger ModInverse(BigInteger a, BigInteger b)
{
// Calculate the inverse of a mod b using the extended Euclidean algorithm
BigInteger b0 = b, t, q;
BigInteger x0 = 0, x1 = 1;
if (b == 1) return 1;
while (a > 1)
{
q = a / b;
t = b;
b = a % b; a = t;
t = x0;
x0 = x1 - q * x0;
x1 = t;
}
if (x1 < 0) x1 += b0;
return x1;
}
private bool IsPrime(BigInteger number)
{
// Check if the number is even
if (number % 2 == 0)
{
return false;
}
// Check if the number is prime by trying to divide it by odd numbers in the range [3, number/2]
for (BigInteger i = 3; i <= BigInteger.Divide(number, 2); i += 2)
{
if (number % i == 0)
{
return false;
}
}
return true;
}
public BigInteger Encrypt(BigInteger message)
{
// c = m^e mod n
return BigInteger.ModPow(message, _e, _n);
}
public BigInteger Decrypt(BigInteger ciphertext)
{
// m = c^d mod n
return BigInteger.ModPow(ciphertext, _d, _n);
}
public void GenerateDemoInputFile()
{
string plaintext = "78787878";
File.WriteAllText("rsa_demoInput.txt", plaintext);
}
public void EncryptFile(string inputFilePath, string outputFilePath)
{
// Read the input file into a string
string message = File.ReadAllText(inputFilePath);
// Convert the ciphertext string to a big integer
BigInteger number = BigInteger.Parse(message);
// Encrypt the plaintext string
BigInteger cipher = Encrypt(number);
// Write the ciphertext to the output file
File.WriteAllText(outputFilePath, cipher.ToString());
}
public void DecryptFile(string inputFilePath, string outputFilePath)
{
// Read the input file into a string
string ciphertextString = File.ReadAllText(inputFilePath);
// Convert the ciphertext string to a big integer
BigInteger cipher = BigInteger.Parse(ciphertextString);
// Decrypt the ciphertext
BigInteger original = Decrypt(cipher);
// Write the plaintext to the output file
File.WriteAllText(outputFilePath, original.ToString());
}
}
}