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