-
Notifications
You must be signed in to change notification settings - Fork 0
/
Szyfr.cs
316 lines (275 loc) · 8.97 KB
/
Szyfr.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
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
using System;
using System.Collections.Generic;
using System.Windows;
namespace Kod
{
class Szyfr
{
public void main(string text)
{
// To bytes
text1 = text;
MessageBox.Show(text1, "Podany tekst to: ");
char sign;
int i = 0;
bool[] charByte;
List<bool> BytesText = new List<bool>();
while (i < text1.Length)
{
sign = text1[i];
charByte = CharsToBytes(sign);
for (int k = 0; k < 8; k++)
{
BytesText.Add(charByte[k]);
}
i++;
}
string help = "";
for (int u = 0; u < text1.Length * 8; u++)
{
help += Convert.ToString(Convert.ToInt32(BytesText[u]));
if ((u + 1) % 8 == 0) help += '\t';
}
MessageBox.Show(help, " POSTAC BINARNA: ");
// Generate key
List<bool> keyBytes = new List<bool>();
keyBytes = BlumMicali(text1.Length);
help = "";
for (int s = 0; s < text1.Length * 8; s++)
{
help += Convert.ToInt32(keyBytes[s]);
if ((s + 1) % 8 == 0) help += '\t';
}
MessageBox.Show(help, " KLUCZ: ");
//Encryption
help = "";
string txt="";
List<bool> bitCipher = new List<bool>();
for (int s = 0; s < text1.Length * 8; s++)
{
bitCipher.Add(BytesText[s] ^ keyBytes[s]);
}
for (int s = 0; s < text1.Length * 8; s++)
{
help += Convert.ToInt32(bitCipher[s]);
txt += Convert.ToInt32(bitCipher[s]);
if ((s + 1) % 8 == 0) help += '\t';
}
MessageBox.Show(help, "ZASZYFROWANY CIAG BITOW:");
// CipherToASCII
string ASCIICipher = "";
bool[] bits = new bool[8];
help = "";
for (int s = 0; s < text1.Length; s++)
{
for (int m = 0; m < 8; m++)
{
bits[m] = bitCipher[s * 8 + m];
}
ASCIICipher += BytesToChars(bits);
}
help = ASCIICipher;
MessageBox.Show(help, "ZASZYFROWANA WIADOMOSC W ASCII: ");
string texts = Convert.ToString(txt);
System.IO.File.WriteAllText(@".\ZaszyfrowanyTekst.txt", texts);
return;
}
public int j = 7;
private string text1;
private int x0i;
//MyBigType powerTo(MyBigType x, MyBigType y, MyBigType p)
//{
// MyBigType res = new MyBigType(1);
// x = x % p;
// MyBigType zero = new MyBigType(0);
// while (y > zero)
// {
// if ((y % 2) == 1)
// {
// res *= x;
// res = res % p;
// }
// x *= x;
// x = x % p;
// y /= 2;
// }
// return res;
//}
static MyBigType powermodulo(MyBigType x, MyBigType y, MyBigType p)
{
MyBigType res = 1;
x = x % p;
while (y > 0)
{
if ((y & 1) == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
List<bool> BlumMicali(int size)
{
var random = new Random();
MyBigType g = new MyBigType("1347981406723692103108327603051596927581014069867");
MyBigType p = new MyBigType("1347981406723692103108327603051596927581014069863");
x0i = random.Next(10, 500);
MyBigType x0 = new MyBigType(x0i);
MyBigType x = new MyBigType(1);
string ccout = "x0 = " + Convert.ToString(x0);
System.IO.File.WriteAllText(@".\ZaszyfrowanyKlucz.txt", Convert.ToString(x0));
MessageBox.Show(ccout);
List<bool> klucz = new List<bool>();
for (int s = 0; s < size * 8; s++)
{
x = powermodulo(g, x0, p);
if (x > (p - 1) / 2) klucz.Add(false);
else klucz.Add(true);
x0 = x;
}
return klucz;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(text1);
}
public bool[] CharsToBytes(char chars)
{
bool[] bytes = new bool[8];
int j = 7;
do
{
bytes[j] = Convert.ToBoolean((int)chars % 2);
chars = (char)((int)chars / 2);
j--;
} while (j >= 0);
return bytes;
}
public char BytesToChars(bool[] bytes)
{
char chars = (char)0;
int ch = 0;
for (int j = 7; j >= 0; j--)
{
ch += Convert.ToInt32(Convert.ToInt64(bytes[j]) * (int)(Math.Pow(2, 7 - j)));
}
chars += (char)ch;
return chars;
}
// Returns true if n is prime
public static bool isPrime(MyBigType n)
{
// Corner cases
if (n <= 1)
{
return false;
}
if (n <= 3)
{
return true;
}
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
{
return false;
}
for (MyBigType i = 5; i * i <= n; i = i + 6)
{
if (n % i == 0 || n % (i + 2) == 0)
{
return false;
}
}
return true;
}
/* Iterative Function to calculate (x^n)%p in
O(logy) */
public static MyBigType power(MyBigType x, MyBigType y, MyBigType p)
{
MyBigType res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0)
{
// If y is odd, multiply x with result
if (y % 2 == 1)
{
res = (res * x) % p;
}
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Utility function to store prime factors of a number
public static void findPrimefactors(HashSet<MyBigType> s, MyBigType n)
{
// Print the number of 2s that divide n
while (n % 2 == 0)
{
s.Add(2);
n = n / 2;
}
// n must be odd at this point. So we can skip
// one element (Note i = i +2)
for (int i = 3; i <= n.Pow(2); i = i + 2)
{
// While i divides n, print i and divide n
while (n % i == 0)
{
s.Add(i);
n = n / i;
}
}
// This condition is to handle the case when
// n is a prime number greater than 2
if (n > 2)
{
s.Add(n);
}
}
// Function to find smallest primitive root of n
public static MyBigType findPrimitive(MyBigType n)
{
HashSet<MyBigType> s = new HashSet<MyBigType>();
// Check if n is prime or not
if (isPrime(n) == false)
{
return -1;
}
// Find value of Euler Totient function of n
// Since n is a prime number, the value of Euler
// Totient function is n-1 as there are n-1
// relatively prime numbers.
MyBigType phi = n - 1;
// Find prime factors of phi and store in a set
findPrimefactors(s, phi);
// Check for every number from 2 to phi
for (int r = 2; r <= phi; r++)
{
// Iterate through all prime factors of phi.
// and check if we found a power with value 1
bool flag = false;
foreach (MyBigType a in s)
{
// Check if r^((phi)/primefactors) mod n
// is 1 or not
if (power(r, phi / (a), n) == 1)
{
flag = true;
break;
}
}
// If there was no power with value 1.
if (flag == false)
{
return r;
}
}
// If no primitive root found
return -1;
}
}
}