des_cfb_examples/
des_cfb_examples.rs

1// Copyright 2025 PARK Youngho.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6// This file may not be copied, modified, or distributed
7// except according to those terms.
8
9#![allow(missing_docs)]
10#![allow(unused_must_use)]
11#![allow(dead_code)]
12#![allow(unused_imports)]
13#![allow(non_snake_case)]
14
15
16// use std::io::Write;
17
18// #![allow(missing_docs)]
19// #![allow(rustdoc::missing_doc_code_examples)]
20// #[allow(non_camel_case_types)]
21// #[allow(dead_code)]
22pub fn main()
23{
24    des_encrypt_cfb();
25    des_encrypt_cfb_into_vec();
26    des_encrypt_cfb_into_array();
27    des_encrypt_str_cfb();
28    des_encrypt_str_cfb_into_vec();
29    des_encrypt_str_cfb_into_array();
30    des_encrypt_string_cfb();
31    des_encrypt_string_cfb_into_vec();
32    des_encrypt_string_cfb_into_array();
33    des_encrypt_vec_cfb();
34    des_encrypt_vec_cfb_into_vec();
35    des_encrypt_vec_cfb_into_array();
36    des_encrypt_array_cfb();
37    des_encrypt_array_cfb_into_vec();
38    des_encrypt_array_cfb_into_array();
39
40    des_decrypt_cfb();
41    des_decrypt_cfb_into_vec();
42    des_decrypt_cfb_into_array();
43    des_decrypt_cfb_into_string();
44    des_decrypt_vec_cfb();
45    des_decrypt_vec_cfb_into_vec();
46    des_decrypt_vec_cfb_into_array();
47    des_decrypt_vec_cfb_into_string();
48    des_decrypt_array_cfb();
49    des_decrypt_array_cfb_into_vec();
50    des_decrypt_array_cfb_into_array();
51    des_decrypt_array_cfb_into_string();
52}
53
54fn des_encrypt_cfb()
55{
56    println!("des_encrypt_cfb()");
57    use std::io::Write;
58    use std::fmt::Write as _;
59    use cryptocol::symmetric::{ DES, DES_Expanded, CFB};
60
61    // Normal case
62    let key = 0x_1234567890ABCDEF_u64;
63    println!("K =\t{:#016X}", key);
64    let mut a_des = DES::new_with_key_u64(key);
65
66    let message = "In the beginning God created the heavens and the earth.";
67    println!("M =\t{}", message);
68    let iv = 0x_FEDCBA0987654321_u64;
69    println!("IV =	{}", iv);
70    let mut cipher = [0_u8; 55];
71    a_des.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
72    print!("C (16 rounds) =\t");
73    for c in cipher.clone()
74        { print!("{:02X} ", c); }
75    println!();
76    let mut txt = String::new();
77    for c in cipher.clone()
78        { write!(txt, "{:02X} ", c); }
79    assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 79 3A A1 78 EC CD 02 72 6A C4 41 7C 25 A4 2C 07 FC 77 25 49 12 55 0F 8A ED 44 C3 E4 DC 91 69 0F 40 72 7F F2 D9 B7 54 9F 36 91 C5 85 4F 9B 30 ");
80    println!();
81
82    // Expanded case for 128 rounds
83    let key = 0x_1234567890ABCDEF_u64;
84    println!("K =\t{:#016X}", key);
85    let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
86
87    let message = "In the beginning God created the heavens and the earth.";
88    println!("M =\t{}", message);
89
90    let iv = 0x_FEDCBA0987654321_u64;
91    println!("IV =	{}", iv);
92    let mut cipher = [0_u8; 55];
93    a_des.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
94    print!("C (128 rounds) =\t");
95    for c in cipher.clone()
96        { print!("{:02X} ", c); }
97    println!();
98    let mut txt = String::new();
99    for c in cipher.clone()
100        { write!(txt, "{:02X} ", c); }
101    assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 F2 9A 70 44 D9 78 F5 E3 CF 55 0F EF BA F0 4A 7E BE 79 C9 B4 68 F4 99 09 48 93 00 D2 22 9D 29 6C 20 74 FF E3 E2 01 0C D3 7E 8D 4D 30 8F EC D6 ");
102    println!();
103
104    // Expanded case for 0 rounds which means that key is meaningless
105    let key1 = 0x_1234567890ABCDEF_u64;
106    let key2 = 0_u64;
107    println!("K =\t{:#016X}", key);
108    let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
109    let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
110
111    let message = "In the beginning God created the heavens and the earth.";
112    println!("M =\t{}", message);
113    let iv = 0x_FEDCBA0987654321_u64;
114    println!("IV =	{}", iv);
115    let mut cipher1 = [0_u8; 55];
116    let mut cipher2 = [0_u8; 55];
117    c_des.encrypt(iv, message.as_ptr(), message.len() as u64, cipher1.as_mut_ptr());
118    d_des.encrypt(iv, message.as_ptr(), message.len() as u64, cipher2.as_mut_ptr());
119    print!("C (0 rounds) =\t");
120    for c in cipher1.clone()
121        { print!("{:02X} ", c); }
122    println!();
123    let mut txt = String::new();
124    for c in cipher1.clone()
125        { write!(txt, "{:02X} ", c); }
126    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
127    print!("D (0 rounds) =\t");
128    for c in cipher2.clone()
129        { print!("{:02X} ", c); }
130    println!();
131    let mut txt = String::new();
132    for c in cipher2.clone()
133        { write!(txt, "{:02X} ", c); }
134    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
135    println!();
136
137    // Normal case for the message of 0 bytes
138    let key = 0x_1234567890ABCDEF_u64;
139    println!("K =\t{:#016X}", key);
140    let mut a_des = DES::new_with_key_u64(key);
141
142    let message = "";
143    println!("M =\t{}", message);
144    let iv = 0x_FEDCBA0987654321_u64;
145    println!("IV =	{}", iv);
146    let mut cipher = [0_u8; 0];
147    a_des.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
148    print!("C =\t");
149    for c in cipher.clone()
150        { print!("{:02X} ", c); }
151    println!();
152    let mut txt = String::new();
153    for c in cipher.clone()
154        { write!(txt, "{:02X} ", c); }
155    assert_eq!(txt, "");
156    println!();
157
158    // Normal case for the message shorter than 8 bytes
159    let key = 0x_1234567890ABCDEF_u64;
160    println!("K =\t{:#016X}", key);
161    let mut a_des = DES::new_with_key_u64(key);
162
163    let message = "7 bytes";
164    println!("M =\t{}", message);
165    let iv = 0x_FEDCBA0987654321_u64;
166    println!("IV =	{}", iv);
167    let mut cipher = [0_u8; 7];
168    a_des.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
169    print!("C =\t");
170    for c in cipher.clone()
171        { print!("{:02X} ", c); }
172    println!();
173    let mut txt = String::new();
174    for c in cipher.clone()
175        { write!(txt, "{:02X} ", c); }
176    assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
177    println!();
178
179    // Normal case for the message of 8 bytes
180    let key = 0x_1234567890ABCDEF_u64;
181    println!("K =\t{:#016X}", key);
182    let mut a_des = DES::new_with_key_u64(key);
183
184    let message = "I am OK.";
185    println!("M =\t{}", message);
186    let iv = 0x_FEDCBA0987654321_u64;
187    println!("IV =	{}", iv);
188    let mut cipher = [0_u8; 8];
189    a_des.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
190    print!("C =\t");
191    for c in cipher.clone()
192        { print!("{:02X} ", c); }
193    println!();
194    let mut txt = String::new();
195    for c in cipher.clone()
196        { write!(txt, "{:02X} ", c); }
197    assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
198    println!();
199
200    // Normal case for the message longer than 8 bytes and shorter than 16 bytes
201    let key = 0x_1234567890ABCDEF_u64;
202    println!("K =\t{:#016X}", key);
203    let mut a_des = DES::new_with_key_u64(key);
204
205    let message = "PARK Youngho";
206    println!("M =\t{}", message);
207    let iv = 0x_FEDCBA0987654321_u64;
208    println!("IV =	{}", iv);
209    let mut cipher = [0_u8; 12];
210    a_des.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
211    print!("C =\t");
212    for c in cipher.clone()
213        { print!("{:02X} ", c); }
214    println!();
215    let mut txt = String::new();
216    for c in cipher.clone()
217        { write!(txt, "{:02X} ", c); }
218    assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 74 F9 B6 4F ");
219    println!();
220
221    // Normal case for the message of 16 bytes
222    let key = 0x_1234567890ABCDEF_u64;
223    println!("K =\t{:#016X}", key);
224    let mut a_des = DES::new_with_key_u64(key);
225
226    let message = "고맙습니다.";
227    println!("M =\t{}", message);
228    let iv = 0x_FEDCBA0987654321_u64;
229    println!("IV =	{}", iv);
230    let mut cipher = [0_u8; 16];
231    a_des.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
232    print!("C =\t");
233    for c in cipher.clone()
234        { print!("{:02X} ", c); }
235    println!();
236    let mut txt = String::new();
237    for c in cipher.clone()
238        { write!(txt, "{:02X} ", c); }
239    assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 F4 FC 94 B5 94 1F B9 BD ");
240    println!("-------------------------------");
241}
242
243fn des_encrypt_cfb_into_vec()
244{
245    println!("des_encrypt_cfb_into_vec()");
246    use std::io::Write;
247    use std::fmt::Write as _;
248    use cryptocol::symmetric::{ DES, DES_Expanded, CFB};
249
250    // Normal case
251    let key = 0x_1234567890ABCDEF_u64;
252    println!("K =\t{:#016X}", key);
253    let mut a_des = DES::new_with_key_u64(key);
254
255    let message = "In the beginning God created the heavens and the earth.";
256    println!("M =\t{}", message);
257    let iv = 0x_FEDCBA0987654321_u64;
258    println!("IV =	{}", iv);
259    let mut cipher = Vec::<u8>::new();
260    a_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher);
261    print!("C (16 rounds) =\t");
262    for c in cipher.clone()
263        { print!("{:02X} ", c); }
264    println!();
265    let mut txt = String::new();
266    for c in cipher.clone()
267        { write!(txt, "{:02X} ", c); }
268    assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 79 3A A1 78 EC CD 02 72 6A C4 41 7C 25 A4 2C 07 FC 77 25 49 12 55 0F 8A ED 44 C3 E4 DC 91 69 0F 40 72 7F F2 D9 B7 54 9F 36 91 C5 85 4F 9B 30 ");
269    println!();
270
271    // Expanded case for 128 rounds
272    let key = 0x_1234567890ABCDEF_u64;
273    println!("K =\t{:#016X}", key);
274    let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
275
276    let message = "In the beginning God created the heavens and the earth.";
277    println!("M =\t{}", message);
278    let iv = 0x_FEDCBA0987654321_u64;
279    println!("IV =	{}", iv);
280    let mut cipher = Vec::<u8>::new();
281    a_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher);
282    print!("C (128 rounds) =\t");
283    for c in cipher.clone()
284        { print!("{:02X} ", c); }
285    println!();
286    let mut txt = String::new();
287    for c in cipher.clone()
288        { write!(txt, "{:02X} ", c); }
289    assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 F2 9A 70 44 D9 78 F5 E3 CF 55 0F EF BA F0 4A 7E BE 79 C9 B4 68 F4 99 09 48 93 00 D2 22 9D 29 6C 20 74 FF E3 E2 01 0C D3 7E 8D 4D 30 8F EC D6 ");
290    println!();
291
292    // Expanded case for 0 rounds which means that key is meaningless
293    let key1 = 0x_1234567890ABCDEF_u64;
294    let key2 = 0_u64;
295    let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
296    let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
297    println!("K =\t{:#016X}", key);
298
299    let message = "In the beginning God created the heavens and the earth.";
300    println!("M =\t{}", message);
301    let iv = 0x_FEDCBA0987654321_u64;
302    println!("IV =	{}", iv);
303    let mut cipher1 = Vec::<u8>::new();
304    let mut cipher2 = Vec::<u8>::new();
305    c_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
306    d_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
307    print!("C (0 rounds) =\t");
308    for c in cipher1.clone()
309        { print!("{:02X} ", c); }
310    println!();
311    let mut txt = String::new();
312    for c in cipher1.clone()
313        { write!(txt, "{:02X} ", c); }
314    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
315    print!("D (0 rounds) =\t");
316    for c in cipher2.clone()
317        { print!("{:02X} ", c); }
318    println!();
319    let mut txt = String::new();
320    for c in cipher2.clone()
321        { write!(txt, "{:02X} ", c); }
322    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
323    println!();
324
325    // Normal case for the message of 0 bytes
326    let key = 0x_1234567890ABCDEF_u64;
327    println!("K =\t{:#016X}", key);
328    let mut a_des = DES::new_with_key_u64(key);
329
330    let message = "";
331    println!("M =\t{}", message);
332    let iv = 0x_FEDCBA0987654321_u64;
333    println!("IV =	{}", iv);
334    let mut cipher = Vec::<u8>::new();
335    a_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher);
336    print!("C =\t");
337    for c in cipher.clone()
338        { print!("{:02X} ", c); }
339    println!();
340    let mut txt = String::new();
341    for c in cipher.clone()
342        { write!(txt, "{:02X} ", c); }
343    assert_eq!(txt, "");
344    println!();
345
346    // Normal case for the message shorter than 8 bytes
347    let key = 0x_1234567890ABCDEF_u64;
348    println!("K =\t{:#016X}", key);
349    let mut a_des = DES::new_with_key_u64(key);
350
351    let message = "7 bytes";
352    println!("M =\t{}", message);
353    let iv = 0x_FEDCBA0987654321_u64;
354    println!("IV =	{}", iv);
355    let mut cipher = Vec::<u8>::new();
356    a_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher);
357    print!("C =\t");
358    for c in cipher.clone()
359        { print!("{:02X} ", c); }
360    println!();
361    let mut txt = String::new();
362    for c in cipher.clone()
363        { write!(txt, "{:02X} ", c); }
364    assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
365    println!();
366
367    // Normal case for the message of 8 bytes
368    let key = 0x_1234567890ABCDEF_u64;
369    println!("K =\t{:#016X}", key);
370    let mut a_des = DES::new_with_key_u64(key);
371
372    let message = "I am OK.";
373    println!("M =\t{}", message);
374    let iv = 0x_FEDCBA0987654321_u64;
375    println!("IV =	{}", iv);
376    let mut cipher = Vec::<u8>::new();
377    a_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher);
378    print!("C =\t");
379    for c in cipher.clone()
380        { print!("{:02X} ", c); }
381    println!();
382    let mut txt = String::new();
383    for c in cipher.clone()
384        { write!(txt, "{:02X} ", c); }
385    assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
386    println!();
387
388    // Normal case for the message longer than 8 bytes and shorter than 16 bytes
389    let key = 0x_1234567890ABCDEF_u64;
390    println!("K =\t{:#016X}", key);
391    let mut a_des = DES::new_with_key_u64(key);
392
393    let message = "PARK Youngho";
394    println!("M =\t{}", message);
395    let iv = 0x_FEDCBA0987654321_u64;
396    println!("IV =	{}", iv);
397    let mut cipher = Vec::<u8>::new();
398    a_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher);
399    print!("C =\t");
400    for c in cipher.clone()
401        { print!("{:02X} ", c); }
402    println!();
403    let mut txt = String::new();
404    for c in cipher.clone()
405        { write!(txt, "{:02X} ", c); }
406    assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 74 F9 B6 4F ");
407    println!();
408
409    // Normal case for the message of 16 bytes
410    let key = 0x_1234567890ABCDEF_u64;
411    println!("K =\t{:#016X}", key);
412    let mut a_des = DES::new_with_key_u64(key);
413
414    let message = "고맙습니다.";
415    println!("M =\t{}", message);
416    let iv = 0x_FEDCBA0987654321_u64;
417    println!("IV =	{}", iv);
418    let mut cipher = Vec::<u8>::new();
419    a_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher);
420    print!("C =\t");
421    for c in cipher.clone()
422        { print!("{:02X} ", c); }
423    println!();
424    let mut txt = String::new();
425    for c in cipher.clone()
426        { write!(txt, "{:02X} ", c); }
427    assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 F4 FC 94 B5 94 1F B9 BD ");
428    println!("-------------------------------");
429}
430
431fn des_encrypt_cfb_into_array()
432{
433    println!("des_encrypt_cfb_into_array()");
434    use std::io::Write;
435    use std::fmt::Write as _;
436    use cryptocol::symmetric::{ DES, DES_Expanded, CFB};
437
438    // Normal case
439    let key = 0x_1234567890ABCDEF_u64;
440    println!("K =\t{:#016X}", key);
441    let mut a_des = DES::new_with_key_u64(key);
442
443    let message = "In the beginning God created the heavens and the earth.";
444    println!("M =\t{}", message);
445    let iv = 0x_FEDCBA0987654321_u64;
446    println!("IV =	{}", iv);
447    let mut cipher = [0_u8; 55];
448    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
449    print!("C (16 rounds) =\t");
450    for c in cipher.clone()
451        { print!("{:02X} ", c); }
452    println!();
453    let mut txt = String::new();
454    for c in cipher.clone()
455        { write!(txt, "{:02X} ", c); }
456    assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 79 3A A1 78 EC CD 02 72 6A C4 41 7C 25 A4 2C 07 FC 77 25 49 12 55 0F 8A ED 44 C3 E4 DC 91 69 0F 40 72 7F F2 D9 B7 54 9F 36 91 C5 85 4F 9B 30 ");
457    println!();
458
459    // Expanded case for 128 rounds
460    let key = 0x_1234567890ABCDEF_u64;
461    println!("K =\t{:#016X}", key);
462    let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
463
464    let message = "In the beginning God created the heavens and the earth.";
465    println!("M =\t{}", message);
466    let iv = 0x_FEDCBA0987654321_u64;
467    println!("IV =	{}", iv);
468    let mut cipher = [0_u8; 55];
469    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
470    print!("C (128 rounds) =\t");
471    for c in cipher.clone()
472        { print!("{:02X} ", c); }
473    println!();
474    let mut txt = String::new();
475    for c in cipher.clone()
476        { write!(txt, "{:02X} ", c); }
477    assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 F2 9A 70 44 D9 78 F5 E3 CF 55 0F EF BA F0 4A 7E BE 79 C9 B4 68 F4 99 09 48 93 00 D2 22 9D 29 6C 20 74 FF E3 E2 01 0C D3 7E 8D 4D 30 8F EC D6 ");
478    println!();
479
480    // Expanded case for 0 rounds which means that key is meaningless
481    let key1 = 0x_1234567890ABCDEF_u64;
482    let key2 = 0_u64;
483    println!("K =\t{:#016X}", key);
484    let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
485    let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
486
487    let message = "In the beginning God created the heavens and the earth.";
488    println!("M =\t{}", message);
489    let iv = 0x_FEDCBA0987654321_u64;
490    println!("IV =	{}", iv);
491    let mut cipher1 = [0_u8; 55];
492    let mut cipher2 = [0_u8; 55];
493    c_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
494    d_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
495    print!("C (0 rounds) =\t");
496    for c in cipher1.clone()
497        { print!("{:02X} ", c); }
498    println!();
499    let mut txt = String::new();
500    for c in cipher1.clone()
501        { write!(txt, "{:02X} ", c); }
502    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
503    print!("D (0 rounds) =\t");
504    for c in cipher2.clone()
505        { print!("{:02X} ", c); }
506    println!();
507    let mut txt = String::new();
508    for c in cipher2.clone()
509        { write!(txt, "{:02X} ", c); }
510    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
511    println!();
512
513    // Normal case for the message of 0 bytes
514    let key = 0x_1234567890ABCDEF_u64;
515    println!("K =\t{:#016X}", key);
516    let mut a_des = DES::new_with_key_u64(key);
517
518    let message = "";
519    println!("M =\t{}", message);
520    let iv = 0x_FEDCBA0987654321_u64;
521    println!("IV =	{}", iv);
522    let mut cipher = [0_u8; 0];
523    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
524    print!("C =\t");
525    for c in cipher.clone()
526        { print!("{:02X} ", c); }
527    println!();
528    let mut txt = String::new();
529    for c in cipher.clone()
530        { write!(txt, "{:02X} ", c); }
531    assert_eq!(txt, "");
532    println!();
533
534    // Normal case for the message shorter than 8 bytes
535    let key = 0x_1234567890ABCDEF_u64;
536    println!("K =\t{:#016X}", key);
537    let mut a_des = DES::new_with_key_u64(key);
538
539    let message = "7 bytes";
540    println!("M =\t{}", message);
541    let iv = 0x_FEDCBA0987654321_u64;
542    println!("IV =	{}", iv);
543    let mut cipher = [0_u8; 7];
544    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
545    print!("C =\t");
546    for c in cipher.clone()
547        { print!("{:02X} ", c); }
548    println!();
549    let mut txt = String::new();
550    for c in cipher.clone()
551        { write!(txt, "{:02X} ", c); }
552    assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
553    println!();
554
555    // Normal case for the message of 8 bytes
556    let key = 0x_1234567890ABCDEF_u64;
557    println!("K =\t{:#016X}", key);
558    let mut a_des = DES::new_with_key_u64(key);
559
560    let message = "I am OK.";
561    println!("M =\t{}", message);
562    let iv = 0x_FEDCBA0987654321_u64;
563    println!("IV =	{}", iv);
564    let mut cipher = [0_u8; 8];
565    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
566    print!("C =\t");
567    for c in cipher.clone()
568        { print!("{:02X} ", c); }
569    println!();
570    let mut txt = String::new();
571    for c in cipher.clone()
572        { write!(txt, "{:02X} ", c); }
573    assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
574    println!();
575
576    // Normal case for the message longer than 8 bytes and shorter than 16 bytes
577    let key = 0x_1234567890ABCDEF_u64;
578    println!("K =\t{:#016X}", key);
579    let mut a_des = DES::new_with_key_u64(key);
580
581    let message = "PARK Youngho";
582    println!("M =\t{}", message);
583    let iv = 0x_FEDCBA0987654321_u64;
584    println!("IV =	{}", iv);
585    let mut cipher = [0_u8; 12];
586    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
587    print!("C =\t");
588    for c in cipher.clone()
589        { print!("{:02X} ", c); }
590    println!();
591    let mut txt = String::new();
592    for c in cipher.clone()
593        { write!(txt, "{:02X} ", c); }
594    assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 74 F9 B6 4F ");
595    println!();
596
597    // Normal case for the message of 16 bytes
598    let key = 0x_1234567890ABCDEF_u64;
599    println!("K =\t{:#016X}", key);
600    let mut a_des = DES::new_with_key_u64(key);
601
602    let message = "고맙습니다.";
603    println!("M =\t{}", message);
604    let iv = 0x_FEDCBA0987654321_u64;
605    println!("IV =	{}", iv);
606    let mut cipher = [0_u8; 16];
607    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
608    print!("C =\t");
609    for c in cipher.clone()
610        { print!("{:02X} ", c); }
611    println!();
612    let mut txt = String::new();
613    for c in cipher.clone()
614        { write!(txt, "{:02X} ", c); }
615    assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 F4 FC 94 B5 94 1F B9 BD ");
616    println!("-------------------------------");
617}
618
619fn des_encrypt_str_cfb()
620{
621    println!("des_encrypt_str_cfb()");
622    use std::io::Write;
623    use std::fmt::Write as _;
624    use cryptocol::symmetric::{ DES, DES_Expanded, CFB};
625
626    // Normal case
627    let key = 0x_1234567890ABCDEF_u64;
628    println!("K =\t{:#016X}", key);
629    let mut a_des = DES::new_with_key_u64(key);
630
631    let message = "In the beginning God created the heavens and the earth.";
632    println!("M =\t{}", message);
633    let iv = 0x_FEDCBA0987654321_u64;
634    println!("IV =	{}", iv);
635    let mut cipher = [0_u8; 55];
636    a_des.encrypt_str(iv, &message, cipher.as_mut_ptr());
637    print!("C (16 rounds) =\t");
638    for c in cipher.clone()
639        { print!("{:02X} ", c); }
640    println!();
641    let mut txt = String::new();
642    for c in cipher.clone()
643        { write!(txt, "{:02X} ", c); }
644    assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 79 3A A1 78 EC CD 02 72 6A C4 41 7C 25 A4 2C 07 FC 77 25 49 12 55 0F 8A ED 44 C3 E4 DC 91 69 0F 40 72 7F F2 D9 B7 54 9F 36 91 C5 85 4F 9B 30 ");
645    println!();
646
647    // Expanded case for 128 rounds
648    let key = 0x_1234567890ABCDEF_u64;
649    println!("K =\t{:#016X}", key);
650    let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
651
652    let message = "In the beginning God created the heavens and the earth.";
653    println!("M =\t{}", message);
654    let iv = 0x_FEDCBA0987654321_u64;
655    println!("IV =	{}", iv);
656    let mut cipher = [0_u8; 55];
657    a_des.encrypt_str(iv, &message, cipher.as_mut_ptr());
658    print!("C (128 rounds) =\t");
659    for c in cipher.clone()
660        { print!("{:02X} ", c); }
661    println!();
662    let mut txt = String::new();
663    for c in cipher.clone()
664        { write!(txt, "{:02X} ", c); }
665    assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 F2 9A 70 44 D9 78 F5 E3 CF 55 0F EF BA F0 4A 7E BE 79 C9 B4 68 F4 99 09 48 93 00 D2 22 9D 29 6C 20 74 FF E3 E2 01 0C D3 7E 8D 4D 30 8F EC D6 ");
666    println!();
667
668    // Expanded case for 0 rounds which means that key is meaningless
669    let key1 = 0x_1234567890ABCDEF_u64;
670    let key2 = 0_u64;
671    println!("K =\t{:#016X}", key);
672    let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
673    let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
674
675    let message = "In the beginning God created the heavens and the earth.";
676    println!("M =\t{}", message);
677    let iv = 0x_FEDCBA0987654321_u64;
678    println!("IV =	{}", iv);
679    let mut cipher1 = [0_u8; 55];
680    let mut cipher2 = [0_u8; 55];
681    c_des.encrypt_str(iv, &message, cipher1.as_mut_ptr());
682    d_des.encrypt_str(iv, &message, cipher2.as_mut_ptr());
683    print!("C (0 rounds) =\t");
684    for c in cipher1.clone()
685        { print!("{:02X} ", c); }
686    println!();
687    let mut txt = String::new();
688    for c in cipher1.clone()
689        { write!(txt, "{:02X} ", c); }
690    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
691    print!("D (0 rounds) =\t");
692    for c in cipher2.clone()
693        { print!("{:02X} ", c); }
694    println!();
695    let mut txt = String::new();
696    for c in cipher2.clone()
697        { write!(txt, "{:02X} ", c); }
698    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
699    println!();
700
701    // Normal case for the message of 0 bytes
702    let key = 0x_1234567890ABCDEF_u64;
703    println!("K =\t{:#016X}", key);
704    let mut a_des = DES::new_with_key_u64(key);
705
706    let message = "";
707    println!("M =\t{}", message);
708    let iv = 0x_FEDCBA0987654321_u64;
709    println!("IV =	{}", iv);
710    let mut cipher = [0_u8; 0];
711    a_des.encrypt_str(iv, &message, cipher.as_mut_ptr());
712    print!("C =\t");
713    for c in cipher.clone()
714        { print!("{:02X} ", c); }
715    println!();
716    let mut txt = String::new();
717    for c in cipher.clone()
718        { write!(txt, "{:02X} ", c); }
719    assert_eq!(txt, "");
720    println!();
721
722    // Normal case for the message shorter than 8 bytes
723    let key = 0x_1234567890ABCDEF_u64;
724    println!("K =\t{:#016X}", key);
725    let mut a_des = DES::new_with_key_u64(key);
726
727    let message = "7 bytes";
728    println!("M =\t{}", message);
729    let iv = 0x_FEDCBA0987654321_u64;
730    println!("IV =	{}", iv);
731    let mut cipher = [0_u8; 7];
732    a_des.encrypt_str(iv, &message, cipher.as_mut_ptr());
733    print!("C =\t");
734    for c in cipher.clone()
735        { print!("{:02X} ", c); }
736    println!();
737    let mut txt = String::new();
738    for c in cipher.clone()
739        { write!(txt, "{:02X} ", c); }
740    assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
741    println!();
742
743    // Normal case for the message of 8 bytes
744    let key = 0x_1234567890ABCDEF_u64;
745    println!("K =\t{:#016X}", key);
746    let mut a_des = DES::new_with_key_u64(key);
747
748    let message = "I am OK.";
749    println!("M =\t{}", message);
750    let iv = 0x_FEDCBA0987654321_u64;
751    println!("IV =	{}", iv);
752    let mut cipher = [0_u8; 8];
753    a_des.encrypt_str(iv, &message, cipher.as_mut_ptr());
754    print!("C =\t");
755    for c in cipher.clone()
756        { print!("{:02X} ", c); }
757    println!();
758    let mut txt = String::new();
759    for c in cipher.clone()
760        { write!(txt, "{:02X} ", c); }
761    assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
762    println!();
763
764    // Normal case for the message longer than 8 bytes and shorter than 16 bytes
765    let key = 0x_1234567890ABCDEF_u64;
766    println!("K =\t{:#016X}", key);
767    let mut a_des = DES::new_with_key_u64(key);
768
769    let message = "PARK Youngho";
770    println!("M =\t{}", message);
771    let iv = 0x_FEDCBA0987654321_u64;
772    println!("IV =	{}", iv);
773    let mut cipher = [0_u8; 12];
774    a_des.encrypt_str(iv, &message, cipher.as_mut_ptr());
775    print!("C =\t");
776    for c in cipher.clone()
777        { print!("{:02X} ", c); }
778    println!();
779    let mut txt = String::new();
780    for c in cipher.clone()
781        { write!(txt, "{:02X} ", c); }
782    assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 74 F9 B6 4F ");
783    println!();
784
785    // Normal case for the message of 16 bytes
786    let key = 0x_1234567890ABCDEF_u64;
787    println!("K =\t{:#016X}", key);
788    let mut a_des = DES::new_with_key_u64(key);
789
790    let message = "고맙습니다.";
791    println!("M =\t{}", message);
792    let iv = 0x_FEDCBA0987654321_u64;
793    println!("IV =	{}", iv);
794    let mut cipher = [0_u8; 16];
795    a_des.encrypt_str(iv, &message, cipher.as_mut_ptr());
796    print!("C =\t");
797    for c in cipher.clone()
798        { print!("{:02X} ", c); }
799    println!();
800    let mut txt = String::new();
801    for c in cipher.clone()
802        { write!(txt, "{:02X} ", c); }
803    assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 F4 FC 94 B5 94 1F B9 BD ");
804    println!("-------------------------------");
805}
806
807fn des_encrypt_str_cfb_into_vec()
808{
809    println!("des_encrypt_str_cfb_into_vec()");
810    use std::io::Write;
811    use std::fmt::Write as _;
812    use cryptocol::symmetric::{ DES, DES_Expanded, CFB};
813
814    // Normal case
815    let key = 0x_1234567890ABCDEF_u64;
816    println!("K =\t{:#016X}", key);
817    let mut a_des = DES::new_with_key_u64(key);
818
819    let message = "In the beginning God created the heavens and the earth.";
820    println!("M =\t{}", message);
821    let iv = 0x_FEDCBA0987654321_u64;
822    println!("IV =	{}", iv);
823    let mut cipher = Vec::<u8>::new();
824    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
825    print!("C (16 rounds) =\t");
826    for c in cipher.clone()
827        { print!("{:02X} ", c); }
828    println!();
829    let mut txt = String::new();
830    for c in cipher.clone()
831        { write!(txt, "{:02X} ", c); }
832    assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 79 3A A1 78 EC CD 02 72 6A C4 41 7C 25 A4 2C 07 FC 77 25 49 12 55 0F 8A ED 44 C3 E4 DC 91 69 0F 40 72 7F F2 D9 B7 54 9F 36 91 C5 85 4F 9B 30 ");
833    println!();
834
835    // Expanded case for 128 rounds
836    let key = 0x_1234567890ABCDEF_u64;
837    println!("K =\t{:#016X}", key);
838    let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
839
840    let message = "In the beginning God created the heavens and the earth.";
841    println!("M =\t{}", message);
842    let iv = 0x_FEDCBA0987654321_u64;
843    println!("IV =	{}", iv);
844    let mut cipher = Vec::<u8>::new();
845    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
846    print!("C (128 rounds) =\t");
847    for c in cipher.clone()
848        { print!("{:02X} ", c); }
849    println!();
850    let mut txt = String::new();
851    for c in cipher.clone()
852        { write!(txt, "{:02X} ", c); }
853    assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 F2 9A 70 44 D9 78 F5 E3 CF 55 0F EF BA F0 4A 7E BE 79 C9 B4 68 F4 99 09 48 93 00 D2 22 9D 29 6C 20 74 FF E3 E2 01 0C D3 7E 8D 4D 30 8F EC D6 ");
854    println!();
855
856    // Expanded case for 0 rounds which means that key is meaningless
857    let key1 = 0x_1234567890ABCDEF_u64;
858    let key2 = 0_u64;
859    println!("K =\t{:#016X}", key);
860    let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
861    let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
862
863    let message = "In the beginning God created the heavens and the earth.";
864    println!("M =\t{}", message);
865    let iv = 0x_FEDCBA0987654321_u64;
866    println!("IV =	{}", iv);
867    let mut cipher1 = Vec::<u8>::new();
868    let mut cipher2 = Vec::<u8>::new();
869    c_des.encrypt_str_into_vec(iv, &message, &mut cipher1);
870    d_des.encrypt_str_into_vec(iv, &message, &mut cipher2);
871    print!("C (0 rounds) =\t");
872    for c in cipher1.clone()
873        { print!("{:02X} ", c); }
874    println!();
875    let mut txt = String::new();
876    for c in cipher1.clone()
877        { write!(txt, "{:02X} ", c); }
878    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
879    print!("D (0 rounds) =\t");
880    for c in cipher2.clone()
881        { print!("{:02X} ", c); }
882    println!();
883    let mut txt = String::new();
884    for c in cipher2.clone()
885        { write!(txt, "{:02X} ", c); }
886    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
887    println!();
888
889    // Normal case for the message of 0 bytes
890    let key = 0x_1234567890ABCDEF_u64;
891    println!("K =\t{:#016X}", key);
892    let mut a_des = DES::new_with_key_u64(key);
893
894    let message = "";
895    println!("M =\t{}", message);
896    let iv = 0x_FEDCBA0987654321_u64;
897    println!("IV =	{}", iv);
898    let mut cipher = Vec::<u8>::new();
899    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
900    print!("C =\t");
901    for c in cipher.clone()
902        { print!("{:02X} ", c); }
903    println!();
904    let mut txt = String::new();
905    for c in cipher.clone()
906        { write!(txt, "{:02X} ", c); }
907    assert_eq!(txt, "");
908    println!();
909
910    // Normal case for the message shorter than 8 bytes
911    let key = 0x_1234567890ABCDEF_u64;
912    println!("K =\t{:#016X}", key);
913    let mut a_des = DES::new_with_key_u64(key);
914
915    let message = "7 bytes";
916    println!("M =\t{}", message);
917    let iv = 0x_FEDCBA0987654321_u64;
918    println!("IV =	{}", iv);
919    let mut cipher = Vec::<u8>::new();
920    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
921    print!("C =\t");
922    for c in cipher.clone()
923        { print!("{:02X} ", c); }
924    println!();
925    let mut txt = String::new();
926    for c in cipher.clone()
927        { write!(txt, "{:02X} ", c); }
928    assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
929    println!();
930
931    // Normal case for the message of 8 bytes
932    let key = 0x_1234567890ABCDEF_u64;
933    println!("K =\t{:#016X}", key);
934    let mut a_des = DES::new_with_key_u64(key);
935
936    let message = "I am OK.";
937    println!("M =\t{}", message);
938    let iv = 0x_FEDCBA0987654321_u64;
939    println!("IV =	{}", iv);
940    let mut cipher = Vec::<u8>::new();
941    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
942    print!("C =\t");
943    for c in cipher.clone()
944        { print!("{:02X} ", c); }
945    println!();
946    let mut txt = String::new();
947    for c in cipher.clone()
948        { write!(txt, "{:02X} ", c); }
949    assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
950    println!();
951
952    // Normal case for the message longer than 8 bytes and shorter than 16 bytes
953    let key = 0x_1234567890ABCDEF_u64;
954    println!("K =\t{:#016X}", key);
955    let mut a_des = DES::new_with_key_u64(key);
956
957    let message = "PARK Youngho";
958    println!("M =\t{}", message);
959    let iv = 0x_FEDCBA0987654321_u64;
960    println!("IV =	{}", iv);
961    let mut cipher = Vec::<u8>::new();
962    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
963    print!("C =\t");
964    for c in cipher.clone()
965        { print!("{:02X} ", c); }
966    println!();
967    let mut txt = String::new();
968    for c in cipher.clone()
969        { write!(txt, "{:02X} ", c); }
970    assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 74 F9 B6 4F ");
971    println!();
972
973    // Normal case for the message of 16 bytes
974    let key = 0x_1234567890ABCDEF_u64;
975    println!("K =\t{:#016X}", key);
976    let mut a_des = DES::new_with_key_u64(key);
977
978    let message = "고맙습니다.";
979    println!("M =\t{}", message);
980    let iv = 0x_FEDCBA0987654321_u64;
981    println!("IV =	{}", iv);
982    let mut cipher = Vec::<u8>::new();
983    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
984    print!("C =\t");
985    for c in cipher.clone()
986        { print!("{:02X} ", c); }
987    println!();
988    let mut txt = String::new();
989    for c in cipher.clone()
990        { write!(txt, "{:02X} ", c); }
991    assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 F4 FC 94 B5 94 1F B9 BD ");
992    println!("-------------------------------");
993}
994
995fn des_encrypt_str_cfb_into_array()
996{
997    println!("des_encrypt_str_cfb_into_array()");
998    use std::io::Write;
999    use std::fmt::Write as _;
1000    use cryptocol::symmetric::{ DES, DES_Expanded, CFB};
1001
1002    // Normal case
1003    let key = 0x_1234567890ABCDEF_u64;
1004    println!("K =\t{:#016X}", key);
1005    let mut a_des = DES::new_with_key_u64(key);
1006
1007    let message = "In the beginning God created the heavens and the earth.";
1008    println!("M =\t{}", message);
1009    let iv = 0x_FEDCBA0987654321_u64;
1010    println!("IV =	{}", iv);
1011    let mut cipher = [0_u8; 55];
1012    a_des.encrypt_str_into_array(iv, &message, &mut cipher);
1013    print!("C (16 rounds) =\t");
1014    for c in cipher.clone()
1015        { print!("{:02X} ", c); }
1016    println!();
1017    let mut txt = String::new();
1018    for c in cipher.clone()
1019        { write!(txt, "{:02X} ", c); }
1020    assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 79 3A A1 78 EC CD 02 72 6A C4 41 7C 25 A4 2C 07 FC 77 25 49 12 55 0F 8A ED 44 C3 E4 DC 91 69 0F 40 72 7F F2 D9 B7 54 9F 36 91 C5 85 4F 9B 30 ");
1021    println!();
1022
1023    // Expanded case for 128 rounds
1024    let key = 0x_1234567890ABCDEF_u64;
1025    println!("K =\t{:#016X}", key);
1026    let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
1027
1028    let message = "In the beginning God created the heavens and the earth.";
1029    println!("M =\t{}", message);
1030    let iv = 0x_FEDCBA0987654321_u64;
1031    println!("IV =	{}", iv);
1032    let mut cipher = [0_u8; 55];
1033    a_des.encrypt_str_into_array(iv, &message, &mut cipher);
1034    print!("C (128 rounds) =\t");
1035    for c in cipher.clone()
1036        { print!("{:02X} ", c); }
1037    println!();
1038    let mut txt = String::new();
1039    for c in cipher.clone()
1040        { write!(txt, "{:02X} ", c); }
1041    assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 F2 9A 70 44 D9 78 F5 E3 CF 55 0F EF BA F0 4A 7E BE 79 C9 B4 68 F4 99 09 48 93 00 D2 22 9D 29 6C 20 74 FF E3 E2 01 0C D3 7E 8D 4D 30 8F EC D6 ");
1042    println!();
1043
1044    // Expanded case for 0 rounds which means that key is meaningless
1045    let key1 = 0x_1234567890ABCDEF_u64;
1046    let key2 = 0_u64;
1047    println!("K =\t{:#016X}", key);
1048    let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
1049    let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
1050
1051    let message = "In the beginning God created the heavens and the earth.";
1052    println!("M =\t{}", message);
1053    let iv = 0x_FEDCBA0987654321_u64;
1054    println!("IV =	{}", iv);
1055    let mut cipher1 = [0_u8; 55];
1056    let mut cipher2 = [0_u8; 55];
1057    c_des.encrypt_str_into_array(iv, &message, &mut cipher1);
1058    d_des.encrypt_str_into_array(iv, &message, &mut cipher2);
1059    print!("C (0 rounds) =\t");
1060    for c in cipher1.clone()
1061        { print!("{:02X} ", c); }
1062    println!();
1063    let mut txt = String::new();
1064    for c in cipher1.clone()
1065        { write!(txt, "{:02X} ", c); }
1066    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
1067    print!("D (0 rounds) =\t");
1068    for c in cipher2.clone()
1069        { print!("{:02X} ", c); }
1070    println!();
1071    let mut txt = String::new();
1072    for c in cipher2.clone()
1073        { write!(txt, "{:02X} ", c); }
1074    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
1075    println!();
1076
1077    // Normal case for the message of 0 bytes
1078    let key = 0x_1234567890ABCDEF_u64;
1079    println!("K =\t{:#016X}", key);
1080    let mut a_des = DES::new_with_key_u64(key);
1081
1082    let message = "";
1083    println!("M =\t{}", message);
1084    let iv = 0x_FEDCBA0987654321_u64;
1085    println!("IV =	{}", iv);
1086    let mut cipher = [0_u8; 0];
1087    a_des.encrypt_str_into_array(iv, &message, &mut cipher);
1088    print!("C =\t");
1089    for c in cipher.clone()
1090        { print!("{:02X} ", c); }
1091    println!();
1092    let mut txt = String::new();
1093    for c in cipher.clone()
1094        { write!(txt, "{:02X} ", c); }
1095    assert_eq!(txt, "");
1096    println!();
1097
1098    // Normal case for the message shorter than 8 bytes
1099    let key = 0x_1234567890ABCDEF_u64;
1100    println!("K =\t{:#016X}", key);
1101    let mut a_des = DES::new_with_key_u64(key);
1102
1103    let message = "7 bytes";
1104    println!("M =\t{}", message);
1105    let iv = 0x_FEDCBA0987654321_u64;
1106    println!("IV =	{}", iv);
1107    let mut cipher = [0_u8; 7];
1108    a_des.encrypt_str_into_array(iv, &message, &mut cipher);
1109    print!("C =\t");
1110    for c in cipher.clone()
1111        { print!("{:02X} ", c); }
1112    println!();
1113    let mut txt = String::new();
1114    for c in cipher.clone()
1115        { write!(txt, "{:02X} ", c); }
1116    assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
1117    println!();
1118
1119    // Normal case for the message of 8 bytes
1120    let key = 0x_1234567890ABCDEF_u64;
1121    println!("K =\t{:#016X}", key);
1122    let mut a_des = DES::new_with_key_u64(key);
1123
1124    let message = "I am OK.";
1125    println!("M =\t{}", message);
1126    let iv = 0x_FEDCBA0987654321_u64;
1127    println!("IV =	{}", iv);
1128    let mut cipher = [0_u8; 8];
1129    a_des.encrypt_str_into_array(iv, &message, &mut cipher);
1130    print!("C =\t");
1131    for c in cipher.clone()
1132        { print!("{:02X} ", c); }
1133    println!();
1134    let mut txt = String::new();
1135    for c in cipher.clone()
1136        { write!(txt, "{:02X} ", c); }
1137    assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
1138    println!();
1139
1140    // Normal case for the message longer than 8 bytes and shorter than 16 bytes
1141    let key = 0x_1234567890ABCDEF_u64;
1142    println!("K =\t{:#016X}", key);
1143    let mut a_des = DES::new_with_key_u64(key);
1144
1145    let message = "PARK Youngho";
1146    println!("M =\t{}", message);
1147    let iv = 0x_FEDCBA0987654321_u64;
1148    println!("IV =	{}", iv);
1149    let mut cipher = [0_u8; 12];
1150    a_des.encrypt_str_into_array(iv, &message, &mut cipher);
1151    print!("C =\t");
1152    for c in cipher.clone()
1153        { print!("{:02X} ", c); }
1154    println!();
1155    let mut txt = String::new();
1156    for c in cipher.clone()
1157        { write!(txt, "{:02X} ", c); }
1158    assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 74 F9 B6 4F ");
1159    println!();
1160
1161    // Normal case for the message of 16 bytes
1162    let key = 0x_1234567890ABCDEF_u64;
1163    println!("K =\t{:#016X}", key);
1164    let mut a_des = DES::new_with_key_u64(key);
1165
1166    let message = "고맙습니다.";
1167    println!("M =\t{}", message);
1168    let iv = 0x_FEDCBA0987654321_u64;
1169    println!("IV =	{}", iv);
1170    let mut cipher = [0_u8; 16];
1171    a_des.encrypt_str_into_array(iv, &message, &mut cipher);
1172    print!("C =\t");
1173    for c in cipher.clone()
1174        { print!("{:02X} ", c); }
1175    println!();
1176    let mut txt = String::new();
1177    for c in cipher.clone()
1178        { write!(txt, "{:02X} ", c); }
1179    assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 F4 FC 94 B5 94 1F B9 BD ");
1180    println!("-------------------------------");
1181}
1182
1183fn des_encrypt_string_cfb()
1184{
1185    println!("des_encrypt_string_cfb()");
1186    use std::io::Write;
1187    use std::fmt::Write as _;
1188    use cryptocol::symmetric::{ DES, DES_Expanded, CFB};
1189
1190    // Normal case
1191    let key = 0x_1234567890ABCDEF_u64;
1192    println!("K =\t{:#016X}", key);
1193    let mut a_des = DES::new_with_key_u64(key);
1194
1195    let message = "In the beginning God created the heavens and the earth.".to_string();
1196    println!("M =\t{}", message);
1197    let iv = 0x_FEDCBA0987654321_u64;
1198    println!("IV =	{}", iv);
1199    let mut cipher = [0_u8; 55];
1200    a_des.encrypt_string(iv, &message, cipher.as_mut_ptr());
1201    print!("C (16 rounds) =\t");
1202    for c in cipher.clone()
1203        { print!("{:02X} ", c); }
1204    println!();
1205    let mut txt = String::new();
1206    for c in cipher.clone()
1207        { write!(txt, "{:02X} ", c); }
1208    assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 79 3A A1 78 EC CD 02 72 6A C4 41 7C 25 A4 2C 07 FC 77 25 49 12 55 0F 8A ED 44 C3 E4 DC 91 69 0F 40 72 7F F2 D9 B7 54 9F 36 91 C5 85 4F 9B 30 ");
1209    println!();
1210
1211    // Expanded case for 128 rounds
1212    let key = 0x_1234567890ABCDEF_u64;
1213    println!("K =\t{:#016X}", key);
1214    let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
1215
1216    let message = "In the beginning God created the heavens and the earth.".to_string();
1217    println!("M =\t{}", message);
1218    let iv = 0x_FEDCBA0987654321_u64;
1219    println!("IV =	{}", iv);
1220    let mut cipher = [0_u8; 55];
1221    a_des.encrypt_string(iv, &message, cipher.as_mut_ptr());
1222    print!("C (128 rounds) =\t");
1223    for c in cipher.clone()
1224        { print!("{:02X} ", c); }
1225    println!();
1226    let mut txt = String::new();
1227    for c in cipher.clone()
1228        { write!(txt, "{:02X} ", c); }
1229    assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 F2 9A 70 44 D9 78 F5 E3 CF 55 0F EF BA F0 4A 7E BE 79 C9 B4 68 F4 99 09 48 93 00 D2 22 9D 29 6C 20 74 FF E3 E2 01 0C D3 7E 8D 4D 30 8F EC D6 ");
1230    println!();
1231
1232    // Expanded case for 0 rounds which means that key is meaningless
1233    let key1 = 0x_1234567890ABCDEF_u64;
1234    let key2 = 0_u64;
1235    println!("K =\t{:#016X}", key);
1236    let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
1237    let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
1238
1239    let message = "In the beginning God created the heavens and the earth.".to_string();
1240    println!("M =\t{}", message);
1241    let iv = 0x_FEDCBA0987654321_u64;
1242    println!("IV =	{}", iv);
1243    let mut cipher1 = [0_u8; 55];
1244    let mut cipher2 = [0_u8; 55];
1245    c_des.encrypt_string(iv, &message, cipher1.as_mut_ptr());
1246    d_des.encrypt_string(iv, &message, cipher2.as_mut_ptr());
1247    print!("C (0 rounds) =\t");
1248    for c in cipher1.clone()
1249        { print!("{:02X} ", c); }
1250    println!();
1251    let mut txt = String::new();
1252    for c in cipher1.clone()
1253        { write!(txt, "{:02X} ", c); }
1254    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
1255    print!("D (0 rounds) =\t");
1256    for c in cipher2.clone()
1257        { print!("{:02X} ", c); }
1258    println!();
1259    let mut txt = String::new();
1260    for c in cipher2.clone()
1261        { write!(txt, "{:02X} ", c); }
1262    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
1263    println!();
1264
1265    // Normal case for the message of 0 bytes
1266    let key = 0x_1234567890ABCDEF_u64;
1267    println!("K =\t{:#016X}", key);
1268    let mut a_des = DES::new_with_key_u64(key);
1269
1270    let message = "".to_string();
1271    println!("M =\t{}", message);
1272    let iv = 0x_FEDCBA0987654321_u64;
1273    println!("IV =	{}", iv);
1274    let mut cipher = [0_u8; 0];
1275    a_des.encrypt_string(iv, &message, cipher.as_mut_ptr());
1276    print!("C =\t");
1277    for c in cipher.clone()
1278        { print!("{:02X} ", c); }
1279    println!();
1280    let mut txt = String::new();
1281    for c in cipher.clone()
1282        { write!(txt, "{:02X} ", c); }
1283    assert_eq!(txt, "");
1284    println!();
1285
1286    // Normal case for the message shorter than 8 bytes
1287    let key = 0x_1234567890ABCDEF_u64;
1288    println!("K =\t{:#016X}", key);
1289    let mut a_des = DES::new_with_key_u64(key);
1290
1291    let message = "7 bytes".to_string();
1292    println!("M =\t{}", message);
1293    let iv = 0x_FEDCBA0987654321_u64;
1294    println!("IV =	{}", iv);
1295    let mut cipher = [0_u8; 7];
1296    a_des.encrypt_string(iv, &message, cipher.as_mut_ptr());
1297    print!("C =\t");
1298    for c in cipher.clone()
1299        { print!("{:02X} ", c); }
1300    println!();
1301    let mut txt = String::new();
1302    for c in cipher.clone()
1303        { write!(txt, "{:02X} ", c); }
1304    assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
1305    println!();
1306
1307    // Normal case for the message of 8 bytes
1308    let key = 0x_1234567890ABCDEF_u64;
1309    println!("K =\t{:#016X}", key);
1310    let mut a_des = DES::new_with_key_u64(key);
1311
1312    let message = "I am OK.".to_string();
1313    println!("M =\t{}", message);
1314    let iv = 0x_FEDCBA0987654321_u64;
1315    println!("IV =	{}", iv);
1316    let mut cipher = [0_u8; 8];
1317    a_des.encrypt_string(iv, &message, cipher.as_mut_ptr());
1318    print!("C =\t");
1319    for c in cipher.clone()
1320        { print!("{:02X} ", c); }
1321    println!();
1322    let mut txt = String::new();
1323    for c in cipher.clone()
1324        { write!(txt, "{:02X} ", c); }
1325    assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
1326    println!();
1327
1328    // Normal case for the message longer than 8 bytes and shorter than 16 bytes
1329    let key = 0x_1234567890ABCDEF_u64;
1330    println!("K =\t{:#016X}", key);
1331    let mut a_des = DES::new_with_key_u64(key);
1332
1333    let message = "PARK Youngho".to_string();
1334    println!("M =\t{}", message);
1335    let iv = 0x_FEDCBA0987654321_u64;
1336    println!("IV =	{}", iv);
1337    let mut cipher = [0_u8; 12];
1338    a_des.encrypt_string(iv, &message, cipher.as_mut_ptr());
1339    print!("C =\t");
1340    for c in cipher.clone()
1341        { print!("{:02X} ", c); }
1342    println!();
1343    let mut txt = String::new();
1344    for c in cipher.clone()
1345        { write!(txt, "{:02X} ", c); }
1346    assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 74 F9 B6 4F ");
1347    println!();
1348
1349    // Normal case for the message of 16 bytes
1350    let key = 0x_1234567890ABCDEF_u64;
1351    println!("K =\t{:#016X}", key);
1352    let mut a_des = DES::new_with_key_u64(key);
1353
1354    let message = "고맙습니다.".to_string();
1355    println!("M =\t{}", message);
1356    let iv = 0x_FEDCBA0987654321_u64;
1357    println!("IV =	{}", iv);
1358    let mut cipher = [0_u8; 16];
1359    a_des.encrypt_string(iv, &message, cipher.as_mut_ptr());
1360    print!("C =\t");
1361    for c in cipher.clone()
1362        { print!("{:02X} ", c); }
1363    println!();
1364    let mut txt = String::new();
1365    for c in cipher.clone()
1366        { write!(txt, "{:02X} ", c); }
1367    assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 F4 FC 94 B5 94 1F B9 BD ");
1368    println!("-------------------------------");
1369}
1370
1371fn des_encrypt_string_cfb_into_vec()
1372{
1373    println!("des_encrypt_string_cfb_into_vec()");
1374    use std::io::Write;
1375    use std::fmt::Write as _;
1376    use cryptocol::symmetric::{ DES, DES_Expanded, CFB};
1377
1378    // Normal case
1379    let key = 0x_1234567890ABCDEF_u64;
1380    println!("K =\t{:#016X}", key);
1381    let mut a_des = DES::new_with_key_u64(key);
1382
1383    let message = "In the beginning God created the heavens and the earth.".to_string();
1384    println!("M =\t{}", message);
1385    let iv = 0x_FEDCBA0987654321_u64;
1386    println!("IV =	{}", iv);
1387    let mut cipher = Vec::<u8>::new();
1388    a_des.encrypt_string_into_vec(iv, &message, &mut cipher);
1389    print!("C (16 rounds) =\t");
1390    for c in cipher.clone()
1391        { print!("{:02X} ", c); }
1392    println!();
1393    let mut txt = String::new();
1394    for c in cipher.clone()
1395        { write!(txt, "{:02X} ", c); }
1396    assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 79 3A A1 78 EC CD 02 72 6A C4 41 7C 25 A4 2C 07 FC 77 25 49 12 55 0F 8A ED 44 C3 E4 DC 91 69 0F 40 72 7F F2 D9 B7 54 9F 36 91 C5 85 4F 9B 30 ");
1397    println!();
1398
1399    // Expanded case for 128 rounds
1400    let key = 0x_1234567890ABCDEF_u64;
1401    println!("K =\t{:#016X}", key);
1402    let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
1403
1404    let message = "In the beginning God created the heavens and the earth.".to_string();
1405    println!("M =\t{}", message);
1406    let iv = 0x_FEDCBA0987654321_u64;
1407    println!("IV =	{}", iv);
1408    let mut cipher = Vec::<u8>::new();
1409    a_des.encrypt_string_into_vec(iv, &message, &mut cipher);
1410    print!("C (128 rounds) =\t");
1411    for c in cipher.clone()
1412        { print!("{:02X} ", c); }
1413    println!();
1414    let mut txt = String::new();
1415    for c in cipher.clone()
1416        { write!(txt, "{:02X} ", c); }
1417    assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 F2 9A 70 44 D9 78 F5 E3 CF 55 0F EF BA F0 4A 7E BE 79 C9 B4 68 F4 99 09 48 93 00 D2 22 9D 29 6C 20 74 FF E3 E2 01 0C D3 7E 8D 4D 30 8F EC D6 ");
1418    println!();
1419
1420    // Expanded case for 0 rounds which means that key is meaningless
1421    let key1 = 0x_1234567890ABCDEF_u64;
1422    let key2 = 0_u64;
1423    println!("K =\t{:#016X}", key);
1424    let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
1425    let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
1426
1427    let message = "In the beginning God created the heavens and the earth.".to_string();
1428    println!("M =\t{}", message);
1429    let iv = 0x_FEDCBA0987654321_u64;
1430    println!("IV =	{}", iv);
1431    let mut cipher1 = Vec::<u8>::new();
1432    let mut cipher2 = Vec::<u8>::new();
1433    c_des.encrypt_string_into_vec(iv, &message, &mut cipher1);
1434    d_des.encrypt_string_into_vec(iv, &message, &mut cipher2);
1435    print!("C (0 rounds) =\t");
1436    for c in cipher1.clone()
1437        { print!("{:02X} ", c); }
1438    println!();
1439    let mut txt = String::new();
1440    for c in cipher1.clone()
1441        { write!(txt, "{:02X} ", c); }
1442    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
1443    print!("D (0 rounds) =\t");
1444    for c in cipher2.clone()
1445        { print!("{:02X} ", c); }
1446    println!();
1447    let mut txt = String::new();
1448    for c in cipher2.clone()
1449        { write!(txt, "{:02X} ", c); }
1450    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
1451    println!();
1452
1453    // Normal case for the message of 0 bytes
1454    let key = 0x_1234567890ABCDEF_u64;
1455    println!("K =\t{:#016X}", key);
1456    let mut a_des = DES::new_with_key_u64(key);
1457
1458    let message = "".to_string();
1459    println!("M =\t{}", message);
1460    let iv = 0x_FEDCBA0987654321_u64;
1461    println!("IV =	{}", iv);
1462    let mut cipher = Vec::<u8>::new();
1463    a_des.encrypt_string_into_vec(iv, &message, &mut cipher);
1464    print!("C =\t");
1465    for c in cipher.clone()
1466        { print!("{:02X} ", c); }
1467    println!();
1468    let mut txt = String::new();
1469    for c in cipher.clone()
1470        { write!(txt, "{:02X} ", c); }
1471    assert_eq!(txt, "");
1472    println!();
1473
1474    // Normal case for the message shorter than 8 bytes
1475    let key = 0x_1234567890ABCDEF_u64;
1476    println!("K =\t{:#016X}", key);
1477    let mut a_des = DES::new_with_key_u64(key);
1478
1479    let message = "7 bytes".to_string();
1480    println!("M =\t{}", message);
1481    let iv = 0x_FEDCBA0987654321_u64;
1482    println!("IV =	{}", iv);
1483    let mut cipher = Vec::<u8>::new();
1484    a_des.encrypt_string_into_vec(iv, &message, &mut cipher);
1485    print!("C =\t");
1486    for c in cipher.clone()
1487        { print!("{:02X} ", c); }
1488    println!();
1489    let mut txt = String::new();
1490    for c in cipher.clone()
1491        { write!(txt, "{:02X} ", c); }
1492    assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
1493    println!();
1494
1495    // Normal case for the message of 8 bytes
1496    let key = 0x_1234567890ABCDEF_u64;
1497    println!("K =\t{:#016X}", key);
1498    let mut a_des = DES::new_with_key_u64(key);
1499
1500    let message = "I am OK.".to_string();
1501    println!("M =\t{}", message);
1502    let iv = 0x_FEDCBA0987654321_u64;
1503    println!("IV =	{}", iv);
1504    let mut cipher = Vec::<u8>::new();
1505    a_des.encrypt_string_into_vec(iv, &message, &mut cipher);
1506    print!("C =\t");
1507    for c in cipher.clone()
1508        { print!("{:02X} ", c); }
1509    println!();
1510    let mut txt = String::new();
1511    for c in cipher.clone()
1512        { write!(txt, "{:02X} ", c); }
1513    assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
1514    println!();
1515
1516    // Normal case for the message longer than 8 bytes and shorter than 16 bytes
1517    let key = 0x_1234567890ABCDEF_u64;
1518    println!("K =\t{:#016X}", key);
1519    let mut a_des = DES::new_with_key_u64(key);
1520
1521    let message = "PARK Youngho".to_string();
1522    println!("M =\t{}", message);
1523    let iv = 0x_FEDCBA0987654321_u64;
1524    println!("IV =	{}", iv);
1525    let mut cipher = Vec::<u8>::new();
1526    a_des.encrypt_string_into_vec(iv, &message, &mut cipher);
1527    print!("C =\t");
1528    for c in cipher.clone()
1529        { print!("{:02X} ", c); }
1530    println!();
1531    let mut txt = String::new();
1532    for c in cipher.clone()
1533        { write!(txt, "{:02X} ", c); }
1534    assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 74 F9 B6 4F ");
1535    println!();
1536
1537    // Normal case for the message of 16 bytes
1538    let key = 0x_1234567890ABCDEF_u64;
1539    println!("K =\t{:#016X}", key);
1540    let mut a_des = DES::new_with_key_u64(key);
1541
1542    let message = "고맙습니다.".to_string();
1543    println!("M =\t{}", message);
1544    let iv = 0x_FEDCBA0987654321_u64;
1545    println!("IV =	{}", iv);
1546    let mut cipher = Vec::<u8>::new();
1547    a_des.encrypt_string_into_vec(iv, &message, &mut cipher);
1548    print!("C =\t");
1549    for c in cipher.clone()
1550        { print!("{:02X} ", c); }
1551    println!();
1552    let mut txt = String::new();
1553    for c in cipher.clone()
1554        { write!(txt, "{:02X} ", c); }
1555    assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 F4 FC 94 B5 94 1F B9 BD ");
1556    println!("-------------------------------");
1557}
1558
1559fn des_encrypt_string_cfb_into_array()
1560{
1561    println!("des_encrypt_string_cfb_into_array()");
1562    use std::io::Write;
1563    use std::fmt::Write as _;
1564    use cryptocol::symmetric::{ DES, DES_Expanded, CFB};
1565
1566    // Normal case
1567    let key = 0x_1234567890ABCDEF_u64;
1568    println!("K =\t{:#016X}", key);
1569    let mut a_des = DES::new_with_key_u64(key);
1570
1571    let message = "In the beginning God created the heavens and the earth.".to_string();
1572    println!("M =\t{}", message);
1573    let iv = 0x_FEDCBA0987654321_u64;
1574    println!("IV =	{}", iv);
1575    let mut cipher = [0_u8; 55];
1576    a_des.encrypt_string_into_array(iv, &message, &mut cipher);
1577    print!("C (16 rounds) =\t");
1578    for c in cipher.clone()
1579        { print!("{:02X} ", c); }
1580    println!();
1581    let mut txt = String::new();
1582    for c in cipher.clone()
1583        { write!(txt, "{:02X} ", c); }
1584    assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 79 3A A1 78 EC CD 02 72 6A C4 41 7C 25 A4 2C 07 FC 77 25 49 12 55 0F 8A ED 44 C3 E4 DC 91 69 0F 40 72 7F F2 D9 B7 54 9F 36 91 C5 85 4F 9B 30 ");
1585    println!();
1586
1587    // Expanded case for 128 rounds
1588    let key = 0x_1234567890ABCDEF_u64;
1589    println!("K =\t{:#016X}", key);
1590    let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
1591
1592    let message = "In the beginning God created the heavens and the earth.".to_string();
1593    println!("M =\t{}", message);
1594    let iv = 0x_FEDCBA0987654321_u64;
1595    println!("IV =	{}", iv);
1596    let mut cipher = [0_u8; 55];
1597    a_des.encrypt_string_into_array(iv, &message, &mut cipher);
1598    print!("C (128 rounds) =\t");
1599    for c in cipher.clone()
1600        { print!("{:02X} ", c); }
1601    println!();
1602    let mut txt = String::new();
1603    for c in cipher.clone()
1604        { write!(txt, "{:02X} ", c); }
1605    assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 F2 9A 70 44 D9 78 F5 E3 CF 55 0F EF BA F0 4A 7E BE 79 C9 B4 68 F4 99 09 48 93 00 D2 22 9D 29 6C 20 74 FF E3 E2 01 0C D3 7E 8D 4D 30 8F EC D6 ");
1606    println!();
1607
1608    // Expanded case for 0 rounds which means that key is meaningless
1609    let key1 = 0x_1234567890ABCDEF_u64;
1610    let key2 = 0_u64;
1611    println!("K =\t{:#016X}", key);
1612    let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
1613    let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
1614
1615    let message = "In the beginning God created the heavens and the earth.".to_string();
1616    println!("M =\t{}", message);
1617    let iv = 0x_FEDCBA0987654321_u64;
1618    println!("IV =	{}", iv);
1619    let mut cipher1 = [0_u8; 55];
1620    let mut cipher2 = [0_u8; 55];
1621    c_des.encrypt_string_into_array(iv, &message, &mut cipher1);
1622    d_des.encrypt_string_into_array(iv, &message, &mut cipher2);
1623    print!("C (0 rounds) =\t");
1624    for c in cipher1.clone()
1625        { print!("{:02X} ", c); }
1626    println!();
1627    let mut txt = String::new();
1628    for c in cipher1.clone()
1629        { write!(txt, "{:02X} ", c); }
1630    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
1631    print!("D (0 rounds) =\t");
1632    for c in cipher2.clone()
1633        { print!("{:02X} ", c); }
1634    println!();
1635    let mut txt = String::new();
1636    for c in cipher2.clone()
1637        { write!(txt, "{:02X} ", c); }
1638    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
1639    println!();
1640
1641    // Normal case for the message of 0 bytes
1642    let key = 0x_1234567890ABCDEF_u64;
1643    println!("K =\t{:#016X}", key);
1644    let mut a_des = DES::new_with_key_u64(key);
1645
1646    let message = "".to_string();
1647    println!("M =\t{}", message);
1648    let iv = 0x_FEDCBA0987654321_u64;
1649    println!("IV =	{}", iv);
1650    let mut cipher = [0_u8; 0];
1651    a_des.encrypt_string_into_array(iv, &message, &mut cipher);
1652    print!("C =\t");
1653    for c in cipher.clone()
1654        { print!("{:02X} ", c); }
1655    println!();
1656    let mut txt = String::new();
1657    for c in cipher.clone()
1658        { write!(txt, "{:02X} ", c); }
1659    assert_eq!(txt, "");
1660    println!();
1661
1662    // Normal case for the message shorter than 8 bytes
1663    let key = 0x_1234567890ABCDEF_u64;
1664    println!("K =\t{:#016X}", key);
1665    let mut a_des = DES::new_with_key_u64(key);
1666
1667    let message = "7 bytes".to_string();
1668    println!("M =\t{}", message);
1669    let iv = 0x_FEDCBA0987654321_u64;
1670    println!("IV =	{}", iv);
1671    let mut cipher = [0_u8; 7];
1672    a_des.encrypt_string_into_array(iv, &message, &mut cipher);
1673    print!("C =\t");
1674    for c in cipher.clone()
1675        { print!("{:02X} ", c); }
1676    println!();
1677    let mut txt = String::new();
1678    for c in cipher.clone()
1679        { write!(txt, "{:02X} ", c); }
1680    assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
1681    println!();
1682
1683    // Normal case for the message of 8 bytes
1684    let key = 0x_1234567890ABCDEF_u64;
1685    println!("K =\t{:#016X}", key);
1686    let mut a_des = DES::new_with_key_u64(key);
1687
1688    let message = "I am OK.".to_string();
1689    println!("M =\t{}", message);
1690    let iv = 0x_FEDCBA0987654321_u64;
1691    println!("IV =	{}", iv);
1692    let mut cipher = [0_u8; 8];
1693    a_des.encrypt_string_into_array(iv, &message, &mut cipher);
1694    print!("C =\t");
1695    for c in cipher.clone()
1696        { print!("{:02X} ", c); }
1697    println!();
1698    let mut txt = String::new();
1699    for c in cipher.clone()
1700        { write!(txt, "{:02X} ", c); }
1701    assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
1702    println!();
1703
1704    // Normal case for the message longer than 8 bytes and shorter than 16 bytes
1705    let key = 0x_1234567890ABCDEF_u64;
1706    println!("K =\t{:#016X}", key);
1707    let mut a_des = DES::new_with_key_u64(key);
1708
1709    let message = "PARK Youngho".to_string();
1710    println!("M =\t{}", message);
1711    let iv = 0x_FEDCBA0987654321_u64;
1712    println!("IV =	{}", iv);
1713    let mut cipher = [0_u8; 12];
1714    a_des.encrypt_string_into_array(iv, &message, &mut cipher);
1715    print!("C =\t");
1716    for c in cipher.clone()
1717        { print!("{:02X} ", c); }
1718    println!();
1719    let mut txt = String::new();
1720    for c in cipher.clone()
1721        { write!(txt, "{:02X} ", c); }
1722    assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 74 F9 B6 4F ");
1723    println!();
1724
1725    // Normal case for the message of 16 bytes
1726    let key = 0x_1234567890ABCDEF_u64;
1727    println!("K =\t{:#016X}", key);
1728    let mut a_des = DES::new_with_key_u64(key);
1729
1730    let message = "고맙습니다.".to_string();
1731    println!("M =\t{}", message);
1732    let iv = 0x_FEDCBA0987654321_u64;
1733    println!("IV =	{}", iv);
1734    let mut cipher = [0_u8; 16];
1735    a_des.encrypt_string_into_array(iv, &message, &mut cipher);
1736    print!("C =\t");
1737    for c in cipher.clone()
1738        { print!("{:02X} ", c); }
1739    println!();
1740    let mut txt = String::new();
1741    for c in cipher.clone()
1742        { write!(txt, "{:02X} ", c); }
1743    assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 F4 FC 94 B5 94 1F B9 BD ");
1744    println!("-------------------------------");
1745}
1746
1747fn des_encrypt_vec_cfb()
1748{
1749    println!("des_encrypt_vec_cfb()");
1750    use std::io::Write;
1751    use std::fmt::Write as _;
1752    use cryptocol::symmetric::{ DES, DES_Expanded, CFB};
1753
1754    // Normal case
1755    let key = 0x_1234567890ABCDEF_u64;
1756    println!("K =\t{:#016X}", key);
1757    let mut a_des = DES::new_with_key_u64(key);
1758
1759    let message = "In the beginning God created the heavens and the earth.";
1760    println!("M =\t{}", message);
1761    let message = unsafe { message.to_string().as_mut_vec().clone() };
1762    let iv = 0x_FEDCBA0987654321_u64;
1763    println!("IV =	{}", iv);
1764    let mut cipher = [0_u8; 55];
1765    a_des.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1766    print!("C (16 rounds) =\t");
1767    for c in cipher.clone()
1768        { print!("{:02X} ", c); }
1769    println!();
1770    let mut txt = String::new();
1771    for c in cipher.clone()
1772        { write!(txt, "{:02X} ", c); }
1773    assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 79 3A A1 78 EC CD 02 72 6A C4 41 7C 25 A4 2C 07 FC 77 25 49 12 55 0F 8A ED 44 C3 E4 DC 91 69 0F 40 72 7F F2 D9 B7 54 9F 36 91 C5 85 4F 9B 30 ");
1774    println!();
1775
1776    // Expanded case for 128 rounds
1777    let key = 0x_1234567890ABCDEF_u64;
1778    println!("K =\t{:#016X}", key);
1779    let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
1780
1781    let message = "In the beginning God created the heavens and the earth.";
1782    println!("M =\t{}", message);
1783    let message = unsafe { message.to_string().as_mut_vec().clone() };
1784    let iv = 0x_FEDCBA0987654321_u64;
1785    println!("IV =	{}", iv);
1786    let mut cipher = [0_u8; 55];
1787    a_des.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1788    print!("C (128 rounds) =\t");
1789    for c in cipher.clone()
1790        { print!("{:02X} ", c); }
1791    println!();
1792    let mut txt = String::new();
1793    for c in cipher.clone()
1794        { write!(txt, "{:02X} ", c); }
1795    assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 F2 9A 70 44 D9 78 F5 E3 CF 55 0F EF BA F0 4A 7E BE 79 C9 B4 68 F4 99 09 48 93 00 D2 22 9D 29 6C 20 74 FF E3 E2 01 0C D3 7E 8D 4D 30 8F EC D6 ");
1796    println!();
1797
1798    // Expanded case for 0 rounds which means that key is meaningless
1799    let key1 = 0x_1234567890ABCDEF_u64;
1800    let key2 = 0_u64;
1801    println!("K1 =\t{:#016X}", key1);
1802    println!("K2 =\t{:#016X}", key2);
1803    let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
1804    let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
1805
1806    let message = "In the beginning God created the heavens and the earth.";
1807    println!("M =\t{}", message);
1808    let message = unsafe { message.to_string().as_mut_vec().clone() };
1809    let iv = 0x_FEDCBA0987654321_u64;
1810    println!("IV =	{}", iv);
1811    let mut cipher1 = [0_u8; 55];
1812    let mut cipher2 = [0_u8; 55];
1813    c_des.encrypt_vec(iv, &message, cipher1.as_mut_ptr());
1814    d_des.encrypt_vec(iv, &message, cipher2.as_mut_ptr());
1815    print!("C (0 rounds) =\t");
1816    for c in cipher1.clone()
1817        { print!("{:02X} ", c); }
1818    println!();
1819    let mut txt = String::new();
1820    for c in cipher1.clone()
1821        { write!(txt, "{:02X} ", c); }
1822    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
1823    print!("D (0 rounds) =\t");
1824    for c in cipher2.clone()
1825        { print!("{:02X} ", c); }
1826    println!();
1827    let mut txt = String::new();
1828    for c in cipher2.clone()
1829        { write!(txt, "{:02X} ", c); }
1830    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
1831    println!();
1832
1833    // Normal case for the message of 0 bytes
1834    let key = 0x_1234567890ABCDEF_u64;
1835    println!("K =\t{:#016X}", key);
1836    let mut a_des = DES::new_with_key_u64(key);
1837
1838    let message = "";
1839    println!("M =\t{}", message);
1840    let message = unsafe { message.to_string().as_mut_vec().clone() };
1841    let iv = 0x_FEDCBA0987654321_u64;
1842    println!("IV =	{}", iv);
1843    let mut cipher = [0_u8; 0];
1844    a_des.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1845    print!("C =\t");
1846    for c in cipher.clone()
1847        { print!("{:02X} ", c); }
1848    println!();
1849    let mut txt = String::new();
1850    for c in cipher.clone()
1851        { write!(txt, "{:02X} ", c); }
1852    assert_eq!(txt, "");
1853    println!();
1854
1855    // Normal case for the message shorter than 8 bytes
1856    let key = 0x_1234567890ABCDEF_u64;
1857    println!("K =\t{:#016X}", key);
1858    let mut a_des = DES::new_with_key_u64(key);
1859
1860    let message = "7 bytes";
1861    println!("M =\t{}", message);
1862    let message = unsafe { message.to_string().as_mut_vec().clone() };
1863    let iv = 0x_FEDCBA0987654321_u64;
1864    println!("IV =	{}", iv);
1865    let mut cipher = [0_u8; 7];
1866    a_des.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1867    print!("C =\t");
1868    for c in cipher.clone()
1869        { print!("{:02X} ", c); }
1870    println!();
1871    let mut txt = String::new();
1872    for c in cipher.clone()
1873        { write!(txt, "{:02X} ", c); }
1874    assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
1875    println!();
1876
1877    // Normal case for the message of 8 bytes
1878    let key = 0x_1234567890ABCDEF_u64;
1879    println!("K =\t{:#016X}", key);
1880    let mut a_des = DES::new_with_key_u64(key);
1881
1882    let message = "I am OK.";
1883    println!("M =\t{}", message);
1884    let message = unsafe { message.to_string().as_mut_vec().clone() };
1885    let iv = 0x_FEDCBA0987654321_u64;
1886    println!("IV =	{}", iv);
1887    let mut cipher = [0_u8; 8];
1888    a_des.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1889    print!("C =\t");
1890    for c in cipher.clone()
1891        { print!("{:02X} ", c); }
1892    println!();
1893    let mut txt = String::new();
1894    for c in cipher.clone()
1895        { write!(txt, "{:02X} ", c); }
1896    assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
1897    println!();
1898
1899    // Normal case for the message longer than 8 bytes and shorter than 16 bytes
1900    let key = 0x_1234567890ABCDEF_u64;
1901    println!("K =\t{:#016X}", key);
1902    let mut a_des = DES::new_with_key_u64(key);
1903
1904    let message = "PARK Youngho";
1905    println!("M =\t{}", message);
1906    let message = unsafe { message.to_string().as_mut_vec().clone() };
1907    let iv = 0x_FEDCBA0987654321_u64;
1908    println!("IV =	{}", iv);
1909    let mut cipher = [0_u8; 12];
1910    a_des.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1911    print!("C =\t");
1912    for c in cipher.clone()
1913        { print!("{:02X} ", c); }
1914    println!();
1915    let mut txt = String::new();
1916    for c in cipher.clone()
1917        { write!(txt, "{:02X} ", c); }
1918    assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 74 F9 B6 4F ");
1919    println!();
1920
1921    // Normal case for the message of 16 bytes
1922    let key = 0x_1234567890ABCDEF_u64;
1923    println!("K =\t{:#016X}", key);
1924    let mut a_des = DES::new_with_key_u64(key);
1925
1926    let message = "고맙습니다.";
1927    println!("M =\t{}", message);
1928    let message = unsafe { message.to_string().as_mut_vec().clone() };
1929    let iv = 0x_FEDCBA0987654321_u64;
1930    println!("IV =	{}", iv);
1931    let mut cipher = [0_u8; 16];
1932    a_des.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1933    print!("C =\t");
1934    for c in cipher.clone()
1935        { print!("{:02X} ", c); }
1936    println!();
1937    let mut txt = String::new();
1938    for c in cipher.clone()
1939        { write!(txt, "{:02X} ", c); }
1940    assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 F4 FC 94 B5 94 1F B9 BD ");
1941    println!("-------------------------------");
1942}
1943
1944fn des_encrypt_vec_cfb_into_vec()
1945{
1946    println!("des_encrypt_vec_cfb_into_vec()");
1947    use std::io::Write;
1948    use std::fmt::Write as _;
1949    use cryptocol::symmetric::{ DES, DES_Expanded, CFB};
1950
1951    // Normal case
1952    let key = 0x_1234567890ABCDEF_u64;
1953    println!("K =\t{:#016X}", key);
1954    let mut a_des = DES::new_with_key_u64(key);
1955
1956    let message = "In the beginning God created the heavens and the earth.";
1957    println!("M =\t{}", message);
1958    let message = unsafe { message.to_string().as_mut_vec().clone() };
1959    let iv = 0x_FEDCBA0987654321_u64;
1960    println!("IV =	{}", iv);
1961    let mut cipher = Vec::<u8>::new();
1962    a_des.encrypt_vec_into_vec(iv, &message, &mut cipher);
1963    print!("C (16 rounds) =\t");
1964    for c in cipher.clone()
1965        { print!("{:02X} ", c); }
1966    println!();
1967    let mut txt = String::new();
1968    for c in cipher.clone()
1969        { write!(txt, "{:02X} ", c); }
1970    assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 79 3A A1 78 EC CD 02 72 6A C4 41 7C 25 A4 2C 07 FC 77 25 49 12 55 0F 8A ED 44 C3 E4 DC 91 69 0F 40 72 7F F2 D9 B7 54 9F 36 91 C5 85 4F 9B 30 ");
1971    println!();
1972
1973    // Expanded case for 128 rounds
1974    let key = 0x_1234567890ABCDEF_u64;
1975    println!("K =\t{:#016X}", key);
1976    let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
1977
1978    let message = "In the beginning God created the heavens and the earth.";
1979    println!("M =\t{}", message);
1980    let message = unsafe { message.to_string().as_mut_vec().clone() };
1981    let iv = 0x_FEDCBA0987654321_u64;
1982    println!("IV =	{}", iv);
1983    let mut cipher = Vec::<u8>::new();
1984    a_des.encrypt_vec_into_vec(iv, &message, &mut cipher);
1985    print!("C (128 rounds) =\t");
1986    for c in cipher.clone()
1987        { print!("{:02X} ", c); }
1988    println!();
1989    let mut txt = String::new();
1990    for c in cipher.clone()
1991        { write!(txt, "{:02X} ", c); }
1992    assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 F2 9A 70 44 D9 78 F5 E3 CF 55 0F EF BA F0 4A 7E BE 79 C9 B4 68 F4 99 09 48 93 00 D2 22 9D 29 6C 20 74 FF E3 E2 01 0C D3 7E 8D 4D 30 8F EC D6 ");
1993    println!();
1994
1995    // Expanded case for 0 rounds which means that key is meaningless
1996    let key1 = 0x_1234567890ABCDEF_u64;
1997    let key2 = 0_u64;
1998    println!("K1 =\t{:#016X}", key1);
1999    println!("K2 =\t{:#016X}", key2);
2000    let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
2001    let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
2002
2003    let message = "In the beginning God created the heavens and the earth.";
2004    println!("M =\t{}", message);
2005    let message = unsafe { message.to_string().as_mut_vec().clone() };
2006
2007    let iv = 0x_FEDCBA0987654321_u64;
2008    println!("IV =	{}", iv);
2009    let mut cipher1 = Vec::<u8>::new();
2010    let mut cipher2 = Vec::<u8>::new();
2011    c_des.encrypt_vec_into_vec(iv, &message, &mut cipher1);
2012    d_des.encrypt_vec_into_vec(iv, &message, &mut cipher2);
2013    print!("C (0 rounds) =\t");
2014    for c in cipher1.clone()
2015        { print!("{:02X} ", c); }
2016    println!();
2017    let mut txt = String::new();
2018    for c in cipher1.clone()
2019        { write!(txt, "{:02X} ", c); }
2020    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
2021    print!("D (0 rounds) =\t");
2022    for c in cipher2.clone()
2023        { print!("{:02X} ", c); }
2024    println!();
2025    let mut txt = String::new();
2026    for c in cipher2.clone()
2027        { write!(txt, "{:02X} ", c); }
2028    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
2029    println!();
2030
2031    // Normal case for the message of 0 bytes
2032    let key = 0x_1234567890ABCDEF_u64;
2033    println!("K =\t{:#016X}", key);
2034    let mut a_des = DES::new_with_key_u64(key);
2035
2036    let message = "";
2037    println!("M =\t{}", message);
2038    let message = unsafe { message.to_string().as_mut_vec().clone() };
2039    let iv = 0x_FEDCBA0987654321_u64;
2040    println!("IV =	{}", iv);
2041    let mut cipher = Vec::<u8>::new();
2042    a_des.encrypt_vec_into_vec(iv, &message, &mut cipher);
2043    print!("C =\t");
2044    for c in cipher.clone()
2045        { print!("{:02X} ", c); }
2046    println!();
2047    let mut txt = String::new();
2048    for c in cipher.clone()
2049        { write!(txt, "{:02X} ", c); }
2050    assert_eq!(txt, "");
2051    println!();
2052
2053    // Normal case for the message shorter than 8 bytes
2054    let key = 0x_1234567890ABCDEF_u64;
2055    println!("K =\t{:#016X}", key);
2056    let mut a_des = DES::new_with_key_u64(key);
2057
2058    let message = "7 bytes";
2059    println!("M =\t{}", message);
2060    let message = unsafe { message.to_string().as_mut_vec().clone() };
2061    let iv = 0x_FEDCBA0987654321_u64;
2062    println!("IV =	{}", iv);
2063    let mut cipher = Vec::<u8>::new();
2064    a_des.encrypt_vec_into_vec(iv, &message, &mut cipher);
2065    print!("C =\t");
2066    for c in cipher.clone()
2067        { print!("{:02X} ", c); }
2068    println!();
2069    let mut txt = String::new();
2070    for c in cipher.clone()
2071        { write!(txt, "{:02X} ", c); }
2072    assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
2073    println!();
2074
2075    // Normal case for the message of 8 bytes
2076    let key = 0x_1234567890ABCDEF_u64;
2077    println!("K =\t{:#016X}", key);
2078    let mut a_des = DES::new_with_key_u64(key);
2079
2080    let message = "I am OK.";
2081    println!("M =\t{}", message);
2082    let message = unsafe { message.to_string().as_mut_vec().clone() };
2083    let iv = 0x_FEDCBA0987654321_u64;
2084    println!("IV =	{}", iv);
2085    let mut cipher = Vec::<u8>::new();
2086    a_des.encrypt_vec_into_vec(iv, &message, &mut cipher);
2087    print!("C =\t");
2088    for c in cipher.clone()
2089        { print!("{:02X} ", c); }
2090    println!();
2091    let mut txt = String::new();
2092    for c in cipher.clone()
2093        { write!(txt, "{:02X} ", c); }
2094    assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
2095    println!();
2096
2097    // Normal case for the message longer than 8 bytes and shorter than 16 bytes
2098    let key = 0x_1234567890ABCDEF_u64;
2099    println!("K =\t{:#016X}", key);
2100    let mut a_des = DES::new_with_key_u64(key);
2101
2102    let message = "PARK Youngho";
2103    println!("M =\t{}", message);
2104    let message = unsafe { message.to_string().as_mut_vec().clone() };
2105    let iv = 0x_FEDCBA0987654321_u64;
2106    println!("IV =	{}", iv);
2107    let mut cipher = Vec::<u8>::new();
2108    a_des.encrypt_vec_into_vec(iv, &message, &mut cipher);
2109    print!("C =\t");
2110    for c in cipher.clone()
2111        { print!("{:02X} ", c); }
2112    println!();
2113    let mut txt = String::new();
2114    for c in cipher.clone()
2115        { write!(txt, "{:02X} ", c); }
2116    assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 74 F9 B6 4F ");
2117    println!();
2118
2119    // Normal case for the message of 16 bytes
2120    let key = 0x_1234567890ABCDEF_u64;
2121    println!("K =\t{:#016X}", key);
2122    let mut a_des = DES::new_with_key_u64(key);
2123
2124    let message = "고맙습니다.";
2125    println!("M =\t{}", message);
2126    let message = unsafe { message.to_string().as_mut_vec().clone() };
2127    let iv = 0x_FEDCBA0987654321_u64;
2128    println!("IV =	{}", iv);
2129    let mut cipher = Vec::<u8>::new();
2130    a_des.encrypt_vec_into_vec(iv, &message, &mut cipher);
2131    print!("C =\t");
2132    for c in cipher.clone()
2133        { print!("{:02X} ", c); }
2134    println!();
2135    let mut txt = String::new();
2136    for c in cipher.clone()
2137        { write!(txt, "{:02X} ", c); }
2138    assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 F4 FC 94 B5 94 1F B9 BD ");
2139    println!("-------------------------------");
2140}
2141
2142fn des_encrypt_vec_cfb_into_array()
2143{
2144    println!("des_encrypt_vec_cfb_into_array()");
2145    use std::io::Write;
2146    use std::fmt::Write as _;
2147    use cryptocol::symmetric::{ DES, DES_Expanded, CFB};
2148
2149    // Normal case
2150    let key = 0x_1234567890ABCDEF_u64;
2151    println!("K =\t{:#016X}", key);
2152    let mut a_des = DES::new_with_key_u64(key);
2153
2154    let message = "In the beginning God created the heavens and the earth.";
2155    println!("M =\t{}", message);
2156    let message = unsafe { message.to_string().as_mut_vec().clone() };
2157    let iv = 0x_FEDCBA0987654321_u64;
2158    println!("IV =	{}", iv);
2159    let mut cipher = [0_u8; 55];
2160    a_des.encrypt_vec_into_array(iv, &message, &mut cipher);
2161    print!("C (16 rounds) =\t");
2162    for c in cipher.clone()
2163        { print!("{:02X} ", c); }
2164    println!();
2165    let mut txt = String::new();
2166    for c in cipher.clone()
2167        { write!(txt, "{:02X} ", c); }
2168    assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 79 3A A1 78 EC CD 02 72 6A C4 41 7C 25 A4 2C 07 FC 77 25 49 12 55 0F 8A ED 44 C3 E4 DC 91 69 0F 40 72 7F F2 D9 B7 54 9F 36 91 C5 85 4F 9B 30 ");
2169    println!();
2170
2171    // Expanded case for 128 rounds
2172    let key = 0x_1234567890ABCDEF_u64;
2173    println!("K =\t{:#016X}", key);
2174    let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
2175
2176    let message = "In the beginning God created the heavens and the earth.";
2177    println!("M =\t{}", message);
2178    let message = unsafe { message.to_string().as_mut_vec().clone() };
2179    let iv = 0x_FEDCBA0987654321_u64;
2180    println!("IV =	{}", iv);
2181    let mut cipher = [0_u8; 55];
2182    a_des.encrypt_vec_into_array(iv, &message, &mut cipher);
2183    print!("C (128 rounds) =\t");
2184    for c in cipher.clone()
2185        { print!("{:02X} ", c); }
2186    println!();
2187    let mut txt = String::new();
2188    for c in cipher.clone()
2189        { write!(txt, "{:02X} ", c); }
2190    assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 F2 9A 70 44 D9 78 F5 E3 CF 55 0F EF BA F0 4A 7E BE 79 C9 B4 68 F4 99 09 48 93 00 D2 22 9D 29 6C 20 74 FF E3 E2 01 0C D3 7E 8D 4D 30 8F EC D6 ");
2191    println!();
2192
2193    // Expanded case for 0 rounds which means that key is meaningless
2194    let key1 = 0x_1234567890ABCDEF_u64;
2195    let key2 = 0_u64;
2196    println!("K1 =\t{:#016X}", key1);
2197    println!("K2 =\t{:#016X}", key2);
2198    let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
2199    let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
2200
2201    let message = "In the beginning God created the heavens and the earth.";
2202    println!("M =\t{}", message);
2203    let message = unsafe { message.to_string().as_mut_vec().clone() };
2204    let iv = 0x_FEDCBA0987654321_u64;
2205    println!("IV =	{}", iv);
2206    let mut cipher1 = [0_u8; 55];
2207    let mut cipher2 = [0_u8; 55];
2208    c_des.encrypt_vec_into_array(iv, &message, &mut cipher1);
2209    d_des.encrypt_vec_into_array(iv, &message, &mut cipher2);
2210    print!("C (0 rounds) =\t");
2211    for c in cipher1.clone()
2212        { print!("{:02X} ", c); }
2213    println!();
2214    let mut txt = String::new();
2215    for c in cipher1.clone()
2216        { write!(txt, "{:02X} ", c); }
2217    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
2218    print!("D (0 rounds) =\t");
2219    for c in cipher2.clone()
2220        { print!("{:02X} ", c); }
2221    println!();
2222    let mut txt = String::new();
2223    for c in cipher2.clone()
2224        { write!(txt, "{:02X} ", c); }
2225    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
2226    println!();
2227
2228    // Normal case for the message of 0 bytes
2229    let key = 0x_1234567890ABCDEF_u64;
2230    println!("K =\t{:#016X}", key);
2231    let mut a_des = DES::new_with_key_u64(key);
2232
2233    let message = "";
2234    println!("M =\t{}", message);
2235    let message = unsafe { message.to_string().as_mut_vec().clone() };
2236    let iv = 0x_FEDCBA0987654321_u64;
2237    println!("IV =	{}", iv);
2238    let mut cipher = [0_u8; 0];
2239    a_des.encrypt_vec_into_array(iv, &message, &mut cipher);
2240    print!("C =\t");
2241    for c in cipher.clone()
2242        { print!("{:02X} ", c); }
2243    println!();
2244    let mut txt = String::new();
2245    for c in cipher.clone()
2246        { write!(txt, "{:02X} ", c); }
2247    assert_eq!(txt, "");
2248    println!();
2249
2250    // Normal case for the message shorter than 8 bytes
2251    let key = 0x_1234567890ABCDEF_u64;
2252    println!("K =\t{:#016X}", key);
2253    let mut a_des = DES::new_with_key_u64(key);
2254
2255    let message = "7 bytes";
2256    println!("M =\t{}", message);
2257    let message = unsafe { message.to_string().as_mut_vec().clone() };
2258    let iv = 0x_FEDCBA0987654321_u64;
2259    println!("IV =	{}", iv);
2260    let mut cipher = [0_u8; 7];
2261    a_des.encrypt_vec_into_array(iv, &message, &mut cipher);
2262    print!("C =\t");
2263    for c in cipher.clone()
2264        { print!("{:02X} ", c); }
2265    println!();
2266    let mut txt = String::new();
2267    for c in cipher.clone()
2268        { write!(txt, "{:02X} ", c); }
2269    assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
2270    println!();
2271
2272    // Normal case for the message of 8 bytes
2273    let key = 0x_1234567890ABCDEF_u64;
2274    println!("K =\t{:#016X}", key);
2275    let mut a_des = DES::new_with_key_u64(key);
2276
2277    let message = "I am OK.";
2278    println!("M =\t{}", message);
2279    let message = unsafe { message.to_string().as_mut_vec().clone() };
2280    let iv = 0x_FEDCBA0987654321_u64;
2281    println!("IV =	{}", iv);
2282    let mut cipher = [0_u8; 8];
2283    a_des.encrypt_vec_into_array(iv, &message, &mut cipher);
2284    print!("C =\t");
2285    for c in cipher.clone()
2286        { print!("{:02X} ", c); }
2287    println!();
2288    let mut txt = String::new();
2289    for c in cipher.clone()
2290        { write!(txt, "{:02X} ", c); }
2291    assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
2292    println!();
2293
2294    // Normal case for the message longer than 8 bytes and shorter than 16 bytes
2295    let key = 0x_1234567890ABCDEF_u64;
2296    println!("K =\t{:#016X}", key);
2297    let mut a_des = DES::new_with_key_u64(key);
2298
2299    let message = "PARK Youngho";
2300    println!("M =\t{}", message);
2301    let message = unsafe { message.to_string().as_mut_vec().clone() };
2302    let iv = 0x_FEDCBA0987654321_u64;
2303    println!("IV =	{}", iv);
2304    let mut cipher = [0_u8; 12];
2305    a_des.encrypt_vec_into_array(iv, &message, &mut cipher);
2306    print!("C =\t");
2307    for c in cipher.clone()
2308        { print!("{:02X} ", c); }
2309    println!();
2310    let mut txt = String::new();
2311    for c in cipher.clone()
2312        { write!(txt, "{:02X} ", c); }
2313    assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 74 F9 B6 4F ");
2314    println!();
2315
2316    // Normal case for the message of 16 bytes
2317    let key = 0x_1234567890ABCDEF_u64;
2318    println!("K =\t{:#016X}", key);
2319    let mut a_des = DES::new_with_key_u64(key);
2320 
2321    let message = "고맙습니다.";
2322    println!("M =\t{}", message);
2323    let message = unsafe { message.to_string().as_mut_vec().clone() };
2324    let iv = 0x_FEDCBA0987654321_u64;
2325    println!("IV =	{}", iv);
2326    let mut cipher = [0_u8; 16];
2327    a_des.encrypt_vec_into_array(iv, &message, &mut cipher);
2328    print!("C =\t");
2329    for c in cipher.clone()
2330        { print!("{:02X} ", c); }
2331    println!();
2332    let mut txt = String::new();
2333    for c in cipher.clone()
2334        { write!(txt, "{:02X} ", c); }
2335    assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 F4 FC 94 B5 94 1F B9 BD ");
2336    println!("-------------------------------");
2337}
2338
2339fn des_encrypt_array_cfb()
2340{
2341    println!("des_encrypt_array_cfb()");
2342    use std::io::Write;
2343    use std::fmt::Write as _;
2344    use cryptocol::symmetric::{ DES, DES_Expanded, CFB};
2345
2346    // Normal case
2347    let key = 0x_1234567890ABCDEF_u64;
2348    println!("K =\t{:#016X}", key);
2349    let mut a_des = DES::new_with_key_u64(key);
2350
2351    let mes = "In the beginning God created the heavens and the earth.";
2352    println!("M =\t{}", mes);
2353    let mut message = [0_u8; 55];
2354    message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2355    let iv = 0x_FEDCBA0987654321_u64;
2356    println!("IV =	{}", iv);
2357    let mut cipher = [0_u8; 55];
2358    a_des.encrypt_array(iv, &message, cipher.as_mut_ptr());
2359    print!("C (16 rounds) =\t");
2360    for c in cipher.clone()
2361        { print!("{:02X} ", c); }
2362    println!();
2363    let mut txt = String::new();
2364    for c in cipher.clone()
2365        { write!(txt, "{:02X} ", c); }
2366    assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 79 3A A1 78 EC CD 02 72 6A C4 41 7C 25 A4 2C 07 FC 77 25 49 12 55 0F 8A ED 44 C3 E4 DC 91 69 0F 40 72 7F F2 D9 B7 54 9F 36 91 C5 85 4F 9B 30 ");
2367    println!();
2368
2369    // Expanded case for 128 rounds
2370    let key = 0x_1234567890ABCDEF_u64;
2371    println!("K =\t{:#016X}", key);
2372    let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
2373
2374    let mes = "In the beginning God created the heavens and the earth.";
2375    println!("M =\t{}", mes);
2376    let mut message = [0_u8; 55];
2377    message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2378    let iv = 0x_FEDCBA0987654321_u64;
2379    println!("IV =	{}", iv);
2380    let mut cipher = [0_u8; 55];
2381    a_des.encrypt_array(iv, &message, cipher.as_mut_ptr());
2382    print!("C (128 rounds) =\t");
2383    for c in cipher.clone()
2384        { print!("{:02X} ", c); }
2385    println!();
2386    let mut txt = String::new();
2387    for c in cipher.clone()
2388        { write!(txt, "{:02X} ", c); }
2389    assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 F2 9A 70 44 D9 78 F5 E3 CF 55 0F EF BA F0 4A 7E BE 79 C9 B4 68 F4 99 09 48 93 00 D2 22 9D 29 6C 20 74 FF E3 E2 01 0C D3 7E 8D 4D 30 8F EC D6 ");
2390    println!();
2391
2392    // Expanded case for 0 rounds which means that key is meaningless
2393    let key1 = 0x_1234567890ABCDEF_u64;
2394    let key2 = 0_u64;
2395    println!("K1 =\t{:#016X}", key1);
2396    println!("K2 =\t{:#016X}", key2);
2397    let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
2398    let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
2399
2400    let mes = "In the beginning God created the heavens and the earth.";
2401    println!("M =\t{}", mes);
2402    let mut message = [0_u8; 55];
2403    message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2404    let iv = 0x_FEDCBA0987654321_u64;
2405    println!("IV =	{}", iv);
2406    let mut cipher1 = [0_u8; 55];
2407    let mut cipher2 = [0_u8; 55];
2408    c_des.encrypt_array(iv, &message, cipher1.as_mut_ptr());
2409    d_des.encrypt_array(iv, &message, cipher2.as_mut_ptr());
2410    print!("C (0 rounds) =\t");
2411    for c in cipher1.clone()
2412        { print!("{:02X} ", c); }
2413    println!();
2414    let mut txt = String::new();
2415    for c in cipher1.clone()
2416        { write!(txt, "{:02X} ", c); }
2417    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
2418    print!("D (0 rounds) =\t");
2419    for c in cipher2.clone()
2420        { print!("{:02X} ", c); }
2421    println!();
2422    let mut txt = String::new();
2423    for c in cipher2.clone()
2424        { write!(txt, "{:02X} ", c); }
2425    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
2426    println!();
2427
2428    // Normal case for the message of 0 bytes
2429    let key = 0x_1234567890ABCDEF_u64;
2430    println!("K =\t{:#016X}", key);
2431    let mut a_des = DES::new_with_key_u64(key);
2432
2433    let mes = "";
2434    println!("M =\t{}", mes);
2435    let mut message = [0_u8; 0];
2436    message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2437    let iv = 0x_FEDCBA0987654321_u64;
2438    println!("IV =	{}", iv);
2439    let mut cipher = [0_u8; 0];
2440    a_des.encrypt_array(iv, &message, cipher.as_mut_ptr());
2441    print!("C =\t");
2442    for c in cipher.clone()
2443        { print!("{:02X} ", c); }
2444    println!();
2445    let mut txt = String::new();
2446    for c in cipher.clone()
2447        { write!(txt, "{:02X} ", c); }
2448    assert_eq!(txt, "");
2449    println!();
2450
2451    // Normal case for the message shorter than 8 bytes
2452    let key = 0x_1234567890ABCDEF_u64;
2453    println!("K =\t{:#016X}", key);
2454    let mut a_des = DES::new_with_key_u64(key);
2455
2456    let mes = "7 bytes";
2457    println!("M =\t{}", mes);
2458    let mut message = [0_u8; 7];
2459    message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2460    let iv = 0x_FEDCBA0987654321_u64;
2461    println!("IV =	{}", iv);
2462    let mut cipher = [0_u8; 7];
2463    a_des.encrypt_array(iv, &message, cipher.as_mut_ptr());
2464    print!("C =\t");
2465    for c in cipher.clone()
2466        { print!("{:02X} ", c); }
2467    println!();
2468    let mut txt = String::new();
2469    for c in cipher.clone()
2470        { write!(txt, "{:02X} ", c); }
2471    assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
2472    println!();
2473
2474    // Normal case for the message of 8 bytes
2475    let key = 0x_1234567890ABCDEF_u64;
2476    println!("K =\t{:#016X}", key);
2477    let mut a_des = DES::new_with_key_u64(key);
2478
2479    let mes = "I am OK.";
2480    println!("M =\t{}", mes);
2481    let mut message = [0_u8; 8];
2482    message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2483    let iv = 0x_FEDCBA0987654321_u64;
2484    println!("IV =	{}", iv);
2485    let mut cipher = [0_u8; 8];
2486    a_des.encrypt_array(iv, &message, cipher.as_mut_ptr());
2487    print!("C =\t");
2488    for c in cipher.clone()
2489        { print!("{:02X} ", c); }
2490    println!();
2491    let mut txt = String::new();
2492    for c in cipher.clone()
2493        { write!(txt, "{:02X} ", c); }
2494    assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
2495    println!();
2496
2497    // Normal case for the message longer than 8 bytes and shorter than 16 bytes
2498    let key = 0x_1234567890ABCDEF_u64;
2499    println!("K =\t{:#016X}", key);
2500    let mut a_des = DES::new_with_key_u64(key);
2501
2502    let mes = "PARK Youngho";
2503    println!("M =\t{}", mes);
2504    let mut message = [0_u8; 12];
2505    message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2506    let iv = 0x_FEDCBA0987654321_u64;
2507    println!("IV =	{}", iv);
2508    let mut cipher = [0_u8; 12];
2509    a_des.encrypt_array(iv, &message, cipher.as_mut_ptr());
2510    print!("C =\t");
2511    for c in cipher.clone()
2512        { print!("{:02X} ", c); }
2513    println!();
2514    let mut txt = String::new();
2515    for c in cipher.clone()
2516        { write!(txt, "{:02X} ", c); }
2517    assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 74 F9 B6 4F ");
2518    println!();
2519
2520    // Normal case for the message of 16 bytes
2521    let key = 0x_1234567890ABCDEF_u64;
2522    println!("K =\t{:#016X}", key);
2523    let mut a_des = DES::new_with_key_u64(key);
2524
2525    let mes = "고맙습니다.";
2526    println!("M =\t{}", mes);
2527    let mut message = [0_u8; 16];
2528    message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2529    let iv = 0x_FEDCBA0987654321_u64;
2530    println!("IV =	{}", iv);
2531    let mut cipher = [0_u8; 16];
2532    a_des.encrypt_array(iv, &message, cipher.as_mut_ptr());
2533    print!("C =\t");
2534    for c in cipher.clone()
2535        { print!("{:02X} ", c); }
2536    println!();
2537    let mut txt = String::new();
2538    for c in cipher.clone()
2539        { write!(txt, "{:02X} ", c); }
2540    assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 F4 FC 94 B5 94 1F B9 BD ");
2541    println!("-------------------------------");
2542}
2543
2544fn des_encrypt_array_cfb_into_vec()
2545{
2546    println!("des_encrypt_array_cfb_into_vec()");
2547    use std::io::Write;
2548    use std::fmt::Write as _;
2549    use cryptocol::symmetric::{ DES, DES_Expanded, CFB};
2550
2551    // Normal case
2552    let key = 0x_1234567890ABCDEF_u64;
2553    println!("K =\t{:#016X}", key);
2554    let mut a_des = DES::new_with_key_u64(key);
2555
2556    let mes = "In the beginning God created the heavens and the earth.";
2557    println!("M =\t{}", mes);
2558    let mut message = [0_u8; 55];
2559    message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2560    let iv = 0x_FEDCBA0987654321_u64;
2561    println!("IV =	{}", iv);
2562    let mut cipher = Vec::<u8>::new();
2563    a_des.encrypt_array_into_vec(iv, &message, &mut cipher);
2564    print!("C (16 rounds) =\t");
2565    for c in cipher.clone()
2566        { print!("{:02X} ", c); }
2567    println!();
2568    let mut txt = String::new();
2569    for c in cipher.clone()
2570        { write!(txt, "{:02X} ", c); }
2571    assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 79 3A A1 78 EC CD 02 72 6A C4 41 7C 25 A4 2C 07 FC 77 25 49 12 55 0F 8A ED 44 C3 E4 DC 91 69 0F 40 72 7F F2 D9 B7 54 9F 36 91 C5 85 4F 9B 30 ");
2572    println!();
2573
2574    // Expanded case for 128 rounds
2575    let key = 0x_1234567890ABCDEF_u64;
2576    println!("K =\t{:#016X}", key);
2577    let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
2578
2579    let mes = "In the beginning God created the heavens and the earth.";
2580    println!("M =\t{}", mes);
2581    let mut message = [0_u8; 55];
2582    message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2583    let iv = 0x_FEDCBA0987654321_u64;
2584    println!("IV =	{}", iv);
2585    let mut cipher = Vec::<u8>::new();
2586    a_des.encrypt_array_into_vec(iv, &message, &mut cipher);
2587    print!("C (128 rounds) =\t");
2588    for c in cipher.clone()
2589        { print!("{:02X} ", c); }
2590    println!();
2591    let mut txt = String::new();
2592    for c in cipher.clone()
2593        { write!(txt, "{:02X} ", c); }
2594    assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 F2 9A 70 44 D9 78 F5 E3 CF 55 0F EF BA F0 4A 7E BE 79 C9 B4 68 F4 99 09 48 93 00 D2 22 9D 29 6C 20 74 FF E3 E2 01 0C D3 7E 8D 4D 30 8F EC D6 ");
2595    println!();
2596
2597    // Expanded case for 0 rounds which means that key is meaningless
2598    let key1 = 0x_1234567890ABCDEF_u64;
2599    let key2 = 0_u64;
2600    println!("K1 =\t{:#016X}", key1);
2601    println!("K2 =\t{:#016X}", key2);
2602    let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
2603    let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
2604
2605    let mes = "In the beginning God created the heavens and the earth.";
2606    println!("M =\t{}", mes);
2607    let mut message = [0_u8; 55];
2608    message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2609
2610    let iv = 0x_FEDCBA0987654321_u64;
2611    println!("IV =	{}", iv);
2612    let mut cipher1 = Vec::<u8>::new();
2613    let mut cipher2 = Vec::<u8>::new();
2614    c_des.encrypt_array_into_vec(iv, &message, &mut cipher1);
2615    d_des.encrypt_array_into_vec(iv, &message, &mut cipher2);
2616    print!("C (0 rounds) =\t");
2617    for c in cipher1.clone()
2618        { print!("{:02X} ", c); }
2619    println!();
2620    let mut txt = String::new();
2621    for c in cipher1.clone()
2622        { write!(txt, "{:02X} ", c); }
2623    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
2624    print!("D (0 rounds) =\t");
2625    for c in cipher2.clone()
2626        { print!("{:02X} ", c); }
2627    println!();
2628    let mut txt = String::new();
2629    for c in cipher2.clone()
2630        { write!(txt, "{:02X} ", c); }
2631    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
2632    println!();
2633
2634    // Normal case for the message of 0 bytes
2635    let key = 0x_1234567890ABCDEF_u64;
2636    println!("K =\t{:#016X}", key);
2637    let mut a_des = DES::new_with_key_u64(key);
2638
2639    let mes = "";
2640    println!("M =\t{}", mes);
2641    let mut message = [0_u8; 0];
2642    message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2643    let iv = 0x_FEDCBA0987654321_u64;
2644    println!("IV =	{}", iv);
2645    let mut cipher = Vec::<u8>::new();
2646    a_des.encrypt_array_into_vec(iv, &message, &mut cipher);
2647    print!("C =\t");
2648    for c in cipher.clone()
2649        { print!("{:02X} ", c); }
2650    println!();
2651    let mut txt = String::new();
2652    for c in cipher.clone()
2653        { write!(txt, "{:02X} ", c); }
2654    assert_eq!(txt, "");
2655    println!();
2656
2657    // Normal case for the message shorter than 8 bytes
2658    let key = 0x_1234567890ABCDEF_u64;
2659    println!("K =\t{:#016X}", key);
2660    let mut a_des = DES::new_with_key_u64(key);
2661
2662    let mes = "7 bytes";
2663    println!("M =\t{}", mes);
2664    let mut message = [0_u8; 7];
2665    message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2666    let iv = 0x_FEDCBA0987654321_u64;
2667    println!("IV =	{}", iv);
2668    let mut cipher = Vec::<u8>::new();
2669    a_des.encrypt_array_into_vec(iv, &message, &mut cipher);
2670    print!("C =\t");
2671    for c in cipher.clone()
2672        { print!("{:02X} ", c); }
2673    println!();
2674    let mut txt = String::new();
2675    for c in cipher.clone()
2676        { write!(txt, "{:02X} ", c); }
2677    assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
2678    println!();
2679
2680    // Normal case for the message of 8 bytes
2681    let key = 0x_1234567890ABCDEF_u64;
2682    println!("K =\t{:#016X}", key);
2683    let mut a_des = DES::new_with_key_u64(key);
2684
2685    let mes = "I am OK.";
2686    println!("M =\t{}", mes);
2687    let mut message = [0_u8; 8];
2688    message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2689    let iv = 0x_FEDCBA0987654321_u64;
2690    println!("IV =	{}", iv);
2691    let mut cipher = Vec::<u8>::new();
2692    a_des.encrypt_array_into_vec(iv, &message, &mut cipher);
2693    print!("C =\t");
2694    for c in cipher.clone()
2695        { print!("{:02X} ", c); }
2696    println!();
2697    let mut txt = String::new();
2698    for c in cipher.clone()
2699        { write!(txt, "{:02X} ", c); }
2700    assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
2701    println!();
2702
2703    // Normal case for the message longer than 8 bytes and shorter than 16 bytes
2704    let key = 0x_1234567890ABCDEF_u64;
2705    println!("K =\t{:#016X}", key);
2706    let mut a_des = DES::new_with_key_u64(key);
2707
2708    let mes = "PARK Youngho";
2709    println!("M =\t{}", mes);
2710    let mut message = [0_u8; 12];
2711    message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2712    let iv = 0x_FEDCBA0987654321_u64;
2713    println!("IV =	{}", iv);
2714    let mut cipher = Vec::<u8>::new();
2715    a_des.encrypt_array_into_vec(iv, &message, &mut cipher);
2716    print!("C =\t");
2717    for c in cipher.clone()
2718        { print!("{:02X} ", c); }
2719    println!();
2720    let mut txt = String::new();
2721    for c in cipher.clone()
2722        { write!(txt, "{:02X} ", c); }
2723    assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 74 F9 B6 4F ");
2724    println!();
2725
2726    // Normal case for the message of 16 bytes
2727    let key = 0x_1234567890ABCDEF_u64;
2728    println!("K =\t{:#016X}", key);
2729    let mut a_des = DES::new_with_key_u64(key);
2730
2731    let mes = "고맙습니다.";
2732    println!("M =\t{}", mes);
2733    let mut message = [0_u8; 16];
2734    message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2735    let iv = 0x_FEDCBA0987654321_u64;
2736    println!("IV =	{}", iv);
2737    let mut cipher = Vec::<u8>::new();
2738    a_des.encrypt_array_into_vec(iv, &message, &mut cipher);
2739    print!("C =\t");
2740    for c in cipher.clone()
2741        { print!("{:02X} ", c); }
2742    println!();
2743    let mut txt = String::new();
2744    for c in cipher.clone()
2745        { write!(txt, "{:02X} ", c); }
2746    assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 F4 FC 94 B5 94 1F B9 BD ");
2747    println!("-------------------------------");
2748}
2749
2750fn des_encrypt_array_cfb_into_array()
2751{
2752    println!("des_encrypt_array_cfb_into_array()");
2753    use std::io::Write;
2754    use std::fmt::Write as _;
2755    use cryptocol::symmetric::{ DES, DES_Expanded, CFB};
2756
2757    // Normal case
2758    let key = 0x_1234567890ABCDEF_u64;
2759    println!("K =\t{:#016X}", key);
2760    let mut a_des = DES::new_with_key_u64(key);
2761
2762    let mes = "In the beginning God created the heavens and the earth.";
2763    println!("M =\t{}", mes);
2764    let mut message = [0_u8; 55];
2765    message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2766    let iv = 0x_FEDCBA0987654321_u64;
2767    println!("IV =	{}", iv);
2768    let mut cipher = [0_u8; 55];
2769    a_des.encrypt_array_into_array(iv, &message, &mut cipher);
2770    for c in cipher.clone()
2771        { print!("{:02X} ", c); }
2772    println!();
2773    let mut txt = String::new();
2774    for c in cipher.clone()
2775        { write!(txt, "{:02X} ", c); }
2776    assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 79 3A A1 78 EC CD 02 72 6A C4 41 7C 25 A4 2C 07 FC 77 25 49 12 55 0F 8A ED 44 C3 E4 DC 91 69 0F 40 72 7F F2 D9 B7 54 9F 36 91 C5 85 4F 9B 30 ");
2777    println!();
2778
2779    // Expanded case for 128 rounds
2780    let key = 0x_1234567890ABCDEF_u64;
2781    println!("K =\t{:#016X}", key);
2782    let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
2783
2784    let mes = "In the beginning God created the heavens and the earth.";
2785    println!("M =\t{}", mes);
2786    let mut message = [0_u8; 55];
2787    message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2788    let iv = 0x_FEDCBA0987654321_u64;
2789    println!("IV =	{}", iv);
2790    let mut cipher = [0_u8; 55];
2791    a_des.encrypt_array_into_array(iv, &message, &mut cipher);
2792    print!("C (128 rounds) =\t");
2793    for c in cipher.clone()
2794        { print!("{:02X} ", c); }
2795    println!();
2796    let mut txt = String::new();
2797    for c in cipher.clone()
2798        { write!(txt, "{:02X} ", c); }
2799    assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 F2 9A 70 44 D9 78 F5 E3 CF 55 0F EF BA F0 4A 7E BE 79 C9 B4 68 F4 99 09 48 93 00 D2 22 9D 29 6C 20 74 FF E3 E2 01 0C D3 7E 8D 4D 30 8F EC D6 ");
2800    println!();
2801
2802    // Expanded case for 0 rounds which means that key is meaningless
2803    let key1 = 0x_1234567890ABCDEF_u64;
2804    let key2 = 0_u64;
2805    println!("K1 =\t{:#016X}", key1);
2806    println!("K2 =\t{:#016X}", key2);
2807    let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
2808    let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
2809
2810    let mes = "In the beginning God created the heavens and the earth.";
2811    println!("M =\t{}", mes);
2812    let mut message = [0_u8; 55];
2813    message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2814    let iv = 0x_FEDCBA0987654321_u64;
2815    println!("IV =	{}", iv);
2816    let mut cipher1 = [0_u8; 55];
2817    let mut cipher2 = [0_u8; 55];
2818    c_des.encrypt_array_into_array(iv, &message, &mut cipher1);
2819    d_des.encrypt_array_into_array(iv, &message, &mut cipher2);
2820    print!("C (0 rounds) =\t");
2821    for c in cipher1.clone()
2822        { print!("{:02X} ", c); }
2823    println!();
2824    let mut txt = String::new();
2825    for c in cipher1.clone()
2826        { write!(txt, "{:02X} ", c); }
2827    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
2828    print!("D (0 rounds) =\t");
2829    for c in cipher2.clone()
2830        { print!("{:02X} ", c); }
2831    println!();
2832    let mut txt = String::new();
2833    for c in cipher2.clone()
2834        { write!(txt, "{:02X} ", c); }
2835    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
2836    println!();
2837
2838    // Normal case for the message of 0 bytes
2839    let key = 0x_1234567890ABCDEF_u64;
2840    println!("K =\t{:#016X}", key);
2841    let mut a_des = DES::new_with_key_u64(key);
2842
2843    let mes = "";
2844    println!("M =\t{}", mes);
2845    let mut message = [0_u8; 0];
2846    message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2847    let iv = 0x_FEDCBA0987654321_u64;
2848    println!("IV =	{}", iv);
2849    let mut cipher = [0_u8; 0];
2850    a_des.encrypt_array_into_array(iv, &message, &mut cipher);
2851    print!("C =\t");
2852    for c in cipher.clone()
2853        { print!("{:02X} ", c); }
2854    println!();
2855    let mut txt = String::new();
2856    for c in cipher.clone()
2857        { write!(txt, "{:02X} ", c); }
2858    assert_eq!(txt, "");
2859    println!();
2860
2861    // Normal case for the message shorter than 8 bytes
2862    let key = 0x_1234567890ABCDEF_u64;
2863    println!("K =\t{:#016X}", key);
2864    let mut a_des = DES::new_with_key_u64(key);
2865
2866    let mes = "7 bytes";
2867    println!("M =\t{}", mes);
2868    let mut message = [0_u8; 7];
2869    message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2870    let iv = 0x_FEDCBA0987654321_u64;
2871    println!("IV =	{}", iv);
2872    let mut cipher = [0_u8; 7];
2873    a_des.encrypt_array_into_array(iv, &message, &mut cipher);
2874    print!("C =\t");
2875    for c in cipher.clone()
2876        { print!("{:02X} ", c); }
2877    println!();
2878    let mut txt = String::new();
2879    for c in cipher.clone()
2880        { write!(txt, "{:02X} ", c); }
2881    assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
2882    println!();
2883
2884    // Normal case for the message of 8 bytes
2885    let key = 0x_1234567890ABCDEF_u64;
2886    println!("K =\t{:#016X}", key);
2887    let mut a_des = DES::new_with_key_u64(key);
2888
2889    let mes = "I am OK.";
2890    println!("M =\t{}", mes);
2891    let mut message = [0_u8; 8];
2892    message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2893    let iv = 0x_FEDCBA0987654321_u64;
2894    println!("IV =	{}", iv);
2895    let mut cipher = [0_u8; 8];
2896    a_des.encrypt_array_into_array(iv, &message, &mut cipher);
2897    print!("C =\t");
2898    for c in cipher.clone()
2899        { print!("{:02X} ", c); }
2900    println!();
2901    let mut txt = String::new();
2902    for c in cipher.clone()
2903        { write!(txt, "{:02X} ", c); }
2904    assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
2905    println!();
2906
2907    // Normal case for the message longer than 8 bytes and shorter than 16 bytes
2908    let key = 0x_1234567890ABCDEF_u64;
2909    println!("K =\t{:#016X}", key);
2910    let mut a_des = DES::new_with_key_u64(key);
2911
2912    let mes = "PARK Youngho";
2913    println!("M =\t{}", mes);
2914    let mut message = [0_u8; 12];
2915    message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2916    let iv = 0x_FEDCBA0987654321_u64;
2917    println!("IV =	{}", iv);
2918    let mut cipher = [0_u8; 12];
2919    a_des.encrypt_array_into_array(iv, &message, &mut cipher);
2920    print!("C =\t");
2921    for c in cipher.clone()
2922        { print!("{:02X} ", c); }
2923    println!();
2924    let mut txt = String::new();
2925    for c in cipher.clone()
2926        { write!(txt, "{:02X} ", c); }
2927    assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 74 F9 B6 4F ");
2928    println!();
2929
2930    // Normal case for the message of 16 bytes
2931    let key = 0x_1234567890ABCDEF_u64;
2932    println!("K =\t{:#016X}", key);
2933    let mut a_des = DES::new_with_key_u64(key);
2934 
2935    let mes = "고맙습니다.";
2936    println!("M =\t{}", mes);
2937    let mut message = [0_u8; 16];
2938    message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2939    let iv = 0x_FEDCBA0987654321_u64;
2940    println!("IV =	{}", iv);
2941    let mut cipher = [0_u8; 16];
2942    a_des.encrypt_array_into_array(iv, &message, &mut cipher);
2943    print!("C =\t");
2944    for c in cipher.clone()
2945        { print!("{:02X} ", c); }
2946    println!();
2947    let mut txt = String::new();
2948    for c in cipher.clone()
2949        { write!(txt, "{:02X} ", c); }
2950    assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 F4 FC 94 B5 94 1F B9 BD ");
2951    println!("-------------------------------");
2952}
2953
2954fn des_decrypt_cfb()
2955{
2956    println!("des_decrypt_cfb()");
2957    use std::io::Write;
2958    use std::fmt::Write as _;
2959    use cryptocol::symmetric::{ DES, DES_Expanded, CFB};
2960
2961    // Normal case
2962    let key = 0x_1234567890ABCDEF_u64;
2963    println!("K =\t{:#016X}", key);
2964    let mut a_des = DES::new_with_key_u64(key);
2965
2966    let message = "In the beginning God created the heavens and the earth.";
2967    println!("M =\t{}", message);
2968    let iv = 0x_FEDCBA0987654321_u64;
2969    println!("IV =	{}", iv);
2970    let mut cipher = Vec::<u8>::new();
2971    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
2972    print!("C (16 rounds) =\t");
2973    for c in cipher.clone()
2974        { print!("{:02X} ", c); }
2975    println!();
2976    let mut txt = String::new();
2977    for c in cipher.clone()
2978        { write!(txt, "{:02X} ", c); }
2979    assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 79 3A A1 78 EC CD 02 72 6A C4 41 7C 25 A4 2C 07 FC 77 25 49 12 55 0F 8A ED 44 C3 E4 DC 91 69 0F 40 72 7F F2 D9 B7 54 9F 36 91 C5 85 4F 9B 30 ");
2980
2981    let mut recovered = vec![0; 55];
2982    a_des.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2983    print!("Ba (16 rounds) =\t");
2984    for b in recovered.clone()
2985        { print!("{:02X} ", b); }
2986    println!();
2987    let mut txt = String::new();
2988    for c in recovered.clone()
2989        { write!(txt, "{:02X} ", c); }
2990    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
2991
2992    let mut converted = String::new();
2993    unsafe { converted.as_mut_vec() }.append(&mut recovered);
2994    
2995    println!("Bb (16 rounds) =\t{}", converted);
2996    assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2997    assert_eq!(converted, message);
2998    println!();
2999
3000    // Expanded case for 128 rounds
3001    let key = 0x_1234567890ABCDEF_u64;
3002    println!("K =\t{:#016X}", key);
3003    let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
3004
3005    let message = "In the beginning God created the heavens and the earth.";
3006    println!("M =\t{}", message);
3007    let iv = 0x_FEDCBA0987654321_u64;
3008    println!("IV =	{}", iv);
3009    let mut cipher = Vec::<u8>::new();
3010    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3011    print!("C (128 rounds) =\t");
3012    for c in cipher.clone()
3013        { print!("{:02X} ", c); }
3014    println!();
3015    let mut txt = String::new();
3016    for c in cipher.clone()
3017        { write!(txt, "{:02X} ", c); }
3018    assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 F2 9A 70 44 D9 78 F5 E3 CF 55 0F EF BA F0 4A 7E BE 79 C9 B4 68 F4 99 09 48 93 00 D2 22 9D 29 6C 20 74 FF E3 E2 01 0C D3 7E 8D 4D 30 8F EC D6 ");
3019
3020    let mut recovered = vec![0; 55];
3021    a_des.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
3022    print!("Ba (128 rounds) =\t");
3023    for b in recovered.clone()
3024        { print!("{:02X} ", b); }
3025    println!();
3026    let mut txt = String::new();
3027    for c in recovered.clone()
3028        { write!(txt, "{:02X} ", c); }
3029    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
3030
3031    let mut converted = String::new();
3032    unsafe { converted.as_mut_vec() }.append(&mut recovered);
3033    
3034    println!("Bb (128 rounds) =\t{}", converted);
3035    assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3036    assert_eq!(converted, message);
3037    println!();
3038
3039    // Expanded case for 0 rounds which means that key is meaningless
3040    let key1 = 0x_1234567890ABCDEF_u64;
3041    let key2 = 0_u64;
3042    println!("K =\t{:#016X}", key);
3043    let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
3044    let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
3045
3046    let message = "In the beginning God created the heavens and the earth.";
3047    println!("M =\t{}", message);
3048    let iv = 0x_FEDCBA0987654321_u64;
3049    println!("IV =	{}", iv);
3050    let mut cipher1 = Vec::<u8>::new();
3051    let mut cipher2 = Vec::<u8>::new();
3052    c_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
3053    d_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
3054    print!("C (0 rounds) =\t");
3055    for c in cipher1.clone()
3056        { print!("{:02X} ", c); }
3057    println!();
3058    let mut txt = String::new();
3059    for c in cipher1.clone()
3060        { write!(txt, "{:02X} ", c); }
3061    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
3062    print!("D (0 rounds) =\t");
3063    for c in cipher2.clone()
3064        { print!("{:02X} ", c); }
3065    println!();
3066    let mut txt = String::new();
3067    for c in cipher2.clone()
3068        { write!(txt, "{:02X} ", c); }
3069    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
3070
3071    let mut recovered1 = vec![0; 55];
3072    let mut recovered2 = vec![0; 55];
3073    c_des.decrypt(iv, cipher1.as_ptr(), cipher1.len() as u64, recovered1.as_mut_ptr());
3074    d_des.decrypt(iv, cipher2.as_ptr(), cipher2.len() as u64, recovered2.as_mut_ptr());
3075    print!("B1a (0 rounds) =\t");
3076    for b in recovered1.clone()
3077        { print!("{:02X} ", b); }
3078    println!();
3079    let mut txt = String::new();
3080    for c in recovered1.clone()
3081        { write!(txt, "{:02X} ", c); }
3082    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
3083    print!("B2a (0 rounds) =\t");
3084    for b in recovered2.clone()
3085        { print!("{:02X} ", b); }
3086    println!();
3087    let mut txt = String::new();
3088    for c in recovered2.clone()
3089        { write!(txt, "{:02X} ", c); }
3090    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
3091
3092    let mut converted1 = String::new();
3093    let mut converted2 = String::new();
3094    unsafe { converted1.as_mut_vec() }.append(&mut recovered1);
3095    unsafe { converted2.as_mut_vec() }.append(&mut recovered2);
3096    
3097    println!("B1b (0 rounds) =\t{}", converted1);
3098    assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
3099    assert_eq!(converted1, message);
3100    println!("B2b (0 rounds) =\t{}", converted2);
3101    assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
3102    assert_eq!(converted2, message);
3103    assert_eq!(converted1, converted1);
3104    println!();
3105
3106    // Normal case for the message of 0 bytes
3107    let key = 0x_1234567890ABCDEF_u64;
3108    println!("K =\t{:#016X}", key);
3109    let mut a_des = DES::new_with_key_u64(key);
3110
3111    let message = "";
3112    println!("M =\t{}", message);
3113    let iv = 0x_FEDCBA0987654321_u64;
3114    println!("IV =	{}", iv);
3115    let mut cipher = Vec::<u8>::new();
3116    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3117    print!("C =\t");
3118    for c in cipher.clone()
3119        { print!("{:02X} ", c); }
3120    println!();
3121    let mut txt = String::new();
3122    for c in cipher.clone()
3123        { write!(txt, "{:02X} ", c); }
3124    assert_eq!(txt, "");
3125
3126    let mut recovered = vec![0; 8];
3127    let len = a_des.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
3128    print!("Ba =\t");
3129    for b in recovered.clone()
3130        { print!("{:02X} ", b); }
3131    println!();
3132    let mut txt = String::new();
3133    for c in recovered.clone()
3134        { write!(txt, "{:02X} ", c); }
3135    assert_eq!(txt, "00 00 00 00 00 00 00 00 ");
3136
3137    let mut converted = String::new();
3138    unsafe { converted.as_mut_vec() }.append(&mut recovered);
3139    converted.truncate(len as usize);
3140    
3141    println!("Bb =\t{}", converted);
3142    assert_eq!(converted, "");
3143    assert_eq!(converted, message);
3144    println!();
3145
3146    // Normal case for the message shorter than 8 bytes
3147    let key = 0x_1234567890ABCDEF_u64;
3148    println!("K =\t{:#016X}", key);
3149    let mut a_des = DES::new_with_key_u64(key);
3150
3151    let message = "7 bytes";
3152    println!("M =\t{}", message);
3153    let iv = 0x_FEDCBA0987654321_u64;
3154    println!("IV =	{}", iv);
3155    let mut cipher = Vec::<u8>::new();
3156    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3157    print!("C =\t");
3158    for c in cipher.clone()
3159        { print!("{:02X} ", c); }
3160    println!();
3161    let mut txt = String::new();
3162    for c in cipher.clone()
3163        { write!(txt, "{:02X} ", c); }
3164    assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
3165    
3166    let mut recovered = vec![0; 8];
3167    let len = a_des.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
3168    print!("Ba =\t");
3169    for b in recovered.clone()
3170        { print!("{:02X} ", b); }
3171    println!();
3172    let mut txt = String::new();
3173    for c in recovered.clone()
3174        { write!(txt, "{:02X} ", c); }
3175    assert_eq!(txt, "37 20 62 79 74 65 73 00 ");
3176
3177    let mut converted = String::new();
3178    unsafe { converted.as_mut_vec() }.append(&mut recovered);
3179    converted.truncate(len as usize);
3180
3181    println!("Bb =\t{}", converted);
3182    assert_eq!(converted, "7 bytes");
3183    assert_eq!(converted, message);
3184    println!();
3185
3186    // Normal case for the message of 8 bytes
3187    let key = 0x_1234567890ABCDEF_u64;
3188    println!("K =\t{:#016X}", key);
3189    let mut a_des = DES::new_with_key_u64(key);
3190
3191    let message = "I am OK.";
3192    println!("M =\t{}", message);
3193    let iv = 0x_FEDCBA0987654321_u64;
3194    println!("IV =	{}", iv);
3195    let mut cipher = Vec::<u8>::new();
3196    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3197    print!("C =\t");
3198    for c in cipher.clone()
3199        { print!("{:02X} ", c); }
3200    println!();
3201    let mut txt = String::new();
3202    for c in cipher.clone()
3203        { write!(txt, "{:02X} ", c); }
3204    assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
3205    
3206    let mut recovered = vec![0; 16];
3207    let len = a_des.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
3208    print!("Ba =\t");
3209    for b in recovered.clone()
3210        { print!("{:02X} ", b); }
3211    println!();
3212    let mut txt = String::new();
3213    for c in recovered.clone()
3214        { write!(txt, "{:02X} ", c); }
3215    assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E 00 00 00 00 00 00 00 00 ");
3216
3217    let mut converted = String::new();
3218    unsafe { converted.as_mut_vec() }.append(&mut recovered);
3219    converted.truncate(len as usize);
3220    
3221    println!("Bb =\t{}", converted);
3222    assert_eq!(converted, "I am OK.");
3223    assert_eq!(converted, message);
3224    println!();
3225
3226    // Normal case for the message longer than 8 bytes and shorter than 16 bytes
3227    let key = 0x_1234567890ABCDEF_u64;
3228    println!("K =\t{:#016X}", key);
3229    let mut a_des = DES::new_with_key_u64(key);
3230
3231    let message = "PARK Youngho";
3232    println!("M =\t{}", message);
3233    let iv = 0x_FEDCBA0987654321_u64;
3234    println!("IV =	{}", iv);
3235    let mut cipher = Vec::<u8>::new();
3236    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3237    print!("C =\t");
3238    for c in cipher.clone()
3239        { print!("{:02X} ", c); }
3240    println!();
3241    let mut txt = String::new();
3242    for c in cipher.clone()
3243        { write!(txt, "{:02X} ", c); }
3244    assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 74 F9 B6 4F ");
3245
3246    let mut recovered = vec![0; 16];
3247    let len = a_des.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
3248    print!("Ba =\t");
3249    for b in recovered.clone()
3250        { print!("{:02X} ", b); }
3251    println!();
3252    let mut txt = String::new();
3253    for c in recovered.clone()
3254        { write!(txt, "{:02X} ", c); }
3255    assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F 00 00 00 00 ");
3256
3257    let mut converted = String::new();
3258    unsafe { converted.as_mut_vec() }.append(&mut recovered);
3259    converted.truncate(len as usize);
3260    
3261    println!("Bb =\t{}", converted);
3262    assert_eq!(converted, "PARK Youngho");
3263    assert_eq!(converted, message);
3264    println!();
3265
3266    // Normal case for the message of 16 bytes
3267    let key = 0x_1234567890ABCDEF_u64;
3268    println!("K =\t{:#016X}", key);
3269    let mut a_des = DES::new_with_key_u64(key);
3270
3271    let message = "고맙습니다.";
3272    println!("M =\t{}", message);
3273    let iv = 0x_FEDCBA0987654321_u64;
3274    println!("IV =	{}", iv);
3275    let mut cipher = Vec::<u8>::new();
3276    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3277    print!("C =\t");
3278    for c in cipher.clone()
3279        { print!("{:02X} ", c); }
3280    println!();
3281    let mut txt = String::new();
3282    for c in cipher.clone()
3283        { write!(txt, "{:02X} ", c); }
3284    assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 F4 FC 94 B5 94 1F B9 BD ");
3285
3286    let mut recovered = vec![0; 24];
3287    let len = a_des.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
3288    print!("Ba =\t");
3289    for b in recovered.clone()
3290        { print!("{:02X} ", b); }
3291    println!();
3292    let mut txt = String::new();
3293    for c in recovered.clone()
3294        { write!(txt, "{:02X} ", c); }
3295    assert_eq!(txt, "EA B3 A0 EB A7 99 EC 8A B5 EB 8B 88 EB 8B A4 2E 00 00 00 00 00 00 00 00 ");
3296
3297    let mut converted = String::new();
3298    unsafe { converted.as_mut_vec() }.append(&mut recovered);
3299    converted.truncate(len as usize);
3300    
3301    println!("Bb =\t{}", converted);
3302    assert_eq!(converted, "고맙습니다.");
3303    assert_eq!(converted, message);
3304    println!("-------------------------------");
3305}
3306
3307fn des_decrypt_cfb_into_vec()
3308{
3309    println!("des_decrypt_cfb_into_vec()");
3310    use std::io::Write;
3311    use std::fmt::Write as _;
3312    use cryptocol::symmetric::{ DES, DES_Expanded, CFB};
3313
3314    // Normal case
3315    let key = 0x_1234567890ABCDEF_u64;
3316    println!("K =\t{:#016X}", key);
3317    let mut a_des = DES::new_with_key_u64(key);
3318
3319    let message = "In the beginning God created the heavens and the earth.";
3320    println!("M =\t{}", message);
3321    let iv = 0x_FEDCBA0987654321_u64;
3322    println!("IV =	{}", iv);
3323    let mut cipher = Vec::<u8>::new();
3324    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3325    print!("C (16 rounds) =\t");
3326    for c in cipher.clone()
3327        { print!("{:02X} ", c); }
3328    println!();
3329    let mut txt = String::new();
3330    for c in cipher.clone()
3331        { write!(txt, "{:02X} ", c); }
3332    assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 79 3A A1 78 EC CD 02 72 6A C4 41 7C 25 A4 2C 07 FC 77 25 49 12 55 0F 8A ED 44 C3 E4 DC 91 69 0F 40 72 7F F2 D9 B7 54 9F 36 91 C5 85 4F 9B 30 ");
3333
3334    let mut recovered = Vec::<u8>::new();
3335    a_des.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3336    print!("Ba (16 rounds) =\t");
3337    for b in recovered.clone()
3338        { print!("{:02X} ", b); }
3339    println!();
3340    let mut txt = String::new();
3341    for c in recovered.clone()
3342        { write!(txt, "{:02X} ", c); }
3343    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
3344
3345    let mut converted = String::new();
3346    unsafe { converted.as_mut_vec() }.append(&mut recovered);
3347    
3348    println!("Bb (16 rounds) =\t{}", converted);
3349    assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3350    assert_eq!(converted, message);
3351    println!();
3352
3353    // Expanded case for 128 rounds
3354    let key = 0x_1234567890ABCDEF_u64;
3355    println!("K =\t{:#016X}", key);
3356    let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
3357
3358    let message = "In the beginning God created the heavens and the earth.";
3359    println!("M =\t{}", message);
3360    let iv = 0x_FEDCBA0987654321_u64;
3361    println!("IV =	{}", iv);
3362    let mut cipher = Vec::<u8>::new();
3363    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3364    print!("C (128 rounds) =\t");
3365    for c in cipher.clone()
3366        { print!("{:02X} ", c); }
3367    println!();
3368    let mut txt = String::new();
3369    for c in cipher.clone()
3370        { write!(txt, "{:02X} ", c); }
3371    assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 F2 9A 70 44 D9 78 F5 E3 CF 55 0F EF BA F0 4A 7E BE 79 C9 B4 68 F4 99 09 48 93 00 D2 22 9D 29 6C 20 74 FF E3 E2 01 0C D3 7E 8D 4D 30 8F EC D6 ");
3372
3373    let mut recovered = Vec::<u8>::new();
3374    a_des.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3375    print!("Ba (128 rounds) =\t");
3376    for b in recovered.clone()
3377        { print!("{:02X} ", b); }
3378    println!();
3379    let mut txt = String::new();
3380    for c in recovered.clone()
3381        { write!(txt, "{:02X} ", c); }
3382    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
3383
3384    let mut converted = String::new();
3385    unsafe { converted.as_mut_vec() }.append(&mut recovered);
3386    
3387    println!("Bb (128 rounds) =\t{}", converted);
3388    assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3389    assert_eq!(converted, message);
3390    println!();
3391
3392    // Expanded case for 0 rounds which means that key is meaningless
3393    let key1 = 0x_1234567890ABCDEF_u64;
3394    let key2 = 0_u64;
3395    println!("K =\t{:#016X}", key);
3396    let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
3397    let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
3398
3399    let message = "In the beginning God created the heavens and the earth.";
3400    println!("M =\t{}", message);
3401    let iv = 0x_FEDCBA0987654321_u64;
3402    println!("IV =	{}", iv);
3403    let mut cipher1 = Vec::<u8>::new();
3404    let mut cipher2 = Vec::<u8>::new();
3405    c_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
3406    d_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
3407    print!("C (0 rounds) =\t");
3408    for c in cipher1.clone()
3409        { print!("{:02X} ", c); }
3410    println!();
3411    let mut txt = String::new();
3412    for c in cipher1.clone()
3413        { write!(txt, "{:02X} ", c); }
3414    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
3415    print!("D (0 rounds) =\t");
3416    for c in cipher2.clone()
3417        { print!("{:02X} ", c); }
3418    println!();
3419    let mut txt = String::new();
3420    for c in cipher2.clone()
3421        { write!(txt, "{:02X} ", c); }
3422    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
3423
3424    let mut recovered1 = Vec::<u8>::new();
3425    let mut recovered2 = Vec::<u8>::new();
3426    c_des.decrypt_into_vec(iv, cipher1.as_ptr(), cipher1.len() as u64, &mut recovered1);
3427    d_des.decrypt_into_vec(iv, cipher2.as_ptr(), cipher2.len() as u64, &mut recovered2);
3428    print!("B1a (0 rounds) =\t");
3429    for b in recovered1.clone()
3430        { print!("{:02X} ", b); }
3431    println!();
3432    let mut txt = String::new();
3433    for c in recovered1.clone()
3434        { write!(txt, "{:02X} ", c); }
3435    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
3436    print!("B2a (0 rounds) =\t");
3437    for b in recovered2.clone()
3438        { print!("{:02X} ", b); }
3439    println!();
3440    let mut txt = String::new();
3441    for c in recovered2.clone()
3442        { write!(txt, "{:02X} ", c); }
3443    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
3444
3445    let mut converted1 = String::new();
3446    let mut converted2 = String::new();
3447    unsafe { converted1.as_mut_vec() }.append(&mut recovered1);
3448    unsafe { converted2.as_mut_vec() }.append(&mut recovered2);
3449    
3450    println!("B1b (0 rounds) =\t{}", converted1);
3451    assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
3452    assert_eq!(converted1, message);
3453    println!("B2b (0 rounds) =\t{}", converted2);
3454    assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
3455    assert_eq!(converted2, message);
3456    assert_eq!(converted1, converted1);
3457    println!();
3458
3459    // Normal case for the message of 0 bytes
3460    let key = 0x_1234567890ABCDEF_u64;
3461    println!("K =\t{:#016X}", key);
3462    let mut a_des = DES::new_with_key_u64(key);
3463
3464    let message = "";
3465    println!("M =\t{}", message);
3466    let iv = 0x_FEDCBA0987654321_u64;
3467    println!("IV =	{}", iv);
3468    let mut cipher = Vec::<u8>::new();
3469    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3470    print!("C =\t");
3471    for c in cipher.clone()
3472        { print!("{:02X} ", c); }
3473    println!();
3474    let mut txt = String::new();
3475    for c in cipher.clone()
3476        { write!(txt, "{:02X} ", c); }
3477    assert_eq!(txt, "");
3478
3479    let mut recovered = Vec::<u8>::new();
3480    a_des.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3481    print!("Ba =\t");
3482    for b in recovered.clone()
3483        { print!("{:02X} ", b); }
3484    println!();
3485    let mut txt = String::new();
3486    for c in recovered.clone()
3487        { write!(txt, "{:02X} ", c); }
3488    assert_eq!(txt, "");
3489
3490    let mut converted = String::new();
3491    unsafe { converted.as_mut_vec() }.append(&mut recovered);
3492    
3493    println!("Bb =\t{}", converted);
3494    assert_eq!(converted, "");
3495    assert_eq!(converted, message);
3496    println!();
3497
3498    // Normal case for the message shorter than 8 bytes
3499    let key = 0x_1234567890ABCDEF_u64;
3500    println!("K =\t{:#016X}", key);
3501    let mut a_des = DES::new_with_key_u64(key);
3502
3503    let message = "7 bytes";
3504    println!("M =\t{}", message);
3505    let iv = 0x_FEDCBA0987654321_u64;
3506    println!("IV =	{}", iv);
3507    let mut cipher = Vec::<u8>::new();
3508    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3509    print!("C =\t");
3510    for c in cipher.clone()
3511        { print!("{:02X} ", c); }
3512    println!();
3513    let mut txt = String::new();
3514    for c in cipher.clone()
3515        { write!(txt, "{:02X} ", c); }
3516    assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
3517    
3518    let mut recovered = Vec::<u8>::new();
3519    a_des.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3520    print!("Ba =\t");
3521    for b in recovered.clone()
3522        { print!("{:02X} ", b); }
3523    println!();
3524    let mut txt = String::new();
3525    for c in recovered.clone()
3526        { write!(txt, "{:02X} ", c); }
3527    assert_eq!(txt, "37 20 62 79 74 65 73 ");
3528
3529    let mut converted = String::new();
3530    unsafe { converted.as_mut_vec() }.append(&mut recovered);
3531    
3532    println!("Bb =\t{}", converted);
3533    assert_eq!(converted, "7 bytes");
3534    assert_eq!(converted, message);
3535    println!();
3536
3537    // Normal case for the message of 8 bytes
3538    let key = 0x_1234567890ABCDEF_u64;
3539    println!("K =\t{:#016X}", key);
3540    let mut a_des = DES::new_with_key_u64(key);
3541
3542    let message = "I am OK.";
3543    println!("M =\t{}", message);
3544    let iv = 0x_FEDCBA0987654321_u64;
3545    println!("IV =	{}", iv);
3546    let mut cipher = Vec::<u8>::new();
3547    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3548    print!("C =\t");
3549    for c in cipher.clone()
3550        { print!("{:02X} ", c); }
3551    println!();
3552    let mut txt = String::new();
3553    for c in cipher.clone()
3554        { write!(txt, "{:02X} ", c); }
3555    assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
3556    
3557    let mut recovered = Vec::<u8>::new();
3558    a_des.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3559    print!("Ba =\t");
3560    for b in recovered.clone()
3561        { print!("{:02X} ", b); }
3562    println!();
3563    let mut txt = String::new();
3564    for c in recovered.clone()
3565        { write!(txt, "{:02X} ", c); }
3566    assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E ");
3567
3568    let mut converted = String::new();
3569    unsafe { converted.as_mut_vec() }.append(&mut recovered);
3570    
3571    println!("Bb =\t{}", converted);
3572    assert_eq!(converted, "I am OK.");
3573    assert_eq!(converted, message);
3574    println!();
3575
3576    // Normal case for the message longer than 8 bytes and shorter than 16 bytes
3577    let key = 0x_1234567890ABCDEF_u64;
3578    println!("K =\t{:#016X}", key);
3579    let mut a_des = DES::new_with_key_u64(key);
3580
3581    let message = "PARK Youngho";
3582    println!("M =\t{}", message);
3583    let iv = 0x_FEDCBA0987654321_u64;
3584    println!("IV =	{}", iv);
3585    let mut cipher = Vec::<u8>::new();
3586    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3587    print!("C =\t");
3588    for c in cipher.clone()
3589        { print!("{:02X} ", c); }
3590    println!();
3591    let mut txt = String::new();
3592    for c in cipher.clone()
3593        { write!(txt, "{:02X} ", c); }
3594    assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 74 F9 B6 4F ");
3595
3596    let mut recovered = Vec::<u8>::new();
3597    a_des.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3598    print!("Ba =\t");
3599    for b in recovered.clone()
3600        { print!("{:02X} ", b); }
3601    println!();
3602    let mut txt = String::new();
3603    for c in recovered.clone()
3604        { write!(txt, "{:02X} ", c); }
3605    assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F ");
3606
3607    let mut converted = String::new();
3608    unsafe { converted.as_mut_vec() }.append(&mut recovered);
3609    
3610    println!("Bb =\t{}", converted);
3611    assert_eq!(converted, "PARK Youngho");
3612    assert_eq!(converted, message);
3613    println!();
3614
3615    // Normal case for the message of 16 bytes
3616    let key = 0x_1234567890ABCDEF_u64;
3617    println!("K =\t{:#016X}", key);
3618    let mut a_des = DES::new_with_key_u64(key);
3619
3620    let message = "고맙습니다.";
3621    println!("M =\t{}", message);
3622    let iv = 0x_FEDCBA0987654321_u64;
3623    println!("IV =	{}", iv);
3624    let mut cipher = Vec::<u8>::new();
3625    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3626    print!("C =\t");
3627    for c in cipher.clone()
3628        { print!("{:02X} ", c); }
3629    println!();
3630    let mut txt = String::new();
3631    for c in cipher.clone()
3632        { write!(txt, "{:02X} ", c); }
3633    assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 F4 FC 94 B5 94 1F B9 BD ");
3634
3635    let mut recovered = Vec::<u8>::new();
3636    a_des.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3637    print!("Ba =\t");
3638    for b in recovered.clone()
3639        { print!("{:02X} ", b); }
3640    println!();
3641    let mut txt = String::new();
3642    for c in recovered.clone()
3643        { write!(txt, "{:02X} ", c); }
3644    assert_eq!(txt, "EA B3 A0 EB A7 99 EC 8A B5 EB 8B 88 EB 8B A4 2E ");
3645
3646    let mut converted = String::new();
3647    unsafe { converted.as_mut_vec() }.append(&mut recovered);
3648    
3649    println!("Bb =\t{}", converted);
3650    assert_eq!(converted, "고맙습니다.");
3651    assert_eq!(converted, message);
3652    println!("-------------------------------");
3653}
3654
3655fn des_decrypt_cfb_into_array()
3656{
3657    println!("des_decrypt_cfb_into_array()");
3658    use std::io::Write;
3659    use std::fmt::Write as _;
3660    use cryptocol::symmetric::{ DES, DES_Expanded, CFB};
3661
3662    // Normal case
3663    let key = 0x_1234567890ABCDEF_u64;
3664    println!("K =\t{:#016X}", key);
3665    let mut a_des = DES::new_with_key_u64(key);
3666
3667    let message = "In the beginning God created the heavens and the earth.";
3668    println!("M =\t{}", message);
3669    let iv = 0x_FEDCBA0987654321_u64;
3670    println!("IV =	{}", iv);
3671    let mut cipher = Vec::<u8>::new();
3672    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3673    print!("C (16 rounds) =\t");
3674    for c in cipher.clone()
3675        { print!("{:02X} ", c); }
3676    println!();
3677    let mut txt = String::new();
3678    for c in cipher.clone()
3679        { write!(txt, "{:02X} ", c); }
3680    assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 79 3A A1 78 EC CD 02 72 6A C4 41 7C 25 A4 2C 07 FC 77 25 49 12 55 0F 8A ED 44 C3 E4 DC 91 69 0F 40 72 7F F2 D9 B7 54 9F 36 91 C5 85 4F 9B 30 ");
3681
3682    let mut recovered = [0u8; 56];
3683    let len = a_des.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3684    print!("Ba (16 rounds) =\t");
3685    for b in recovered.clone()
3686        { print!("{:02X} ", b); }
3687    println!();
3688    let mut txt = String::new();
3689    for c in recovered.clone()
3690        { write!(txt, "{:02X} ", c); }
3691    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 ");
3692
3693    let mut converted = String::new();
3694    unsafe { converted.as_mut_vec() }.write(&recovered);
3695    unsafe { converted.as_mut_vec() }.truncate(len as usize);
3696    println!("Bb (16 rounds) =\t{}", converted);
3697    assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3698    assert_eq!(converted, message);
3699    println!();
3700
3701    // Expanded case for 128 rounds
3702    let key = 0x_1234567890ABCDEF_u64;
3703    println!("K =\t{:#016X}", key);
3704    let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
3705
3706    let message = "In the beginning God created the heavens and the earth.";
3707    println!("M =\t{}", message);
3708    let iv = 0x_FEDCBA0987654321_u64;
3709    println!("IV =	{}", iv);
3710    let mut cipher = Vec::<u8>::new();
3711    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3712    print!("C (128 rounds) =\t");
3713    for c in cipher.clone()
3714        { print!("{:02X} ", c); }
3715    println!();
3716    let mut txt = String::new();
3717    for c in cipher.clone()
3718        { write!(txt, "{:02X} ", c); }
3719    assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 F2 9A 70 44 D9 78 F5 E3 CF 55 0F EF BA F0 4A 7E BE 79 C9 B4 68 F4 99 09 48 93 00 D2 22 9D 29 6C 20 74 FF E3 E2 01 0C D3 7E 8D 4D 30 8F EC D6 ");
3720
3721    let mut recovered = [0u8; 56];
3722    let len = a_des.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3723    print!("Ba (16 rounds) =\t");
3724    for b in recovered.clone()
3725        { print!("{:02X} ", b); }
3726    println!();
3727    let mut txt = String::new();
3728    for c in recovered.clone()
3729        { write!(txt, "{:02X} ", c); }
3730    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 ");
3731
3732    let mut converted = String::new();
3733    unsafe { converted.as_mut_vec() }.write(&recovered);
3734    unsafe { converted.as_mut_vec() }.truncate(len as usize);
3735    println!("Bb (16 rounds) =\t{}", converted);
3736    assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3737    assert_eq!(converted, message);
3738    println!();
3739
3740    // Expanded case for 0 rounds which means that key is meaningless
3741    let key1 = 0x_1234567890ABCDEF_u64;
3742    let key2 = 0_u64;
3743    println!("K =\t{:#016X}", key);
3744    let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
3745    let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
3746
3747    let message = "In the beginning God created the heavens and the earth.";
3748    println!("M =\t{}", message);
3749    let iv = 0x_FEDCBA0987654321_u64;
3750    println!("IV =	{}", iv);
3751    let mut cipher1 = Vec::<u8>::new();
3752    let mut cipher2 = Vec::<u8>::new();
3753    c_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
3754    d_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
3755    print!("C (0 rounds) =\t");
3756    for c in cipher1.clone()
3757        { print!("{:02X} ", c); }
3758    println!();
3759    let mut txt = String::new();
3760    for c in cipher1.clone()
3761        { write!(txt, "{:02X} ", c); }
3762    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
3763    print!("D (0 rounds) =\t");
3764    for c in cipher2.clone()
3765        { print!("{:02X} ", c); }
3766    println!();
3767    let mut txt = String::new();
3768    for c in cipher2.clone()
3769        { write!(txt, "{:02X} ", c); }
3770    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
3771
3772    let mut recovered1 = [0u8; 56];
3773    let mut recovered2 = [0u8; 56];
3774    let len1 = c_des.decrypt_into_array(iv, cipher1.as_ptr(), cipher1.len() as u64, &mut recovered1);
3775    let len2 = d_des.decrypt_into_array(iv, cipher2.as_ptr(), cipher2.len() as u64, &mut recovered2);
3776    print!("B1a (0 rounds) =\t");
3777    for b in recovered1.clone()
3778        { print!("{:02X} ", b); }
3779    println!();
3780    let mut txt = String::new();
3781    for c in recovered1.clone()
3782        { write!(txt, "{:02X} ", c); }
3783    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 ");
3784    print!("B2a (0 rounds) =\t");
3785    for b in recovered2.clone()
3786        { print!("{:02X} ", b); }
3787    println!();
3788    let mut txt = String::new();
3789    for c in recovered.clone()
3790        { write!(txt, "{:02X} ", c); }
3791    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 ");
3792
3793    let mut converted1 = String::new();
3794    let mut converted2 = String::new();
3795    unsafe { converted1.as_mut_vec() }.write(&recovered1);
3796    unsafe { converted2.as_mut_vec() }.write(&recovered2);
3797    unsafe { converted1.as_mut_vec() }.truncate(len1 as usize);
3798    unsafe { converted2.as_mut_vec() }.truncate(len2 as usize);
3799    println!("B1b (0 rounds) =\t{}", converted1);
3800    println!("B2b (0 rounds) =\t{}", converted2);
3801    assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
3802    assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
3803    assert_eq!(converted1, message);
3804    assert_eq!(converted2, message);
3805    assert_eq!(converted1, converted2);
3806    println!();
3807
3808    // Normal case for the message of 0 bytes
3809    let key = 0x_1234567890ABCDEF_u64;
3810    println!("K =\t{:#016X}", key);
3811    let mut a_des = DES::new_with_key_u64(key);
3812
3813    let message = "";
3814    println!("M =\t{}", message);
3815    let iv = 0x_FEDCBA0987654321_u64;
3816    println!("IV =	{}", iv);
3817    let mut cipher = Vec::<u8>::new();
3818    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3819    print!("C =\t");
3820    for c in cipher.clone()
3821        { print!("{:02X} ", c); }
3822    println!();
3823    let mut txt = String::new();
3824    for c in cipher.clone()
3825        { write!(txt, "{:02X} ", c); }
3826    assert_eq!(txt, "");
3827
3828    let mut recovered = [0u8; 8];
3829    let len = a_des.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3830
3831    print!("Ba =\t");
3832    for b in recovered.clone()
3833        { print!("{:02X} ", b); }
3834    println!();
3835    let mut txt = String::new();
3836    for c in recovered.clone()
3837        { write!(txt, "{:02X} ", c); }
3838    assert_eq!(txt, "00 00 00 00 00 00 00 00 ");
3839
3840    let mut converted = String::new();
3841    unsafe { converted.as_mut_vec() }.write(&recovered);
3842    unsafe { converted.as_mut_vec() }.truncate(len as usize);
3843    println!("Bb =\t{}", converted);
3844    assert_eq!(converted, "");
3845    assert_eq!(converted, message);
3846    println!();
3847
3848    // Normal case for the message shorter than 8 bytes
3849    let key = 0x_1234567890ABCDEF_u64;
3850    println!("K =\t{:#016X}", key);
3851    let mut a_des = DES::new_with_key_u64(key);
3852
3853    let message = "7 bytes";
3854    println!("M =\t{}", message);
3855    let iv = 0x_FEDCBA0987654321_u64;
3856    println!("IV =	{}", iv);
3857    let mut cipher = Vec::<u8>::new();
3858    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3859    print!("C =\t");
3860    for c in cipher.clone()
3861        { print!("{:02X} ", c); }
3862    println!();
3863    let mut txt = String::new();
3864    for c in cipher.clone()
3865        { write!(txt, "{:02X} ", c); }
3866    assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
3867
3868    let mut recovered = [0u8; 8];
3869    let len = a_des.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3870
3871    print!("Ba =\t");
3872    for b in recovered.clone()
3873        { print!("{:02X} ", b); }
3874    println!();
3875    let mut txt = String::new();
3876    for c in recovered.clone()
3877        { write!(txt, "{:02X} ", c); }
3878    assert_eq!(txt, "37 20 62 79 74 65 73 00 ");
3879
3880    let mut converted = String::new();
3881    unsafe { converted.as_mut_vec() }.write(&recovered);
3882    unsafe { converted.as_mut_vec() }.truncate(len as usize);
3883    println!("Bb =\t{}", converted);
3884    assert_eq!(converted, "7 bytes");
3885    assert_eq!(converted, message);
3886    println!();
3887
3888    // Normal case for the message of 8 bytes
3889    let key = 0x_1234567890ABCDEF_u64;
3890    println!("K =\t{:#016X}", key);
3891    let mut a_des = DES::new_with_key_u64(key);
3892
3893    let message = "I am OK.";
3894    println!("M =\t{}", message);
3895    let iv = 0x_FEDCBA0987654321_u64;
3896    println!("IV =	{}", iv);
3897    let mut cipher = Vec::<u8>::new();
3898    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3899    print!("C =\t");
3900    for c in cipher.clone()
3901        { print!("{:02X} ", c); }
3902    println!();
3903    let mut txt = String::new();
3904    for c in cipher.clone()
3905        { write!(txt, "{:02X} ", c); }
3906    assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
3907
3908    let mut recovered = [0u8; 16];
3909    let len = a_des.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3910
3911    print!("Ba =\t");
3912    for b in recovered.clone()
3913        { print!("{:02X} ", b); }
3914    println!();
3915    let mut txt = String::new();
3916    for c in recovered.clone()
3917        { write!(txt, "{:02X} ", c); }
3918    assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E 00 00 00 00 00 00 00 00 ");
3919
3920    let mut converted = String::new();
3921    unsafe { converted.as_mut_vec() }.write(&recovered);
3922    unsafe { converted.as_mut_vec() }.truncate(len as usize);
3923    println!("Bb =\t{}", converted);
3924    assert_eq!(converted, "I am OK.");
3925    assert_eq!(converted, message);
3926    println!();
3927
3928    // Normal case for the message longer than 8 bytes and shorter than 16 bytes
3929    let key = 0x_1234567890ABCDEF_u64;
3930    println!("K =\t{:#016X}", key);
3931    let mut a_des = DES::new_with_key_u64(key);
3932
3933    let message = "PARK Youngho";
3934    println!("M =\t{}", message);
3935    let iv = 0x_FEDCBA0987654321_u64;
3936    println!("IV =	{}", iv);
3937    let mut cipher = Vec::<u8>::new();
3938    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3939    print!("C =\t");
3940    for c in cipher.clone()
3941        { print!("{:02X} ", c); }
3942    println!();
3943    let mut txt = String::new();
3944    for c in cipher.clone()
3945        { write!(txt, "{:02X} ", c); }
3946    assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 74 F9 B6 4F ");
3947
3948    let mut recovered = [0u8; 16];
3949    let len = a_des.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3950
3951    print!("Ba =\t");
3952    for b in recovered.clone()
3953        { print!("{:02X} ", b); }
3954    println!();
3955    let mut txt = String::new();
3956    for c in recovered.clone()
3957        { write!(txt, "{:02X} ", c); }
3958    assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F 00 00 00 00 ");
3959
3960    let mut converted = String::new();
3961    unsafe { converted.as_mut_vec() }.write(&recovered);
3962    unsafe { converted.as_mut_vec() }.truncate(len as usize);
3963    println!("Bb =\t{}", converted);
3964    assert_eq!(converted, "PARK Youngho");
3965    assert_eq!(converted, message);
3966    println!();
3967
3968    // Normal case for the message of 16 bytes
3969    let key = 0x_1234567890ABCDEF_u64;
3970    println!("K =\t{:#016X}", key);
3971    let mut a_des = DES::new_with_key_u64(key);
3972
3973    let message = "고맙습니다.";
3974    println!("M =\t{}", message);
3975    let iv = 0x_FEDCBA0987654321_u64;
3976    println!("IV =	{}", iv);
3977    let mut cipher = Vec::<u8>::new();
3978    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3979    print!("C =\t");
3980    for c in cipher.clone()
3981        { print!("{:02X} ", c); }
3982    println!();
3983    let mut txt = String::new();
3984    for c in cipher.clone()
3985        { write!(txt, "{:02X} ", c); }
3986    assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 F4 FC 94 B5 94 1F B9 BD ");
3987
3988    let mut recovered = [0u8; 24];
3989    let len = a_des.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3990
3991    print!("Ba =\t");
3992    for b in recovered.clone()
3993        { print!("{:02X} ", b); }
3994    println!();
3995    let mut txt = String::new();
3996    for c in recovered.clone()
3997        { write!(txt, "{:02X} ", c); }
3998    assert_eq!(txt, "EA B3 A0 EB A7 99 EC 8A B5 EB 8B 88 EB 8B A4 2E 00 00 00 00 00 00 00 00 ");
3999
4000    let mut converted = String::new();
4001    unsafe { converted.as_mut_vec() }.write(&recovered);
4002    unsafe { converted.as_mut_vec() }.truncate(len as usize);
4003    println!("Bb =\t{}", converted);
4004    assert_eq!(converted, "고맙습니다.");
4005    assert_eq!(converted, message);
4006    println!("-------------------------------");
4007}
4008
4009fn des_decrypt_cfb_into_string()
4010{
4011    println!("des_decrypt_cfb_into_string()");
4012    use std::io::Write;
4013    use std::fmt::Write as _;
4014    use cryptocol::symmetric::{ DES, DES_Expanded, CFB};
4015
4016    // Normal case
4017    let key = 0x_1234567890ABCDEF_u64;
4018    println!("K =\t{:#016X}", key);
4019    let mut a_des = DES::new_with_key_u64(key);
4020
4021    let message = "In the beginning God created the heavens and the earth.";
4022    println!("M =\t{}", message);
4023    let iv = 0x_FEDCBA0987654321_u64;
4024    println!("IV =	{}", iv);
4025    let mut cipher = Vec::<u8>::new();
4026    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4027    print!("C (16 rounds) =\t");
4028    for c in cipher.clone()
4029        { print!("{:02X} ", c); }
4030    println!();
4031    let mut txt = String::new();
4032    for c in cipher.clone()
4033        { write!(txt, "{:02X} ", c); }
4034    assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 79 3A A1 78 EC CD 02 72 6A C4 41 7C 25 A4 2C 07 FC 77 25 49 12 55 0F 8A ED 44 C3 E4 DC 91 69 0F 40 72 7F F2 D9 B7 54 9F 36 91 C5 85 4F 9B 30 ");
4035
4036    let mut recovered = String::new();
4037    a_des.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
4038    println!("B (16 rounds) =\t{}", recovered);
4039    assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
4040    assert_eq!(recovered, message);
4041    println!();
4042
4043    // Expanded case for 128 rounds
4044    let key = 0x_1234567890ABCDEF_u64;
4045    println!("K =\t{:#016X}", key);
4046    let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
4047
4048    let message = "In the beginning God created the heavens and the earth.";
4049    println!("M =\t{}", message);
4050    let iv = 0x_FEDCBA0987654321_u64;
4051    println!("IV =	{}", iv);
4052    let mut cipher = Vec::<u8>::new();
4053    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4054    print!("C (128 rounds) =\t");
4055    for c in cipher.clone()
4056        { print!("{:02X} ", c); }
4057    println!();
4058    let mut txt = String::new();
4059    for c in cipher.clone()
4060        { write!(txt, "{:02X} ", c); }
4061    assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 F2 9A 70 44 D9 78 F5 E3 CF 55 0F EF BA F0 4A 7E BE 79 C9 B4 68 F4 99 09 48 93 00 D2 22 9D 29 6C 20 74 FF E3 E2 01 0C D3 7E 8D 4D 30 8F EC D6 ");
4062
4063    let mut recovered = String::new();
4064    a_des.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
4065    println!("B (128 rounds) =\t{}", recovered);
4066    assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
4067    assert_eq!(recovered, message);
4068    println!();
4069
4070    // Expanded case for 0 rounds which means that key is meaningless
4071    let key1 = 0x_1234567890ABCDEF_u64;
4072    let key2 = 0_u64;
4073    println!("K =\t{:#016X}", key);
4074    let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
4075    let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
4076
4077    let message = "In the beginning God created the heavens and the earth.";
4078    println!("M =\t{}", message);
4079    let iv = 0x_FEDCBA0987654321_u64;
4080    println!("IV =	{}", iv);
4081    let mut cipher1 = Vec::<u8>::new();
4082    let mut cipher2 = Vec::<u8>::new();
4083    c_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
4084    d_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
4085    print!("C (0 rounds) =\t");
4086    for c in cipher1.clone()
4087        { print!("{:02X} ", c); }
4088    println!();
4089    let mut txt = String::new();
4090    for c in cipher1.clone()
4091        { write!(txt, "{:02X} ", c); }
4092    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
4093    print!("D (0 rounds) =\t");
4094    for c in cipher2.clone()
4095        { print!("{:02X} ", c); }
4096    println!();
4097    let mut txt = String::new();
4098    for c in cipher2.clone()
4099        { write!(txt, "{:02X} ", c); }
4100    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
4101
4102    let mut recovered1 = String::new();
4103    let mut recovered2 = String::new();
4104    c_des.decrypt_into_string(iv, cipher1.as_ptr(), cipher1.len() as u64, &mut recovered1);
4105    d_des.decrypt_into_string(iv, cipher2.as_ptr(), cipher2.len() as u64, &mut recovered2);
4106    println!("B1 (0 rounds) =\t{}", recovered1);
4107    println!("B2 (0 rounds) =\t{}", recovered2);
4108    assert_eq!(recovered1, "In the beginning God created the heavens and the earth.");
4109    assert_eq!(recovered2, "In the beginning God created the heavens and the earth.");
4110    assert_eq!(recovered1, message);
4111    assert_eq!(recovered2, message);
4112    assert_eq!(recovered1, recovered2);
4113    println!();
4114
4115    // Normal case for the message of 0 bytes
4116    let key = 0x_1234567890ABCDEF_u64;
4117    println!("K =\t{:#016X}", key);
4118    let mut a_des = DES::new_with_key_u64(key);
4119
4120    let message = "";
4121    println!("M =\t{}", message);
4122    let iv = 0x_FEDCBA0987654321_u64;
4123    println!("IV =	{}", iv);
4124    let mut cipher = Vec::<u8>::new();
4125    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4126    print!("C =\t");
4127    for c in cipher.clone()
4128        { print!("{:02X} ", c); }
4129    println!();
4130    let mut txt = String::new();
4131    for c in cipher.clone()
4132        { write!(txt, "{:02X} ", c); }
4133    assert_eq!(txt, "");
4134
4135    let mut recovered = String::new();
4136    a_des.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
4137    println!("B =\t{}", recovered);
4138    assert_eq!(recovered, "");
4139    assert_eq!(recovered, message);
4140    println!();
4141
4142    // Normal case for the message shorter than 8 bytes
4143    let key = 0x_1234567890ABCDEF_u64;
4144    println!("K =\t{:#016X}", key);
4145    let mut a_des = DES::new_with_key_u64(key);
4146
4147    let message = "7 bytes";
4148    println!("M =\t{}", message);
4149    let iv = 0x_FEDCBA0987654321_u64;
4150    println!("IV =	{}", iv);
4151    let mut cipher = Vec::<u8>::new();
4152    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4153    print!("C =\t");
4154    for c in cipher.clone()
4155        { print!("{:02X} ", c); }
4156    println!();
4157    let mut txt = String::new();
4158    for c in cipher.clone()
4159        { write!(txt, "{:02X} ", c); }
4160    assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
4161
4162    let mut recovered = String::new();
4163    a_des.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
4164    println!("B =\t{}", recovered);
4165    assert_eq!(recovered, "7 bytes");
4166    assert_eq!(recovered, message);
4167    println!();
4168
4169    // Normal case for the message of 8 bytes
4170    let key = 0x_1234567890ABCDEF_u64;
4171    println!("K =\t{:#016X}", key);
4172    let mut a_des = DES::new_with_key_u64(key);
4173
4174    let message = "I am OK.";
4175    println!("M =\t{}", message);
4176    let iv = 0x_FEDCBA0987654321_u64;
4177    println!("IV =	{}", iv);
4178    let mut cipher = Vec::<u8>::new();
4179    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4180    print!("C =\t");
4181    for c in cipher.clone()
4182        { print!("{:02X} ", c); }
4183    println!();
4184    let mut txt = String::new();
4185    for c in cipher.clone()
4186        { write!(txt, "{:02X} ", c); }
4187    assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
4188
4189    let mut recovered = String::new();
4190    a_des.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
4191    println!("B =\t{}", recovered);
4192    assert_eq!(recovered, "I am OK.");
4193    assert_eq!(recovered, message);
4194    println!();
4195
4196    // Normal case for the message longer than 8 bytes and shorter than 16 bytes
4197    let key = 0x_1234567890ABCDEF_u64;
4198    println!("K =\t{:#016X}", key);
4199    let mut a_des = DES::new_with_key_u64(key);
4200
4201    let message = "PARK Youngho";
4202    println!("M =\t{}", message);
4203    let iv = 0x_FEDCBA0987654321_u64;
4204    println!("IV =	{}", iv);
4205    let mut cipher = Vec::<u8>::new();
4206    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4207    print!("C =\t");
4208    for c in cipher.clone()
4209        { print!("{:02X} ", c); }
4210    println!();
4211    let mut txt = String::new();
4212    for c in cipher.clone()
4213        { write!(txt, "{:02X} ", c); }
4214    assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 74 F9 B6 4F ");
4215
4216    let mut recovered = String::new();
4217    a_des.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
4218    println!("B =\t{}", recovered);
4219    assert_eq!(recovered, "PARK Youngho");
4220    assert_eq!(recovered, message);
4221    println!();
4222
4223    // Normal case for the message of 16 bytes
4224    let key = 0x_1234567890ABCDEF_u64;
4225    println!("K =\t{:#016X}", key);
4226    let mut a_des = DES::new_with_key_u64(key);
4227
4228    let message = "고맙습니다.";
4229    println!("M =\t{}", message);
4230    let iv = 0x_FEDCBA0987654321_u64;
4231    println!("IV =	{}", iv);
4232    let mut cipher = Vec::<u8>::new();
4233    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4234    print!("C =\t");
4235    for c in cipher.clone()
4236        { print!("{:02X} ", c); }
4237    println!();
4238    let mut txt = String::new();
4239    for c in cipher.clone()
4240        { write!(txt, "{:02X} ", c); }
4241    assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 F4 FC 94 B5 94 1F B9 BD ");
4242
4243    let mut recovered = String::new();
4244    a_des.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
4245    println!("B =\t{}", recovered);
4246    assert_eq!(recovered, "고맙습니다.");
4247    assert_eq!(recovered, message);
4248    println!("-------------------------------");
4249}
4250
4251fn des_decrypt_vec_cfb()
4252{
4253    println!("des_decrypt_vec_cfb()");
4254    use std::io::Write;
4255    use std::fmt::Write as _;
4256    use cryptocol::symmetric::{ DES, DES_Expanded, CFB};
4257
4258    // Normal case
4259    let key = 0x_1234567890ABCDEF_u64;
4260    println!("K =\t{:#016X}", key);
4261    let mut a_des = DES::new_with_key_u64(key);
4262
4263    let message = "In the beginning God created the heavens and the earth.";
4264    println!("M =\t{}", message);
4265    let iv = 0x_FEDCBA0987654321_u64;
4266    println!("IV =	{}", iv);
4267    let mut cipher = Vec::<u8>::new();
4268    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4269    print!("C (16 rounds) =\t");
4270    for c in cipher.clone()
4271        { print!("{:02X} ", c); }
4272    println!();
4273    let mut txt = String::new();
4274    for c in cipher.clone()
4275        { write!(txt, "{:02X} ", c); }
4276    assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 79 3A A1 78 EC CD 02 72 6A C4 41 7C 25 A4 2C 07 FC 77 25 49 12 55 0F 8A ED 44 C3 E4 DC 91 69 0F 40 72 7F F2 D9 B7 54 9F 36 91 C5 85 4F 9B 30 ");
4277
4278    let mut recovered = vec![0; 55];
4279    a_des.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
4280    print!("Ba (16 rounds) =\t");
4281    for b in recovered.clone()
4282        { print!("{:02X} ", b); }
4283    println!();
4284    let mut txt = String::new();
4285    for c in recovered.clone()
4286        { write!(txt, "{:02X} ", c); }
4287    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
4288
4289    let mut converted = String::new();
4290    unsafe { converted.as_mut_vec() }.append(&mut recovered);
4291    
4292    println!("Bb (16 rounds) =\t{}", converted);
4293    assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4294    assert_eq!(converted, message);
4295    println!();
4296
4297    // Expanded case for 128 rounds
4298    let key = 0x_1234567890ABCDEF_u64;
4299    println!("K =\t{:#016X}", key);
4300    let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
4301
4302    let message = "In the beginning God created the heavens and the earth.";
4303    println!("M =\t{}", message);
4304    let iv = 0x_FEDCBA0987654321_u64;
4305    println!("IV =	{}", iv);
4306    let mut cipher = Vec::<u8>::new();
4307    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4308    print!("C (128 rounds) =\t");
4309    for c in cipher.clone()
4310        { print!("{:02X} ", c); }
4311    println!();
4312    let mut txt = String::new();
4313    for c in cipher.clone()
4314        { write!(txt, "{:02X} ", c); }
4315    assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 F2 9A 70 44 D9 78 F5 E3 CF 55 0F EF BA F0 4A 7E BE 79 C9 B4 68 F4 99 09 48 93 00 D2 22 9D 29 6C 20 74 FF E3 E2 01 0C D3 7E 8D 4D 30 8F EC D6 ");
4316
4317    let mut recovered = vec![0; 55];
4318    a_des.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
4319    print!("Ba (128 rounds) =\t");
4320    for b in recovered.clone()
4321        { print!("{:02X} ", b); }
4322    println!();
4323    let mut txt = String::new();
4324    for c in recovered.clone()
4325        { write!(txt, "{:02X} ", c); }
4326    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
4327
4328    let mut converted = String::new();
4329    unsafe { converted.as_mut_vec() }.append(&mut recovered);
4330    
4331    println!("Bb (128 rounds) =\t{}", converted);
4332    assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4333    assert_eq!(converted, message);
4334    println!();
4335
4336    // Expanded case for 0 rounds which means that key is meaningless
4337    let key1 = 0x_1234567890ABCDEF_u64;
4338    let key2 = 0_u64;
4339    println!("K =\t{:#016X}", key);
4340    let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
4341    let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
4342
4343    let message = "In the beginning God created the heavens and the earth.";
4344    println!("M =\t{}", message);
4345    let iv = 0x_FEDCBA0987654321_u64;
4346    println!("IV =	{}", iv);
4347    let mut cipher1 = Vec::<u8>::new();
4348    let mut cipher2 = Vec::<u8>::new();
4349    c_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
4350    d_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
4351    print!("C (0 rounds) =\t");
4352    for c in cipher1.clone()
4353        { print!("{:02X} ", c); }
4354    println!();
4355    let mut txt = String::new();
4356    for c in cipher1.clone()
4357        { write!(txt, "{:02X} ", c); }
4358    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
4359    print!("D (0 rounds) =\t");
4360    for c in cipher2.clone()
4361        { print!("{:02X} ", c); }
4362    println!();
4363    let mut txt = String::new();
4364    for c in cipher2.clone()
4365        { write!(txt, "{:02X} ", c); }
4366    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
4367
4368    let mut recovered1 = vec![0; 55];
4369    let mut recovered2 = vec![0; 55];
4370    c_des.decrypt_vec(iv, &cipher1, recovered1.as_mut_ptr());
4371    d_des.decrypt_vec(iv, &cipher2, recovered2.as_mut_ptr());
4372    print!("B1a (0 rounds) =\t");
4373    for b in recovered1.clone()
4374        { print!("{:02X} ", b); }
4375    println!();
4376    let mut txt = String::new();
4377    for c in recovered1.clone()
4378        { write!(txt, "{:02X} ", c); }
4379    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
4380    print!("B2a (0 rounds) =\t");
4381    for b in recovered2.clone()
4382        { print!("{:02X} ", b); }
4383    println!();
4384    let mut txt = String::new();
4385    for c in recovered2.clone()
4386        { write!(txt, "{:02X} ", c); }
4387    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
4388
4389    let mut converted1 = String::new();
4390    let mut converted2 = String::new();
4391    unsafe { converted1.as_mut_vec() }.append(&mut recovered1);
4392    unsafe { converted2.as_mut_vec() }.append(&mut recovered2);
4393    
4394    println!("B1b (0 rounds) =\t{}", converted1);
4395    assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
4396    assert_eq!(converted1, message);
4397    println!("B2b (0 rounds) =\t{}", converted2);
4398    assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
4399    assert_eq!(converted2, message);
4400    assert_eq!(converted1, converted1);
4401    println!();
4402
4403    // Normal case for the message of 0 bytes
4404    let key = 0x_1234567890ABCDEF_u64;
4405    println!("K =\t{:#016X}", key);
4406    let mut a_des = DES::new_with_key_u64(key);
4407
4408    let message = "";
4409    println!("M =\t{}", message);
4410    let iv = 0x_FEDCBA0987654321_u64;
4411    println!("IV =	{}", iv);
4412    let mut cipher = Vec::<u8>::new();
4413    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4414    print!("C =\t");
4415    for c in cipher.clone()
4416        { print!("{:02X} ", c); }
4417    println!();
4418    let mut txt = String::new();
4419    for c in cipher.clone()
4420        { write!(txt, "{:02X} ", c); }
4421    assert_eq!(txt, "");
4422
4423    let mut recovered = vec![0; 8];
4424    let len = a_des.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
4425    print!("Ba =\t");
4426    for b in recovered.clone()
4427        { print!("{:02X} ", b); }
4428    println!();
4429    let mut txt = String::new();
4430    for c in recovered.clone()
4431        { write!(txt, "{:02X} ", c); }
4432    assert_eq!(txt, "00 00 00 00 00 00 00 00 ");
4433
4434    let mut converted = String::new();
4435    unsafe { converted.as_mut_vec() }.append(&mut recovered);
4436    converted.truncate(len as usize);
4437    
4438    println!("Bb =\t{}", converted);
4439    assert_eq!(converted, "");
4440    assert_eq!(converted, message);
4441    println!();
4442
4443    // Normal case for the message shorter than 8 bytes
4444    let key = 0x_1234567890ABCDEF_u64;
4445    println!("K =\t{:#016X}", key);
4446    let mut a_des = DES::new_with_key_u64(key);
4447
4448    let message = "7 bytes";
4449    println!("M =\t{}", message);
4450    let iv = 0x_FEDCBA0987654321_u64;
4451    println!("IV =	{}", iv);
4452    let mut cipher = Vec::<u8>::new();
4453    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4454    print!("C =\t");
4455    for c in cipher.clone()
4456        { print!("{:02X} ", c); }
4457    println!();
4458    let mut txt = String::new();
4459    for c in cipher.clone()
4460        { write!(txt, "{:02X} ", c); }
4461    assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
4462    
4463    let mut recovered = vec![0; 8];
4464    let len = a_des.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
4465    print!("Ba =\t");
4466    for b in recovered.clone()
4467        { print!("{:02X} ", b); }
4468    println!();
4469    let mut txt = String::new();
4470    for c in recovered.clone()
4471        { write!(txt, "{:02X} ", c); }
4472    assert_eq!(txt, "37 20 62 79 74 65 73 00 ");
4473
4474    let mut converted = String::new();
4475    unsafe { converted.as_mut_vec() }.append(&mut recovered);
4476    converted.truncate(len as usize);
4477
4478    println!("Bb =\t{}", converted);
4479    assert_eq!(converted, "7 bytes");
4480    assert_eq!(converted, message);
4481    println!();
4482
4483    // Normal case for the message of 8 bytes
4484    let key = 0x_1234567890ABCDEF_u64;
4485    println!("K =\t{:#016X}", key);
4486    let mut a_des = DES::new_with_key_u64(key);
4487
4488    let message = "I am OK.";
4489    println!("M =\t{}", message);
4490    let iv = 0x_FEDCBA0987654321_u64;
4491    println!("IV =	{}", iv);
4492    let mut cipher = Vec::<u8>::new();
4493    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4494    print!("C =\t");
4495    for c in cipher.clone()
4496        { print!("{:02X} ", c); }
4497    println!();
4498    let mut txt = String::new();
4499    for c in cipher.clone()
4500        { write!(txt, "{:02X} ", c); }
4501    assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
4502    
4503    let mut recovered = vec![0; 16];
4504    let len = a_des.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
4505    print!("Ba =\t");
4506    for b in recovered.clone()
4507        { print!("{:02X} ", b); }
4508    println!();
4509    let mut txt = String::new();
4510    for c in recovered.clone()
4511        { write!(txt, "{:02X} ", c); }
4512    assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E 00 00 00 00 00 00 00 00 ");
4513
4514    let mut converted = String::new();
4515    unsafe { converted.as_mut_vec() }.append(&mut recovered);
4516    converted.truncate(len as usize);
4517    
4518    println!("Bb =\t{}", converted);
4519    assert_eq!(converted, "I am OK.");
4520    assert_eq!(converted, message);
4521    println!();
4522
4523    // Normal case for the message longer than 8 bytes and shorter than 16 bytes
4524    let key = 0x_1234567890ABCDEF_u64;
4525    println!("K =\t{:#016X}", key);
4526    let mut a_des = DES::new_with_key_u64(key);
4527
4528    let message = "PARK Youngho";
4529    println!("M =\t{}", message);
4530    let iv = 0x_FEDCBA0987654321_u64;
4531    println!("IV =	{}", iv);
4532    let mut cipher = Vec::<u8>::new();
4533    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4534    print!("C =\t");
4535    for c in cipher.clone()
4536        { print!("{:02X} ", c); }
4537    println!();
4538    let mut txt = String::new();
4539    for c in cipher.clone()
4540        { write!(txt, "{:02X} ", c); }
4541    assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 74 F9 B6 4F ");
4542
4543    let mut recovered = vec![0; 16];
4544    let len = a_des.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
4545    print!("Ba =\t");
4546    for b in recovered.clone()
4547        { print!("{:02X} ", b); }
4548    println!();
4549    let mut txt = String::new();
4550    for c in recovered.clone()
4551        { write!(txt, "{:02X} ", c); }
4552    assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F 00 00 00 00 ");
4553
4554    let mut converted = String::new();
4555    unsafe { converted.as_mut_vec() }.append(&mut recovered);
4556    converted.truncate(len as usize);
4557    
4558    println!("Bb =\t{}", converted);
4559    assert_eq!(converted, "PARK Youngho");
4560    assert_eq!(converted, message);
4561    println!();
4562
4563    // Normal case for the message of 16 bytes
4564    let key = 0x_1234567890ABCDEF_u64;
4565    println!("K =\t{:#016X}", key);
4566    let mut a_des = DES::new_with_key_u64(key);
4567
4568    let message = "고맙습니다.";
4569    println!("M =\t{}", message);
4570    let iv = 0x_FEDCBA0987654321_u64;
4571    println!("IV =	{}", iv);
4572    let mut cipher = Vec::<u8>::new();
4573    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4574    print!("C =\t");
4575    for c in cipher.clone()
4576        { print!("{:02X} ", c); }
4577    println!();
4578    let mut txt = String::new();
4579    for c in cipher.clone()
4580        { write!(txt, "{:02X} ", c); }
4581    assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 F4 FC 94 B5 94 1F B9 BD ");
4582
4583    let mut recovered = vec![0; 24];
4584    let len = a_des.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
4585    print!("Ba =\t");
4586    for b in recovered.clone()
4587        { print!("{:02X} ", b); }
4588    println!();
4589    let mut txt = String::new();
4590    for c in recovered.clone()
4591        { write!(txt, "{:02X} ", c); }
4592    assert_eq!(txt, "EA B3 A0 EB A7 99 EC 8A B5 EB 8B 88 EB 8B A4 2E 00 00 00 00 00 00 00 00 ");
4593
4594    let mut converted = String::new();
4595    unsafe { converted.as_mut_vec() }.append(&mut recovered);
4596    converted.truncate(len as usize);
4597    
4598    println!("Bb =\t{}", converted);
4599    assert_eq!(converted, "고맙습니다.");
4600    assert_eq!(converted, message);
4601    println!("-------------------------------");
4602}
4603
4604fn des_decrypt_vec_cfb_into_vec()
4605{
4606    println!("des_decrypt_vec_cfb_into_vec()");
4607    use std::io::Write;
4608    use std::fmt::Write as _;
4609    use cryptocol::symmetric::{ DES, DES_Expanded, CFB};
4610
4611    // Normal case
4612    let key = 0x_1234567890ABCDEF_u64;
4613    println!("K =\t{:#016X}", key);
4614    let mut a_des = DES::new_with_key_u64(key);
4615
4616    let message = "In the beginning God created the heavens and the earth.";
4617    println!("M =\t{}", message);
4618    let iv = 0x_FEDCBA0987654321_u64;
4619    println!("IV =	{}", iv);
4620    let mut cipher = Vec::<u8>::new();
4621    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4622    print!("C (16 rounds) =\t");
4623    for c in cipher.clone()
4624        { print!("{:02X} ", c); }
4625    println!();
4626    let mut txt = String::new();
4627    for c in cipher.clone()
4628        { write!(txt, "{:02X} ", c); }
4629    assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 79 3A A1 78 EC CD 02 72 6A C4 41 7C 25 A4 2C 07 FC 77 25 49 12 55 0F 8A ED 44 C3 E4 DC 91 69 0F 40 72 7F F2 D9 B7 54 9F 36 91 C5 85 4F 9B 30 ");
4630
4631    let mut recovered = Vec::<u8>::new();
4632    a_des.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
4633    print!("Ba (16 rounds) =\t");
4634    for b in recovered.clone()
4635        { print!("{:02X} ", b); }
4636    println!();
4637    let mut txt = String::new();
4638    for c in recovered.clone()
4639        { write!(txt, "{:02X} ", c); }
4640    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
4641
4642    let mut converted = String::new();
4643    unsafe { converted.as_mut_vec() }.append(&mut recovered);
4644    
4645    println!("Bb (16 rounds) =\t{}", converted);
4646    assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4647    assert_eq!(converted, message);
4648    println!();
4649
4650    // Expanded case for 128 rounds
4651    let key = 0x_1234567890ABCDEF_u64;
4652    println!("K =\t{:#016X}", key);
4653    let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
4654
4655    let message = "In the beginning God created the heavens and the earth.";
4656    println!("M =\t{}", message);
4657    let iv = 0x_FEDCBA0987654321_u64;
4658    println!("IV =	{}", iv);
4659    let mut cipher = Vec::<u8>::new();
4660    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4661    print!("C (128 rounds) =\t");
4662    for c in cipher.clone()
4663        { print!("{:02X} ", c); }
4664    println!();
4665    let mut txt = String::new();
4666    for c in cipher.clone()
4667        { write!(txt, "{:02X} ", c); }
4668    assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 F2 9A 70 44 D9 78 F5 E3 CF 55 0F EF BA F0 4A 7E BE 79 C9 B4 68 F4 99 09 48 93 00 D2 22 9D 29 6C 20 74 FF E3 E2 01 0C D3 7E 8D 4D 30 8F EC D6 ");
4669
4670    let mut recovered = Vec::<u8>::new();
4671    a_des.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
4672    print!("Ba (128 rounds) =\t");
4673    for b in recovered.clone()
4674        { print!("{:02X} ", b); }
4675    println!();
4676    let mut txt = String::new();
4677    for c in recovered.clone()
4678        { write!(txt, "{:02X} ", c); }
4679    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
4680
4681    let mut converted = String::new();
4682    unsafe { converted.as_mut_vec() }.append(&mut recovered);
4683    
4684    println!("Bb (128 rounds) =\t{}", converted);
4685    assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4686    assert_eq!(converted, message);
4687    println!();
4688
4689    // Expanded case for 0 rounds which means that key is meaningless
4690    let key1 = 0x_1234567890ABCDEF_u64;
4691    let key2 = 0_u64;
4692    println!("K =\t{:#016X}", key);
4693    let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
4694    let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
4695
4696    let message = "In the beginning God created the heavens and the earth.";
4697    println!("M =\t{}", message);
4698    let iv = 0x_FEDCBA0987654321_u64;
4699    println!("IV =	{}", iv);
4700    let mut cipher1 = Vec::<u8>::new();
4701    let mut cipher2 = Vec::<u8>::new();
4702    c_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
4703    d_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
4704    print!("C (0 rounds) =\t");
4705    for c in cipher1.clone()
4706        { print!("{:02X} ", c); }
4707    println!();
4708    let mut txt = String::new();
4709    for c in cipher1.clone()
4710        { write!(txt, "{:02X} ", c); }
4711    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
4712    print!("D (0 rounds) =\t");
4713    for c in cipher2.clone()
4714        { print!("{:02X} ", c); }
4715    println!();
4716    let mut txt = String::new();
4717    for c in cipher2.clone()
4718        { write!(txt, "{:02X} ", c); }
4719    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
4720
4721    let mut recovered1 = Vec::<u8>::new();
4722    let mut recovered2 = Vec::<u8>::new();
4723    c_des.decrypt_vec_into_vec(iv, &cipher1, &mut recovered1);
4724    d_des.decrypt_vec_into_vec(iv, &cipher2, &mut recovered2);
4725    print!("B1a (0 rounds) =\t");
4726    for b in recovered1.clone()
4727        { print!("{:02X} ", b); }
4728    println!();
4729    let mut txt = String::new();
4730    for c in recovered1.clone()
4731        { write!(txt, "{:02X} ", c); }
4732    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
4733    print!("B2a (0 rounds) =\t");
4734    for b in recovered2.clone()
4735        { print!("{:02X} ", b); }
4736    println!();
4737    let mut txt = String::new();
4738    for c in recovered2.clone()
4739        { write!(txt, "{:02X} ", c); }
4740    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
4741
4742    let mut converted1 = String::new();
4743    let mut converted2 = String::new();
4744    unsafe { converted1.as_mut_vec() }.append(&mut recovered1);
4745    unsafe { converted2.as_mut_vec() }.append(&mut recovered2);
4746    
4747    println!("B1b (0 rounds) =\t{}", converted1);
4748    assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
4749    assert_eq!(converted1, message);
4750    println!("B2b (0 rounds) =\t{}", converted2);
4751    assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
4752    assert_eq!(converted2, message);
4753    assert_eq!(converted1, converted1);
4754    println!();
4755
4756    // Normal case for the message of 0 bytes
4757    let key = 0x_1234567890ABCDEF_u64;
4758    println!("K =\t{:#016X}", key);
4759    let mut a_des = DES::new_with_key_u64(key);
4760
4761    let message = "";
4762    println!("M =\t{}", message);
4763    let iv = 0x_FEDCBA0987654321_u64;
4764    println!("IV =	{}", iv);
4765    let mut cipher = Vec::<u8>::new();
4766    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4767    print!("C =\t");
4768    for c in cipher.clone()
4769        { print!("{:02X} ", c); }
4770    println!();
4771    let mut txt = String::new();
4772    for c in cipher.clone()
4773        { write!(txt, "{:02X} ", c); }
4774    assert_eq!(txt, "");
4775
4776    let mut recovered = Vec::<u8>::new();
4777    a_des.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
4778    print!("Ba =\t");
4779    for b in recovered.clone()
4780        { print!("{:02X} ", b); }
4781    println!();
4782    let mut txt = String::new();
4783    for c in recovered.clone()
4784        { write!(txt, "{:02X} ", c); }
4785    assert_eq!(txt, "");
4786
4787    let mut converted = String::new();
4788    unsafe { converted.as_mut_vec() }.append(&mut recovered);
4789    
4790    println!("Bb =\t{}", converted);
4791    assert_eq!(converted, "");
4792    assert_eq!(converted, message);
4793    println!();
4794
4795    // Normal case for the message shorter than 8 bytes
4796    let key = 0x_1234567890ABCDEF_u64;
4797    println!("K =\t{:#016X}", key);
4798    let mut a_des = DES::new_with_key_u64(key);
4799
4800    let message = "7 bytes";
4801    println!("M =\t{}", message);
4802    let iv = 0x_FEDCBA0987654321_u64;
4803    println!("IV =	{}", iv);
4804    let mut cipher = Vec::<u8>::new();
4805    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4806    print!("C =\t");
4807    for c in cipher.clone()
4808        { print!("{:02X} ", c); }
4809    println!();
4810    let mut txt = String::new();
4811    for c in cipher.clone()
4812        { write!(txt, "{:02X} ", c); }
4813    assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
4814    
4815    let mut recovered = Vec::<u8>::new();
4816    a_des.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
4817    print!("Ba =\t");
4818    for b in recovered.clone()
4819        { print!("{:02X} ", b); }
4820    println!();
4821    let mut txt = String::new();
4822    for c in recovered.clone()
4823        { write!(txt, "{:02X} ", c); }
4824    assert_eq!(txt, "37 20 62 79 74 65 73 ");
4825
4826    let mut converted = String::new();
4827    unsafe { converted.as_mut_vec() }.append(&mut recovered);
4828    
4829    println!("Bb =\t{}", converted);
4830    assert_eq!(converted, "7 bytes");
4831    assert_eq!(converted, message);
4832    println!();
4833
4834    // Normal case for the message of 8 bytes
4835    let key = 0x_1234567890ABCDEF_u64;
4836    println!("K =\t{:#016X}", key);
4837    let mut a_des = DES::new_with_key_u64(key);
4838
4839    let message = "I am OK.";
4840    println!("M =\t{}", message);
4841    let iv = 0x_FEDCBA0987654321_u64;
4842    println!("IV =	{}", iv);
4843    let mut cipher = Vec::<u8>::new();
4844    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4845    print!("C =\t");
4846    for c in cipher.clone()
4847        { print!("{:02X} ", c); }
4848    println!();
4849    let mut txt = String::new();
4850    for c in cipher.clone()
4851        { write!(txt, "{:02X} ", c); }
4852    assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
4853    
4854    let mut recovered = Vec::<u8>::new();
4855    a_des.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
4856    print!("Ba =\t");
4857    for b in recovered.clone()
4858        { print!("{:02X} ", b); }
4859    println!();
4860    let mut txt = String::new();
4861    for c in recovered.clone()
4862        { write!(txt, "{:02X} ", c); }
4863    assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E ");
4864
4865    let mut converted = String::new();
4866    unsafe { converted.as_mut_vec() }.append(&mut recovered);
4867    
4868    println!("Bb =\t{}", converted);
4869    assert_eq!(converted, "I am OK.");
4870    assert_eq!(converted, message);
4871    println!();
4872
4873    // Normal case for the message longer than 8 bytes and shorter than 16 bytes
4874    let key = 0x_1234567890ABCDEF_u64;
4875    println!("K =\t{:#016X}", key);
4876    let mut a_des = DES::new_with_key_u64(key);
4877
4878    let message = "PARK Youngho";
4879    println!("M =\t{}", message);
4880    let iv = 0x_FEDCBA0987654321_u64;
4881    println!("IV =	{}", iv);
4882    let mut cipher = Vec::<u8>::new();
4883    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4884    print!("C =\t");
4885    for c in cipher.clone()
4886        { print!("{:02X} ", c); }
4887    println!();
4888    let mut txt = String::new();
4889    for c in cipher.clone()
4890        { write!(txt, "{:02X} ", c); }
4891    assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 74 F9 B6 4F ");
4892
4893    let mut recovered = Vec::<u8>::new();
4894    a_des.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
4895    print!("Ba =\t");
4896    for b in recovered.clone()
4897        { print!("{:02X} ", b); }
4898    println!();
4899    let mut txt = String::new();
4900    for c in recovered.clone()
4901        { write!(txt, "{:02X} ", c); }
4902    assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F ");
4903
4904    let mut converted = String::new();
4905    unsafe { converted.as_mut_vec() }.append(&mut recovered);
4906    
4907    println!("Bb =\t{}", converted);
4908    assert_eq!(converted, "PARK Youngho");
4909    assert_eq!(converted, message);
4910    println!();
4911
4912    // Normal case for the message of 16 bytes
4913    let key = 0x_1234567890ABCDEF_u64;
4914    println!("K =\t{:#016X}", key);
4915    let mut a_des = DES::new_with_key_u64(key);
4916
4917    let message = "고맙습니다.";
4918    println!("M =\t{}", message);
4919    let iv = 0x_FEDCBA0987654321_u64;
4920    println!("IV =	{}", iv);
4921    let mut cipher = Vec::<u8>::new();
4922    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4923    print!("C =\t");
4924    for c in cipher.clone()
4925        { print!("{:02X} ", c); }
4926    println!();
4927    let mut txt = String::new();
4928    for c in cipher.clone()
4929        { write!(txt, "{:02X} ", c); }
4930    assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 F4 FC 94 B5 94 1F B9 BD ");
4931
4932    let mut recovered = Vec::<u8>::new();
4933    a_des.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
4934    print!("Ba =\t");
4935    for b in recovered.clone()
4936        { print!("{:02X} ", b); }
4937    println!();
4938    let mut txt = String::new();
4939    for c in recovered.clone()
4940        { write!(txt, "{:02X} ", c); }
4941    assert_eq!(txt, "EA B3 A0 EB A7 99 EC 8A B5 EB 8B 88 EB 8B A4 2E ");
4942
4943    let mut converted = String::new();
4944    unsafe { converted.as_mut_vec() }.append(&mut recovered);
4945    
4946    println!("Bb =\t{}", converted);
4947    assert_eq!(converted, "고맙습니다.");
4948    assert_eq!(converted, message);
4949    println!("-------------------------------");
4950}
4951
4952fn des_decrypt_vec_cfb_into_array()
4953{
4954    println!("des_decrypt_vec_cfb_into_array()");
4955    use std::io::Write;
4956    use std::fmt::Write as _;
4957    use cryptocol::symmetric::{ DES, DES_Expanded, CFB};
4958
4959    // Normal case
4960    let key = 0x_1234567890ABCDEF_u64;
4961    println!("K =\t{:#016X}", key);
4962    let mut a_des = DES::new_with_key_u64(key);
4963
4964    let message = "In the beginning God created the heavens and the earth.";
4965    println!("M =\t{}", message);
4966    let iv = 0x_FEDCBA0987654321_u64;
4967    println!("IV =	{}", iv);
4968    let mut cipher = Vec::<u8>::new();
4969    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4970    print!("C (16 rounds) =\t");
4971    for c in cipher.clone()
4972        { print!("{:02X} ", c); }
4973    println!();
4974    let mut txt = String::new();
4975    for c in cipher.clone()
4976        { write!(txt, "{:02X} ", c); }
4977    assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 79 3A A1 78 EC CD 02 72 6A C4 41 7C 25 A4 2C 07 FC 77 25 49 12 55 0F 8A ED 44 C3 E4 DC 91 69 0F 40 72 7F F2 D9 B7 54 9F 36 91 C5 85 4F 9B 30 ");
4978
4979    let mut recovered = [0u8; 56];
4980    let len = a_des.decrypt_vec_into_array(iv, &cipher, &mut recovered);
4981    print!("Ba (16 rounds) =\t");
4982    for b in recovered.clone()
4983        { print!("{:02X} ", b); }
4984    println!();
4985    let mut txt = String::new();
4986    for c in recovered.clone()
4987        { write!(txt, "{:02X} ", c); }
4988    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 ");
4989
4990    let mut converted = String::new();
4991    unsafe { converted.as_mut_vec() }.write(&recovered);
4992    unsafe { converted.as_mut_vec() }.truncate(len as usize);
4993    println!("Bb (16 rounds) =\t{}", converted);
4994    assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4995    assert_eq!(converted, message);
4996    println!();
4997
4998    // Expanded case for 128 rounds
4999    let key = 0x_1234567890ABCDEF_u64;
5000    println!("K =\t{:#016X}", key);
5001    let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
5002
5003    let message = "In the beginning God created the heavens and the earth.";
5004    println!("M =\t{}", message);
5005    let iv = 0x_FEDCBA0987654321_u64;
5006    println!("IV =	{}", iv);
5007    let mut cipher = Vec::<u8>::new();
5008    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5009    print!("C (128 rounds) =\t");
5010    for c in cipher.clone()
5011        { print!("{:02X} ", c); }
5012    println!();
5013    let mut txt = String::new();
5014    for c in cipher.clone()
5015        { write!(txt, "{:02X} ", c); }
5016    assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 F2 9A 70 44 D9 78 F5 E3 CF 55 0F EF BA F0 4A 7E BE 79 C9 B4 68 F4 99 09 48 93 00 D2 22 9D 29 6C 20 74 FF E3 E2 01 0C D3 7E 8D 4D 30 8F EC D6 ");
5017
5018    let mut recovered = [0u8; 56];
5019    let len = a_des.decrypt_vec_into_array(iv, &cipher, &mut recovered);
5020    print!("Ba (16 rounds) =\t");
5021    for b in recovered.clone()
5022        { print!("{:02X} ", b); }
5023    println!();
5024    let mut txt = String::new();
5025    for c in recovered.clone()
5026        { write!(txt, "{:02X} ", c); }
5027    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 ");
5028
5029    let mut converted = String::new();
5030    unsafe { converted.as_mut_vec() }.write(&recovered);
5031    unsafe { converted.as_mut_vec() }.truncate(len as usize);
5032    println!("Bb (16 rounds) =\t{}", converted);
5033    assert_eq!(converted, "In the beginning God created the heavens and the earth.");
5034    assert_eq!(converted, message);
5035    println!();
5036
5037    // Expanded case for 0 rounds which means that key is meaningless
5038    let key1 = 0x_1234567890ABCDEF_u64;
5039    let key2 = 0_u64;
5040    println!("K =\t{:#016X}", key);
5041    let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
5042    let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
5043
5044    let message = "In the beginning God created the heavens and the earth.";
5045    println!("M =\t{}", message);
5046    let iv = 0x_FEDCBA0987654321_u64;
5047    println!("IV =	{}", iv);
5048    let mut cipher1 = Vec::<u8>::new();
5049    let mut cipher2 = Vec::<u8>::new();
5050    c_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
5051    d_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
5052    print!("C (0 rounds) =\t");
5053    for c in cipher1.clone()
5054        { print!("{:02X} ", c); }
5055    println!();
5056    let mut txt = String::new();
5057    for c in cipher1.clone()
5058        { write!(txt, "{:02X} ", c); }
5059    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
5060    print!("D (0 rounds) =\t");
5061    for c in cipher2.clone()
5062        { print!("{:02X} ", c); }
5063    println!();
5064    let mut txt = String::new();
5065    for c in cipher2.clone()
5066        { write!(txt, "{:02X} ", c); }
5067    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
5068
5069    let mut recovered1 = [0u8; 56];
5070    let mut recovered2 = [0u8; 56];
5071    let len1 = c_des.decrypt_vec_into_array(iv, &cipher1, &mut recovered1);
5072    let len2 = d_des.decrypt_vec_into_array(iv, &cipher2, &mut recovered2);
5073    print!("B1a (0 rounds) =\t");
5074    for b in recovered1.clone()
5075        { print!("{:02X} ", b); }
5076    println!();
5077    let mut txt = String::new();
5078    for c in recovered1.clone()
5079        { write!(txt, "{:02X} ", c); }
5080    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 ");
5081    print!("B2a (0 rounds) =\t");
5082    for b in recovered2.clone()
5083        { print!("{:02X} ", b); }
5084    println!();
5085    let mut txt = String::new();
5086    for c in recovered.clone()
5087        { write!(txt, "{:02X} ", c); }
5088    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 ");
5089
5090    let mut converted1 = String::new();
5091    let mut converted2 = String::new();
5092    unsafe { converted1.as_mut_vec() }.write(&recovered1);
5093    unsafe { converted2.as_mut_vec() }.write(&recovered2);
5094    unsafe { converted1.as_mut_vec() }.truncate(len1 as usize);
5095    unsafe { converted2.as_mut_vec() }.truncate(len2 as usize);
5096    println!("B1b (0 rounds) =\t{}", converted1);
5097    println!("B2b (0 rounds) =\t{}", converted2);
5098    assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
5099    assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
5100    assert_eq!(converted1, message);
5101    assert_eq!(converted2, message);
5102    assert_eq!(converted1, converted2);
5103    println!();
5104
5105    // Normal case for the message of 0 bytes
5106    let key = 0x_1234567890ABCDEF_u64;
5107    println!("K =\t{:#016X}", key);
5108    let mut a_des = DES::new_with_key_u64(key);
5109
5110    let message = "";
5111    println!("M =\t{}", message);
5112    let iv = 0x_FEDCBA0987654321_u64;
5113    println!("IV =	{}", iv);
5114    let mut cipher = Vec::<u8>::new();
5115    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5116    print!("C =\t");
5117    for c in cipher.clone()
5118        { print!("{:02X} ", c); }
5119    println!();
5120    let mut txt = String::new();
5121    for c in cipher.clone()
5122        { write!(txt, "{:02X} ", c); }
5123    assert_eq!(txt, "");
5124
5125    let mut recovered = [0u8; 8];
5126    let len = a_des.decrypt_vec_into_array(iv, &cipher, &mut recovered);
5127
5128    print!("Ba =\t");
5129    for b in recovered.clone()
5130        { print!("{:02X} ", b); }
5131    println!();
5132    let mut txt = String::new();
5133    for c in recovered.clone()
5134        { write!(txt, "{:02X} ", c); }
5135    assert_eq!(txt, "00 00 00 00 00 00 00 00 ");
5136
5137    let mut converted = String::new();
5138    unsafe { converted.as_mut_vec() }.write(&recovered);
5139    unsafe { converted.as_mut_vec() }.truncate(len as usize);
5140    println!("Bb =\t{}", converted);
5141    assert_eq!(converted, "");
5142    assert_eq!(converted, message);
5143    println!();
5144
5145    // Normal case for the message shorter than 8 bytes
5146    let key = 0x_1234567890ABCDEF_u64;
5147    println!("K =\t{:#016X}", key);
5148    let mut a_des = DES::new_with_key_u64(key);
5149
5150    let message = "7 bytes";
5151    println!("M =\t{}", message);
5152    let iv = 0x_FEDCBA0987654321_u64;
5153    println!("IV =	{}", iv);
5154    let mut cipher = Vec::<u8>::new();
5155    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5156    print!("C =\t");
5157    for c in cipher.clone()
5158        { print!("{:02X} ", c); }
5159    println!();
5160    let mut txt = String::new();
5161    for c in cipher.clone()
5162        { write!(txt, "{:02X} ", c); }
5163    assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
5164
5165    let mut recovered = [0u8; 8];
5166    let len = a_des.decrypt_vec_into_array(iv, &cipher, &mut recovered);
5167
5168    print!("Ba =\t");
5169    for b in recovered.clone()
5170        { print!("{:02X} ", b); }
5171    println!();
5172    let mut txt = String::new();
5173    for c in recovered.clone()
5174        { write!(txt, "{:02X} ", c); }
5175    assert_eq!(txt, "37 20 62 79 74 65 73 00 ");
5176
5177    let mut converted = String::new();
5178    unsafe { converted.as_mut_vec() }.write(&recovered);
5179    unsafe { converted.as_mut_vec() }.truncate(len as usize);
5180    println!("Bb =\t{}", converted);
5181    assert_eq!(converted, "7 bytes");
5182    assert_eq!(converted, message);
5183    println!();
5184
5185    // Normal case for the message of 8 bytes
5186    let key = 0x_1234567890ABCDEF_u64;
5187    println!("K =\t{:#016X}", key);
5188    let mut a_des = DES::new_with_key_u64(key);
5189
5190    let message = "I am OK.";
5191    println!("M =\t{}", message);
5192    let iv = 0x_FEDCBA0987654321_u64;
5193    println!("IV =	{}", iv);
5194    let mut cipher = Vec::<u8>::new();
5195    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5196    print!("C =\t");
5197    for c in cipher.clone()
5198        { print!("{:02X} ", c); }
5199    println!();
5200    let mut txt = String::new();
5201    for c in cipher.clone()
5202        { write!(txt, "{:02X} ", c); }
5203    assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
5204
5205    let mut recovered = [0u8; 16];
5206    let len = a_des.decrypt_vec_into_array(iv, &cipher, &mut recovered);
5207
5208    print!("Ba =\t");
5209    for b in recovered.clone()
5210        { print!("{:02X} ", b); }
5211    println!();
5212    let mut txt = String::new();
5213    for c in recovered.clone()
5214        { write!(txt, "{:02X} ", c); }
5215    assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E 00 00 00 00 00 00 00 00 ");
5216
5217    let mut converted = String::new();
5218    unsafe { converted.as_mut_vec() }.write(&recovered);
5219    unsafe { converted.as_mut_vec() }.truncate(len as usize);
5220    println!("Bb =\t{}", converted);
5221    assert_eq!(converted, "I am OK.");
5222    assert_eq!(converted, message);
5223    println!();
5224
5225    // Normal case for the message longer than 8 bytes and shorter than 16 bytes
5226    let key = 0x_1234567890ABCDEF_u64;
5227    println!("K =\t{:#016X}", key);
5228    let mut a_des = DES::new_with_key_u64(key);
5229
5230    let message = "PARK Youngho";
5231    println!("M =\t{}", message);
5232    let iv = 0x_FEDCBA0987654321_u64;
5233    println!("IV =	{}", iv);
5234    let mut cipher = Vec::<u8>::new();
5235    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5236    print!("C =\t");
5237    for c in cipher.clone()
5238        { print!("{:02X} ", c); }
5239    println!();
5240    let mut txt = String::new();
5241    for c in cipher.clone()
5242        { write!(txt, "{:02X} ", c); }
5243    assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 74 F9 B6 4F ");
5244
5245    let mut recovered = [0u8; 16];
5246    let len = a_des.decrypt_vec_into_array(iv, &cipher, &mut recovered);
5247
5248    print!("Ba =\t");
5249    for b in recovered.clone()
5250        { print!("{:02X} ", b); }
5251    println!();
5252    let mut txt = String::new();
5253    for c in recovered.clone()
5254        { write!(txt, "{:02X} ", c); }
5255    assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F 00 00 00 00 ");
5256
5257    let mut converted = String::new();
5258    unsafe { converted.as_mut_vec() }.write(&recovered);
5259    unsafe { converted.as_mut_vec() }.truncate(len as usize);
5260    println!("Bb =\t{}", converted);
5261    assert_eq!(converted, "PARK Youngho");
5262    assert_eq!(converted, message);
5263    println!();
5264
5265    // Normal case for the message of 16 bytes
5266    let key = 0x_1234567890ABCDEF_u64;
5267    println!("K =\t{:#016X}", key);
5268    let mut a_des = DES::new_with_key_u64(key);
5269
5270    let message = "고맙습니다.";
5271    println!("M =\t{}", message);
5272    let iv = 0x_FEDCBA0987654321_u64;
5273    println!("IV =	{}", iv);
5274    let mut cipher = Vec::<u8>::new();
5275    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5276    print!("C =\t");
5277    for c in cipher.clone()
5278        { print!("{:02X} ", c); }
5279    println!();
5280    let mut txt = String::new();
5281    for c in cipher.clone()
5282        { write!(txt, "{:02X} ", c); }
5283    assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 F4 FC 94 B5 94 1F B9 BD ");
5284
5285    let mut recovered = [0u8; 24];
5286    let len = a_des.decrypt_vec_into_array(iv, &cipher, &mut recovered);
5287
5288    print!("Ba =\t");
5289    for b in recovered.clone()
5290        { print!("{:02X} ", b); }
5291    println!();
5292    let mut txt = String::new();
5293    for c in recovered.clone()
5294        { write!(txt, "{:02X} ", c); }
5295    assert_eq!(txt, "EA B3 A0 EB A7 99 EC 8A B5 EB 8B 88 EB 8B A4 2E 00 00 00 00 00 00 00 00 ");
5296
5297    let mut converted = String::new();
5298    unsafe { converted.as_mut_vec() }.write(&recovered);
5299    unsafe { converted.as_mut_vec() }.truncate(len as usize);
5300    println!("Bb =\t{}", converted);
5301    assert_eq!(converted, "고맙습니다.");
5302    assert_eq!(converted, message);
5303    println!("-------------------------------");
5304}
5305
5306fn des_decrypt_vec_cfb_into_string()
5307{
5308    println!("des_decrypt_vec_cfb_into_string()");
5309    use std::io::Write;
5310    use std::fmt::Write as _;
5311    use cryptocol::symmetric::{ DES, DES_Expanded, CFB};
5312
5313    // Normal case
5314    let key = 0x_1234567890ABCDEF_u64;
5315    println!("K =\t{:#016X}", key);
5316    let mut a_des = DES::new_with_key_u64(key);
5317
5318    let message = "In the beginning God created the heavens and the earth.";
5319    println!("M =\t{}", message);
5320    let iv = 0x_FEDCBA0987654321_u64;
5321    println!("IV =	{}", iv);
5322    let mut cipher = Vec::<u8>::new();
5323    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5324    print!("C (16 rounds) =\t");
5325    for c in cipher.clone()
5326        { print!("{:02X} ", c); }
5327    println!();
5328    let mut txt = String::new();
5329    for c in cipher.clone()
5330        { write!(txt, "{:02X} ", c); }
5331    assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 79 3A A1 78 EC CD 02 72 6A C4 41 7C 25 A4 2C 07 FC 77 25 49 12 55 0F 8A ED 44 C3 E4 DC 91 69 0F 40 72 7F F2 D9 B7 54 9F 36 91 C5 85 4F 9B 30 ");
5332
5333    let mut recovered = String::new();
5334    a_des.decrypt_vec_into_string(iv, &cipher, &mut recovered);
5335    println!("B (16 rounds) =\t{}", recovered);
5336    assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
5337    assert_eq!(recovered, message);
5338    println!();
5339
5340    // Expanded case for 128 rounds
5341    let key = 0x_1234567890ABCDEF_u64;
5342    println!("K =\t{:#016X}", key);
5343    let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
5344
5345    let message = "In the beginning God created the heavens and the earth.";
5346    println!("M =\t{}", message);
5347    let iv = 0x_FEDCBA0987654321_u64;
5348    println!("IV =	{}", iv);
5349    let mut cipher = Vec::<u8>::new();
5350    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5351    print!("C (128 rounds) =\t");
5352    for c in cipher.clone()
5353        { print!("{:02X} ", c); }
5354    println!();
5355    let mut txt = String::new();
5356    for c in cipher.clone()
5357        { write!(txt, "{:02X} ", c); }
5358    assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 F2 9A 70 44 D9 78 F5 E3 CF 55 0F EF BA F0 4A 7E BE 79 C9 B4 68 F4 99 09 48 93 00 D2 22 9D 29 6C 20 74 FF E3 E2 01 0C D3 7E 8D 4D 30 8F EC D6 ");
5359
5360    let mut recovered = String::new();
5361    a_des.decrypt_vec_into_string(iv, &cipher, &mut recovered);
5362    println!("B (128 rounds) =\t{}", recovered);
5363    assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
5364    assert_eq!(recovered, message);
5365    println!();
5366
5367    // Expanded case for 0 rounds which means that key is meaningless
5368    let key1 = 0x_1234567890ABCDEF_u64;
5369    let key2 = 0_u64;
5370    println!("K =\t{:#016X}", key);
5371    let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
5372    let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
5373
5374    let message = "In the beginning God created the heavens and the earth.";
5375    println!("M =\t{}", message);
5376    let iv = 0x_FEDCBA0987654321_u64;
5377    println!("IV =	{}", iv);
5378    let mut cipher1 = Vec::<u8>::new();
5379    let mut cipher2 = Vec::<u8>::new();
5380    c_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
5381    d_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
5382    print!("C (0 rounds) =\t");
5383    for c in cipher1.clone()
5384        { print!("{:02X} ", c); }
5385    println!();
5386    let mut txt = String::new();
5387    for c in cipher1.clone()
5388        { write!(txt, "{:02X} ", c); }
5389    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
5390    print!("D (0 rounds) =\t");
5391    for c in cipher2.clone()
5392        { print!("{:02X} ", c); }
5393    println!();
5394    let mut txt = String::new();
5395    for c in cipher2.clone()
5396        { write!(txt, "{:02X} ", c); }
5397    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
5398
5399    let mut recovered1 = String::new();
5400    let mut recovered2 = String::new();
5401    c_des.decrypt_vec_into_string(iv, &cipher1, &mut recovered1);
5402    d_des.decrypt_vec_into_string(iv, &cipher2, &mut recovered2);
5403    println!("B1 (0 rounds) =\t{}", recovered1);
5404    println!("B2 (0 rounds) =\t{}", recovered2);
5405    assert_eq!(recovered1, "In the beginning God created the heavens and the earth.");
5406    assert_eq!(recovered2, "In the beginning God created the heavens and the earth.");
5407    assert_eq!(recovered1, message);
5408    assert_eq!(recovered2, message);
5409    assert_eq!(recovered1, recovered2);
5410    println!();
5411
5412    // Normal case for the message of 0 bytes
5413    let key = 0x_1234567890ABCDEF_u64;
5414    println!("K =\t{:#016X}", key);
5415    let mut a_des = DES::new_with_key_u64(key);
5416
5417    let message = "";
5418    println!("M =\t{}", message);
5419    let iv = 0x_FEDCBA0987654321_u64;
5420    println!("IV =	{}", iv);
5421    let mut cipher = Vec::<u8>::new();
5422    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5423    print!("C =\t");
5424    for c in cipher.clone()
5425        { print!("{:02X} ", c); }
5426    println!();
5427    let mut txt = String::new();
5428    for c in cipher.clone()
5429        { write!(txt, "{:02X} ", c); }
5430    assert_eq!(txt, "");
5431
5432    let mut recovered = String::new();
5433    a_des.decrypt_vec_into_string(iv, &cipher, &mut recovered);
5434    println!("B =\t{}", recovered);
5435    assert_eq!(recovered, "");
5436    assert_eq!(recovered, message);
5437    println!();
5438
5439    // Normal case for the message shorter than 8 bytes
5440    let key = 0x_1234567890ABCDEF_u64;
5441    println!("K =\t{:#016X}", key);
5442    let mut a_des = DES::new_with_key_u64(key);
5443
5444    let message = "7 bytes";
5445    println!("M =\t{}", message);
5446    let iv = 0x_FEDCBA0987654321_u64;
5447    println!("IV =	{}", iv);
5448    let mut cipher = Vec::<u8>::new();
5449    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5450    print!("C =\t");
5451    for c in cipher.clone()
5452        { print!("{:02X} ", c); }
5453    println!();
5454    let mut txt = String::new();
5455    for c in cipher.clone()
5456        { write!(txt, "{:02X} ", c); }
5457    assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
5458
5459    let mut recovered = String::new();
5460    a_des.decrypt_vec_into_string(iv, &cipher, &mut recovered);
5461    println!("B =\t{}", recovered);
5462    assert_eq!(recovered, "7 bytes");
5463    assert_eq!(recovered, message);
5464    println!();
5465
5466    // Normal case for the message of 8 bytes
5467    let key = 0x_1234567890ABCDEF_u64;
5468    println!("K =\t{:#016X}", key);
5469    let mut a_des = DES::new_with_key_u64(key);
5470
5471    let message = "I am OK.";
5472    println!("M =\t{}", message);
5473    let iv = 0x_FEDCBA0987654321_u64;
5474    println!("IV =	{}", iv);
5475    let mut cipher = Vec::<u8>::new();
5476    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5477    print!("C =\t");
5478    for c in cipher.clone()
5479        { print!("{:02X} ", c); }
5480    println!();
5481    let mut txt = String::new();
5482    for c in cipher.clone()
5483        { write!(txt, "{:02X} ", c); }
5484    assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
5485
5486    let mut recovered = String::new();
5487    a_des.decrypt_vec_into_string(iv, &cipher, &mut recovered);
5488    println!("B =\t{}", recovered);
5489    assert_eq!(recovered, "I am OK.");
5490    assert_eq!(recovered, message);
5491    println!();
5492
5493    // Normal case for the message longer than 8 bytes and shorter than 16 bytes
5494    let key = 0x_1234567890ABCDEF_u64;
5495    println!("K =\t{:#016X}", key);
5496    let mut a_des = DES::new_with_key_u64(key);
5497
5498    let message = "PARK Youngho";
5499    println!("M =\t{}", message);
5500    let iv = 0x_FEDCBA0987654321_u64;
5501    println!("IV =	{}", iv);
5502    let mut cipher = Vec::<u8>::new();
5503    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5504    print!("C =\t");
5505    for c in cipher.clone()
5506        { print!("{:02X} ", c); }
5507    println!();
5508    let mut txt = String::new();
5509    for c in cipher.clone()
5510        { write!(txt, "{:02X} ", c); }
5511    assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 74 F9 B6 4F ");
5512
5513    let mut recovered = String::new();
5514    a_des.decrypt_vec_into_string(iv, &cipher, &mut recovered);
5515    println!("B =\t{}", recovered);
5516    assert_eq!(recovered, "PARK Youngho");
5517    assert_eq!(recovered, message);
5518    println!();
5519
5520    // Normal case for the message of 16 bytes
5521    let key = 0x_1234567890ABCDEF_u64;
5522    println!("K =\t{:#016X}", key);
5523    let mut a_des = DES::new_with_key_u64(key);
5524
5525    let message = "고맙습니다.";
5526    println!("M =\t{}", message);
5527    let iv = 0x_FEDCBA0987654321_u64;
5528    println!("IV =	{}", iv);
5529    let mut cipher = Vec::<u8>::new();
5530    a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5531    print!("C =\t");
5532    for c in cipher.clone()
5533        { print!("{:02X} ", c); }
5534    println!();
5535    let mut txt = String::new();
5536    for c in cipher.clone()
5537        { write!(txt, "{:02X} ", c); }
5538    assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 F4 FC 94 B5 94 1F B9 BD ");
5539
5540    let mut recovered = String::new();
5541    a_des.decrypt_vec_into_string(iv, &cipher, &mut recovered);
5542    println!("B =\t{}", recovered);
5543    assert_eq!(recovered, "고맙습니다.");
5544    assert_eq!(recovered, message);
5545    println!("-------------------------------");
5546}
5547
5548fn des_decrypt_array_cfb()
5549{
5550    println!("des_decrypt_array_cfb()");
5551    use std::io::Write;
5552    use std::fmt::Write as _;
5553    use cryptocol::symmetric::{ DES, DES_Expanded, CFB};
5554
5555    // Normal case
5556    let key = 0x_1234567890ABCDEF_u64;
5557    println!("K =\t{:#016X}", key);
5558    let mut a_des = DES::new_with_key_u64(key);
5559
5560    let message = "In the beginning God created the heavens and the earth.";
5561    println!("M =\t{}", message);
5562    let iv = 0x_FEDCBA0987654321_u64;
5563    println!("IV =	{}", iv);
5564    let mut cipher = [0_u8; 55];
5565    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
5566    print!("C (16 rounds) =\t");
5567    for c in cipher.clone()
5568        { print!("{:02X} ", c); }
5569    println!();
5570    let mut txt = String::new();
5571    for c in cipher.clone()
5572        { write!(txt, "{:02X} ", c); }
5573    assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 79 3A A1 78 EC CD 02 72 6A C4 41 7C 25 A4 2C 07 FC 77 25 49 12 55 0F 8A ED 44 C3 E4 DC 91 69 0F 40 72 7F F2 D9 B7 54 9F 36 91 C5 85 4F 9B 30 ");
5574
5575    let mut recovered = vec![0; 55];
5576    let len = a_des.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
5577    recovered.truncate(len as usize);
5578    print!("Ba (16 rounds) =\t");
5579    for b in recovered.clone()
5580        { print!("{:02X} ", b); }
5581    println!();
5582    let mut txt = String::new();
5583    for c in recovered.clone()
5584        { write!(txt, "{:02X} ", c); }
5585    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
5586
5587    let mut converted = String::new();
5588    unsafe { converted.as_mut_vec() }.append(&mut recovered);
5589    
5590    println!("Bb (16 rounds) =\t{}", converted);
5591    assert_eq!(converted, "In the beginning God created the heavens and the earth.");
5592    assert_eq!(converted, message);
5593    println!();
5594
5595    // Expanded case for 128 rounds
5596    let key = 0x_1234567890ABCDEF_u64;
5597    println!("K =\t{:#016X}", key);
5598    let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
5599
5600    let message = "In the beginning God created the heavens and the earth.";
5601    println!("M =\t{}", message);
5602    let iv = 0x_FEDCBA0987654321_u64;
5603    println!("IV =	{}", iv);
5604    let mut cipher = [0_u8; 55];
5605    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
5606    print!("C (128 rounds) =\t");
5607    for c in cipher.clone()
5608        { print!("{:02X} ", c); }
5609    println!();
5610    let mut txt = String::new();
5611    for c in cipher.clone()
5612        { write!(txt, "{:02X} ", c); }
5613    assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 F2 9A 70 44 D9 78 F5 E3 CF 55 0F EF BA F0 4A 7E BE 79 C9 B4 68 F4 99 09 48 93 00 D2 22 9D 29 6C 20 74 FF E3 E2 01 0C D3 7E 8D 4D 30 8F EC D6 ");
5614
5615    let mut recovered = vec![0; 55];
5616    let len = a_des.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
5617    recovered.truncate(len as usize);
5618    print!("Ba (128 rounds) =\t");
5619    for b in recovered.clone()
5620        { print!("{:02X} ", b); }
5621    println!();
5622    let mut txt = String::new();
5623    for c in recovered.clone()
5624        { write!(txt, "{:02X} ", c); }
5625    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
5626
5627    let mut converted = String::new();
5628    unsafe { converted.as_mut_vec() }.append(&mut recovered);
5629
5630    println!("Bb (128 rounds) =\t{}", converted);
5631    assert_eq!(converted, "In the beginning God created the heavens and the earth.");
5632    assert_eq!(converted, message);
5633    println!();
5634
5635    // Expanded case for 0 rounds which means that key is meaningless
5636    let key1 = 0x_1234567890ABCDEF_u64;
5637    let key2 = 0_u64;
5638    println!("K =\t{:#016X}", key);
5639    let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
5640    let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
5641
5642    let message = "In the beginning God created the heavens and the earth.";
5643    println!("M =\t{}", message);
5644    let iv = 0x_FEDCBA0987654321_u64;
5645    println!("IV =	{}", iv);
5646    let mut cipher1 = [0_u8; 55];
5647    let mut cipher2 = [0_u8; 55];
5648    c_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
5649    d_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
5650    print!("C (0 rounds) =\t");
5651    for c in cipher1.clone()
5652        { print!("{:02X} ", c); }
5653    println!();
5654    let mut txt = String::new();
5655    for c in cipher1.clone()
5656        { write!(txt, "{:02X} ", c); }
5657    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
5658    print!("D (0 rounds) =\t");
5659    for c in cipher2.clone()
5660        { print!("{:02X} ", c); }
5661    println!();
5662    let mut txt = String::new();
5663    for c in cipher2.clone()
5664        { write!(txt, "{:02X} ", c); }
5665    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
5666
5667    let mut recovered1 = vec![0; 55];
5668    let mut recovered2 = vec![0; 55];
5669    let len1 = c_des.decrypt_array(iv, &cipher1, recovered1.as_mut_ptr());
5670    let len2 = d_des.decrypt_array(iv, &cipher2, recovered2.as_mut_ptr());
5671    recovered1.truncate(len1 as usize);
5672    recovered2.truncate(len2 as usize);
5673
5674    print!("B1a (0 rounds) =\t");
5675    for b in recovered1.clone()
5676        { print!("{:02X} ", b); }
5677    println!();
5678    let mut txt = String::new();
5679    for c in recovered1.clone()
5680        { write!(txt, "{:02X} ", c); }
5681    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
5682    print!("B2a (0 rounds) =\t");
5683    for b in recovered2.clone()
5684        { print!("{:02X} ", b); }
5685    println!();
5686    let mut txt = String::new();
5687    for c in recovered2.clone()
5688        { write!(txt, "{:02X} ", c); }
5689    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
5690
5691    let mut converted1 = String::new();
5692    let mut converted2 = String::new();
5693    unsafe { converted1.as_mut_vec() }.append(&mut recovered1);
5694    unsafe { converted2.as_mut_vec() }.append(&mut recovered2);
5695    
5696    println!("B1b (0 rounds) =\t{}", converted1);
5697    assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
5698    assert_eq!(converted1, message);
5699    println!("B2b (0 rounds) =\t{}", converted2);
5700    assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
5701    assert_eq!(converted2, message);
5702    assert_eq!(converted1, converted1);
5703    println!();
5704
5705    // Normal case for the message of 0 bytes
5706    let key = 0x_1234567890ABCDEF_u64;
5707    println!("K =\t{:#016X}", key);
5708    let mut a_des = DES::new_with_key_u64(key);
5709
5710    let message = "";
5711    println!("M =\t{}", message);
5712    let iv = 0x_FEDCBA0987654321_u64;
5713    println!("IV =	{}", iv);
5714    let mut cipher = [0_u8; 0];
5715    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
5716    print!("C =\t");
5717    for c in cipher.clone()
5718        { print!("{:02X} ", c); }
5719    println!();
5720    let mut txt = String::new();
5721    for c in cipher.clone()
5722        { write!(txt, "{:02X} ", c); }
5723    assert_eq!(txt, "");
5724
5725    let mut recovered = vec![0; 8];
5726    let len = a_des.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
5727    recovered.truncate(len as usize);
5728
5729    print!("Ba =\t");
5730    for b in recovered.clone()
5731        { print!("{:02X} ", b); }
5732    println!();
5733    let mut txt = String::new();
5734    for c in recovered.clone()
5735        { write!(txt, "{:02X} ", c); }
5736    assert_eq!(txt, "");
5737
5738    let mut converted = String::new();
5739    unsafe { converted.as_mut_vec() }.append(&mut recovered);
5740    
5741    println!("Bb =\t{}", converted);
5742    assert_eq!(converted, "");
5743    assert_eq!(converted, message);
5744    println!();
5745
5746    // Normal case for the message shorter than 8 bytes
5747    let key = 0x_1234567890ABCDEF_u64;
5748    println!("K =\t{:#016X}", key);
5749    let mut a_des = DES::new_with_key_u64(key);
5750
5751    let message = "7 bytes";
5752    println!("M =\t{}", message);
5753    let iv = 0x_FEDCBA0987654321_u64;
5754    println!("IV =	{}", iv);
5755    let mut cipher = [0_u8; 7];
5756    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
5757    print!("C =\t");
5758    for c in cipher.clone()
5759        { print!("{:02X} ", c); }
5760    println!();
5761    let mut txt = String::new();
5762    for c in cipher.clone()
5763        { write!(txt, "{:02X} ", c); }
5764    assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
5765    
5766    let mut recovered = vec![0; 8];
5767    let len = a_des.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
5768    recovered.truncate(len as usize);
5769
5770    print!("Ba =\t");
5771    for b in recovered.clone()
5772        { print!("{:02X} ", b); }
5773    println!();
5774    let mut txt = String::new();
5775    for c in recovered.clone()
5776        { write!(txt, "{:02X} ", c); }
5777    assert_eq!(txt, "37 20 62 79 74 65 73 ");
5778
5779    let mut converted = String::new();
5780    unsafe { converted.as_mut_vec() }.append(&mut recovered);
5781
5782    println!("Bb =\t{}", converted);
5783    assert_eq!(converted, "7 bytes");
5784    assert_eq!(converted, message);
5785    println!();
5786
5787    // Normal case for the message of 8 bytes
5788    let key = 0x_1234567890ABCDEF_u64;
5789    println!("K =\t{:#016X}", key);
5790    let mut a_des = DES::new_with_key_u64(key);
5791
5792    let message = "I am OK.";
5793    println!("M =\t{}", message);
5794    let iv = 0x_FEDCBA0987654321_u64;
5795    println!("IV =	{}", iv);
5796    let mut cipher = [0_u8; 8];
5797    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
5798    print!("C =\t");
5799    for c in cipher.clone()
5800        { print!("{:02X} ", c); }
5801    println!();
5802    let mut txt = String::new();
5803    for c in cipher.clone()
5804        { write!(txt, "{:02X} ", c); }
5805    assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
5806    
5807    let mut recovered = vec![0; 16];
5808    let len = a_des.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
5809    recovered.truncate(len as usize);
5810
5811    print!("Ba =\t");
5812    for b in recovered.clone()
5813        { print!("{:02X} ", b); }
5814    println!();
5815    let mut txt = String::new();
5816    for c in recovered.clone()
5817        { write!(txt, "{:02X} ", c); }
5818    assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E ");
5819
5820    let mut converted = String::new();
5821    unsafe { converted.as_mut_vec() }.append(&mut recovered);
5822    
5823    println!("Bb =\t{}", converted);
5824    assert_eq!(converted, "I am OK.");
5825    assert_eq!(converted, message);
5826    println!();
5827
5828    // Normal case for the message longer than 8 bytes and shorter than 16 bytes
5829    let key = 0x_1234567890ABCDEF_u64;
5830    println!("K =\t{:#016X}", key);
5831    let mut a_des = DES::new_with_key_u64(key);
5832
5833    let message = "PARK Youngho";
5834    println!("M =\t{}", message);
5835    let iv = 0x_FEDCBA0987654321_u64;
5836    println!("IV =	{}", iv);
5837    let mut cipher = [0_u8; 12];
5838    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
5839    print!("C =\t");
5840    for c in cipher.clone()
5841        { print!("{:02X} ", c); }
5842    println!();
5843    let mut txt = String::new();
5844    for c in cipher.clone()
5845        { write!(txt, "{:02X} ", c); }
5846    assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 74 F9 B6 4F ");
5847
5848    let mut recovered = vec![0; 16];
5849    let len = a_des.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
5850    recovered.truncate(len as usize);
5851    print!("Ba =\t");
5852    for b in recovered.clone()
5853        { print!("{:02X} ", b); }
5854    println!();
5855    let mut txt = String::new();
5856    for c in recovered.clone()
5857        { write!(txt, "{:02X} ", c); }
5858    assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F ");
5859
5860    let mut converted = String::new();
5861    unsafe { converted.as_mut_vec() }.append(&mut recovered);
5862    
5863    println!("Bb =\t{}", converted);
5864    assert_eq!(converted, "PARK Youngho");
5865    assert_eq!(converted, message);
5866    println!();
5867
5868    // Normal case for the message of 16 bytes
5869    let key = 0x_1234567890ABCDEF_u64;
5870    println!("K =\t{:#016X}", key);
5871    let mut a_des = DES::new_with_key_u64(key);
5872
5873    let message = "고맙습니다.";
5874    println!("M =\t{}", message);
5875    let iv = 0x_FEDCBA0987654321_u64;
5876    println!("IV =	{}", iv);
5877    let mut cipher = [0_u8; 16];
5878    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
5879    print!("C =\t");
5880    for c in cipher.clone()
5881        { print!("{:02X} ", c); }
5882    println!();
5883    let mut txt = String::new();
5884    for c in cipher.clone()
5885        { write!(txt, "{:02X} ", c); }
5886    assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 F4 FC 94 B5 94 1F B9 BD ");
5887
5888    let mut recovered = vec![0; 24];
5889    let len = a_des.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
5890    recovered.truncate(len as usize);
5891
5892    print!("Ba =\t");
5893    for b in recovered.clone()
5894        { print!("{:02X} ", b); }
5895    println!();
5896    let mut txt = String::new();
5897    for c in recovered.clone()
5898        { write!(txt, "{:02X} ", c); }
5899    assert_eq!(txt, "EA B3 A0 EB A7 99 EC 8A B5 EB 8B 88 EB 8B A4 2E ");
5900
5901    let mut converted = String::new();
5902    unsafe { converted.as_mut_vec() }.append(&mut recovered);
5903    
5904    println!("Bb =\t{}", converted);
5905    assert_eq!(converted, "고맙습니다.");
5906    assert_eq!(converted, message);
5907    println!("-------------------------------");
5908}
5909
5910fn des_decrypt_array_cfb_into_vec()
5911{
5912    println!("des_decrypt_array_cfb_into_vec()");
5913    use std::io::Write;
5914    use std::fmt::Write as _;
5915    use cryptocol::symmetric::{ DES, DES_Expanded, CFB};
5916
5917    // Normal case
5918    let key = 0x_1234567890ABCDEF_u64;
5919    println!("K =\t{:#016X}", key);
5920    let mut a_des = DES::new_with_key_u64(key);
5921
5922    let message = "In the beginning God created the heavens and the earth.";
5923    println!("M =\t{}", message);
5924    let iv = 0x_FEDCBA0987654321_u64;
5925    println!("IV =	{}", iv);
5926    let mut cipher = [0_u8; 55];
5927    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
5928    print!("C (16 rounds) =\t");
5929    for c in cipher.clone()
5930        { print!("{:02X} ", c); }
5931    println!();
5932    let mut txt = String::new();
5933    for c in cipher.clone()
5934        { write!(txt, "{:02X} ", c); }
5935    assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 79 3A A1 78 EC CD 02 72 6A C4 41 7C 25 A4 2C 07 FC 77 25 49 12 55 0F 8A ED 44 C3 E4 DC 91 69 0F 40 72 7F F2 D9 B7 54 9F 36 91 C5 85 4F 9B 30 ");
5936
5937    let mut recovered = Vec::<u8>::new();
5938    a_des.decrypt_array_into_vec(iv, &cipher, &mut recovered);
5939    print!("Ba (16 rounds) =\t");
5940    for b in recovered.clone()
5941        { print!("{:02X} ", b); }
5942    println!();
5943    let mut txt = String::new();
5944    for c in recovered.clone()
5945        { write!(txt, "{:02X} ", c); }
5946    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
5947
5948    let mut converted = String::new();
5949    unsafe { converted.as_mut_vec() }.append(&mut recovered);
5950    
5951    println!("Bb (16 rounds) =\t{}", converted);
5952    assert_eq!(converted, "In the beginning God created the heavens and the earth.");
5953    assert_eq!(converted, message);
5954    println!();
5955
5956    // Expanded case for 128 rounds
5957    let key = 0x_1234567890ABCDEF_u64;
5958    println!("K =\t{:#016X}", key);
5959    let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
5960
5961    let message = "In the beginning God created the heavens and the earth.";
5962    println!("M =\t{}", message);
5963    let iv = 0x_FEDCBA0987654321_u64;
5964    println!("IV =	{}", iv);
5965    let mut cipher = [0_u8; 55];
5966    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
5967    print!("C (128 rounds) =\t");
5968    for c in cipher.clone()
5969        { print!("{:02X} ", c); }
5970    println!();
5971    let mut txt = String::new();
5972    for c in cipher.clone()
5973        { write!(txt, "{:02X} ", c); }
5974    assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 F2 9A 70 44 D9 78 F5 E3 CF 55 0F EF BA F0 4A 7E BE 79 C9 B4 68 F4 99 09 48 93 00 D2 22 9D 29 6C 20 74 FF E3 E2 01 0C D3 7E 8D 4D 30 8F EC D6 ");
5975
5976    let mut recovered = Vec::<u8>::new();
5977    a_des.decrypt_array_into_vec(iv, &cipher, &mut recovered);
5978    print!("Ba (128 rounds) =\t");
5979    for b in recovered.clone()
5980        { print!("{:02X} ", b); }
5981    println!();
5982    let mut txt = String::new();
5983    for c in recovered.clone()
5984        { write!(txt, "{:02X} ", c); }
5985    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
5986
5987    let mut converted = String::new();
5988    unsafe { converted.as_mut_vec() }.append(&mut recovered);
5989    
5990    println!("Bb (128 rounds) =\t{}", converted);
5991    assert_eq!(converted, "In the beginning God created the heavens and the earth.");
5992    assert_eq!(converted, message);
5993    println!();
5994
5995    // Expanded case for 0 rounds which means that key is meaningless
5996    let key1 = 0x_1234567890ABCDEF_u64;
5997    let key2 = 0_u64;
5998    println!("K =\t{:#016X}", key);
5999    let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
6000    let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
6001
6002    let message = "In the beginning God created the heavens and the earth.";
6003    println!("M =\t{}", message);
6004    let iv = 0x_FEDCBA0987654321_u64;
6005    println!("IV =	{}", iv);
6006    let mut cipher1 = [0_u8; 55];
6007    let mut cipher2 = [0_u8; 55];
6008    c_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
6009    d_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
6010    print!("C (0 rounds) =\t");
6011    for c in cipher1.clone()
6012        { print!("{:02X} ", c); }
6013    println!();
6014    let mut txt = String::new();
6015    for c in cipher1.clone()
6016        { write!(txt, "{:02X} ", c); }
6017    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
6018    print!("D (0 rounds) =\t");
6019    for c in cipher2.clone()
6020        { print!("{:02X} ", c); }
6021    println!();
6022    let mut txt = String::new();
6023    for c in cipher2.clone()
6024        { write!(txt, "{:02X} ", c); }
6025    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
6026
6027    let mut recovered1 = Vec::<u8>::new();
6028    let mut recovered2 = Vec::<u8>::new();
6029    c_des.decrypt_array_into_vec(iv, &cipher1, &mut recovered1);
6030    d_des.decrypt_array_into_vec(iv, &cipher2, &mut recovered2);
6031    print!("B1a (0 rounds) =\t");
6032    for b in recovered1.clone()
6033        { print!("{:02X} ", b); }
6034    println!();
6035    let mut txt = String::new();
6036    for c in recovered1.clone()
6037        { write!(txt, "{:02X} ", c); }
6038    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
6039    print!("B2a (0 rounds) =\t");
6040    for b in recovered2.clone()
6041        { print!("{:02X} ", b); }
6042    println!();
6043    let mut txt = String::new();
6044    for c in recovered2.clone()
6045        { write!(txt, "{:02X} ", c); }
6046    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
6047
6048    let mut converted1 = String::new();
6049    let mut converted2 = String::new();
6050    unsafe { converted1.as_mut_vec() }.append(&mut recovered1);
6051    unsafe { converted2.as_mut_vec() }.append(&mut recovered2);
6052    
6053    println!("B1b (0 rounds) =\t{}", converted1);
6054    assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
6055    assert_eq!(converted1, message);
6056    println!("B2b (0 rounds) =\t{}", converted2);
6057    assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
6058    assert_eq!(converted2, message);
6059    assert_eq!(converted1, converted1);
6060    println!();
6061
6062    // Normal case for the message of 0 bytes
6063    let key = 0x_1234567890ABCDEF_u64;
6064    println!("K =\t{:#016X}", key);
6065    let mut a_des = DES::new_with_key_u64(key);
6066
6067    let message = "";
6068    println!("M =\t{}", message);
6069    let iv = 0x_FEDCBA0987654321_u64;
6070    println!("IV =	{}", iv);
6071    let mut cipher = [0_u8; 0];
6072    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6073    print!("C =\t");
6074    for c in cipher.clone()
6075        { print!("{:02X} ", c); }
6076    println!();
6077    let mut txt = String::new();
6078    for c in cipher.clone()
6079        { write!(txt, "{:02X} ", c); }
6080    assert_eq!(txt, "");
6081
6082    let mut recovered = Vec::<u8>::new();
6083    a_des.decrypt_array_into_vec(iv, &cipher, &mut recovered);
6084    print!("Ba =\t");
6085    for b in recovered.clone()
6086        { print!("{:02X} ", b); }
6087    println!();
6088    let mut txt = String::new();
6089    for c in recovered.clone()
6090        { write!(txt, "{:02X} ", c); }
6091    assert_eq!(txt, "");
6092
6093    let mut converted = String::new();
6094    unsafe { converted.as_mut_vec() }.append(&mut recovered);
6095    
6096    println!("Bb =\t{}", converted);
6097    assert_eq!(converted, "");
6098    assert_eq!(converted, message);
6099    println!();
6100
6101    // Normal case for the message shorter than 8 bytes
6102    let key = 0x_1234567890ABCDEF_u64;
6103    println!("K =\t{:#016X}", key);
6104    let mut a_des = DES::new_with_key_u64(key);
6105
6106    let message = "7 bytes";
6107    println!("M =\t{}", message);
6108    let iv = 0x_FEDCBA0987654321_u64;
6109    println!("IV =	{}", iv);
6110    let mut cipher = [0_u8; 7];
6111    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6112    print!("C =\t");
6113    for c in cipher.clone()
6114        { print!("{:02X} ", c); }
6115    println!();
6116    let mut txt = String::new();
6117    for c in cipher.clone()
6118        { write!(txt, "{:02X} ", c); }
6119    assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
6120    
6121    let mut recovered = Vec::<u8>::new();
6122    a_des.decrypt_array_into_vec(iv, &cipher, &mut recovered);
6123    print!("Ba =\t");
6124    for b in recovered.clone()
6125        { print!("{:02X} ", b); }
6126    println!();
6127    let mut txt = String::new();
6128    for c in recovered.clone()
6129        { write!(txt, "{:02X} ", c); }
6130    assert_eq!(txt, "37 20 62 79 74 65 73 ");
6131
6132    let mut converted = String::new();
6133    unsafe { converted.as_mut_vec() }.append(&mut recovered);
6134    
6135    println!("Bb =\t{}", converted);
6136    assert_eq!(converted, "7 bytes");
6137    assert_eq!(converted, message);
6138    println!();
6139
6140    // Normal case for the message of 8 bytes
6141    let key = 0x_1234567890ABCDEF_u64;
6142    println!("K =\t{:#016X}", key);
6143    let mut a_des = DES::new_with_key_u64(key);
6144
6145    let message = "I am OK.";
6146    println!("M =\t{}", message);
6147    let iv = 0x_FEDCBA0987654321_u64;
6148    println!("IV =	{}", iv);
6149    let mut cipher = [0_u8; 8];
6150    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6151    print!("C =\t");
6152    for c in cipher.clone()
6153        { print!("{:02X} ", c); }
6154    println!();
6155    let mut txt = String::new();
6156    for c in cipher.clone()
6157        { write!(txt, "{:02X} ", c); }
6158    assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
6159    
6160    let mut recovered = Vec::<u8>::new();
6161    a_des.decrypt_array_into_vec(iv, &cipher, &mut recovered);
6162    print!("Ba =\t");
6163    for b in recovered.clone()
6164        { print!("{:02X} ", b); }
6165    println!();
6166    let mut txt = String::new();
6167    for c in recovered.clone()
6168        { write!(txt, "{:02X} ", c); }
6169    assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E ");
6170
6171    let mut converted = String::new();
6172    unsafe { converted.as_mut_vec() }.append(&mut recovered);
6173    
6174    println!("Bb =\t{}", converted);
6175    assert_eq!(converted, "I am OK.");
6176    assert_eq!(converted, message);
6177    println!();
6178
6179    // Normal case for the message longer than 8 bytes and shorter than 16 bytes
6180    let key = 0x_1234567890ABCDEF_u64;
6181    println!("K =\t{:#016X}", key);
6182    let mut a_des = DES::new_with_key_u64(key);
6183
6184    let message = "PARK Youngho";
6185    println!("M =\t{}", message);
6186    let iv = 0x_FEDCBA0987654321_u64;
6187    println!("IV =	{}", iv);
6188    let mut cipher = [0_u8; 12];
6189    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6190    print!("C =\t");
6191    for c in cipher.clone()
6192        { print!("{:02X} ", c); }
6193    println!();
6194    let mut txt = String::new();
6195    for c in cipher.clone()
6196        { write!(txt, "{:02X} ", c); }
6197    assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 74 F9 B6 4F ");
6198
6199    let mut recovered = Vec::<u8>::new();
6200    a_des.decrypt_array_into_vec(iv, &cipher, &mut recovered);
6201    print!("Ba =\t");
6202    for b in recovered.clone()
6203        { print!("{:02X} ", b); }
6204    println!();
6205    let mut txt = String::new();
6206    for c in recovered.clone()
6207        { write!(txt, "{:02X} ", c); }
6208    assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F ");
6209
6210    let mut converted = String::new();
6211    unsafe { converted.as_mut_vec() }.append(&mut recovered);
6212    
6213    println!("Bb =\t{}", converted);
6214    assert_eq!(converted, "PARK Youngho");
6215    assert_eq!(converted, message);
6216    println!();
6217
6218    // Normal case for the message of 16 bytes
6219    let key = 0x_1234567890ABCDEF_u64;
6220    println!("K =\t{:#016X}", key);
6221    let mut a_des = DES::new_with_key_u64(key);
6222
6223    let message = "고맙습니다.";
6224    println!("M =\t{}", message);
6225    let iv = 0x_FEDCBA0987654321_u64;
6226    println!("IV =	{}", iv);
6227    let mut cipher = [0_u8; 16];
6228    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6229    print!("C =\t");
6230    for c in cipher.clone()
6231        { print!("{:02X} ", c); }
6232    println!();
6233    let mut txt = String::new();
6234    for c in cipher.clone()
6235        { write!(txt, "{:02X} ", c); }
6236    assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 F4 FC 94 B5 94 1F B9 BD ");
6237
6238    let mut recovered = Vec::<u8>::new();
6239    a_des.decrypt_array_into_vec(iv, &cipher, &mut recovered);
6240    print!("Ba =\t");
6241    for b in recovered.clone()
6242        { print!("{:02X} ", b); }
6243    println!();
6244    let mut txt = String::new();
6245    for c in recovered.clone()
6246        { write!(txt, "{:02X} ", c); }
6247    assert_eq!(txt, "EA B3 A0 EB A7 99 EC 8A B5 EB 8B 88 EB 8B A4 2E ");
6248
6249    let mut converted = String::new();
6250    unsafe { converted.as_mut_vec() }.append(&mut recovered);
6251    
6252    println!("Bb =\t{}", converted);
6253    assert_eq!(converted, "고맙습니다.");
6254    assert_eq!(converted, message);
6255    println!("-------------------------------");
6256}
6257
6258fn des_decrypt_array_cfb_into_array()
6259{
6260    println!("des_decrypt_array_cfb_into_array()");
6261    use std::io::Write;
6262    use std::fmt::Write as _;
6263    use cryptocol::symmetric::{ DES, DES_Expanded, CFB};
6264
6265    // Normal case
6266    let key = 0x_1234567890ABCDEF_u64;
6267    println!("K =\t{:#016X}", key);
6268    let mut a_des = DES::new_with_key_u64(key);
6269
6270    let message = "In the beginning God created the heavens and the earth.";
6271    println!("M =\t{}", message);
6272    let iv = 0x_FEDCBA0987654321_u64;
6273    println!("IV =	{}", iv);
6274    let mut cipher = [0_u8; 55];
6275    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6276    print!("C (16 rounds) =\t");
6277    for c in cipher.clone()
6278        { print!("{:02X} ", c); }
6279    println!();
6280    let mut txt = String::new();
6281    for c in cipher.clone()
6282        { write!(txt, "{:02X} ", c); }
6283    assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 79 3A A1 78 EC CD 02 72 6A C4 41 7C 25 A4 2C 07 FC 77 25 49 12 55 0F 8A ED 44 C3 E4 DC 91 69 0F 40 72 7F F2 D9 B7 54 9F 36 91 C5 85 4F 9B 30 ");
6284
6285    let mut recovered = [0u8; 56];
6286    let len = a_des.decrypt_array_into_array(iv, &cipher, &mut recovered);
6287    print!("Ba (16 rounds) =\t");
6288    for b in recovered.clone()
6289        { print!("{:02X} ", b); }
6290    println!();
6291    let mut txt = String::new();
6292    for c in recovered.clone()
6293        { write!(txt, "{:02X} ", c); }
6294    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 ");
6295
6296    let mut converted = String::new();
6297    unsafe { converted.as_mut_vec() }.write(&recovered);
6298    unsafe { converted.as_mut_vec() }.truncate(len as usize);
6299    println!("Bb (16 rounds) =\t{}", converted);
6300    assert_eq!(converted, "In the beginning God created the heavens and the earth.");
6301    assert_eq!(converted, message);
6302    println!();
6303
6304    // Expanded case for 128 rounds
6305    let key = 0x_1234567890ABCDEF_u64;
6306    println!("K =\t{:#016X}", key);
6307    let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
6308
6309    let message = "In the beginning God created the heavens and the earth.";
6310    println!("M =\t{}", message);
6311    let iv = 0x_FEDCBA0987654321_u64;
6312    println!("IV =	{}", iv);
6313    let mut cipher = [0_u8; 55];
6314    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6315    print!("C (128 rounds) =\t");
6316    for c in cipher.clone()
6317        { print!("{:02X} ", c); }
6318    println!();
6319    let mut txt = String::new();
6320    for c in cipher.clone()
6321        { write!(txt, "{:02X} ", c); }
6322    assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 F2 9A 70 44 D9 78 F5 E3 CF 55 0F EF BA F0 4A 7E BE 79 C9 B4 68 F4 99 09 48 93 00 D2 22 9D 29 6C 20 74 FF E3 E2 01 0C D3 7E 8D 4D 30 8F EC D6 ");
6323
6324    let mut recovered = [0u8; 56];
6325    let len = a_des.decrypt_array_into_array(iv, &cipher, &mut recovered);
6326    print!("Ba (16 rounds) =\t");
6327    for b in recovered.clone()
6328        { print!("{:02X} ", b); }
6329    println!();
6330    let mut txt = String::new();
6331    for c in recovered.clone()
6332        { write!(txt, "{:02X} ", c); }
6333    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 ");
6334
6335    let mut converted = String::new();
6336    unsafe { converted.as_mut_vec() }.write(&recovered);
6337    unsafe { converted.as_mut_vec() }.truncate(len as usize);
6338    println!("Bb (16 rounds) =\t{}", converted);
6339    assert_eq!(converted, "In the beginning God created the heavens and the earth.");
6340    assert_eq!(converted, message);
6341    println!();
6342
6343    // Expanded case for 0 rounds which means that key is meaningless
6344    let key1 = 0x_1234567890ABCDEF_u64;
6345    let key2 = 0_u64;
6346    println!("K =\t{:#016X}", key);
6347    let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
6348    let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
6349
6350    let message = "In the beginning God created the heavens and the earth.";
6351    println!("M =\t{}", message);
6352    let iv = 0x_FEDCBA0987654321_u64;
6353    println!("IV =	{}", iv);
6354    let mut cipher1 = [0_u8; 55];
6355    let mut cipher2 = [0_u8; 55];
6356    c_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
6357    d_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
6358    print!("C (0 rounds) =\t");
6359    for c in cipher1.clone()
6360        { print!("{:02X} ", c); }
6361    println!();
6362    let mut txt = String::new();
6363    for c in cipher1.clone()
6364        { write!(txt, "{:02X} ", c); }
6365    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
6366    print!("D (0 rounds) =\t");
6367    for c in cipher2.clone()
6368        { print!("{:02X} ", c); }
6369    println!();
6370    let mut txt = String::new();
6371    for c in cipher2.clone()
6372        { write!(txt, "{:02X} ", c); }
6373    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
6374
6375    let mut recovered1 = [0u8; 56];
6376    let mut recovered2 = [0u8; 56];
6377    let len1 = c_des.decrypt_array_into_array(iv, &cipher1, &mut recovered1);
6378    let len2 = d_des.decrypt_array_into_array(iv, &cipher2, &mut recovered2);
6379    print!("B1a (0 rounds) =\t");
6380    for b in recovered1.clone()
6381        { print!("{:02X} ", b); }
6382    println!();
6383    let mut txt = String::new();
6384    for c in recovered1.clone()
6385        { write!(txt, "{:02X} ", c); }
6386    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 ");
6387    print!("B2a (0 rounds) =\t");
6388    for b in recovered2.clone()
6389        { print!("{:02X} ", b); }
6390    println!();
6391    let mut txt = String::new();
6392    for c in recovered.clone()
6393        { write!(txt, "{:02X} ", c); }
6394    assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 ");
6395
6396    let mut converted1 = String::new();
6397    let mut converted2 = String::new();
6398    unsafe { converted1.as_mut_vec() }.write(&recovered1);
6399    unsafe { converted2.as_mut_vec() }.write(&recovered2);
6400    unsafe { converted1.as_mut_vec() }.truncate(len1 as usize);
6401    unsafe { converted2.as_mut_vec() }.truncate(len2 as usize);
6402    println!("B1b (0 rounds) =\t{}", converted1);
6403    println!("B2b (0 rounds) =\t{}", converted2);
6404    assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
6405    assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
6406    assert_eq!(converted1, message);
6407    assert_eq!(converted2, message);
6408    assert_eq!(converted1, converted2);
6409    println!();
6410
6411    // Normal case for the message of 0 bytes
6412    let key = 0x_1234567890ABCDEF_u64;
6413    println!("K =\t{:#016X}", key);
6414    let mut a_des = DES::new_with_key_u64(key);
6415
6416    let message = "";
6417    println!("M =\t{}", message);
6418    let iv = 0x_FEDCBA0987654321_u64;
6419    println!("IV =	{}", iv);
6420    let mut cipher = [0_u8; 0];
6421    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6422    print!("C =\t");
6423    for c in cipher.clone()
6424        { print!("{:02X} ", c); }
6425    println!();
6426    let mut txt = String::new();
6427    for c in cipher.clone()
6428        { write!(txt, "{:02X} ", c); }
6429    assert_eq!(txt, "");
6430
6431    let mut recovered = [0u8; 8];
6432    let len = a_des.decrypt_array_into_array(iv, &cipher, &mut recovered);
6433
6434    print!("Ba =\t");
6435    for b in recovered.clone()
6436        { print!("{:02X} ", b); }
6437    println!();
6438    let mut txt = String::new();
6439    for c in recovered.clone()
6440        { write!(txt, "{:02X} ", c); }
6441    assert_eq!(txt, "00 00 00 00 00 00 00 00 ");
6442
6443    let mut converted = String::new();
6444    unsafe { converted.as_mut_vec() }.write(&recovered);
6445    unsafe { converted.as_mut_vec() }.truncate(len as usize);
6446    println!("Bb =\t{}", converted);
6447    assert_eq!(converted, "");
6448    assert_eq!(converted, message);
6449    println!();
6450
6451    // Normal case for the message shorter than 8 bytes
6452    let key = 0x_1234567890ABCDEF_u64;
6453    println!("K =\t{:#016X}", key);
6454    let mut a_des = DES::new_with_key_u64(key);
6455
6456    let message = "7 bytes";
6457    println!("M =\t{}", message);
6458    let iv = 0x_FEDCBA0987654321_u64;
6459    println!("IV =	{}", iv);
6460    let mut cipher = [0_u8; 7];
6461    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6462    print!("C =\t");
6463    for c in cipher.clone()
6464        { print!("{:02X} ", c); }
6465    println!();
6466    let mut txt = String::new();
6467    for c in cipher.clone()
6468        { write!(txt, "{:02X} ", c); }
6469    assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
6470
6471    let mut recovered = [0u8; 8];
6472    let len = a_des.decrypt_array_into_array(iv, &cipher, &mut recovered);
6473
6474    print!("Ba =\t");
6475    for b in recovered.clone()
6476        { print!("{:02X} ", b); }
6477    println!();
6478    let mut txt = String::new();
6479    for c in recovered.clone()
6480        { write!(txt, "{:02X} ", c); }
6481    assert_eq!(txt, "37 20 62 79 74 65 73 00 ");
6482
6483    let mut converted = String::new();
6484    unsafe { converted.as_mut_vec() }.write(&recovered);
6485    unsafe { converted.as_mut_vec() }.truncate(len as usize);
6486    println!("Bb =\t{}", converted);
6487    assert_eq!(converted, "7 bytes");
6488    assert_eq!(converted, message);
6489    println!();
6490
6491    // Normal case for the message of 8 bytes
6492    let key = 0x_1234567890ABCDEF_u64;
6493    println!("K =\t{:#016X}", key);
6494    let mut a_des = DES::new_with_key_u64(key);
6495
6496    let message = "I am OK.";
6497    println!("M =\t{}", message);
6498    let iv = 0x_FEDCBA0987654321_u64;
6499    println!("IV =	{}", iv);
6500    let mut cipher = [0_u8; 8];
6501    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6502    print!("C =\t");
6503    for c in cipher.clone()
6504        { print!("{:02X} ", c); }
6505    println!();
6506    let mut txt = String::new();
6507    for c in cipher.clone()
6508        { write!(txt, "{:02X} ", c); }
6509    assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
6510
6511    let mut recovered = [0u8; 16];
6512    let len = a_des.decrypt_array_into_array(iv, &cipher, &mut recovered);
6513
6514    print!("Ba =\t");
6515    for b in recovered.clone()
6516        { print!("{:02X} ", b); }
6517    println!();
6518    let mut txt = String::new();
6519    for c in recovered.clone()
6520        { write!(txt, "{:02X} ", c); }
6521    assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E 00 00 00 00 00 00 00 00 ");
6522
6523    let mut converted = String::new();
6524    unsafe { converted.as_mut_vec() }.write(&recovered);
6525    unsafe { converted.as_mut_vec() }.truncate(len as usize);
6526    println!("Bb =\t{}", converted);
6527    assert_eq!(converted, "I am OK.");
6528    assert_eq!(converted, message);
6529    println!();
6530
6531    // Normal case for the message longer than 8 bytes and shorter than 16 bytes
6532    let key = 0x_1234567890ABCDEF_u64;
6533    println!("K =\t{:#016X}", key);
6534    let mut a_des = DES::new_with_key_u64(key);
6535
6536    let message = "PARK Youngho";
6537    println!("M =\t{}", message);
6538    let iv = 0x_FEDCBA0987654321_u64;
6539    println!("IV =	{}", iv);
6540    let mut cipher = [0_u8; 12];
6541    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6542    print!("C =\t");
6543    for c in cipher.clone()
6544        { print!("{:02X} ", c); }
6545    println!();
6546    let mut txt = String::new();
6547    for c in cipher.clone()
6548        { write!(txt, "{:02X} ", c); }
6549    assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 74 F9 B6 4F ");
6550
6551    let mut recovered = [0u8; 16];
6552    let len = a_des.decrypt_array_into_array(iv, &cipher, &mut recovered);
6553
6554    print!("Ba =\t");
6555    for b in recovered.clone()
6556        { print!("{:02X} ", b); }
6557    println!();
6558    let mut txt = String::new();
6559    for c in recovered.clone()
6560        { write!(txt, "{:02X} ", c); }
6561    assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F 00 00 00 00 ");
6562
6563    let mut converted = String::new();
6564    unsafe { converted.as_mut_vec() }.write(&recovered);
6565    unsafe { converted.as_mut_vec() }.truncate(len as usize);
6566    println!("Bb =\t{}", converted);
6567    assert_eq!(converted, "PARK Youngho");
6568    assert_eq!(converted, message);
6569    println!();
6570
6571    // Normal case for the message of 16 bytes
6572    let key = 0x_1234567890ABCDEF_u64;
6573    println!("K =\t{:#016X}", key);
6574    let mut a_des = DES::new_with_key_u64(key);
6575
6576    let message = "고맙습니다.";
6577    println!("M =\t{}", message);
6578    let iv = 0x_FEDCBA0987654321_u64;
6579    println!("IV =	{}", iv);
6580    let mut cipher = [0_u8; 16];
6581    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6582    print!("C =\t");
6583    for c in cipher.clone()
6584        { print!("{:02X} ", c); }
6585    println!();
6586    let mut txt = String::new();
6587    for c in cipher.clone()
6588        { write!(txt, "{:02X} ", c); }
6589    assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 F4 FC 94 B5 94 1F B9 BD ");
6590
6591    let mut recovered = [0u8; 24];
6592    let len = a_des.decrypt_array_into_array(iv, &cipher, &mut recovered);
6593
6594    print!("Ba =\t");
6595    for b in recovered.clone()
6596        { print!("{:02X} ", b); }
6597    println!();
6598    let mut txt = String::new();
6599    for c in recovered.clone()
6600        { write!(txt, "{:02X} ", c); }
6601    assert_eq!(txt, "EA B3 A0 EB A7 99 EC 8A B5 EB 8B 88 EB 8B A4 2E 00 00 00 00 00 00 00 00 ");
6602
6603    let mut converted = String::new();
6604    unsafe { converted.as_mut_vec() }.write(&recovered);
6605    unsafe { converted.as_mut_vec() }.truncate(len as usize);
6606    println!("Bb =\t{}", converted);
6607    assert_eq!(converted, "고맙습니다.");
6608    assert_eq!(converted, message);
6609    println!("-------------------------------");
6610}
6611
6612fn des_decrypt_array_cfb_into_string()
6613{
6614    println!("des_decrypt_array_cfb_into_string()");
6615    use std::io::Write;
6616    use std::fmt::Write as _;
6617    use cryptocol::symmetric::{ DES, DES_Expanded, CFB};
6618
6619    // Normal case
6620    let key = 0x_1234567890ABCDEF_u64;
6621    println!("K =\t{:#016X}", key);
6622    let mut a_des = DES::new_with_key_u64(key);
6623
6624    let message = "In the beginning God created the heavens and the earth.";
6625    println!("M =\t{}", message);
6626    let iv = 0x_FEDCBA0987654321_u64;
6627    println!("IV =	{}", iv);
6628    let mut cipher = [0_u8; 55];
6629    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6630    print!("C (16 rounds) =\t");
6631    for c in cipher.clone()
6632        { print!("{:02X} ", c); }
6633    println!();
6634    let mut txt = String::new();
6635    for c in cipher.clone()
6636        { write!(txt, "{:02X} ", c); }
6637    assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 79 3A A1 78 EC CD 02 72 6A C4 41 7C 25 A4 2C 07 FC 77 25 49 12 55 0F 8A ED 44 C3 E4 DC 91 69 0F 40 72 7F F2 D9 B7 54 9F 36 91 C5 85 4F 9B 30 ");
6638
6639    let mut recovered = String::new();
6640    a_des.decrypt_array_into_string(iv, &cipher, &mut recovered);
6641    println!("B (16 rounds) =\t{}", recovered);
6642    assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
6643    assert_eq!(recovered, message);
6644    println!();
6645
6646    // Expanded case for 128 rounds
6647    let key = 0x_1234567890ABCDEF_u64;
6648    println!("K =\t{:#016X}", key);
6649    let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
6650
6651    let message = "In the beginning God created the heavens and the earth.";
6652    println!("M =\t{}", message);
6653    let iv = 0x_FEDCBA0987654321_u64;
6654    println!("IV =	{}", iv);
6655    let mut cipher = [0_u8; 55];
6656    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6657    print!("C (128 rounds) =\t");
6658    for c in cipher.clone()
6659        { print!("{:02X} ", c); }
6660    println!();
6661    let mut txt = String::new();
6662    for c in cipher.clone()
6663        { write!(txt, "{:02X} ", c); }
6664    assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 F2 9A 70 44 D9 78 F5 E3 CF 55 0F EF BA F0 4A 7E BE 79 C9 B4 68 F4 99 09 48 93 00 D2 22 9D 29 6C 20 74 FF E3 E2 01 0C D3 7E 8D 4D 30 8F EC D6 ");
6665
6666    let mut recovered = String::new();
6667    a_des.decrypt_array_into_string(iv, &cipher, &mut recovered);
6668    println!("B (128 rounds) =\t{}", recovered);
6669    assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
6670    assert_eq!(recovered, message);
6671    println!();
6672
6673    // Expanded case for 0 rounds which means that key is meaningless
6674    let key1 = 0x_1234567890ABCDEF_u64;
6675    let key2 = 0_u64;
6676    println!("K =\t{:#016X}", key);
6677    let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
6678    let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
6679
6680    let message = "In the beginning God created the heavens and the earth.";
6681    println!("M =\t{}", message);
6682    let iv = 0x_FEDCBA0987654321_u64;
6683    println!("IV =	{}", iv);
6684    let mut cipher1 = [0_u8; 55];
6685    let mut cipher2 = [0_u8; 55];
6686    c_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
6687    d_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
6688    print!("C (0 rounds) =\t");
6689    for c in cipher1.clone()
6690        { print!("{:02X} ", c); }
6691    println!();
6692    let mut txt = String::new();
6693    for c in cipher1.clone()
6694        { write!(txt, "{:02X} ", c); }
6695    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
6696    print!("D (0 rounds) =\t");
6697    for c in cipher2.clone()
6698        { print!("{:02X} ", c); }
6699    println!();
6700    let mut txt = String::new();
6701    for c in cipher2.clone()
6702        { write!(txt, "{:02X} ", c); }
6703    assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F C2 B9 1C 51 F3 49 A2 08 E1 31 43 C6 D3 E5 23 61 B3 46 E6 AD C3 AE 7B F7 53 E1 BC 3F B5 38 D9 88 83 B3 12 5B 5A 40 8E 21 63 16 40 D5 D1 E8 63 ");
6704
6705    let mut recovered1 = String::new();
6706    let mut recovered2 = String::new();
6707    c_des.decrypt_array_into_string(iv, &cipher1, &mut recovered1);
6708    d_des.decrypt_array_into_string(iv, &cipher2, &mut recovered2);
6709    println!("B1 (0 rounds) =\t{}", recovered1);
6710    println!("B2 (0 rounds) =\t{}", recovered2);
6711    assert_eq!(recovered1, "In the beginning God created the heavens and the earth.");
6712    assert_eq!(recovered2, "In the beginning God created the heavens and the earth.");
6713    assert_eq!(recovered1, message);
6714    assert_eq!(recovered2, message);
6715    assert_eq!(recovered1, recovered2);
6716    println!();
6717
6718    // Normal case for the message of 0 bytes
6719    let key = 0x_1234567890ABCDEF_u64;
6720    println!("K =\t{:#016X}", key);
6721    let mut a_des = DES::new_with_key_u64(key);
6722
6723    let message = "";
6724    println!("M =\t{}", message);
6725    let iv = 0x_FEDCBA0987654321_u64;
6726    println!("IV =	{}", iv);
6727    let mut cipher = [0_u8; 0];
6728    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6729    print!("C =\t");
6730    for c in cipher.clone()
6731        { print!("{:02X} ", c); }
6732    println!();
6733    let mut txt = String::new();
6734    for c in cipher.clone()
6735        { write!(txt, "{:02X} ", c); }
6736    assert_eq!(txt, "");
6737
6738    let mut recovered = String::new();
6739    a_des.decrypt_array_into_string(iv, &cipher, &mut recovered);
6740    println!("B =\t{}", recovered);
6741    assert_eq!(recovered, "");
6742    assert_eq!(recovered, message);
6743    println!();
6744
6745    // Normal case for the message shorter than 8 bytes
6746    let key = 0x_1234567890ABCDEF_u64;
6747    println!("K =\t{:#016X}", key);
6748    let mut a_des = DES::new_with_key_u64(key);
6749
6750    let message = "7 bytes";
6751    println!("M =\t{}", message);
6752    let iv = 0x_FEDCBA0987654321_u64;
6753    println!("IV =	{}", iv);
6754    let mut cipher = [0_u8; 7];
6755    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6756    print!("C =\t");
6757    for c in cipher.clone()
6758        { print!("{:02X} ", c); }
6759    println!();
6760    let mut txt = String::new();
6761    for c in cipher.clone()
6762        { write!(txt, "{:02X} ", c); }
6763    assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
6764
6765    let mut recovered = String::new();
6766    a_des.decrypt_array_into_string(iv, &cipher, &mut recovered);
6767    println!("B =\t{}", recovered);
6768    assert_eq!(recovered, "7 bytes");
6769    assert_eq!(recovered, message);
6770    println!();
6771
6772    // Normal case for the message of 8 bytes
6773    let key = 0x_1234567890ABCDEF_u64;
6774    println!("K =\t{:#016X}", key);
6775    let mut a_des = DES::new_with_key_u64(key);
6776
6777    let message = "I am OK.";
6778    println!("M =\t{}", message);
6779    let iv = 0x_FEDCBA0987654321_u64;
6780    println!("IV =	{}", iv);
6781    let mut cipher = [0_u8; 8];
6782    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6783    print!("C =\t");
6784    for c in cipher.clone()
6785        { print!("{:02X} ", c); }
6786    println!();
6787    let mut txt = String::new();
6788    for c in cipher.clone()
6789        { write!(txt, "{:02X} ", c); }
6790    assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
6791
6792    let mut recovered = String::new();
6793    a_des.decrypt_array_into_string(iv, &cipher, &mut recovered);
6794    println!("B =\t{}", recovered);
6795    assert_eq!(recovered, "I am OK.");
6796    assert_eq!(recovered, message);
6797    println!();
6798
6799    // Normal case for the message longer than 8 bytes and shorter than 16 bytes
6800    let key = 0x_1234567890ABCDEF_u64;
6801    println!("K =\t{:#016X}", key);
6802    let mut a_des = DES::new_with_key_u64(key);
6803
6804    let message = "PARK Youngho";
6805    println!("M =\t{}", message);
6806    let iv = 0x_FEDCBA0987654321_u64;
6807    println!("IV =	{}", iv);
6808    let mut cipher = [0_u8; 12];
6809    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6810    print!("C =\t");
6811    for c in cipher.clone()
6812        { print!("{:02X} ", c); }
6813    println!();
6814    let mut txt = String::new();
6815    for c in cipher.clone()
6816        { write!(txt, "{:02X} ", c); }
6817    assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 74 F9 B6 4F ");
6818
6819    let mut recovered = String::new();
6820    a_des.decrypt_array_into_string(iv, &cipher, &mut recovered);
6821    println!("B =\t{}", recovered);
6822    assert_eq!(recovered, "PARK Youngho");
6823    assert_eq!(recovered, message);
6824    println!();
6825
6826    // Normal case for the message of 16 bytes
6827    let key = 0x_1234567890ABCDEF_u64;
6828    println!("K =\t{:#016X}", key);
6829    let mut a_des = DES::new_with_key_u64(key);
6830
6831    let message = "고맙습니다.";
6832    println!("M =\t{}", message);
6833    let iv = 0x_FEDCBA0987654321_u64;
6834    println!("IV =	{}", iv);
6835    let mut cipher = [0_u8; 16];
6836    a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6837    print!("C =\t");
6838    for c in cipher.clone()
6839        { print!("{:02X} ", c); }
6840    println!();
6841    let mut txt = String::new();
6842    for c in cipher.clone()
6843        { write!(txt, "{:02X} ", c); }
6844    assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 F4 FC 94 B5 94 1F B9 BD ");
6845
6846    let mut recovered = String::new();
6847    a_des.decrypt_array_into_string(iv, &cipher, &mut recovered);
6848    println!("B =\t{}", recovered);
6849    assert_eq!(recovered, "고맙습니다.");
6850    assert_eq!(recovered, message);
6851    println!("-------------------------------");
6852}