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