-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
333 lines (241 loc) · 6.14 KB
/
Program.cs
File metadata and controls
333 lines (241 loc) · 6.14 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
using EncryptionDecryptionUsingSymmetricKey;
using System;
using TestConsoleApplication;
namespace rsaproject
{
class Program
{
static void Main(string[] args)
{
int p = pbul();
int q = qbul();
int n = p * q;
int phi = (p - 1) * (q - 1);
int e = 0;
int d = 0;
while (true)
{
Random random = new Random();
int randsayi = random.Next(1, phi);
if (OBEB(randsayi, phi) == 1)
{
e = randsayi;
break;
}
}
d = oklid(e, phi);
Console.WriteLine("n: " + n + ", " + "e: " + e + ", " + "d: " + d);
Console.WriteLine("genel anahtar ( n,e ) :(" + n + ", " + e + ")");
Console.WriteLine("ozel anahtar d : (" + d + ")");
Console.WriteLine("Anahtar metnini giriniz: ");
String text = Console.ReadLine();
text = RSA(text, n, e);
Console.WriteLine("sifreli metin : " + text);
String desfparca = Desifre(text, d, n);
Console.WriteLine("desifreli metin : " + desfparca);
var key = desfparca;
Console.WriteLine("Sifrelenecek metini giriniz: ");
var str = Console.ReadLine();
var encryptedString = AesOperation.EncryptString(key, str);
Console.WriteLine($"Sifreli Metin = {encryptedString}");
var decryptedString = AesOperation.DecryptString(key, encryptedString);
Console.WriteLine($"Desifreli metin = {decryptedString}");
digitalSign.dgsign(decryptedString);
Console.ReadKey();
}
static int oklid(int d, int f)
{ //d=e f=phi gelicek
int x1, x2, x3, y1, y2, y3;
x1 = 1; x2 = 0; x3 = f; //p
y1 = 0; y2 = 1; y3 = d; //d
int q = 0, i = 1;
int t1 = 0, t2 = 0, t3 = 0;
do
{
if (i == 1)
{
q = x3 / y3;
t1 = x1 - (q * y1);
t2 = x2 - (q * y2);
t3 = x3 - (q * y3);
}
else
{
x1 = y1; x2 = y2; x3 = y3;
y1 = t1; y2 = t2; y3 = t3;
q = x3 / y3;
t1 = x1 - (q * y1);
t2 = x2 - (q * y2);
t3 = x3 - (q * y3);
}
i++;
if (y3 == 0)
{
break;
}
} while (y3 != 1);
if (y3 == 0)
{
Console.WriteLine("Sayinin tersi yoktur!!!!");
}
else
{
//Console.WriteLine("nSayinin tersi: " + y2);
}
return y2 + f;
}
private static Boolean asalSayi(int n)
{
int i;
for (i = 2; i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
public static int pbul()
{
int p;
while (true)
{
Random random1 = new Random();
p = random1.Next(100, 9999);
if (asalSayi(p))
{
Console.WriteLine(" p degeri :" + p);
break;
}
}
return p;
}
public static int qbul()
{
int q;
while (true)
{
Random random2 = new Random();
q = random2.Next(100, 9999);
if (asalSayi(q))
{
Console.WriteLine(" q degeri :" + q);
break;
}
}
return q;
}
static int OBEB(int x, int y)
{
int min = Math.Min(x, y);
int obeb = 1;
for (int i = 2; i <= min; i++)
{
if (x % i == 0 && y % i == 0)
{
obeb = i;
}
}
return obeb;
}
static double ustelMod(int a, int e, int n)
{ //a^e(mod(n))
double _a = a % n;
double _e = e;
if (e == 0) return 1;
while (_e > 1)
{
_a *= a;
_a %= n;
_e--;
}
return _a;
}
static String RSA(String metin, int n, int e) //m^e(mod(n))
{
String strn = Convert.ToString(n); //int -->string
int lcipher = strn.Length;
int lclear = strn.Length - 1;
String birlesmisHal = "";
char[] chars = metin.ToCharArray(); //stringi diziye at
for (int i = 0; i < chars.Length; i++)
{
Console.WriteLine(chars[i]); //dizinin elmanlarını yazar
int ascii = (int)chars[i];
Console.WriteLine("ascii degeri: " + ascii);
String sascii = Convert.ToString(ascii); //asci -> stri
while (sascii.Length < 3)
{
sascii = 0 + sascii;
}
Console.WriteLine("sola 0 eklenmis hali: " + sascii);
birlesmisHal += sascii;
}
Console.WriteLine("en son sola sıfır ekli hali: " + birlesmisHal);
while (birlesmisHal.Length % lclear != 0)
{
birlesmisHal = birlesmisHal + 0;
}
Console.WriteLine("lclear bolunmus hali saga 0 ekli tamam hali: " + birlesmisHal);
int gond;
String enSonHal = "";
for (int i = 0; i < birlesmisHal.Length; i += lclear)
{
String parca = birlesmisHal.Substring(i, lclear);
Console.WriteLine("parca: " + parca);
gond = Convert.ToInt32(parca); //str-->int
double gelendeger = ustelMod(gond, e, n);
Console.WriteLine("modgelen deger: " + gelendeger);
String strn2 = Convert.ToString(gelendeger); //long -->string
while (strn2.Length < lcipher)
{
strn2 = 0 + strn2;
}
enSonHal += strn2;
}
return enSonHal;
}
static String Desifre(String enSonHal, int d, int n) //m^d mod(n)
{
//String ustelmodgelen = "";
String strn = Convert.ToString(n); //int -->string
int lcipher = strn.Length;
int lclear = strn.Length - 1;
String enSonHal2 = "";
Console.WriteLine("--------desifre-----");
for (int i = 0; i < enSonHal.Length; i += lcipher)
{
String parca2 = enSonHal.Substring(i, lcipher);
Console.WriteLine("parca2: " + parca2);
int gond2 = Convert.ToInt32(parca2); //str-->int
double dustelmod = ustelMod(gond2, d, n);
Console.WriteLine("desifreden ustel gelen deger: " + dustelmod);
String strn2 = Convert.ToString(dustelmod);
while (strn2.Length < lclear)
{
strn2 = 0 + strn2;
}
Console.WriteLine("sola ekli hali desifre: " + strn2);
enSonHal2 += strn2;
}
Console.WriteLine("sola 0 ekli tamam hali desifreli anahtar: " + enSonHal2);
while (enSonHal2.Length % 3 != 0)
{
enSonHal2 = enSonHal2.Substring(0, enSonHal2.Length - 1); //amac ascııye tamamlamak 3 un katı olsun dıye
}
String desfbirlesmis = "";
char karakter;
for (int i = 0; i < enSonHal2.Length; i += 3)
{
String desfparca = enSonHal2.Substring(i, 3);
Console.WriteLine("parcalihali 3lu:" + desfparca);
int desfascii = Convert.ToInt32(desfparca); //string-->ascıı(inT)
karakter = (char)desfascii;
Console.WriteLine("Ascii karsiligi: " + karakter);
desfbirlesmis += karakter;
}
return desfbirlesmis;
}
}
}