1 /* Testing AES-CBC Implementation */
2
3 using System;
4
5 namespace AesLib {
6
7 public class Aes { // Advanced Encryption Standard
8 public enum KeySize { Bits128, Bits192, Bits256 }; // key size, in bits, for construtor
9
10 private int Nb; // block size in 32-bit words. Always 4 for AES. (128 bits).
11 private int Nk; // key size in 32-bit words. 4, 6, 8. (128, 192, 256 bits).
12 private int Nr; // number of rounds. 10, 12, 14.
13
14 private byte[] key; // the seed key. size will be 4 * keySize from ctor.
15 private byte[,] Sbox; // Substitution box
16 private byte[,] iSbox; // inverse Substitution box
17 private byte[,] w; // key schedule array.
18 private byte[,] Rcon; // Round constants.
19 private byte[,] State; // State matrix
20
21 public Aes(KeySize keySize, byte[] keyBytes) {
22 SetNbNkNr(keySize);
23
24 this.key = new byte[this.Nk * 4]; // 16, 24, 32 bytes
25 keyBytes.CopyTo(this.key, 0);
26
27 BuildSbox();
28 BuildInvSbox();
29 BuildRcon();
30 KeyExpansion(); // expand the seed key into a key schedule and store in w
31
32 } // Aes constructor
33
34 public void Cipher(byte[] input, byte[] output) { // encipher 16-bit input
35 // state = input
36 this.State = new byte[4,Nb]; // always [4,4]
37
38 for (int i = 0; i < (4 * Nb); ++i) {
39 this.State[i % 4, i / 4] = input[i];
40 }
41
42 AddRoundKey(0);
43
44 for (int round = 1; round <= (Nr - 1); ++round) { // main round loop
45 SubBytes();
46 ShiftRows();
47 MixColumns();
48 AddRoundKey(round);
49 } // main round loop
50
51 SubBytes();
52
53 ShiftRows();
54
55 AddRoundKey(Nr);
56
57 // output = state
58 for (int i = 0; i < (4 * Nb); ++i) {
59 output[i] = this.State[i % 4, i / 4];
60 }
61
62 } // Cipher()
63
64 public void InvCipher(byte[] input, byte[] output) { // decipher 16-bit input
65 // state = input
66 this.State = new byte[4,Nb]; // always [4,4]
67
68 for (int i = 0; i < (4 * Nb); ++i) {
69 this.State[i % 4, i / 4] = input[i];
70 }
71
72 AddRoundKey(Nr);
73
74 for (int round = Nr - 1; round >= 1; --round) { // main round loop
75 InvShiftRows();
76 InvSubBytes();
77 AddRoundKey(round);
78 InvMixColumns();
79 } // end main round loop for InvCipher
80
81 InvShiftRows();
82
83 InvSubBytes();
84
85 AddRoundKey(0);
86
87 // output = state
88 for (int i = 0; i < (4 * Nb); ++i) {
89 output[i] = this.State[i % 4, i / 4];
90 }
91
92 } // InvCipher()
93
94 private void SetNbNkNr(KeySize keySize) {
95 this.Nb = 4; // block size always = 4 words = 16 bytes = 128 bits for AES
96
97 if (keySize == KeySize.Bits128) {
98 this.Nk = 4; // key size = 4 words = 16 bytes = 128 bits
99 this.Nr = 10; // rounds for algorithm = 10
100
101 } else if (keySize == KeySize.Bits192) {
102 this.Nk = 6; // 6 words = 24 bytes = 192 bits
103 this.Nr = 12;
104
105 } else if (keySize == KeySize.Bits256) {
106 this.Nk = 8; // 8 words = 32 bytes = 256 bits
107 this.Nr = 14;
108 }
109 } // SetNbNkNr()
110
111 private void BuildSbox() {
112 this.Sbox = new byte[16,16] { // populate the Sbox matrix
113 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
114 /*0*/ {0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76},
115 /*1*/ {0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0},
116 /*2*/ {0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15},
117 /*3*/ {0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75},
118 /*4*/ {0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84},
119 /*5*/ {0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf},
120 /*6*/ {0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8},
121 /*7*/ {0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2},
122 /*8*/ {0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73},
123 /*9*/ {0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb},
124 /*a*/ {0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79},
125 /*b*/ {0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08},
126 /*c*/ {0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a},
127 /*d*/ {0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e},
128 /*e*/ {0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf},
129 /*f*/ {0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16}
130 };
131
132 } // BuildSbox()
133
134 private void BuildInvSbox() {
135 this.iSbox = new byte[16,16] { // populate the iSbox matrix
136 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
137 /*0*/ {0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb},
138 /*1*/ {0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb},
139 /*2*/ {0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e},
140 /*3*/ {0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25},
141 /*4*/ {0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92},
142 /*5*/ {0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84},
143 /*6*/ {0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06},
144 /*7*/ {0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b},
145 /*8*/ {0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73},
146 /*9*/ {0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e},
147 /*a*/ {0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b},
148 /*b*/ {0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4},
149 /*c*/ {0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f},
150 /*d*/ {0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef},
151 /*e*/ {0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61},
152 /*f*/ {0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d}
153 };
154
155 } // BuildInvSbox()
156
157 private void BuildRcon() {
158 this.Rcon = new byte[11,4] { {0x00, 0x00, 0x00, 0x00},
159 {0x01, 0x00, 0x00, 0x00},
160 {0x02, 0x00, 0x00, 0x00},
161 {0x04, 0x00, 0x00, 0x00},
162 {0x08, 0x00, 0x00, 0x00},
163 {0x10, 0x00, 0x00, 0x00},
164 {0x20, 0x00, 0x00, 0x00},
165 {0x40, 0x00, 0x00, 0x00},
166 {0x80, 0x00, 0x00, 0x00},
167 {0x1b, 0x00, 0x00, 0x00},
168 {0x36, 0x00, 0x00, 0x00}
169 };
170 } // BuildRcon()
171
172 private void AddRoundKey(int round) {
173
174 for (int r = 0; r < 4; ++r) {
175 for (int c = 0; c < 4; ++c) {
176 this.State[r,c] = (byte) ( (int)this.State[r,c] ^ (int)w[(round*4)+c,r] );
177 }
178 }
179 } // AddRoundKey()
180
181 private void SubBytes() {
182 for (int r = 0; r < 4; ++r) {
183 for (int c = 0; c < 4; ++c) {
184 this.State[r,c] = this.Sbox[ (this.State[r,c] >> 4), (this.State[r,c] & 0x0f) ];
185 }
186 }
187 } // SubBytes
188
189 private void InvSubBytes() {
190 for (int r = 0; r < 4; ++r) {
191 for (int c = 0; c < 4; ++c) {
192 this.State[r,c] = this.iSbox[ (this.State[r,c] >> 4), (this.State[r,c] & 0x0f) ];
193 }
194 }
195 } // InvSubBytes
196
197 private void ShiftRows() {
198 byte[,] temp = new byte[4,4];
199
200 for (int r = 0; r < 4; ++r) { // copy State into temp[]
201 for (int c = 0; c < 4; ++c) {
202 temp[r,c] = this.State[r,c];
203 }
204 }
205
206 for (int r = 1; r < 4; ++r) { // shift temp into State
207 for (int c = 0; c < 4; ++c) {
208 this.State[r,c] = temp[ r, (c + r) % Nb ];
209 }
210 }
211 } // ShiftRows()
212
213 private void InvShiftRows() {
214 byte[,] temp = new byte[4,4];
215
216 for (int r = 0; r < 4; ++r) { // copy State into temp[]
217 for (int c = 0; c < 4; ++c) {
218 temp[r,c] = this.State[r,c];
219 }
220 }
221
222 for (int r = 1; r < 4; ++r) { // shift temp into State
223 for (int c = 0; c < 4; ++c) {
224 this.State[r, (c + r) % Nb ] = temp[r,c];
225 }
226 }
227 } // InvShiftRows()
228
229 private void MixColumns() {
230 byte[,] temp = new byte[4,4];
231
232 for (int r = 0; r < 4; ++r) { // copy State into temp[]
233 for (int c = 0; c < 4; ++c) {
234 temp[r,c] = this.State[r,c];
235 }
236 }
237
238 for (int c = 0; c < 4; ++c) {
239 this.State[0,c] = (byte) ( (int)gfmultby02(temp[0,c]) ^ (int)gfmultby03(temp[1,c]) ^
240 (int)gfmultby01(temp[2,c]) ^ (int)gfmultby01(temp[3,c]) );
241 this.State[1,c] = (byte) ( (int)gfmultby01(temp[0,c]) ^ (int)gfmultby02(temp[1,c]) ^
242 (int)gfmultby03(temp[2,c]) ^ (int)gfmultby01(temp[3,c]) );
243 this.State[2,c] = (byte) ( (int)gfmultby01(temp[0,c]) ^ (int)gfmultby01(temp[1,c]) ^
244 (int)gfmultby02(temp[2,c]) ^ (int)gfmultby03(temp[3,c]) );
245 this.State[3,c] = (byte) ( (int)gfmultby03(temp[0,c]) ^ (int)gfmultby01(temp[1,c]) ^
246 (int)gfmultby01(temp[2,c]) ^ (int)gfmultby02(temp[3,c]) );
247 }
248 } // MixColumns
249
250 private void InvMixColumns() {
251 byte[,] temp = new byte[4,4];
252
253 for (int r = 0; r < 4; ++r) { // copy State into temp[]
254 for (int c = 0; c < 4; ++c) {
255 temp[r,c] = this.State[r,c];
256 }
257 }
258
259 for (int c = 0; c < 4; ++c) {
260 this.State[0,c] = (byte) ( (int)gfmultby0e(temp[0,c]) ^ (int)gfmultby0b(temp[1,c]) ^
261 (int)gfmultby0d(temp[2,c]) ^ (int)gfmultby09(temp[3,c]) );
262 this.State[1,c] = (byte) ( (int)gfmultby09(temp[0,c]) ^ (int)gfmultby0e(temp[1,c]) ^
263 (int)gfmultby0b(temp[2,c]) ^ (int)gfmultby0d(temp[3,c]) );
264 this.State[2,c] = (byte) ( (int)gfmultby0d(temp[0,c]) ^ (int)gfmultby09(temp[1,c]) ^
265 (int)gfmultby0e(temp[2,c]) ^ (int)gfmultby0b(temp[3,c]) );
266 this.State[3,c] = (byte) ( (int)gfmultby0b(temp[0,c]) ^ (int)gfmultby0d(temp[1,c]) ^
267 (int)gfmultby09(temp[2,c]) ^ (int)gfmultby0e(temp[3,c]) );
268 }
269 } // InvMixColumns
270
271 private static byte gfmultby01(byte b) {
272 return b;
273 }
274
275 private static byte gfmultby02(byte b) {
276 if (b < 0x80)
277 return (byte)(int)(b << 1);
278 else
279 return (byte)( (int)(b << 1) ^ (int)(0x1b) );
280 }
281
282 private static byte gfmultby03(byte b) {
283 return (byte) ( (int)gfmultby02(b) ^ (int)b );
284 }
285
286 private static byte gfmultby09(byte b) {
287 return (byte)( (int)gfmultby02(gfmultby02(gfmultby02(b))) ^
288 (int)b );
289 }
290
291 private static byte gfmultby0b(byte b) {
292 return (byte)( (int)gfmultby02(gfmultby02(gfmultby02(b))) ^
293 (int)gfmultby02(b) ^
294 (int)b );
295 }
296
297 private static byte gfmultby0d(byte b) {
298 return (byte)( (int)gfmultby02(gfmultby02(gfmultby02(b))) ^
299 (int)gfmultby02(gfmultby02(b)) ^
300 (int)(b) );
301 }
302
303 private static byte gfmultby0e(byte b) {
304 return (byte)( (int)gfmultby02(gfmultby02(gfmultby02(b))) ^
305 (int)gfmultby02(gfmultby02(b)) ^
306 (int)gfmultby02(b) );
307 }
308
309 private void KeyExpansion() {
310 this.w = new byte[Nb * (Nr+1), 4]; // 4 columns of bytes corresponds to a word
311
312 for (int row = 0; row < Nk; ++row) {
313 this.w[row,0] = this.key[4*row];
314 this.w[row,1] = this.key[4*row+1];
315 this.w[row,2] = this.key[4*row+2];
316 this.w[row,3] = this.key[4*row+3];
317 }
318
319 byte[] temp = new byte[4];
320
321 for (int row = Nk; row < Nb * (Nr + 1); ++row) {
322 temp[0] = this.w[row-1,0]; temp[1] = this.w[row-1,1];
323 temp[2] = this.w[row-1,2]; temp[3] = this.w[row-1,3];
324
325 if (row % Nk == 0) {
326 temp = SubWord(RotWord(temp));
327
328 temp[0] = (byte)( (int)temp[0] ^ (int)this.Rcon[row/Nk,0] );
329 temp[1] = (byte)( (int)temp[1] ^ (int)this.Rcon[row/Nk,1] );
330 temp[2] = (byte)( (int)temp[2] ^ (int)this.Rcon[row/Nk,2] );
331 temp[3] = (byte)( (int)temp[3] ^ (int)this.Rcon[row/Nk,3] );
332
333 } else if ( Nk > 6 && (row % Nk == 4) ) {
334 temp = SubWord(temp);
335 }
336
337 // w[row] = w[row-Nk] xor temp
338 this.w[row,0] = (byte) ( (int)this.w[row-Nk,0] ^ (int)temp[0] );
339
340 this.w[row,1] = (byte) ( (int)this.w[row-Nk,1] ^ (int)temp[1] );
341
342 this.w[row,2] = (byte) ( (int)this.w[row-Nk,2] ^ (int)temp[2] );
343
344 this.w[row,3] = (byte) ( (int)this.w[row-Nk,3] ^ (int)temp[3] );
345
346 } // for loop
347 } // KeyExpansion()
348
349 private byte[] SubWord(byte[] word) {
350 byte[] result = new byte[4];
351 result[0] = this.Sbox[ word[0] >> 4, word[0] & 0x0f ];
352 result[1] = this.Sbox[ word[1] >> 4, word[1] & 0x0f ];
353 result[2] = this.Sbox[ word[2] >> 4, word[2] & 0x0f ];
354 result[3] = this.Sbox[ word[3] >> 4, word[3] & 0x0f ];
355 return result;
356 }
357
358 private byte[] RotWord(byte[] word) {
359 byte[] result = new byte[4];
360 result[0] = word[1];
361 result[1] = word[2];
362 result[2] = word[3];
363 result[3] = word[0];
364 return result;
365 }
366
367 public void Dump() {
368 Console.WriteLine("Nb = " + Nb + " Nk = " + Nk + " Nr = " + Nr);
369 Console.WriteLine("\nThe key is \n" + DumpKey() );
370 Console.WriteLine("\nThe Sbox is \n" + DumpTwoByTwo(Sbox));
371 Console.WriteLine("\nThe w array is \n" + DumpTwoByTwo(w));
372 Console.WriteLine("\nThe State array is \n" + DumpTwoByTwo(State));
373 }
374
375 public string DumpKey() {
376 string s = "";
377
378 for (int i = 0; i < key.Length; ++i)
379 s += key[i].ToString("x2") + " ";
380
381 return s;
382 }
383
384 public string DumpTwoByTwo(byte[,] a) {
385 string s = "";
386
387 for (int r = 0; r < a.GetLength(0); ++r) {
388 s += "[" + r + "]" + " ";
389
390 for (int c = 0; c < a.GetLength(1); ++c) {
391 s += a[r,c].ToString("x2") + " " ;
392 }
393
394 s += "\n";
395 }
396
397 return s;
398 }
399
400 } // class Aes
401
402 class Test {
403
404 private const int cBlockSizeInBytes = 16;
405
406 /* PKCS5/PKCS7 compliant padding */
407 private static byte [] appendPadding (byte [] data) {
408
409 int cnt = data.Length;
410 int paddingLenght = cBlockSizeInBytes - (cnt % cBlockSizeInBytes);
411 int paddedLenght = cnt + paddingLenght;
412 byte [] res = new byte [paddedLenght];
413
414 for (int i = 0; i < paddedLenght; i++)
415 res [i] = (i < cnt) ? data [i] : (byte) paddingLenght;
416
417 System.Console.WriteLine ("paddedLenght=" + paddingLenght);
418 DisplayAsBytes (res);
419 return res;
420 }
421
422 private static byte [] removePadding (byte [] paddedData) {
423
424 int paddedLength = paddedData.Length;
425 int cnt = 1;
426
427 for (int i = paddedLength -1; i >= 0; i--, cnt++)
428 if (paddedData [i] != paddedData [i -1]) break;
429
430 if ((cnt % cBlockSizeInBytes) != (int) paddedData [paddedLength -1])
431 throw new System.Exception ("Padding error: cnt=" + cnt +
432 "; bs=" + cBlockSizeInBytes + "; byte=" +
433 paddedData [paddedLength -1] + ";");
434
435 byte [] res = new byte [paddedLength - cnt];
436
437 for (int i = 0; i < paddedLength - cnt; i++)
438 res [i] = paddedData [i];
439
440 return res;
441 }
442
443 private static byte [] encrypt (byte [] input, byte [] key) {
444
445 /* XXX: Currently encrypts only first block -> implement CBC */
446 Aes a = new Aes (Aes.KeySize.Bits256, key);
447 input = appendPadding (input);
448 byte [] res = new byte [input.Length];
449 a.Cipher (input, res);
450
451 return res;
452 }
453
454 private static byte [] decrypt (byte [] input, byte [] key) {
455
456 /* XXX: Currently decrypts only first block -> implement CBC */
457 byte [] res = new byte [input.Length];
458 Aes a = new Aes (Aes.KeySize.Bits256, key);
459 a.InvCipher (input, res);
460 res = removePadding (res);
461
462 return res;
463 }
464
465 [STAThread]
466 static void Main(string[] args) {
467
468 string plain = (args.Length > 0) ? args [0] : "Hello Erlang!";
469 byte[] plainText = System.Text.Encoding.UTF8.GetBytes (plain);
470
471 Console.WriteLine("\nThe plaintext is: ");
472 DisplayAsBytes(plainText);
473 System.Console.WriteLine (byte2Hex (plainText));
474
475 byte[] cipherText;
476 byte[] decipheredText;
477 byte[] keyBytes = hex2Byte ("b7a08f20cdd82d5625f5504f9cf6053240f742c0cee6e057f8065e8877dc9bee");
478
479 Console.WriteLine("\nUsing a " + Aes.KeySize.Bits256.ToString() + "-key of: ");
480 DisplayAsBytes(keyBytes);
481 System.Console.WriteLine (byte2Hex (keyBytes));
482
483 cipherText = encrypt (plainText, keyBytes);
484
485 Console.WriteLine ("\nThe resulting ciphertext is: ");
486 DisplayAsBytes (cipherText);
487 System.Console.WriteLine (byte2Hex (cipherText));
488
489 decipheredText = decrypt (cipherText, keyBytes);
490
491 Console.WriteLine("\nAfter deciphering the ciphertext, the result is: ");
492 DisplayAsBytes(decipheredText);
493 System.Console.WriteLine (byte2Hex (decipheredText));
494 System.Console.WriteLine (">>>{0}<<<", System.Text.Encoding.UTF7.GetString (decipheredText));
495
496 Console.WriteLine("\nDone");
497 }
498
499 private static string byte2Hex (byte [] bytes) {
500
501 System.Text.StringBuilder sb = new System.Text.StringBuilder (bytes.Length * 2);
502
503 foreach (byte b in bytes)
504 sb.AppendFormat("{0:x2}", b);
505
506 return sb.ToString ();
507 }
508
509 public static byte [] hex2Byte (string hexString) {
510
511 int n = hexString.Length;
512
513 byte[] bytes = new byte [n / 2];
514
515 for (int i = 0; i < n; i += 2)
516 bytes [i / 2] = System.Convert.ToByte (hexString.Substring (i, 2), 16);
517
518 return bytes;
519 }
520
521 static void DisplayAsBytes (byte [] bytes) {
522
523 for (int i = 0; i < bytes.Length; ++i) {
524 System.Console.Write (bytes [i].ToString ("x2") + " ");
525 if (i > 0 && i % 16 == 0) System.Console.WriteLine ();
526 }
527 System.Console.WriteLine ();
528 }
529 }
530 }
531