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
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int[] sbox = new int[] { 3, 14, 1, 10, 4, 9, 5, 6, 8, 14, 2, 11, 0, 4, 6, 7, 15, 8, 5, 3, 9, 13, 12, 1, 10, 11, 15, 2, 13, 12, 0, 5 };
int[] sboxRev = new int[] { 14, 2, 11, 0, 4, 6, 7, 15, 8, 5, 3, 9, 13, 12, 1, 10, 3, 14, 1, 10, 4, 9, 5, 6, 8, 14, 2, 11, 0, 4, 6, 7 };
int[,] chars = new int[32, 32];
int numPairs=8;
int [] chardat0=new int [32];
int chardatmax = 0;
int [] knownP0 = new int [10000];
int [] knownP1 = new int [10000];
int [] knownC0 = new int [10000];
int [] knownC1 = new int [10000];
int goodP0, goodP1, goodC0, goodC1;
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
int plaintext = Convert.ToInt16(textBox1.Text);
}
private void button1_Click(object sender, EventArgs e)
{
int i, j;
findDiffs(); //Find some good differentials in the S-Boxes
richTextBox1.Text+="Creating XOR differential table:";
for(i = 0; i < 32; i++)
for(j = 0; j < 32; j++)
chars[i ^ j, sbox[i] ^ sbox[j]]++;
for (i=0; i<16;i++)
{
for (j=0; j<16; j++)
richTextBox1.Text+= chars[ i, j] + '\n';
}
int a, b, c, plaint;
c = encrypt(1,2,3);
richTextBox1.Text +="ciphertext is: ";
richTextBox1.Text += c.ToString();
plaint = decrypt(4,5,6);
richTextBox1.Text+="plaintext is:";
richTextBox1.Text+=plaint.ToString();
//encrypt(int a, int b, int c);
//public int decrypt(9, 4, 2);
//int numPairs = 8; //Define number of known pairs
//public string genCharData(4, 7); //Find inputs that lead a certain characteristic
//public string genPairs(4); //Generate chosen-plaintext pairs
//public findGoodPair(7); //Choose a known pair that satisfies the characteristic
//crack(); //Use charData and "good pair" in find key
while (true) {
}
}
public void genPairs(int indiff)
{
Console.WriteLine("\nGenerating %i known pairs with input differential of %i.\n", numPairs, indiff);
Random realk0 = new Random(); //Create random subkey0
Random realk1 = new Random(); //Create random subkey0
Console.WriteLine(" Real K0 = %i\n", realk0);
Console.WriteLine(" Real K1 = %i\n", realk1);
int c;
for (c = 0; c < numPairs; c++) //Create plaintext pairs with XOR difference of indiff
{
Random knownP0 = new Random();
knownP1 = knownP0[c] ^ indiff;
knownC0 = encrypt(knownP0, realk0, realk1);
knownC1 = encrypt(knownP1, realk0, realk1);
}
}
public int roundFunc(int input, int key)
{
return sbox[key ^ input];
}
public int encrypt(int input, int k0, int k1)
{
int x0 = roundFunc(input, k0);
int c = x0 ^ k1;
return c;
}
public int decrypt (int cipher, int k0, int k1)
{
int plaint = k0^ sbox[cipher^k1];
return plaint;
}
public void findDiffs()
{
int c, d, e, f;
int max;
for(c = 0; c < 32; c++)
for(d = 0; d < 32; d++)
// chars[c,d][sbox[c] ^ sbox[d]]++;
for(c = 0; c < 32; c++)
{
for(d = 0; d < 32; d++)
Console.WriteLine (" %x ", chars [c, d]);
Console.WriteLine ("\n");
}
}
public void genCharData(int indiff, int outdiff)
{
Console.WriteLine ("\nGenerating possible intermediate values based on differential(%i —> %i):\n", indiff, outdiff);
chardatmax = 0;
int f;
for (f = 0; f < 32; f++)
{
int myComp = f ^ indiff;
if ((sbox[f] ^ sbox[myComp]) == outdiff)
{
Console.WriteLine (" Possibles: %i + %i —> %i + %i\n", f, myComp, sbox[f], sbox[myComp]);
chardat0[chardatmax] = f;
chardatmax++;
}
}
}
public void findGoodPair(int outdiff)
{
Console.WriteLine("\nSearching for good pair:\n");
int c;
for (c = 0; c < numPairs; c++)
if ((knownC0[] ^ knownC1[]) == outdiff) //Does the ciphertext pair fit the characteristic?
{
goodC0 = knownC0[c];
goodC1 = knownC1[c];
goodP0 = knownP0[c];
goodP1 = knownP1[c];
Console.WriteLine (" FOUND GOOD PAIR: (P0 = %i, P1 = %i) —> (C0 = %i, C1 = %i)\n", goodP0, goodP1, goodC0, goodC1);
return;
}
Console.WriteLine ("NO GOOD PAIR FOUND!\n");
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
}
} |