ndes_basic_quick_examples/
ndes_basic_quick_examples.rs1#![allow(missing_docs)]
10#![allow(unused_must_use)]
11#![allow(dead_code)]
12
13
14pub fn main()
15{
16 ndes_quick_start_main();
17 ndes_basic_operation_main();
18}
19
20fn ndes_quick_start_main()
21{
22 ndes_quick_start_instantiation_with_keys();
23}
24
25fn ndes_quick_start_instantiation_with_keys()
26{
27 println!("des_quick_start_instantiation_with_keys()");
28 println!("-------------------------------");
30}
31
32
33fn ndes_basic_operation_main()
34{
35 ndes_new_with_keys();
36 ndes_new_with_keys_u64();
37}
38
39fn ndes_new_with_keys()
40{
41 println!("ndes_new_with_keys()");
42 use cryptocol::symmetric::{ BigCryptor64, DES, SmallCryptor };
43
44 let keys: [Box<dyn SmallCryptor<u64, 8>>; 3]
45 = [ Box::new(DES::encryptor_with_key_u64(0x1234567890ABCDEF_u64)),
46 Box::new(DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)),
47 Box::new(DES::encryptor_with_key_u64(0x1234567890ABCDEF_u64)) ];
48 let mut tdes = BigCryptor64::new_with_small_cryptor_array(keys);
49 let plaintext = 0x1234567890ABCDEF_u64;
50 let ciphertext = tdes.encrypt_u64(plaintext);
51
52 println!("Plaintext:\t\t{:#016X}", plaintext);
53 println!("Ciphertext:\t\t{:#016X}", ciphertext);
54 assert_eq!(ciphertext, 0x272A2AC7B4E66748_u64);
55
56 let cipher_cipher_text = tdes.decrypt_u64(ciphertext);
57 println!("Cipher-ciphertext:\t{:#016X}", cipher_cipher_text);
58 assert_eq!(cipher_cipher_text, 0x1234567890ABCDEF_u64);
59 assert_eq!(cipher_cipher_text, plaintext); println!("-------------------------------");
61}
62
63fn ndes_new_with_keys_u64()
64{
65 println!("ndes_new_with_keys_u64()");
66 use std::fmt::Write as _;
67 use cryptocol::symmetric::{ BigCryptor64, DES, ECB_PKCS7 };
68
69 let mut tdes = BigCryptor64::new_with_small_cryptor_array(
70 [Box::new(DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)),
71 Box::new(DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)),
72 Box::new(DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64))]
73 );
74
75 let message = "I am OK.";
76 println!("M =\t{}", message);
77 let mut cipher = [0_u8; 16];
78 tdes.encrypt_str_into_array(&message, &mut cipher);
79 print!("C =\t");
80 for c in cipher.clone()
81 { print!("{:02X} ", c); }
82 println!();
83 let mut txt = String::new();
84 for c in cipher.clone()
85 { write!(txt, "{:02X} ", c); }
86 assert_eq!(txt, "88 89 99 44 30 72 CA 2F 22 4F 7C E0 55 FA 28 C3 ");
87 println!();
88
89 let mut recovered = vec![0; 16];
90 let len = tdes.decrypt_array_into_vec(&cipher, &mut recovered);
91 print!("Ba =\t");
92 for b in recovered.clone()
93 { print!("{:02X} ", b); }
94 println!();
95 let mut txt = String::new();
96 for c in recovered.clone()
97 { write!(txt, "{:02X} ", c); }
98 assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E ");
99
100 let mut converted = String::new();
101 unsafe { converted.as_mut_vec() }.append(&mut recovered);
102 converted.truncate(len as usize);
103
104 println!("Bb =\t{}", converted);
105 assert_eq!(converted, "I am OK.");
106 assert_eq!(converted, message);
107 println!("-------------------------------");
108}