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