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