-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamCipher.cs
More file actions
121 lines (110 loc) · 3.62 KB
/
Copy pathStreamCipher.cs
File metadata and controls
121 lines (110 loc) · 3.62 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
using System;
using System.Collections.Generic;
using VigenereCipherSolver;
namespace CiphersSolvers
{
public class StreamCipher : ICipher
{
public double MyRand { get; set; }
public Dictionary<double, string> best;
public double Key { get; set; }
public string Text { get; set; }
public StreamCipher(string text)
{
best = new Dictionary<double, string>();
Text = text;
}
public string Cipher(string text, bool encrypt)
{
SetKey();
SetSeed(Key);
text = text.ToUpper();
int i;
var cipherText = "";
for (i = 0; i < text.Length; i++)
{
int ch = text[i];
if (ch >= 'A' && ch <= 'Z')
{
int p = ch - 'A';
int k = (int)(26 * NextRand());
int c = 0;
if (encrypt) c = (p + k) % 26;
else c = (p + (26 - k)) % 26;
cipherText += (char)('A' + c);
}
else
{
cipherText += (char)ch;
}
}
return cipherText;
}
private void BruteForce(int from, int to)
{
var s = new Spinner(Console.CursorLeft,Console.CursorTop);
s.Start();
for (int j = from; j < to; j++)
{
SetSeed(j);
Text = Text.ToUpper();
int i;
var cipherText = "";
for (i = 0; i < Text.Length; i++)
{
int ch = Text[i];
if (ch >= 'A' && ch <= 'Z')
{
int p = ch - 'A';
int k = (int)(26 * NextRand());
int c = (p + (26 - k)) % 26;
cipherText += (char)('A' + c);
}
else
{
cipherText += (char)ch;
}
}
var ic = ICTest.IndexOfCoincidence(cipherText);
if (ic > 0.055)
{
best.Add(j,"IC: "+ ic.ToString().Substring(0,5) + " SAMPLE: " + cipherText.Substring(0, 60));
}
}
s.Stop();
}
private void SetKey()
{
Console.WriteLine("Do you know key?");
if (Console.ReadLine().ToLower().StartsWith('y'))
{
Console.WriteLine("Please provide key to encrypt this message: ");
Key = double.Parse(Console.ReadLine());
}
else
{
Console.WriteLine("Please provide range to test seeds for random gen... FROM: ");
int from = int.Parse(Console.ReadLine());
Console.WriteLine("TO: ");
int to = int.Parse(Console.ReadLine());
BruteForce(from, to);
Console.WriteLine("Best IC for these texts:");
foreach (var keyValuePair in best)
{
Console.WriteLine("SEED: " + keyValuePair.Key + " " + keyValuePair.Value + "...");
}
Console.WriteLine("Select SEED:");
Key = double.Parse(Console.ReadLine());
}
}
void SetSeed(double val)
{
MyRand = val;
}
double NextRand()
{
MyRand = (84589 * MyRand + 45989) % 217728;
return (double)MyRand / 217728.0;
}
}
}