cryptocol/symmetric/trait_cbc_with_padding_iso.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
10#![allow(missing_docs)]
11#![allow(unused_must_use)]
12#![allow(dead_code)]
13#![allow(unused_variables)]
14// #![warn(rustdoc::missing_doc_code_examples)]
15
16
17use std::vec::Vec;
18use crate::number::SmallUInt;
19use crate::symmetric::pre_decrypt_into_vec;
20
21
22
23/// CBC (Cipher-Block Chaining) is one of the operation modes for
24/// encryption/decryption. And ISO 7816-4 is the one of the padding ways.
25#[allow(non_camel_case_types)]
26pub trait CBC_ISO<T> : Sized
27{
28 // fn encrypt(&mut self, iv: T, message: *const u8, length_in_bytes: u64, cipher: *mut u8) -> u64;
29 /// Encrypts the data with the padding defined according to ISO 7816-4
30 /// in CBC (Cipher-Block Chaining) mode.
31 ///
32 /// # Arguments
33 /// - `iv` is an initialization vector for CBC mode.
34 /// - `message` is an immutable pointer to `u8` which is `*const u8`,
35 /// and is the place where the plaintext to be encrypted is stored.
36 /// - `length_in_bytes` is of `u64`-type,
37 /// and is the length of the plaintext `message` in bytes.
38 /// - `cipher` is a mutable pointer to `u8` which is `*mut u8`, and
39 /// is the place where the encrypted data will be stored.
40 ///
41 /// # Output
42 /// - This method returns the size of ciphertext including padding bits
43 /// in bytes.
44 /// - The output will be at least `size_of::<T>()`,
45 /// and cannot be other than a multiple of `size_of::<T>()`.
46 /// - If this method failed in encryption,
47 /// this method returns `zero`.
48 ///
49 /// # Features
50 /// - You are not encouraged to use this method in pure Rust programming.
51 /// Instead, use other safer methods such as `encrypt_*_into_*()`.
52 /// - This method is useful to use in hybrid programming with C/C++.
53 /// - If `length_in_bytes` is `0`, it means the message is null string.
54 /// So, only padding bytes will be encrypted,
55 /// and stored in the memory area that starts from `cipher`.
56 /// - The size of the memory area which starts at `cipher` is assumed to be
57 /// enough to store the ciphertext.
58 /// - The size of the area for ciphertext should be prepared to be
59 /// (`length_in_bytes` + `1`).next_multiple_of(`size_of::<T>()`) at least.
60 /// So, it is responsible for you to prepare the `cipher` area big enough!
61 /// - The padding bits are composed of the byte `0b_1000_0000` that
62 /// indicates the delimiter one bit `1` followed by seven bits `0`s and
63 /// all padding bits `0`s according to ISO 7816-4.
64 /// - For more information about the padding bits according to ISO 7816-4,
65 /// Read [here](https://en.wikipedia.org/wiki/Padding_(cryptography)#ISO/IEC_7816-4).
66 ///
67 /// # For Rijndael or AES, and its variants
68 /// ## Example 1 for AES-128
69 /// ```
70 /// use std::io::Write;
71 /// use std::fmt::Write as _;
72 /// use cryptocol::symmetric::{ AES_128, CBC_ISO };
73 ///
74 /// let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
75 /// println!("K =\t{:#016X}", key);
76 /// let mut a_aes = AES_128::new_with_key_u128(key);
77 /// let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
78 /// println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
79 ///
80 /// let message = "In the beginning God created the heavens and the earth.";
81 /// println!("M =\t{}", message);
82 /// let mut cipher = [0_u8; 64];
83 /// a_aes.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
84 /// print!("C =\t");
85 /// for c in cipher.clone()
86 /// { print!("{:02X} ", c); }
87 /// println!();
88 /// let mut txt = String::new();
89 /// for c in cipher.clone()
90 /// { write!(txt, "{:02X} ", c); }
91 /// assert_eq!(txt, "C9 1C 27 CE 83 92 A1 CF 7D A4 64 35 16 48 01 72 CC E3 6D CD BB 19 FC D0 80 22 09 9F 23 32 73 27 58 37 F9 9B 3C 44 7B 03 B3 80 7E 99 DF 97 4E E9 EC D3 12 F1 1A 78 77 A6 A5 CB 73 AB 65 22 5B 7D ");
92 /// ```
93 ///
94 /// ## For more examples,
95 /// click [here](./documentation/rijndael_cbc_iso/struct.Rijndael_Generic.html#method.encrypt)
96 ///
97 /// # For DES and its variants
98 /// ## Example 1 for Normal case
99 /// ```
100 /// use std::io::Write;
101 /// use std::fmt::Write as _;
102 /// use cryptocol::symmetric::{ DES, CBC_ISO };
103 ///
104 /// let key = 0x_1234567890ABCDEF_u64;
105 /// println!("K =\t{:#016X}", key);
106 /// let mut a_des = DES::new_with_key_u64(key);
107 ///
108 /// let message = "In the beginning God created the heavens and the earth.";
109 /// println!("M =\t{}", message);
110 /// let iv = 0x_FEDCBA0987654321_u64;
111 /// println!("IV = {}", iv);
112 /// let mut cipher = [0_u8; 56];
113 /// a_des.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
114 /// print!("C (16 rounds) =\t");
115 /// for c in cipher.clone()
116 /// { print!("{:02X} ", c); }
117 /// println!();
118 /// let mut txt = String::new();
119 /// for c in cipher.clone()
120 /// { write!(txt, "{:02X} ", c); }
121 /// assert_eq!(txt, "4B B5 ED DC A0 58 7E 6D 6C 3B A2 00 38 C3 D4 29 42 B1 CF 0D E9 FA EA 11 11 6B C8 30 73 39 DD B7 3F 96 9B A3 76 05 34 7E 64 2F D4 CC B2 68 33 64 11 78 69 FB 0B 32 CF 92 ");
122 /// ```
123 ///
124 /// ## For more examples,
125 /// click [here](./documentation/des_cbc_iso/struct.DES_Generic.html#method.encrypt)
126 ///
127 /// # For BigCryptor128
128 /// ## Example 1 for TAES case
129 /// ```
130 /// use std::io::Write;
131 /// use std::fmt::Write as _;
132 /// use cryptocol::symmetric::{ BigCryptor128, AES_128, CBC_ISO };
133 ///
134 /// let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
135 /// + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
136 /// + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
137 /// let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
138 /// println!("IV = {:#034X}", iv);
139 /// let message = "In the beginning God created the heavens and the earth.";
140 /// println!("M =\t{}", message);
141 /// let mut cipher = [0_u8; 64];
142 /// taes.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
143 /// print!("C =\t");
144 /// for c in cipher.clone()
145 /// { print!("{:02X} ", c); }
146 /// println!();
147 /// let mut txt = String::new();
148 /// for c in cipher.clone()
149 /// { write!(txt, "{:02X} ", c); }
150 /// assert_eq!(txt, "87 B7 B0 67 C9 41 CF C6 CF B8 69 0D 39 D3 9B EC 0C 18 28 9A FA 51 7D 2A E4 D1 0C 2F 2B A0 0E E3 D1 04 65 E4 B6 66 2B 23 04 42 66 31 7A C5 76 E0 73 1D B1 F8 BB 75 C5 FB C2 1B 73 26 67 57 02 A0 ");
151 /// ```
152 ///
153 /// ## For more examples,
154 /// click [here](./documentation/big_cryptor128_cbc_iso/struct.BigCryptor128.html#method.encrypt)
155 ///
156 /// # For BigCryptor64
157 /// ## Example 1 for TDES case
158 /// ```
159 /// use std::io::Write;
160 /// use std::fmt::Write as _;
161 /// use cryptocol::symmetric::{ BigCryptor64, DES, CBC_ISO };
162 ///
163 /// let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
164 /// + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
165 /// + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
166 /// let iv = 0x_FEDCBA0987654321_u64;
167 /// println!("IV = {:#018X}", iv);
168 /// let message = "In the beginning God created the heavens and the earth.";
169 /// println!("M =\t{}", message);
170 /// let mut cipher = [0_u8; 56];
171 /// tdes.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
172 /// print!("C =\t");
173 /// for c in cipher.clone()
174 /// { print!("{:02X} ", c); }
175 /// println!();
176 /// let mut txt = String::new();
177 /// for c in cipher.clone()
178 /// { write!(txt, "{:02X} ", c); }
179 /// assert_eq!(txt, "01 A5 7E BC ED 83 28 FB 7A 06 61 60 7D 8B BA AF 5C A7 76 0B FE 94 1C C4 C2 BC DD 15 98 A9 98 6C 85 18 92 6B 00 72 FB 72 DB 9D 46 BD B1 3C 56 C0 ED 3C 12 45 CA 1D 7E A3 ");
180 /// ```
181 ///
182 /// ## For more examples,
183 /// click [here](./documentation/big_cryptor64_cbc_iso/struct.BigCryptor64.html#method.encrypt)
184 fn encrypt(&mut self, iv: T, message: *const u8, length_in_bytes: u64, cipher: *mut u8) -> u64;
185
186 // fn encrypt_into_vec<U>(&mut self, iv: T, message: *const u8, length_in_bytes: u64, cipher: &mut Vec<U>) -> u64
187 /// Encrypts the data with the padding defined according to ISO 7816-4
188 /// in CBC (Cipher-Block Chaining) mode, and stores the encrypted data
189 /// in `Vec<U>`.
190 ///
191 /// # Arguments
192 /// - `iv` is an initialization vector for CBC mode.
193 /// - `message` is an immutable pointer to `u8` which is `*const u8`,
194 /// and is the place where the plaintext to be encrypted is stored.
195 /// - `length_in_bytes` is of `u64`-type,
196 /// and is the length of the plaintext `message` in bytes.
197 /// - `cipher` is a mutable reference to `Vec<U>` object, and
198 /// is the place where the encrypted data will be stored.
199 ///
200 /// # Output
201 /// - This method returns the size of ciphertext including padding bits
202 /// in bytes.
203 /// - The output will be at least `size_of::<T>()`,
204 /// and cannot be other than a multiple of `size_of::<T>()`.
205 /// - If this method failed in encryption,
206 /// this method returns `zero`.
207 ///
208 /// # Features
209 /// - You are not encouraged to use this method in pure Rust programming.
210 /// Instead, use other safer methods such as encrypt_*_into_vec().
211 /// - This method is useful to use in hybrid programming with C/C++.
212 /// - If `length_in_bytes` is `0`, it means the message is a null string.
213 /// So, only padding bytes will be encrypted,
214 /// and stored in the `Vec<U>` object which is referred to as `cipher`.
215 /// - The padding bits are composed of the byte `0b_1000_0000` that
216 /// indicates the delimiter one bit `1` followed by seven bits `0`s and
217 /// all padding bits `0`s according to ISO 7816-4.
218 /// - For more information about the padding bits according to ISO 7816-4,
219 /// Read [here](https://en.wikipedia.org/wiki/Padding_(cryptography)#ISO/IEC_7816-4).
220 /// - You don't have to worry about whether or not the size of the memory
221 /// area where the ciphertext will be stored is enough.
222 ///
223 /// # For Rijndael or AES, and its variants
224 /// ## Example 1 for AES-128
225 /// ```
226 /// use std::io::Write;
227 /// use std::fmt::Write as _;
228 /// use cryptocol::symmetric::{ AES_128, CBC_ISO };
229 ///
230 /// let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
231 /// println!("K =\t{:#016X}", key);
232 /// let mut a_aes = AES_128::new_with_key_u128(key);
233 /// let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
234 /// println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
235 ///
236 /// let message = "In the beginning God created the heavens and the earth.";
237 /// println!("M =\t{}", message);
238 /// let mut cipher = Vec::<u8>::new();
239 /// a_aes.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher);
240 /// print!("C =\t");
241 /// for c in cipher.clone()
242 /// { print!("{:02X} ", c); }
243 /// println!();
244 /// let mut txt = String::new();
245 /// for c in cipher.clone()
246 /// { write!(txt, "{:02X} ", c); }
247 /// assert_eq!(txt, "C9 1C 27 CE 83 92 A1 CF 7D A4 64 35 16 48 01 72 CC E3 6D CD BB 19 FC D0 80 22 09 9F 23 32 73 27 58 37 F9 9B 3C 44 7B 03 B3 80 7E 99 DF 97 4E E9 EC D3 12 F1 1A 78 77 A6 A5 CB 73 AB 65 22 5B 7D ");
248 /// ```
249 ///
250 /// ## For more examples,
251 /// click [here](./documentation/rijndael_cbc_iso/struct.Rijndael_Generic.html#method.encrypt_into_vec)
252 ///
253 /// # For DES and its variants
254 /// ## Example 1 for Normal case
255 /// ```
256 /// use std::io::Write;
257 /// use std::fmt::Write as _;
258 /// use cryptocol::symmetric::{ DES, CBC_ISO };
259 ///
260 /// let key = 0x_1234567890ABCDEF_u64;
261 /// println!("K =\t{:#016X}", key);
262 /// let mut a_des = DES::new_with_key_u64(key);
263 ///
264 /// let message = "In the beginning God created the heavens and the earth.";
265 /// println!("M =\t{}", message);
266 /// let iv = 0x_FEDCBA0987654321_u64;
267 /// println!("IV = {}", iv);
268 /// let mut cipher = Vec::<u8>::new();
269 /// a_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher);
270 /// print!("C (16 rounds) =\t");
271 /// for c in cipher.clone()
272 /// { print!("{:02X} ", c); }
273 /// println!();
274 /// let mut txt = String::new();
275 /// for c in cipher.clone()
276 /// { write!(txt, "{:02X} ", c); }
277 /// assert_eq!(txt, "4B B5 ED DC A0 58 7E 6D 6C 3B A2 00 38 C3 D4 29 42 B1 CF 0D E9 FA EA 11 11 6B C8 30 73 39 DD B7 3F 96 9B A3 76 05 34 7E 64 2F D4 CC B2 68 33 64 11 78 69 FB 0B 32 CF 92 ");
278 /// ```
279 ///
280 /// ## For more examples,
281 /// click [here](./documentation/des_cbc_iso/struct.DES_Generic.html#method.encrypt_into_vec)
282 ///
283 /// # For BigCryptor128
284 /// ## Example 1 for TAES case
285 /// ```
286 /// use std::io::Write;
287 /// use std::fmt::Write as _;
288 /// use cryptocol::symmetric::{ BigCryptor128, AES_128, CBC_ISO };
289 ///
290 /// let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
291 /// + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
292 /// + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
293 /// let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
294 /// println!("IV = {:#034X}", iv);
295 /// let message = "In the beginning God created the heavens and the earth.";
296 /// println!("M =\t{}", message);
297 /// let mut cipher = Vec::<u8>::new();
298 /// taes.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher);
299 /// print!("C =\t");
300 /// for c in cipher.clone()
301 /// { print!("{:02X} ", c); }
302 /// println!();
303 /// let mut txt = String::new();
304 /// for c in cipher.clone()
305 /// { write!(txt, "{:02X} ", c); }
306 /// assert_eq!(txt, "87 B7 B0 67 C9 41 CF C6 CF B8 69 0D 39 D3 9B EC 0C 18 28 9A FA 51 7D 2A E4 D1 0C 2F 2B A0 0E E3 D1 04 65 E4 B6 66 2B 23 04 42 66 31 7A C5 76 E0 73 1D B1 F8 BB 75 C5 FB C2 1B 73 26 67 57 02 A0 ");
307 /// ```
308 ///
309 /// ## For more examples,
310 /// click [here](./documentation/big_cryptor128_cbc_iso/struct.BigCryptor128.html#method.encrypt_into_vec)
311 ///
312 /// # For BigCryptor64
313 /// ## Example 1 for TDES case
314 /// ```
315 /// use std::io::Write;
316 /// use std::fmt::Write as _;
317 /// use cryptocol::symmetric::{ BigCryptor64, DES, CBC_ISO };
318 ///
319 /// let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
320 /// + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
321 /// + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
322 /// let iv = 0x_FEDCBA0987654321_u64;
323 /// println!("IV = {:#018X}", iv);
324 /// let message = "In the beginning God created the heavens and the earth.";
325 /// println!("M =\t{}", message);
326 /// let mut cipher = Vec::<u8>::new();
327 /// tdes.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher);
328 /// print!("C =\t");
329 /// for c in cipher.clone()
330 /// { print!("{:02X} ", c); }
331 /// println!();
332 /// let mut txt = String::new();
333 /// for c in cipher.clone()
334 /// { write!(txt, "{:02X} ", c); }
335 /// assert_eq!(txt, "01 A5 7E BC ED 83 28 FB 7A 06 61 60 7D 8B BA AF 5C A7 76 0B FE 94 1C C4 C2 BC DD 15 98 A9 98 6C 85 18 92 6B 00 72 FB 72 DB 9D 46 BD B1 3C 56 C0 ED 3C 12 45 CA 1D 7E A3 ");
336 /// ```
337 ///
338 /// ## For more examples,
339 /// click [here](./documentation/big_cryptor64_cbc_iso/struct.BigCryptor64.html#method.encrypt_into_vec)
340 fn encrypt_into_vec<U>(&mut self, iv: T, message: *const u8, length_in_bytes: u64, cipher: &mut Vec<U>) -> u64
341 where U: SmallUInt + Copy + Clone;
342
343 // fn encrypt_into_array<U, const N: usize>(&mut self, iv: T, message: *const u8, length_in_bytes: u64, cipher: &mut [U; N]) -> u64
344 /// Encrypts the data with the padding defined according to ISO 7816-4
345 /// in CBC (Cipher-Block Chaining) mode, and stores the encrypted data
346 /// in array `[U; N]`.
347 ///
348 /// # Arguments
349 /// - `iv` is an initialization vector for CBC mode.
350 /// - `message` is an immutable pointer to `u8` which is `*const u8`,
351 /// and is the place where the plaintext to be encrypted is stored.
352 /// - `length_in_bytes` is of `u64`-type,
353 /// and is the length of the plaintext `message` in bytes.
354 /// - `cipher` is a mutable reference to an array `[U; N]` object, and
355 /// is the place where the encrypted data will be stored.
356 ///
357 /// # Output
358 /// - This method returns the size of ciphertext including padding bits
359 /// in bytes.
360 /// - The output will be at least `size_of::<T>()`,
361 /// and cannot be other than a multiple of `size_of::<T>()`.
362 /// - If this method failed in encryption, this method returns `zero`.
363 ///
364 /// # Features
365 /// - You are not encouraged to use this method in pure Rust programming.
366 /// Instead, use other safer methods such as encrypt_*_into_array().
367 /// - This method is useful to use in hybrid programming with C/C++.
368 /// - If `length_in_bytes` is `0`, it means the message is null data.
369 /// So, only padding bytes will be encrypted,
370 /// and stored in the array `[U; N]` object `cipher`.
371 /// - If `U::size_in_bytes()` * `N` is less than `length_in_bytes`'s next
372 /// multiple of `size_of::<T>()`, this method does not perform
373 /// encryption but returns `zero`.
374 /// - If `U::size_in_bytes()` * `N` is equal to `length_in_bytes`'s next
375 /// multiple of `size_of::<T>()`, this method performs encryption,
376 /// fills the array `cipher` with the encrypted data, and returns
377 /// the size of the ciphertext including padding bits in bytes.
378 /// - If `U::size_in_bytes()` * `N` is greater than `length_in_bytes`'s next
379 /// multiple of `size_of::<T>()`, this method performs encryption, fills
380 /// the array `cipher` with the encrypted data, and then fills the
381 /// rest of the elements of the array `cipher` with zeros, and returns
382 /// the size of the ciphertext including padding bits in bytes.
383 /// - The size of the area for ciphertext should be prepared to be
384 /// (`length_in_bytes` + `1`).next_multiple_of(`size_of::<T>()`) at least.
385 /// So, it is responsible for you to prepare the `cipher` area big enough!
386 /// - The padding bits are composed of the byte `0b_1000_0000` that
387 /// indicates the delimiter one bit `1` followed by seven bits `0`s and
388 /// all padding bits `0`s according to ISO 7816-4.
389 /// - For more information about the padding bits according to ISO 7816-4,
390 /// Read [here](https://en.wikipedia.org/wiki/Padding_(cryptography)#ISO/IEC_7816-4).
391 ///
392 /// # For Rijndael or AES, and its variants
393 /// ## Example 1 for AES-128
394 /// ```
395 /// use std::io::Write;
396 /// use std::fmt::Write as _;
397 /// use cryptocol::symmetric::{ AES_128, CBC_ISO };
398 ///
399 /// let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
400 /// println!("K =\t{:#016X}", key);
401 /// let mut a_aes = AES_128::new_with_key_u128(key);
402 /// let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
403 /// println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
404 ///
405 /// let message = "In the beginning God created the heavens and the earth.";
406 /// println!("M =\t{}", message);
407 /// let mut cipher = [0_u8; 64];
408 /// a_aes.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
409 /// print!("C =\t");
410 /// for c in cipher.clone()
411 /// { print!("{:02X} ", c); }
412 /// println!();
413 /// let mut txt = String::new();
414 /// for c in cipher.clone()
415 /// { write!(txt, "{:02X} ", c); }
416 /// assert_eq!(txt, "C9 1C 27 CE 83 92 A1 CF 7D A4 64 35 16 48 01 72 CC E3 6D CD BB 19 FC D0 80 22 09 9F 23 32 73 27 58 37 F9 9B 3C 44 7B 03 B3 80 7E 99 DF 97 4E E9 EC D3 12 F1 1A 78 77 A6 A5 CB 73 AB 65 22 5B 7D ");
417 /// ```
418 ///
419 /// ## For more examples,
420 /// click [here](./documentation/rijndael_cbc_iso/struct.Rijndael_Generic.html#method.encrypt_into_array)
421 ///
422 /// # For DES and its variants
423 /// ## Example 1 for Normal case
424 /// ```
425 /// use std::io::Write;
426 /// use std::fmt::Write as _;
427 /// use cryptocol::symmetric::{ DES, CBC_ISO };
428 ///
429 /// let key = 0x_1234567890ABCDEF_u64;
430 /// println!("K =\t{:#016X}", key);
431 /// let mut a_des = DES::new_with_key_u64(key);
432 ///
433 /// let message = "In the beginning God created the heavens and the earth.";
434 /// println!("M =\t{}", message);
435 /// let iv = 0x_FEDCBA0987654321_u64;
436 /// println!("IV = {}", iv);
437 /// let mut cipher = [0_u8; 56];
438 /// a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
439 /// print!("C (16 rounds) =\t");
440 /// for c in cipher.clone()
441 /// { print!("{:02X} ", c); }
442 /// println!();
443 /// let mut txt = String::new();
444 /// for c in cipher.clone()
445 /// { write!(txt, "{:02X} ", c); }
446 /// assert_eq!(txt, "4B B5 ED DC A0 58 7E 6D 6C 3B A2 00 38 C3 D4 29 42 B1 CF 0D E9 FA EA 11 11 6B C8 30 73 39 DD B7 3F 96 9B A3 76 05 34 7E 64 2F D4 CC B2 68 33 64 11 78 69 FB 0B 32 CF 92 ");
447 /// ```
448 ///
449 /// ## For more examples,
450 /// click [here](./documentation/des_cbc_iso/struct.DES_Generic.html#method.encrypt_into_array)
451 ///
452 /// # For BigCryptor128
453 /// ## Example 1 for TAES case
454 /// ```
455 /// use std::io::Write;
456 /// use std::fmt::Write as _;
457 /// use cryptocol::symmetric::{ BigCryptor128, AES_128, CBC_ISO };
458 ///
459 /// let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
460 /// + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
461 /// + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
462 /// let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
463 /// println!("IV = {:#034X}", iv);
464 /// let message = "In the beginning God created the heavens and the earth.";
465 /// println!("M =\t{}", message);
466 /// let mut cipher = [0_u8; 64];
467 /// taes.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
468 /// print!("C =\t");
469 /// for c in cipher.clone()
470 /// { print!("{:02X} ", c); }
471 /// println!();
472 /// let mut txt = String::new();
473 /// for c in cipher.clone()
474 /// { write!(txt, "{:02X} ", c); }
475 /// assert_eq!(txt, "87 B7 B0 67 C9 41 CF C6 CF B8 69 0D 39 D3 9B EC 0C 18 28 9A FA 51 7D 2A E4 D1 0C 2F 2B A0 0E E3 D1 04 65 E4 B6 66 2B 23 04 42 66 31 7A C5 76 E0 73 1D B1 F8 BB 75 C5 FB C2 1B 73 26 67 57 02 A0 ");
476 /// ```
477 ///
478 /// ## For more examples,
479 /// click [here](./documentation/big_cryptor128_cbc_iso/struct.BigCryptor128.html#method.encrypt_into_array)
480 ///
481 /// # For BigCryptor64
482 /// ## Example 1 for TDES case
483 /// ```
484 /// use std::io::Write;
485 /// use std::fmt::Write as _;
486 /// use cryptocol::symmetric::{ BigCryptor64, DES, CBC_ISO };
487 ///
488 /// let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
489 /// + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
490 /// + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
491 /// let iv = 0x_FEDCBA0987654321_u64;
492 /// println!("IV = {:#018X}", iv);
493 /// let message = "In the beginning God created the heavens and the earth.";
494 /// println!("M =\t{}", message);
495 /// let mut cipher = [0_u8; 56];
496 /// tdes.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
497 /// print!("C =\t");
498 /// for c in cipher.clone()
499 /// { print!("{:02X} ", c); }
500 /// println!();
501 /// let mut txt = String::new();
502 /// for c in cipher.clone()
503 /// { write!(txt, "{:02X} ", c); }
504 /// assert_eq!(txt, "01 A5 7E BC ED 83 28 FB 7A 06 61 60 7D 8B BA AF 5C A7 76 0B FE 94 1C C4 C2 BC DD 15 98 A9 98 6C 85 18 92 6B 00 72 FB 72 DB 9D 46 BD B1 3C 56 C0 ED 3C 12 45 CA 1D 7E A3 ");
505 /// ```
506 ///
507 /// ## For more examples,
508 /// click [here](./documentation/big_cryptor64_cbc_iso/struct.BigCryptor64.html#method.encrypt_into_array)
509 fn encrypt_into_array<U, const N: usize>(&mut self, iv: T, message: *const u8, length_in_bytes: u64, cipher: &mut [U; N]) -> u64
510 where U: SmallUInt + Copy + Clone;
511
512 // fn encrypt_str(&mut self, iv: T, message: &str, cipher: *mut u8) -> u64
513 /// Encrypts the data in a `str` object with the padding defined
514 /// according to ISO 7816-4 in CBC (Cipher-Block Chaining) mode.
515 ///
516 /// # Arguments
517 /// - `iv` is an initialization vector for CBC mode.
518 /// - `message` is an immutable reference to `str` object which is `&str`,
519 /// and is the place where the plaintext to be encrypted is stored.
520 /// - `cipher` is a mutable pointer to `u8` which is `*mut u8`, and
521 /// is the place where the encrypted data will be stored.
522 ///
523 /// # Output
524 /// - This method returns the size of ciphertext including padding bits
525 /// in bytes.
526 /// - The output will be at least `size_of::<T>()`,
527 /// and cannot be other than a multiple of `size_of::<T>()`.
528 /// - If this method failed in encryption, this method returns `zero`.
529 ///
530 /// # Features
531 /// - You are not encouraged to use this method in pure Rust programming.
532 /// Instead, use other safer methods such as encrypt_str_into_*().
533 /// - This method is useful to use in hybrid programming with C/C++.
534 /// - If `message` is a null string "", only padding bytes will be encrypted,
535 /// and stored in the memory area that starts from `cipher`.
536 /// - The size of the memory area which starts at `cipher` is assumed to be
537 /// enough to store the ciphertext.
538 /// - The size of the area for ciphertext should be prepared to be
539 /// (`message.len()` + `1`).next_multiple_of(`size_of::<T>()`) at least.
540 /// So, it is responsible for you to prepare the `cipher` area big enough!
541 /// - The padding bits are composed of the byte `0b_1000_0000` that
542 /// indicates the delimiter one bit `1` followed by seven bits `0`s and
543 /// all padding bits `0`s according to ISO 7816-4.
544 /// - For more information about the padding bits according to ISO 7816-4,
545 /// Read [here](https://en.wikipedia.org/wiki/Padding_(cryptography)#ISO/IEC_7816-4).
546 ///
547 /// # For Rijndael or AES, and its variants
548 /// ## Example 1 for AES-128
549 /// ```
550 /// use std::io::Write;
551 /// use std::fmt::Write as _;
552 /// use cryptocol::symmetric::{ AES_128, CBC_ISO };
553 ///
554 /// let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
555 /// println!("K =\t{:#016X}", key);
556 /// let mut a_aes = AES_128::new_with_key_u128(key);
557 /// let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
558 /// println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
559 ///
560 /// let message = "In the beginning God created the heavens and the earth.";
561 /// println!("M =\t{}", message);
562 /// let mut cipher = [0_u8; 64];
563 /// a_aes.encrypt_str(iv, &message, cipher.as_mut_ptr());
564 /// print!("C =\t");
565 /// for c in cipher.clone()
566 /// { print!("{:02X} ", c); }
567 /// println!();
568 /// let mut txt = String::new();
569 /// for c in cipher.clone()
570 /// { write!(txt, "{:02X} ", c); }
571 /// assert_eq!(txt, "C9 1C 27 CE 83 92 A1 CF 7D A4 64 35 16 48 01 72 CC E3 6D CD BB 19 FC D0 80 22 09 9F 23 32 73 27 58 37 F9 9B 3C 44 7B 03 B3 80 7E 99 DF 97 4E E9 EC D3 12 F1 1A 78 77 A6 A5 CB 73 AB 65 22 5B 7D ");
572 /// ```
573 ///
574 /// ## For more examples,
575 /// click [here](./documentation/rijndael_cbc_iso/struct.Rijndael_Generic.html#method.encrypt_str)
576 ///
577 /// # For DES and its variants
578 /// ## Example 1 for Normal case
579 /// ```
580 /// use std::io::Write;
581 /// use std::fmt::Write as _;
582 /// use cryptocol::symmetric::{ DES, CBC_ISO };
583 ///
584 /// let key = 0x_1234567890ABCDEF_u64;
585 /// println!("K =\t{:#016X}", key);
586 /// let mut a_des = DES::new_with_key_u64(key);
587 ///
588 /// let message = "In the beginning God created the heavens and the earth.";
589 /// println!("M =\t{}", message);
590 /// let iv = 0x_FEDCBA0987654321_u64;
591 /// println!("IV = {}", iv);
592 /// let mut cipher = [0_u8; 56];
593 /// a_des.encrypt_str(iv, &message, cipher.as_mut_ptr());
594 /// print!("C (16 rounds) =\t");
595 /// for c in cipher.clone()
596 /// { print!("{:02X} ", c); }
597 /// println!();
598 /// let mut txt = String::new();
599 /// for c in cipher.clone()
600 /// { write!(txt, "{:02X} ", c); }
601 /// assert_eq!(txt, "4B B5 ED DC A0 58 7E 6D 6C 3B A2 00 38 C3 D4 29 42 B1 CF 0D E9 FA EA 11 11 6B C8 30 73 39 DD B7 3F 96 9B A3 76 05 34 7E 64 2F D4 CC B2 68 33 64 11 78 69 FB 0B 32 CF 92 ");
602 /// ```
603 ///
604 /// ## For more examples,
605 /// click [here](./documentation/des_cbc_iso/struct.DES_Generic.html#method.encrypt_str)
606 ///
607 /// # For BigCryptor128
608 /// ## Example 1 for TAES case
609 /// ```
610 /// use std::io::Write;
611 /// use std::fmt::Write as _;
612 /// use cryptocol::symmetric::{ BigCryptor128, AES_128, CBC_ISO };
613 ///
614 /// let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
615 /// + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
616 /// + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
617 /// let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
618 /// println!("IV = {:#034X}", iv);
619 /// let message = "In the beginning God created the heavens and the earth.";
620 /// println!("M =\t{}", message);
621 /// let mut cipher = [0_u8; 64];
622 /// taes.encrypt_str(iv, &message, cipher.as_mut_ptr());
623 /// print!("C =\t");
624 /// for c in cipher.clone()
625 /// { print!("{:02X} ", c); }
626 /// println!();
627 /// let mut txt = String::new();
628 /// for c in cipher.clone()
629 /// { write!(txt, "{:02X} ", c); }
630 /// assert_eq!(txt, "87 B7 B0 67 C9 41 CF C6 CF B8 69 0D 39 D3 9B EC 0C 18 28 9A FA 51 7D 2A E4 D1 0C 2F 2B A0 0E E3 D1 04 65 E4 B6 66 2B 23 04 42 66 31 7A C5 76 E0 73 1D B1 F8 BB 75 C5 FB C2 1B 73 26 67 57 02 A0 ");
631 /// ```
632 ///
633 /// ## For more examples,
634 /// click [here](./documentation/big_cryptor128_cbc_iso/struct.BigCryptor128.html#method.encrypt_str)
635 ///
636 /// # For BigCryptor64
637 /// ## Example 1 for TDES case
638 /// ```
639 /// use std::io::Write;
640 /// use std::fmt::Write as _;
641 /// use cryptocol::symmetric::{ BigCryptor64, DES, CBC_ISO };
642 ///
643 /// let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
644 /// + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
645 /// + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
646 /// let iv = 0x_FEDCBA0987654321_u64;
647 /// println!("IV = {:#018X}", iv);
648 /// let message = "In the beginning God created the heavens and the earth.";
649 /// println!("M =\t{}", message);
650 /// let mut cipher = [0_u8; 56];
651 /// tdes.encrypt_str(iv, &message, cipher.as_mut_ptr());
652 /// print!("C =\t");
653 /// for c in cipher.clone()
654 /// { print!("{:02X} ", c); }
655 /// println!();
656 /// let mut txt = String::new();
657 /// for c in cipher.clone()
658 /// { write!(txt, "{:02X} ", c); }
659 /// assert_eq!(txt, "01 A5 7E BC ED 83 28 FB 7A 06 61 60 7D 8B BA AF 5C A7 76 0B FE 94 1C C4 C2 BC DD 15 98 A9 98 6C 85 18 92 6B 00 72 FB 72 DB 9D 46 BD B1 3C 56 C0 ED 3C 12 45 CA 1D 7E A3 ");
660 /// ```
661 ///
662 /// ## For more examples,
663 /// click [here](./documentation/big_cryptor64_cbc_iso/struct.BigCryptor64.html#method.encrypt_str)
664 #[inline]
665 fn encrypt_str(&mut self, iv: T, message: &str, cipher: *mut u8) -> u64
666 {
667 self.encrypt(iv, message.as_ptr(), message.len() as u64, cipher)
668 }
669
670 // fn encrypt_str_into_vec<U>(&mut self, iv: T, message: &str, cipher: &mut Vec<U>) -> u64
671 /// Encrypts the data in `str` with the padding defined according to
672 /// ISO 7816-4 in CBC (Cipher-Block Chaining) mode, and stores the
673 /// encrypted data in `Vec<U>`.
674 ///
675 /// # Arguments
676 /// - `iv` is an initialization vector for CBC mode.
677 /// - `message` is an immutable reference to `str` object which is `&str`,
678 /// and is the place where the plaintext to be encrypted is stored.
679 /// - `cipher` is a mutable reference to `Vec<U>` object, and
680 /// is the place where the encrypted data will be stored.
681 ///
682 /// # Output
683 /// - This method returns the size of ciphertext including padding bits
684 /// in bytes.
685 /// - The output will be at least `size_of::<T>()`,
686 /// and cannot be other than a multiple of `size_of::<T>()`.
687 /// - If this method failed in encryption, this method returns `zero`.
688 ///
689 /// # Features
690 /// - If `message` is a null string "", only padding bytes will be encrypted,
691 /// and stored in the `Vec<U>` object which is referred to as `cipher`.
692 /// - The padding bits are composed of the byte `0b_1000_0000` that
693 /// indicates the delimiter one bit `1` followed by seven bits `0`s and
694 /// all padding bits `0`s according to ISO 7816-4.
695 /// - For more information about the padding bits according to ISO 7816-4,
696 /// Read [here](https://en.wikipedia.org/wiki/Padding_(cryptography)#ISO/IEC_7816-4).
697 /// - You don't have to worry about whether or not the size of the memory
698 /// area where the ciphertext will be stored is enough.
699 ///
700 /// # For Rijndael or AES, and its variants
701 /// ## Example 1 for AES-128
702 /// ```
703 /// use std::io::Write;
704 /// use std::fmt::Write as _;
705 /// use cryptocol::symmetric::{ AES_128, CBC_ISO };
706 ///
707 /// let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
708 /// println!("K =\t{:#016X}", key);
709 /// let mut a_aes = AES_128::new_with_key_u128(key);
710 /// let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
711 /// println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
712 ///
713 /// let message = "In the beginning God created the heavens and the earth.";
714 /// println!("M =\t{}", message);
715 /// let mut cipher = Vec::<u8>::new();
716 /// a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
717 /// print!("C =\t");
718 /// for c in cipher.clone()
719 /// { print!("{:02X} ", c); }
720 /// println!();
721 /// let mut txt = String::new();
722 /// for c in cipher.clone()
723 /// { write!(txt, "{:02X} ", c); }
724 /// assert_eq!(txt, "C9 1C 27 CE 83 92 A1 CF 7D A4 64 35 16 48 01 72 CC E3 6D CD BB 19 FC D0 80 22 09 9F 23 32 73 27 58 37 F9 9B 3C 44 7B 03 B3 80 7E 99 DF 97 4E E9 EC D3 12 F1 1A 78 77 A6 A5 CB 73 AB 65 22 5B 7D ");
725 /// ```
726 ///
727 /// ## For more examples,
728 /// click [here](./documentation/rijndael_cbc_iso/struct.Rijndael_Generic.html#method.encrypt_str_into_vec)
729 ///
730 /// # For DES and its variants
731 /// ## Example 1 for Normal case
732 /// ```
733 /// use std::io::Write;
734 /// use std::fmt::Write as _;
735 /// use cryptocol::symmetric::{ DES, CBC_ISO };
736 ///
737 /// let key = 0x_1234567890ABCDEF_u64;
738 /// println!("K =\t{:#016X}", key);
739 /// let mut a_des = DES::new_with_key_u64(key);
740 ///
741 /// let message = "In the beginning God created the heavens and the earth.";
742 /// println!("M =\t{}", message);
743 /// let iv = 0x_FEDCBA0987654321_u64;
744 /// println!("IV = {}", iv);
745 /// let mut cipher = Vec::<u8>::new();
746 /// a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
747 /// print!("C (16 rounds) =\t");
748 /// for c in cipher.clone()
749 /// { print!("{:02X} ", c); }
750 /// println!();
751 /// let mut txt = String::new();
752 /// for c in cipher.clone()
753 /// { write!(txt, "{:02X} ", c); }
754 /// assert_eq!(txt, "4B B5 ED DC A0 58 7E 6D 6C 3B A2 00 38 C3 D4 29 42 B1 CF 0D E9 FA EA 11 11 6B C8 30 73 39 DD B7 3F 96 9B A3 76 05 34 7E 64 2F D4 CC B2 68 33 64 11 78 69 FB 0B 32 CF 92 ");
755 /// ```
756 ///
757 /// ## For more examples,
758 /// click [here](./documentation/des_cbc_iso/struct.DES_Generic.html#method.encrypt_str_into_vec)
759 ///
760 /// # For BigCryptor128
761 /// ## Example 1 for TAES case
762 /// ```
763 /// use std::io::Write;
764 /// use std::fmt::Write as _;
765 /// use cryptocol::symmetric::{ BigCryptor128, AES_128, CBC_ISO };
766 ///
767 /// let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
768 /// + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
769 /// + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
770 /// let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
771 /// println!("IV = {:#034X}", iv);
772 /// let message = "In the beginning God created the heavens and the earth.";
773 /// let mut cipher = Vec::<u8>::new();
774 /// taes.encrypt_str_into_vec(iv, &message, &mut cipher);
775 /// print!("C =\t");
776 /// for c in cipher.clone()
777 /// { print!("{:02X} ", c); }
778 /// println!();
779 /// let mut txt = String::new();
780 /// for c in cipher.clone()
781 /// { write!(txt, "{:02X} ", c); }
782 /// assert_eq!(txt, "87 B7 B0 67 C9 41 CF C6 CF B8 69 0D 39 D3 9B EC 0C 18 28 9A FA 51 7D 2A E4 D1 0C 2F 2B A0 0E E3 D1 04 65 E4 B6 66 2B 23 04 42 66 31 7A C5 76 E0 73 1D B1 F8 BB 75 C5 FB C2 1B 73 26 67 57 02 A0 ");
783 /// ```
784 ///
785 /// ## For more examples,
786 /// click [here](./documentation/big_cryptor128_cbc_iso/struct.BigCryptor128.html#method.encrypt_str_into_vec)
787 ///
788 /// # For BigCryptor64
789 /// ## Example 1 for TDES case
790 /// ```
791 /// use std::io::Write;
792 /// use std::fmt::Write as _;
793 /// use cryptocol::symmetric::{ BigCryptor64, DES, CBC_ISO };
794 ///
795 /// let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
796 /// + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
797 /// + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
798 /// let iv = 0x_FEDCBA0987654321_u64;
799 /// println!("IV = {:#018X}", iv);
800 /// let message = "In the beginning God created the heavens and the earth.";
801 /// let mut cipher = Vec::<u8>::new();
802 /// tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
803 /// print!("C =\t");
804 /// for c in cipher.clone()
805 /// { print!("{:02X} ", c); }
806 /// println!();
807 /// let mut txt = String::new();
808 /// for c in cipher.clone()
809 /// { write!(txt, "{:02X} ", c); }
810 /// assert_eq!(txt, "01 A5 7E BC ED 83 28 FB 7A 06 61 60 7D 8B BA AF 5C A7 76 0B FE 94 1C C4 C2 BC DD 15 98 A9 98 6C 85 18 92 6B 00 72 FB 72 DB 9D 46 BD B1 3C 56 C0 ED 3C 12 45 CA 1D 7E A3 ");
811 /// ```
812 ///
813 /// ## For more examples,
814 /// click [here](./documentation/big_cryptor64_cbc_iso/struct.BigCryptor64.html#method.encrypt_str_into_vec)
815 #[inline]
816 fn encrypt_str_into_vec<U>(&mut self, iv: T, message: &str, cipher: &mut Vec<U>) -> u64
817 where U: SmallUInt + Copy + Clone
818 {
819 self.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, cipher)
820 }
821
822 // fn encrypt_str_into_array<U, const N: usize>(&mut self, iv: T, message: &str, cipher: &mut [U; N]) -> u64
823 /// Encrypts the data in a `str` object with the padding defined
824 /// according to ISO 7816-4 in CBC (Cipher-Block Chaining) mode,
825 /// and stores the encrypted data in array `[U; N]`.
826 ///
827 /// # Arguments
828 /// - `iv` is an initialization vector for CBC mode.
829 /// - `message` is an immutable reference to `str` object which is `&str`,
830 /// and is the place where the plaintext to be encrypted is stored.
831 /// - `cipher` is a mutable reference to an array `[U; N]` object, and
832 /// is the place where the encrypted data will be stored.
833 ///
834 /// # Output
835 /// - This method returns the size of ciphertext including padding bits
836 /// in bytes.
837 /// - The output will be at least `size_of::<T>()`,
838 /// and cannot be other than a multiple of `size_of::<T>()`.
839 /// - If this method failed in encryption, this method returns `zero`.
840 ///
841 /// # Features
842 /// - If `message` is a null string "", only padding bytes will be encrypted,
843 /// and stored in the array `[U; N]` object `cipher`.
844 /// - If `U::size_in_bytes()` * `N` is less than `message.len()`'s next
845 /// multiple of `size_of::<T>()`, this method does not perform
846 /// encryption but returns `zero`.
847 /// - If `U::size_in_bytes()` * `N` is equal to `message.len()`'s next
848 /// multiple of `size_of::<T>()`, this method performs encryption,
849 /// fills the array `cipher` with the encrypted data, and returns
850 /// the size of the ciphertext including padding bits in bytes.
851 /// - If `U::size_in_bytes()` * `N` is greater than `message.len()`'s next
852 /// multiple of `size_of::<T>()`, this method performs encryption, fills
853 /// the array `cipher` with the encrypted data, and then fills the
854 /// rest of the elements of the array `cipher` with zeros, and returns
855 /// the size of the ciphertext including padding bits in bytes.
856 /// - The size of the area for ciphertext should be prepared to be
857 /// (`message.len()` + `1`).next_multiple_of(`size_of::<T>()`) at least.
858 /// So, it is responsible for you to prepare the `cipher` area big enough!
859 /// - The padding bits are composed of the byte `0b_1000_0000` that
860 /// indicates the delimiter one bit `1` followed by seven bits `0`s and
861 /// all padding bits `0`s according to ISO 7816-4.
862 /// - For more information about the padding bits according to ISO 7816-4,
863 /// Read [here](https://en.wikipedia.org/wiki/Padding_(cryptography)#ISO/IEC_7816-4).
864 ///
865 /// # For Rijndael or AES, and its variants
866 /// ## Example 1 for AES-128
867 /// ```
868 /// use std::io::Write;
869 /// use std::fmt::Write as _;
870 /// use cryptocol::symmetric::{ AES_128, CBC_ISO };
871 ///
872 /// let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
873 /// println!("K =\t{:#016X}", key);
874 /// let mut a_aes = AES_128::new_with_key_u128(key);
875 /// let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
876 /// println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
877 ///
878 /// let message = "In the beginning God created the heavens and the earth.";
879 /// println!("M =\t{}", message);
880 /// let mut cipher = [0_u8; 64];
881 /// a_aes.encrypt_str_into_array(iv, &message, &mut cipher);
882 /// print!("C =\t");
883 /// for c in cipher.clone()
884 /// { print!("{:02X} ", c); }
885 /// println!();
886 /// let mut txt = String::new();
887 /// for c in cipher.clone()
888 /// { write!(txt, "{:02X} ", c); }
889 /// assert_eq!(txt, "C9 1C 27 CE 83 92 A1 CF 7D A4 64 35 16 48 01 72 CC E3 6D CD BB 19 FC D0 80 22 09 9F 23 32 73 27 58 37 F9 9B 3C 44 7B 03 B3 80 7E 99 DF 97 4E E9 EC D3 12 F1 1A 78 77 A6 A5 CB 73 AB 65 22 5B 7D ");
890 /// ```
891 ///
892 /// ## For more examples,
893 /// click [here](./documentation/rijndael_cbc_iso/struct.Rijndael_Generic.html#method.encrypt_str_into_array)
894 ///
895 /// # For DES and its variants
896 /// ## Example 1 for Normal case
897 /// ```
898 /// use std::io::Write;
899 /// use std::fmt::Write as _;
900 /// use cryptocol::symmetric::{ DES, CBC_ISO };
901 ///
902 /// let key = 0x_1234567890ABCDEF_u64;
903 /// println!("K =\t{:#016X}", key);
904 /// let mut a_des = DES::new_with_key_u64(key);
905 ///
906 /// let message = "In the beginning God created the heavens and the earth.";
907 /// println!("M =\t{}", message);
908 /// let iv = 0x_FEDCBA0987654321_u64;
909 /// println!("IV = {}", iv);
910 /// let mut cipher = [0_u8; 56];
911 /// a_des.encrypt_str_into_array(iv, &message, &mut cipher);
912 /// print!("C (16 rounds) =\t");
913 /// for c in cipher.clone()
914 /// { print!("{:02X} ", c); }
915 /// println!();
916 /// let mut txt = String::new();
917 /// for c in cipher.clone()
918 /// { write!(txt, "{:02X} ", c); }
919 /// assert_eq!(txt, "4B B5 ED DC A0 58 7E 6D 6C 3B A2 00 38 C3 D4 29 42 B1 CF 0D E9 FA EA 11 11 6B C8 30 73 39 DD B7 3F 96 9B A3 76 05 34 7E 64 2F D4 CC B2 68 33 64 11 78 69 FB 0B 32 CF 92 ");
920 /// ```
921 ///
922 /// ## For more examples,
923 /// click [here](./documentation/des_cbc_iso/struct.DES_Generic.html#method.encrypt_str_into_array)
924 ///
925 /// # For BigCryptor128
926 /// ## Example 1 for TAES case
927 /// ```
928 /// use std::io::Write;
929 /// use std::fmt::Write as _;
930 /// use cryptocol::symmetric::{ BigCryptor128, AES_128, CBC_ISO };
931 ///
932 /// let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
933 /// + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
934 /// + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
935 /// let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
936 /// println!("IV = {:#034X}", iv);
937 /// let message = "In the beginning God created the heavens and the earth.";
938 /// let mut cipher = [0_u8; 64];
939 /// taes.encrypt_str_into_array(iv, &message, &mut cipher);
940 /// print!("C =\t");
941 /// for c in cipher.clone()
942 /// { print!("{:02X} ", c); }
943 /// println!();
944 /// let mut txt = String::new();
945 /// for c in cipher.clone()
946 /// { write!(txt, "{:02X} ", c); }
947 /// assert_eq!(txt, "87 B7 B0 67 C9 41 CF C6 CF B8 69 0D 39 D3 9B EC 0C 18 28 9A FA 51 7D 2A E4 D1 0C 2F 2B A0 0E E3 D1 04 65 E4 B6 66 2B 23 04 42 66 31 7A C5 76 E0 73 1D B1 F8 BB 75 C5 FB C2 1B 73 26 67 57 02 A0 ");
948 /// ```
949 ///
950 /// ## For more examples,
951 /// click [here](./documentation/big_cryptor128_cbc_iso/struct.BigCryptor128.html#method.encrypt_str_into_array)
952 ///
953 /// # For BigCryptor64
954 /// ## Example 1 for TDES case
955 /// ```
956 /// use std::io::Write;
957 /// use std::fmt::Write as _;
958 /// use cryptocol::symmetric::{ BigCryptor64, DES, CBC_ISO };
959 ///
960 /// let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
961 /// + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
962 /// + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
963 /// let iv = 0x_FEDCBA0987654321_u64;
964 /// println!("IV = {:#018X}", iv);
965 /// let message = "In the beginning God created the heavens and the earth.";
966 /// let mut cipher = [0_u8; 56];
967 /// tdes.encrypt_str_into_array(iv, &message, &mut cipher);
968 /// print!("C =\t");
969 /// for c in cipher.clone()
970 /// { print!("{:02X} ", c); }
971 /// println!();
972 /// let mut txt = String::new();
973 /// for c in cipher.clone()
974 /// { write!(txt, "{:02X} ", c); }
975 /// assert_eq!(txt, "01 A5 7E BC ED 83 28 FB 7A 06 61 60 7D 8B BA AF 5C A7 76 0B FE 94 1C C4 C2 BC DD 15 98 A9 98 6C 85 18 92 6B 00 72 FB 72 DB 9D 46 BD B1 3C 56 C0 ED 3C 12 45 CA 1D 7E A3 ");
976 /// ```
977 ///
978 /// ## For more examples,
979 /// click [here](./documentation/big_cryptor64_cbc_iso/struct.BigCryptor64.html#method.encrypt_str_into_array)
980 #[inline]
981 fn encrypt_str_into_array<U, const N: usize>(&mut self, iv: T, message: &str, cipher: &mut [U; N]) -> u64
982 where U: SmallUInt + Copy + Clone
983 {
984 self.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, cipher)
985 }
986
987 // fn encrypt_string(&mut self, iv: T, message: &String, cipher: *mut u8) -> u64
988 /// Encrypts the data stored in a `String` object with the padding
989 /// according to ISO 7816-4 in CBC (Cipher-Block Chaining) mode.
990 ///
991 /// # Arguments
992 /// - `iv` is an initialization vector for CBC mode.
993 /// - `message` is an immutable reference to `String` object, and
994 /// is the place where the plaintext to be encrypted is stored.
995 /// - `cipher` is a mutable pointer to `u8` which is `*mut u8`, and
996 /// is the place where the encrypted data will be stored.
997 ///
998 /// # Output
999 /// - This method returns the size of ciphertext including padding bits
1000 /// in bytes.
1001 /// - The output will be at least `size_of::<T>()`,
1002 /// and cannot be other than a multiple of `size_of::<T>()`.
1003 /// - If this method failed in encryption, this method returns `zero`.
1004 ///
1005 /// # Features
1006 /// - You are not encouraged to use this method in pure Rust programming.
1007 /// Instead, use other safer methods such as encrypt_string_into_*().
1008 /// - This method is useful to use in hybrid programming with C/C++.
1009 /// - If `message` is a `String` object that has a null string "", only
1010 /// padding bytes will be encrypted, and stored in the memory area that
1011 /// starts from `cipher`.
1012 /// - The size of the memory area which starts at `cipher` is assumed to be
1013 /// enough to store the ciphertext.
1014 /// - The size of the area for ciphertext should be prepared to be
1015 /// (`message.len()` + `1`).next_multiple_of(`size_of::<T>()`) at least.
1016 /// So, it is responsible for you to prepare the `cipher` area big enough!
1017 /// - The padding bits are composed of the byte `0b_1000_0000` that
1018 /// indicates the delimiter one bit `1` followed by seven bits `0`s and
1019 /// all padding bits `0`s according to ISO 7816-4.
1020 /// - For more information about the padding bits according to ISO 7816-4,
1021 /// Read [here](https://en.wikipedia.org/wiki/Padding_(cryptography)#ISO/IEC_7816-4).
1022 ///
1023 /// # For Rijndael or AES, and its variants
1024 /// ## Example 1 for AES-128
1025 /// ```
1026 /// use std::io::Write;
1027 /// use std::fmt::Write as _;
1028 /// use cryptocol::symmetric::{ AES_128, CBC_ISO };
1029 ///
1030 /// let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1031 /// println!("K =\t{:#016X}", key);
1032 /// let mut a_aes = AES_128::new_with_key_u128(key);
1033 /// let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1034 /// println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1035 ///
1036 /// let message = "In the beginning God created the heavens and the earth.".to_string();
1037 /// println!("M =\t{}", message);
1038 /// let mut cipher = [0_u8; 64];
1039 /// a_aes.encrypt_string(iv, &message, cipher.as_mut_ptr());
1040 /// print!("C =\t");
1041 /// for c in cipher.clone()
1042 /// { print!("{:02X} ", c); }
1043 /// println!();
1044 /// let mut txt = String::new();
1045 /// for c in cipher.clone()
1046 /// { write!(txt, "{:02X} ", c); }
1047 /// assert_eq!(txt, "C9 1C 27 CE 83 92 A1 CF 7D A4 64 35 16 48 01 72 CC E3 6D CD BB 19 FC D0 80 22 09 9F 23 32 73 27 58 37 F9 9B 3C 44 7B 03 B3 80 7E 99 DF 97 4E E9 EC D3 12 F1 1A 78 77 A6 A5 CB 73 AB 65 22 5B 7D ");
1048 /// ```
1049 ///
1050 /// ## For more examples,
1051 /// click [here](./documentation/rijndael_cbc_iso/struct.Rijndael_Generic.html#method.encrypt_string)
1052 ///
1053 /// # For DES and its variants
1054 /// ## Example 1 for Normal case
1055 /// ```
1056 /// use std::io::Write;
1057 /// use std::fmt::Write as _;
1058 /// use cryptocol::symmetric::{ DES, CBC_ISO };
1059 ///
1060 /// let key = 0x_1234567890ABCDEF_u64;
1061 /// println!("K =\t{:#016X}", key);
1062 /// let mut a_des = DES::new_with_key_u64(key);
1063 ///
1064 /// let message = "In the beginning God created the heavens and the earth.".to_string();
1065 /// println!("M =\t{}", message);
1066 /// let iv = 0x_FEDCBA0987654321_u64;
1067 /// println!("IV = {}", iv);
1068 /// let mut cipher = [0_u8; 56];
1069 /// a_des.encrypt_string(iv, &message, cipher.as_mut_ptr());
1070 /// print!("C (16 rounds) =\t");
1071 /// for c in cipher.clone()
1072 /// { print!("{:02X} ", c); }
1073 /// println!();
1074 /// let mut txt = String::new();
1075 /// for c in cipher.clone()
1076 /// { write!(txt, "{:02X} ", c); }
1077 /// assert_eq!(txt, "4B B5 ED DC A0 58 7E 6D 6C 3B A2 00 38 C3 D4 29 42 B1 CF 0D E9 FA EA 11 11 6B C8 30 73 39 DD B7 3F 96 9B A3 76 05 34 7E 64 2F D4 CC B2 68 33 64 11 78 69 FB 0B 32 CF 92 ");
1078 /// ```
1079 ///
1080 /// ## For more examples,
1081 /// click [here](./documentation/des_cbc_iso/struct.DES_Generic.html#method.encrypt_string)
1082 ///
1083 /// # For BigCryptor128
1084 /// ## Example 1 for TAES case
1085 /// ```
1086 /// use std::io::Write;
1087 /// use std::fmt::Write as _;
1088 /// use cryptocol::symmetric::{ BigCryptor128, AES_128, CBC_ISO };
1089 ///
1090 /// let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
1091 /// + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
1092 /// + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
1093 /// let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
1094 /// println!("IV = {:#034X}", iv);
1095 /// let message = "In the beginning God created the heavens and the earth.".to_string();
1096 /// let mut cipher = [0_u8; 64];
1097 /// taes.encrypt_string(iv, &message, cipher.as_mut_ptr());
1098 /// print!("C =\t");
1099 /// for c in cipher.clone()
1100 /// { print!("{:02X} ", c); }
1101 /// println!();
1102 /// let mut txt = String::new();
1103 /// for c in cipher.clone()
1104 /// { write!(txt, "{:02X} ", c); }
1105 /// assert_eq!(txt, "87 B7 B0 67 C9 41 CF C6 CF B8 69 0D 39 D3 9B EC 0C 18 28 9A FA 51 7D 2A E4 D1 0C 2F 2B A0 0E E3 D1 04 65 E4 B6 66 2B 23 04 42 66 31 7A C5 76 E0 73 1D B1 F8 BB 75 C5 FB C2 1B 73 26 67 57 02 A0 ");
1106 /// ```
1107 ///
1108 /// ## For more examples,
1109 /// click [here](./documentation/big_cryptor64_cbc_iso/struct.BigCryptor128.html#method.encrypt_string)
1110 ///
1111 /// # For BigCryptor128
1112 /// ## Example 1 for TAES case
1113 /// ```
1114 /// use std::io::Write;
1115 /// use std::fmt::Write as _;
1116 /// use cryptocol::symmetric::{ BigCryptor128, AES_128, CBC_ISO };
1117 ///
1118 /// let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
1119 /// + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
1120 /// + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
1121 /// let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
1122 /// println!("IV = {:#034X}", iv);
1123 /// let message = "In the beginning God created the heavens and the earth.".to_string();
1124 /// let mut cipher = [0_u8; 64];
1125 /// taes.encrypt_string(iv, &message, cipher.as_mut_ptr());
1126 /// print!("C =\t");
1127 /// for c in cipher.clone()
1128 /// { print!("{:02X} ", c); }
1129 /// println!();
1130 /// let mut txt = String::new();
1131 /// for c in cipher.clone()
1132 /// { write!(txt, "{:02X} ", c); }
1133 /// assert_eq!(txt, "87 B7 B0 67 C9 41 CF C6 CF B8 69 0D 39 D3 9B EC 0C 18 28 9A FA 51 7D 2A E4 D1 0C 2F 2B A0 0E E3 D1 04 65 E4 B6 66 2B 23 04 42 66 31 7A C5 76 E0 73 1D B1 F8 BB 75 C5 FB C2 1B 73 26 67 57 02 A0 ");
1134 /// ```
1135 ///
1136 /// ## For more examples,
1137 /// click [here](./documentation/big_cryptor128_cbc_iso/struct.BigCryptor128.html#method.encrypt_string)
1138 ///
1139 /// # For BigCryptor64
1140 /// ## Example 1 for TDES case
1141 /// ```
1142 /// use std::io::Write;
1143 /// use std::fmt::Write as _;
1144 /// use cryptocol::symmetric::{ BigCryptor64, DES, CBC_ISO };
1145 ///
1146 /// let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
1147 /// + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
1148 /// + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
1149 /// let iv = 0x_FEDCBA0987654321_u64;
1150 /// println!("IV = {:#018X}", iv);
1151 /// let message = "In the beginning God created the heavens and the earth.".to_string();
1152 /// let mut cipher = [0_u8; 56];
1153 /// tdes.encrypt_string(iv, &message, cipher.as_mut_ptr());
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, "01 A5 7E BC ED 83 28 FB 7A 06 61 60 7D 8B BA AF 5C A7 76 0B FE 94 1C C4 C2 BC DD 15 98 A9 98 6C 85 18 92 6B 00 72 FB 72 DB 9D 46 BD B1 3C 56 C0 ED 3C 12 45 CA 1D 7E A3 ");
1162 /// ```
1163 ///
1164 /// ## For more examples,
1165 /// click [here](./documentation/big_cryptor64_cbc_iso/struct.BigCryptor64.html#method.encrypt_string)
1166 #[inline]
1167 fn encrypt_string(&mut self, iv: T, message: &String, cipher: *mut u8) -> u64
1168 {
1169 self.encrypt(iv, message.as_ptr(), message.len() as u64, cipher)
1170 }
1171
1172 // fn encrypt_string_into_vec<U>(&mut self, iv: T, message: &String, cipher: &mut Vec<U>) -> u64
1173 /// Encrypts the data stored in a `String` object with the padding
1174 /// according to ISO 7816-4 in CBC (Cipher-Block Chaining) mode,
1175 /// and stores the encrypted data in `Vec<U>`.
1176 ///
1177 /// # Arguments
1178 /// - `iv` is an initialization vector for CBC mode.
1179 /// - `message` is an immutable reference to `String` object, and
1180 /// is the place where the plaintext to be encrypted is stored.
1181 /// - `cipher` is a mutable reference to `Vec<U>` object, and
1182 /// is the place where the encrypted data will be stored.
1183 ///
1184 /// # Output
1185 /// - This method returns the size of ciphertext including padding bits
1186 /// in bytes.
1187 /// - The output will be at least `size_of::<T>()`,
1188 /// and cannot be other than a multiple of `size_of::<T>()`.
1189 /// - If this method failed in encryption, this method returns `zero`.
1190 ///
1191 /// # Features
1192 /// - If `message` is a `String` object that has a null string "", only
1193 /// padding bytes will be encrypted, and stored in the `Vec<U>` object
1194 /// which is referred to as `cipher`.
1195 /// - The padding bits are composed of the byte `0b_1000_0000` that
1196 /// indicates the delimiter one bit `1` followed by seven bits `0`s and
1197 /// all padding bits `0`s according to ISO 7816-4.
1198 /// - For more information about the padding bits according to ISO 7816-4,
1199 /// Read [here](https://en.wikipedia.org/wiki/Padding_(cryptography)#ISO/IEC_7816-4).
1200 /// - You don't have to worry about whether or not the size of the memory
1201 /// area where the ciphertext will be stored is enough.
1202 ///
1203 /// # For Rijndael or AES, and its variants
1204 /// ## Example 1 for AES-128
1205 /// ```
1206 /// use std::io::Write;
1207 /// use std::fmt::Write as _;
1208 /// use cryptocol::symmetric::{ AES_128, CBC_ISO };
1209 ///
1210 /// let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1211 /// println!("K =\t{:#016X}", key);
1212 /// let mut a_aes = AES_128::new_with_key_u128(key);
1213 /// let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1214 /// println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1215 ///
1216 /// let message = "In the beginning God created the heavens and the earth.".to_string();
1217 /// println!("M =\t{}", message);
1218 /// let mut cipher = Vec::<u8>::new();
1219 /// a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
1220 /// print!("C =\t");
1221 /// for c in cipher.clone()
1222 /// { print!("{:02X} ", c); }
1223 /// println!();
1224 /// let mut txt = String::new();
1225 /// for c in cipher.clone()
1226 /// { write!(txt, "{:02X} ", c); }
1227 /// assert_eq!(txt, "C9 1C 27 CE 83 92 A1 CF 7D A4 64 35 16 48 01 72 CC E3 6D CD BB 19 FC D0 80 22 09 9F 23 32 73 27 58 37 F9 9B 3C 44 7B 03 B3 80 7E 99 DF 97 4E E9 EC D3 12 F1 1A 78 77 A6 A5 CB 73 AB 65 22 5B 7D ");
1228 /// ```
1229 ///
1230 /// ## For more examples,
1231 /// click [here](./documentation/rijndael_cbc_iso/struct.Rijndael_Generic.html#method.encrypt_string_into_vec)
1232 ///
1233 /// # For DES and its variants
1234 /// ## Example 1 for Normal case
1235 /// ```
1236 /// use std::io::Write;
1237 /// use std::fmt::Write as _;
1238 /// use cryptocol::symmetric::{ DES, CBC_ISO };
1239 ///
1240 /// let key = 0x_1234567890ABCDEF_u64;
1241 /// println!("K =\t{:#016X}", key);
1242 /// let mut a_des = DES::new_with_key_u64(key);
1243 ///
1244 /// let message = "In the beginning God created the heavens and the earth.".to_string();
1245 /// println!("M =\t{}", message);
1246 /// let iv = 0x_FEDCBA0987654321_u64;
1247 /// println!("IV = {}", iv);
1248 /// let mut cipher = Vec::<u8>::new();
1249 /// a_des.encrypt_string_into_vec(iv, &message, &mut cipher);
1250 /// print!("C (16 rounds) =\t");
1251 /// for c in cipher.clone()
1252 /// { print!("{:02X} ", c); }
1253 /// println!();
1254 /// let mut txt = String::new();
1255 /// for c in cipher.clone()
1256 /// { write!(txt, "{:02X} ", c); }
1257 /// assert_eq!(txt, "4B B5 ED DC A0 58 7E 6D 6C 3B A2 00 38 C3 D4 29 42 B1 CF 0D E9 FA EA 11 11 6B C8 30 73 39 DD B7 3F 96 9B A3 76 05 34 7E 64 2F D4 CC B2 68 33 64 11 78 69 FB 0B 32 CF 92 ");
1258 /// ```
1259 ///
1260 /// ## For more examples,
1261 /// click [here](./documentation/des_cbc_iso/struct.DES_Generic.html#method.encrypt_string_into_vec)
1262 ///
1263 /// # For BigCryptor128
1264 /// ## Example 1 for TAES case
1265 /// ```
1266 /// use std::io::Write;
1267 /// use std::fmt::Write as _;
1268 /// use cryptocol::symmetric::{ BigCryptor128, AES_128, CBC_ISO };
1269 ///
1270 /// let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
1271 /// + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
1272 /// + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
1273 /// let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
1274 /// println!("IV = {:#034X}", iv);
1275 /// let message = "In the beginning God created the heavens and the earth.".to_string();
1276 /// let mut cipher = Vec::<u8>::new();
1277 /// taes.encrypt_string_into_vec(iv, &message, &mut cipher);
1278 /// print!("C =\t");
1279 /// for c in cipher.clone()
1280 /// { print!("{:02X} ", c); }
1281 /// println!();
1282 /// let mut txt = String::new();
1283 /// for c in cipher.clone()
1284 /// { write!(txt, "{:02X} ", c); }
1285 /// assert_eq!(txt, "87 B7 B0 67 C9 41 CF C6 CF B8 69 0D 39 D3 9B EC 0C 18 28 9A FA 51 7D 2A E4 D1 0C 2F 2B A0 0E E3 D1 04 65 E4 B6 66 2B 23 04 42 66 31 7A C5 76 E0 73 1D B1 F8 BB 75 C5 FB C2 1B 73 26 67 57 02 A0 ");
1286 /// ```
1287 ///
1288 /// ## For more examples,
1289 /// click [here](./documentation/big_cryptor128_cbc_iso/struct.BigCryptor128.html#method.encrypt_string_into_vec)
1290 ///
1291 /// # For BigCryptor64
1292 /// ## Example 1 for TDES case
1293 /// ```
1294 /// use std::io::Write;
1295 /// use std::fmt::Write as _;
1296 /// use cryptocol::symmetric::{ BigCryptor64, DES, CBC_ISO };
1297 ///
1298 /// let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
1299 /// + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
1300 /// + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
1301 /// let iv = 0x_FEDCBA0987654321_u64;
1302 /// println!("IV = {:#018X}", iv);
1303 /// let message = "In the beginning God created the heavens and the earth.".to_string();
1304 /// let mut cipher = Vec::<u8>::new();
1305 /// tdes.encrypt_string_into_vec(iv, &message, &mut cipher);
1306 /// print!("C =\t");
1307 /// for c in cipher.clone()
1308 /// { print!("{:02X} ", c); }
1309 /// println!();
1310 /// let mut txt = String::new();
1311 /// for c in cipher.clone()
1312 /// { write!(txt, "{:02X} ", c); }
1313 /// assert_eq!(txt, "01 A5 7E BC ED 83 28 FB 7A 06 61 60 7D 8B BA AF 5C A7 76 0B FE 94 1C C4 C2 BC DD 15 98 A9 98 6C 85 18 92 6B 00 72 FB 72 DB 9D 46 BD B1 3C 56 C0 ED 3C 12 45 CA 1D 7E A3 ");
1314 /// ```
1315 ///
1316 /// ## For more examples,
1317 /// click [here](./documentation/big_cryptor64_cbc_iso/struct.BigCryptor64.html#method.encrypt_string_into_vec)
1318 #[inline]
1319 fn encrypt_string_into_vec<U>(&mut self, iv: T, message: &String, cipher: &mut Vec<U>) -> u64
1320 where U: SmallUInt + Copy + Clone
1321 {
1322 self.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, cipher)
1323 }
1324
1325 // fn encrypt_string_into_array<U, const N: usize>(&mut self, iv: T, message: &String, cipher: &mut [U; N]) -> u64
1326 /// Encrypts the data stored in a `String` object with the padding
1327 /// according to ISO 7816-4 in CBC (Cipher-Block Chaining) mode,
1328 /// and stores the encrypted data in array `[U; N]`.
1329 ///
1330 /// # Arguments
1331 /// - `iv` is an initialization vector for CBC mode.
1332 /// - `message` is an immutable reference to `String` object, and
1333 /// is the place where the plaintext to be encrypted is stored.
1334 /// - `cipher` is a mutable reference to an array `[U; N]` object, and
1335 /// is the place where the encrypted data will be stored.
1336 ///
1337 /// # Output
1338 /// - This method returns the size of ciphertext including padding bits
1339 /// in bytes.
1340 /// - The output will be at least `size_of::<T>()`,
1341 /// and cannot be other than a multiple of `size_of::<T>()`.
1342 /// - If this method failed in encryption, this method returns `zero`.
1343 ///
1344 /// # Features
1345 /// - If `message` is a `String` object that has a null string "", only
1346 /// padding bytes will be encrypted, and stored in the array `[U; N]`
1347 /// object `cipher`.
1348 /// - If `size_of::<U>()` * `N` is less than `message.len()`'s next
1349 /// multiple of `size_of::<T>()`, this method does not perform
1350 /// encryption but returns `zero`.
1351 /// - If `size_of::<U>()` * `N` is equal to `message.len()`'s next
1352 /// multiple of `size_of::<T>()`, this method performs encryption,
1353 /// fills the array `cipher` with the encrypted data, and returns
1354 /// the size of the ciphertext including padding bits in bytes.
1355 /// - If `size_of::<U>()` * `N` is greater than `message.len()`'s next
1356 /// multiple of `size_of::<T>()`, this method performs encryption, fills
1357 /// the array `cipher` with the encrypted data, and then fills the
1358 /// rest of the elements of the array `cipher` with zeros, and returns
1359 /// the size of the ciphertext including padding bits in bytes.
1360 /// - The size of the area for ciphertext should be prepared to be
1361 /// (`message.len()` + `1`).next_multiple_of(`size_of::<T>()`) at least.
1362 /// So, it is responsible for you to prepare the `cipher` area big enough!
1363 /// - The padding bits are composed of the byte `0b_1000_0000` that
1364 /// indicates the delimiter one bit `1` followed by seven bits `0`s and
1365 /// all padding bits `0`s according to ISO 7816-4.
1366 /// - For more information about the padding bits according to ISO 7816-4,
1367 /// Read [here](https://en.wikipedia.org/wiki/Padding_(cryptography)#ISO/IEC_7816-4).
1368 ///
1369 /// # For Rijndael or AES, and its variants
1370 /// ## Example 1 for AES-128
1371 /// ```
1372 /// use std::io::Write;
1373 /// use std::fmt::Write as _;
1374 /// use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, CBC_ISO };
1375 ///
1376 /// let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1377 /// println!("K =\t{:#016X}", key);
1378 /// let mut a_aes = AES_128::new_with_key_u128(key);
1379 /// let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1380 /// println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1381 ///
1382 /// let message = "In the beginning God created the heavens and the earth.".to_string();
1383 /// println!("M =\t{}", message);
1384 /// let mut cipher = [0_u8; 64];
1385 /// a_aes.encrypt_str_into_array(iv, &message, &mut cipher);
1386 /// print!("C =\t");
1387 /// for c in cipher.clone()
1388 /// { print!("{:02X} ", c); }
1389 /// println!();
1390 /// let mut txt = String::new();
1391 /// for c in cipher.clone()
1392 /// { write!(txt, "{:02X} ", c); }
1393 /// assert_eq!(txt, "C9 1C 27 CE 83 92 A1 CF 7D A4 64 35 16 48 01 72 CC E3 6D CD BB 19 FC D0 80 22 09 9F 23 32 73 27 58 37 F9 9B 3C 44 7B 03 B3 80 7E 99 DF 97 4E E9 EC D3 12 F1 1A 78 77 A6 A5 CB 73 AB 65 22 5B 7D ");
1394 /// ```
1395 ///
1396 /// ## For more examples,
1397 /// click [here](./documentation/rijndael_cbc_iso/struct.Rijndael_Generic.html#method.encrypt_string_into_array)
1398 ///
1399 /// # For DES and its variants
1400 /// ## Example 1 for Normal case
1401 /// ```
1402 /// use std::io::Write;
1403 /// use std::fmt::Write as _;
1404 /// use cryptocol::symmetric::{ DES, CBC_ISO };
1405 ///
1406 /// let key = 0x_1234567890ABCDEF_u64;
1407 /// println!("K =\t{:#016X}", key);
1408 /// let mut a_des = DES::new_with_key_u64(key);
1409 ///
1410 /// let message = "In the beginning God created the heavens and the earth.".to_string();
1411 /// println!("M =\t{}", message);
1412 /// let iv = 0x_FEDCBA0987654321_u64;
1413 /// println!("IV = {}", iv);
1414 /// let mut cipher = [0_u8; 56];
1415 /// a_des.encrypt_string_into_array(iv, &message, &mut cipher);
1416 /// print!("C (16 rounds) =\t");
1417 /// for c in cipher.clone()
1418 /// { print!("{:02X} ", c); }
1419 /// println!();
1420 /// let mut txt = String::new();
1421 /// for c in cipher.clone()
1422 /// { write!(txt, "{:02X} ", c); }
1423 /// assert_eq!(txt, "4B B5 ED DC A0 58 7E 6D 6C 3B A2 00 38 C3 D4 29 42 B1 CF 0D E9 FA EA 11 11 6B C8 30 73 39 DD B7 3F 96 9B A3 76 05 34 7E 64 2F D4 CC B2 68 33 64 11 78 69 FB 0B 32 CF 92 ");
1424 /// ```
1425 ///
1426 /// ## For more examples,
1427 /// click [here](./documentation/des_cbc_iso/struct.DES_Generic.html#method.encrypt_string_into_array)
1428 ///
1429 /// # For BigCryptor128
1430 /// ## Example 1 for TAES case
1431 /// ```
1432 /// use std::io::Write;
1433 /// use std::fmt::Write as _;
1434 /// use cryptocol::symmetric::{ BigCryptor128, AES_128, CBC_ISO };
1435 ///
1436 /// let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
1437 /// + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
1438 /// + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
1439 /// let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
1440 /// println!("IV = {:#034X}", iv);
1441 /// let message = "In the beginning God created the heavens and the earth.".to_string();
1442 /// let mut cipher = [0_u8; 64];
1443 /// taes.encrypt_string_into_array(iv, &message, &mut cipher);
1444 /// print!("C =\t");
1445 /// for c in cipher.clone()
1446 /// { print!("{:02X} ", c); }
1447 /// println!();
1448 /// let mut txt = String::new();
1449 /// for c in cipher.clone()
1450 /// { write!(txt, "{:02X} ", c); }
1451 /// assert_eq!(txt, "87 B7 B0 67 C9 41 CF C6 CF B8 69 0D 39 D3 9B EC 0C 18 28 9A FA 51 7D 2A E4 D1 0C 2F 2B A0 0E E3 D1 04 65 E4 B6 66 2B 23 04 42 66 31 7A C5 76 E0 73 1D B1 F8 BB 75 C5 FB C2 1B 73 26 67 57 02 A0 ");
1452 /// ```
1453 ///
1454 /// ## For more examples,
1455 /// click [here](./documentation/big_cryptor128_cbc_iso/struct.BigCryptor128.html#method.encrypt_string_into_array)
1456 ///
1457 /// # For BigCryptor64
1458 /// ## Example 1 for TDES case
1459 /// ```
1460 /// use std::io::Write;
1461 /// use std::fmt::Write as _;
1462 /// use cryptocol::symmetric::{ BigCryptor64, DES, CBC_ISO };
1463 ///
1464 /// let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
1465 /// + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
1466 /// + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
1467 /// let iv = 0x_FEDCBA0987654321_u64;
1468 /// println!("IV = {:#018X}", iv);
1469 /// let message = "In the beginning God created the heavens and the earth.".to_string();
1470 /// let mut cipher = [0_u8; 56];
1471 /// tdes.encrypt_string_into_array(iv, &message, &mut cipher);
1472 /// print!("C =\t");
1473 /// for c in cipher.clone()
1474 /// { print!("{:02X} ", c); }
1475 /// println!();
1476 /// let mut txt = String::new();
1477 /// for c in cipher.clone()
1478 /// { write!(txt, "{:02X} ", c); }
1479 /// assert_eq!(txt, "01 A5 7E BC ED 83 28 FB 7A 06 61 60 7D 8B BA AF 5C A7 76 0B FE 94 1C C4 C2 BC DD 15 98 A9 98 6C 85 18 92 6B 00 72 FB 72 DB 9D 46 BD B1 3C 56 C0 ED 3C 12 45 CA 1D 7E A3 ");
1480 /// ```
1481 ///
1482 /// ## For more examples,
1483 /// click [here](./documentation/big_cryptor64_cbc_iso/struct.BigCryptor64.html#method.encrypt_string_into_array)
1484 #[inline]
1485 fn encrypt_string_into_array<U, const N: usize>(&mut self, iv: T, message: &String, cipher: &mut [U; N]) -> u64
1486 where U: SmallUInt + Copy + Clone
1487 {
1488 self.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, cipher)
1489 }
1490
1491 // fn encrypt_vec<U>(&mut self, iv: T, message: &Vec<U>, cipher: *mut u8) -> u64
1492 /// Encrypts the data stored in a `Vec<U>` object with the padding defined
1493 /// according to ISO 7816-4 in CBC (Cipher-Block Chaining) mode.
1494 ///
1495 /// # Arguments
1496 /// - `iv` is an initialization vector for CBC mode.
1497 /// - `message` is an immutable reference to `Vec<U>` object, and
1498 /// is the place where the plaintext to be encrypted is stored.
1499 /// - `cipher` is a mutable pointer to `u8` which is `*mut u8`, and
1500 /// is the place where the encrypted data will be stored.
1501 ///
1502 /// # Output
1503 /// - This method returns the size of ciphertext including padding bits
1504 /// in bytes.
1505 /// - The output will be at least `size_of::<T>()`,
1506 /// and cannot be other than a multiple of `size_of::<T>()`.
1507 /// - If this method failed in encryption, this method returns `zero`.
1508 ///
1509 /// # Features
1510 /// - You are not encouraged to use this method in pure Rust programming.
1511 /// Instead, use other safer methods such as encrypt_vec_into_*().
1512 /// - This method is useful to use in hybrid programming with C/C++.
1513 /// - The size of the memory area which starts at `cipher` is assumed to be
1514 /// enough to store the ciphertext.
1515 /// - The size of the area for ciphertext should be prepared to be
1516 /// (`size_of::<U>()` * `message.len()` + `1`).next_multiple_of(`size_of::<T>()`)
1517 /// at least.
1518 /// So, it is responsible for you to prepare the `cipher` area big enough!
1519 /// - If `message` is an empty `Vec<U>` object `Vec::<U>::new()`, only
1520 /// padding bytes will be encrypted, and stored in the memory area that
1521 /// starts from `cipher`.
1522 /// - The padding bits are composed of the byte `0b_1000_0000` that
1523 /// indicates the delimiter one bit `1` followed by seven bits `0`s and
1524 /// all padding bits `0`s according to ISO 7816-4.
1525 /// - For more information about the padding bits according to ISO 7816-4,
1526 /// Read [here](https://en.wikipedia.org/wiki/Padding_(cryptography)#ISO/IEC_7816-4).
1527 ///
1528 /// # For Rijndael or AES, and its variants
1529 /// ## Example 1 for AES-128
1530 /// ```
1531 /// use std::io::Write;
1532 /// use std::fmt::Write as _;
1533 /// use cryptocol::symmetric::{ AES_128, CBC_ISO };
1534 ///
1535 /// let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1536 /// println!("K =\t{:#016X}", key);
1537 /// let mut a_aes = AES_128::new_with_key_u128(key);
1538 /// let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1539 /// println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1540 ///
1541 /// let message = "In the beginning God created the heavens and the earth.";
1542 /// println!("M =\t{}", message);
1543 /// let message = unsafe { message.to_string().as_mut_vec().clone() };
1544 /// let mut cipher = [0_u8; 64];
1545 /// a_aes.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1546 /// print!("C =\t");
1547 /// for c in cipher.clone()
1548 /// { print!("{:02X} ", c); }
1549 /// println!();
1550 /// let mut txt = String::new();
1551 /// for c in cipher.clone()
1552 /// { write!(txt, "{:02X} ", c); }
1553 /// assert_eq!(txt, "C9 1C 27 CE 83 92 A1 CF 7D A4 64 35 16 48 01 72 CC E3 6D CD BB 19 FC D0 80 22 09 9F 23 32 73 27 58 37 F9 9B 3C 44 7B 03 B3 80 7E 99 DF 97 4E E9 EC D3 12 F1 1A 78 77 A6 A5 CB 73 AB 65 22 5B 7D ");
1554 /// ```
1555 ///
1556 /// ## For more examples,
1557 /// click [here](./documentation/rijndael_cbc_iso/struct.Rijndael_Generic.html#method.encrypt_vec)
1558 ///
1559 /// # For DES and its variants
1560 /// ## Example 1 for Normal case
1561 /// ```
1562 /// use std::io::Write;
1563 /// use std::fmt::Write as _;
1564 /// use cryptocol::symmetric::{ DES, CBC_ISO };
1565 ///
1566 /// let key = 0x_1234567890ABCDEF_u64;
1567 /// println!("K =\t{:#016X}", key);
1568 /// let mut a_des = DES::new_with_key_u64(key);
1569 ///
1570 /// let message = "In the beginning God created the heavens and the earth.";
1571 /// println!("M =\t{}", message);
1572 /// let message = unsafe { message.to_string().as_mut_vec().clone() };
1573 /// let iv = 0x_FEDCBA0987654321_u64;
1574 /// println!("IV = {}", iv);
1575 /// let mut cipher = [0_u8; 56];
1576 /// a_des.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1577 /// print!("C (16 rounds) =\t");
1578 /// for c in cipher.clone()
1579 /// { print!("{:02X} ", c); }
1580 /// println!();
1581 /// let mut txt = String::new();
1582 /// for c in cipher.clone()
1583 /// { write!(txt, "{:02X} ", c); }
1584 /// assert_eq!(txt, "4B B5 ED DC A0 58 7E 6D 6C 3B A2 00 38 C3 D4 29 42 B1 CF 0D E9 FA EA 11 11 6B C8 30 73 39 DD B7 3F 96 9B A3 76 05 34 7E 64 2F D4 CC B2 68 33 64 11 78 69 FB 0B 32 CF 92 ");
1585 /// ```
1586 ///
1587 /// ## For more examples,
1588 /// click [here](./documentation/des_cbc_iso/struct.DES_Generic.html#method.encrypt_vec)
1589 ///
1590 /// # For BigCryptor128
1591 /// ## Example 1 for TAES case
1592 /// ```
1593 /// use std::io::Write;
1594 /// use std::fmt::Write as _;
1595 /// use cryptocol::symmetric::{ BigCryptor128, AES_128, CBC_ISO };
1596 ///
1597 /// let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
1598 /// + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
1599 /// + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
1600 /// let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
1601 /// println!("IV = {:#034X}", iv);
1602 /// let message = "In the beginning God created the heavens and the earth.";
1603 /// println!("M =\t{}", message);
1604 /// let message = unsafe { message.to_string().as_mut_vec().clone() };
1605 /// let mut cipher = [0_u8; 64];
1606 /// taes.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1607 /// print!("C =\t");
1608 /// for c in cipher.clone()
1609 /// { print!("{:02X} ", c); }
1610 /// println!();
1611 /// let mut txt = String::new();
1612 /// for c in cipher.clone()
1613 /// { write!(txt, "{:02X} ", c); }
1614 /// assert_eq!(txt, "87 B7 B0 67 C9 41 CF C6 CF B8 69 0D 39 D3 9B EC 0C 18 28 9A FA 51 7D 2A E4 D1 0C 2F 2B A0 0E E3 D1 04 65 E4 B6 66 2B 23 04 42 66 31 7A C5 76 E0 73 1D B1 F8 BB 75 C5 FB C2 1B 73 26 67 57 02 A0 ");
1615 /// ```
1616 ///
1617 /// ## For more examples,
1618 /// click [here](./documentation/big_cryptor128_cbc_iso/struct.BigCryptor128.html#method.encrypt_vec)
1619 ///
1620 /// # For BigCryptor64
1621 /// ## Example 1 for TDES case
1622 /// ```
1623 /// use std::io::Write;
1624 /// use std::fmt::Write as _;
1625 /// use cryptocol::symmetric::{ BigCryptor64, DES, CBC_ISO };
1626 ///
1627 /// let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
1628 /// + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
1629 /// + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
1630 /// let iv = 0x_FEDCBA0987654321_u64;
1631 /// println!("IV = {:#018X}", iv);
1632 /// let message = "In the beginning God created the heavens and the earth.";
1633 /// println!("M =\t{}", message);
1634 /// let message = unsafe { message.to_string().as_mut_vec().clone() };
1635 /// let mut cipher = [0_u8; 56];
1636 /// tdes.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1637 /// print!("C =\t");
1638 /// for c in cipher.clone()
1639 /// { print!("{:02X} ", c); }
1640 /// println!();
1641 /// let mut txt = String::new();
1642 /// for c in cipher.clone()
1643 /// { write!(txt, "{:02X} ", c); }
1644 /// assert_eq!(txt, "01 A5 7E BC ED 83 28 FB 7A 06 61 60 7D 8B BA AF 5C A7 76 0B FE 94 1C C4 C2 BC DD 15 98 A9 98 6C 85 18 92 6B 00 72 FB 72 DB 9D 46 BD B1 3C 56 C0 ED 3C 12 45 CA 1D 7E A3 ");
1645 /// ```
1646 ///
1647 /// ## For more examples,
1648 /// click [here](./documentation/big_cryptor64_cbc_iso/struct.BigCryptor64.html#method.encrypt_vec)
1649 #[inline]
1650 fn encrypt_vec<U>(&mut self, iv: T, message: &Vec<U>, cipher: *mut u8) -> u64
1651 where U: SmallUInt + Copy + Clone
1652 {
1653 self.encrypt(iv, message.as_ptr() as *const u8, (message.len() as u32 * U::size_in_bytes()) as u64, cipher)
1654 }
1655
1656 // fn encrypt_vec_into_vec<U, V>(&mut self, iv: T, message: &Vec<U>, cipher: &mut Vec<V>) -> u64
1657 /// Encrypts the data stored in a `Vec<U>` object with the padding defined
1658 /// according to ISO 7816-4 in CBC (Cipher-Block Chaining) mode, and
1659 /// stores the encrypted data in `Vec<V>`.
1660 ///
1661 /// # Arguments
1662 /// - `iv` is an initialization vector for CBC mode.
1663 /// - `message` is an immutable reference to `Vec<U>` object, and
1664 /// is the place where the plaintext to be encrypted is stored.
1665 /// - `cipher` is a mutable reference to `Vec<U>` object, and
1666 /// is the place where the encrypted data will be stored.
1667 ///
1668 /// # Output
1669 /// - This method returns the size of ciphertext including padding bits
1670 /// in bytes.
1671 /// - The output will be at least `size_of::<T>()`,
1672 /// and cannot be other than a multiple of `size_of::<T>()`.
1673 /// - If this method failed in encryption, this method returns `zero`.
1674 ///
1675 /// # Features
1676 /// - If `message` is an empty `Vec<U>` object `Vec::<U>::new()`, only
1677 /// padding bytes will be encrypted, and stored in the `Vec<U>` object
1678 /// which is referred to as `cipher`.
1679 /// - The padding bits are composed of the byte `0b_1000_0000` that
1680 /// indicates the delimiter one bit `1` followed by seven bits `0`s and
1681 /// all padding bits `0`s according to ISO 7816-4.
1682 /// - For more information about the padding bits according to ISO 7816-4,
1683 /// Read [here](https://en.wikipedia.org/wiki/Padding_(cryptography)#ISO/IEC_7816-4).
1684 /// - You don't have to worry about whether or not the size of the memory
1685 /// area where the ciphertext will be stored is enough.
1686 ///
1687 /// # For Rijndael or AES, and its variants
1688 /// ## Example 1 for AES-128
1689 /// ```
1690 /// use std::io::Write;
1691 /// use std::fmt::Write as _;
1692 /// use cryptocol::symmetric::{ AES_128, CBC_ISO };
1693 ///
1694 /// let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1695 /// println!("K =\t{:#016X}", key);
1696 /// let mut a_aes = AES_128::new_with_key_u128(key);
1697 /// let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1698 /// println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1699 ///
1700 /// let message = "In the beginning God created the heavens and the earth.";
1701 /// println!("M =\t{}", message);
1702 /// let message = unsafe { message.to_string().as_mut_vec().clone() };
1703 /// let mut cipher = Vec::<u8>::new();
1704 /// a_aes.encrypt_vec_into_vec(iv, &message, &mut cipher);
1705 /// print!("C =\t");
1706 /// for c in cipher.clone()
1707 /// { print!("{:02X} ", c); }
1708 /// println!();
1709 /// let mut txt = String::new();
1710 /// for c in cipher.clone()
1711 /// { write!(txt, "{:02X} ", c); }
1712 /// assert_eq!(txt, "C9 1C 27 CE 83 92 A1 CF 7D A4 64 35 16 48 01 72 CC E3 6D CD BB 19 FC D0 80 22 09 9F 23 32 73 27 58 37 F9 9B 3C 44 7B 03 B3 80 7E 99 DF 97 4E E9 EC D3 12 F1 1A 78 77 A6 A5 CB 73 AB 65 22 5B 7D ");
1713 /// ```
1714 ///
1715 /// ## For more examples,
1716 /// click [here](./documentation/rijndael_cbc_iso/struct.Rijndael_Generic.html#method.encrypt_vec_into_vec)
1717 ///
1718 /// # For DES and its variants
1719 /// ## Example 1 for Normal case
1720 /// ```
1721 /// use std::io::Write;
1722 /// use std::fmt::Write as _;
1723 /// use cryptocol::symmetric::{ DES, CBC_ISO };
1724 ///
1725 /// let key = 0x_1234567890ABCDEF_u64;
1726 /// println!("K =\t{:#016X}", key);
1727 /// let mut a_des = DES::new_with_key_u64(key);
1728 ///
1729 /// let message = "In the beginning God created the heavens and the earth.";
1730 /// println!("M =\t{}", message);
1731 /// let message = unsafe { message.to_string().as_mut_vec().clone() };
1732 /// let iv = 0x_FEDCBA0987654321_u64;
1733 /// println!("IV = {}", iv);
1734 /// let mut cipher = Vec::<u8>::new();
1735 /// a_des.encrypt_vec_into_vec(iv, &message, &mut cipher);
1736 /// print!("C (16 rounds) =\t");
1737 /// for c in cipher.clone()
1738 /// { print!("{:02X} ", c); }
1739 /// println!();
1740 /// let mut txt = String::new();
1741 /// for c in cipher.clone()
1742 /// { write!(txt, "{:02X} ", c); }
1743 /// assert_eq!(txt, "4B B5 ED DC A0 58 7E 6D 6C 3B A2 00 38 C3 D4 29 42 B1 CF 0D E9 FA EA 11 11 6B C8 30 73 39 DD B7 3F 96 9B A3 76 05 34 7E 64 2F D4 CC B2 68 33 64 11 78 69 FB 0B 32 CF 92 ");
1744 /// ```
1745 ///
1746 /// ## For more examples,
1747 /// click [here](./documentation/des_cbc_iso/struct.DES_Generic.html#method.encrypt_vec_into_vec)
1748 ///
1749 /// # For BigCryptor128
1750 /// ## Example 1 for TAES case
1751 /// ```
1752 /// use std::io::Write;
1753 /// use std::fmt::Write as _;
1754 /// use cryptocol::symmetric::{ BigCryptor128, AES_128, CBC_ISO };
1755 ///
1756 /// let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
1757 /// + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
1758 /// + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
1759 /// let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
1760 /// println!("IV = {:#034X}", iv);
1761 /// let message = "In the beginning God created the heavens and the earth.";
1762 /// println!("M =\t{}", message);
1763 /// let message = unsafe { message.to_string().as_mut_vec().clone() };
1764 /// let mut cipher = Vec::<u8>::new();
1765 /// taes.encrypt_vec_into_vec(iv, &message, &mut cipher);
1766 /// print!("C =\t");
1767 /// for c in cipher.clone()
1768 /// { print!("{:02X} ", c); }
1769 /// println!();
1770 /// let mut txt = String::new();
1771 /// for c in cipher.clone()
1772 /// { write!(txt, "{:02X} ", c); }
1773 /// assert_eq!(txt, "87 B7 B0 67 C9 41 CF C6 CF B8 69 0D 39 D3 9B EC 0C 18 28 9A FA 51 7D 2A E4 D1 0C 2F 2B A0 0E E3 D1 04 65 E4 B6 66 2B 23 04 42 66 31 7A C5 76 E0 73 1D B1 F8 BB 75 C5 FB C2 1B 73 26 67 57 02 A0 ");
1774 /// ```
1775 ///
1776 /// ## For more examples,
1777 /// click [here](./documentation/big_cryptor128_cbc_iso/struct.BigCryptor128.html#method.encrypt_vec_into_vec)
1778 ///
1779 /// # For BigCryptor64
1780 /// ## Example 1 for TDES case
1781 /// ```
1782 /// use std::io::Write;
1783 /// use std::fmt::Write as _;
1784 /// use cryptocol::symmetric::{ BigCryptor64, DES, CBC_ISO };
1785 ///
1786 /// let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
1787 /// + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
1788 /// + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
1789 /// let iv = 0x_FEDCBA0987654321_u64;
1790 /// println!("IV = {:#018X}", iv);
1791 /// let message = "In the beginning God created the heavens and the earth.";
1792 /// println!("M =\t{}", message);
1793 /// let message = unsafe { message.to_string().as_mut_vec().clone() };
1794 /// let mut cipher = Vec::<u8>::new();
1795 /// tdes.encrypt_vec_into_vec(iv, &message, &mut cipher);
1796 /// print!("C =\t");
1797 /// for c in cipher.clone()
1798 /// { print!("{:02X} ", c); }
1799 /// println!();
1800 /// let mut txt = String::new();
1801 /// for c in cipher.clone()
1802 /// { write!(txt, "{:02X} ", c); }
1803 /// assert_eq!(txt, "01 A5 7E BC ED 83 28 FB 7A 06 61 60 7D 8B BA AF 5C A7 76 0B FE 94 1C C4 C2 BC DD 15 98 A9 98 6C 85 18 92 6B 00 72 FB 72 DB 9D 46 BD B1 3C 56 C0 ED 3C 12 45 CA 1D 7E A3 ");
1804 /// ```
1805 ///
1806 /// ## For more examples,
1807 /// click [here](./documentation/big_cryptor64_cbc_iso/struct.BigCryptor64.html#method.encrypt_vec_into_vec)
1808 #[inline]
1809 fn encrypt_vec_into_vec<U, V>(&mut self, iv: T, message: &Vec<U>, cipher: &mut Vec<V>) -> u64
1810 where U: SmallUInt + Copy + Clone, V: SmallUInt + Copy + Clone
1811 {
1812 self.encrypt_into_vec(iv, message.as_ptr() as *const u8, (message.len() as u32 * U::size_in_bytes()) as u64, cipher)
1813 }
1814
1815 // fn encrypt_vec_into_array<U, V, const N: usize>(&mut self, iv: T, message: &Vec<U>, cipher: &mut [V; N]) -> u64
1816 /// Encrypts the data stored in a `Vec<U>` object with the padding defined
1817 /// according to ISO 7816-4 in CBC (Cipher-Block Chaining) mode, and
1818 /// stores the encrypted data in array `[V; N]`.
1819 ///
1820 /// # Arguments
1821 /// - `iv` is an initialization vector for CBC mode.
1822 /// - `message` is an immutable reference to `Vec<U>` object, and
1823 /// is the place where the plaintext to be encrypted is stored.
1824 /// - `cipher` is a mutable reference to an array `[U; N]` object, and
1825 /// is the place where the encrypted data will be stored.
1826 ///
1827 /// # Output
1828 /// - This method returns the size of ciphertext including padding bits
1829 /// in bytes.
1830 /// - The output will be at least `size_of::<T>()`,
1831 /// and cannot be other than a multiple of `size_of::<T>()`.
1832 /// - If this method failed in encryption, this method returns `zero`.
1833 ///
1834 /// # Features
1835 /// - If `message` is an empty `Vec<U>` object `Vec::<U>::new()`, only
1836 /// padding bytes will be encrypted, and stored in the array `[U; N]`
1837 /// object `cipher`.
1838 /// - If `size_of::<V>()` * `N` is less than
1839 /// `size_of::<U>() * message.len()`'s next multiple of
1840 /// `size_of::<T>()`, this method does not perform
1841 /// encryption but returns `zero`.
1842 /// - If `size_of::<V>()` * `N` is equal to
1843 /// `size_of::<U>() * message.len()`'s next multiple of
1844 /// `size_of::<T>()`, this method performs encryption,
1845 /// fills the array `cipher` with the encrypted data, and returns
1846 /// the size of the ciphertext including padding bits in bytes.
1847 /// - If `size_of::<V>()` * `N` is greater than
1848 /// `size_of::<U>() * message.len()`'s next multiple of `size_of::<T>()`,
1849 /// this method performs encryption, fills the array `cipher` with the
1850 /// encrypted data, and then fills the rest of the elements of the array
1851 /// `cipher` with zeros, and returns the size of the ciphertext including
1852 /// padding bits in bytes.
1853 /// - The size of the area for ciphertext should be prepared to be
1854 /// (`size_of::<U>()` * `message.len()` + `1`).next_multiple_of(`size_of::<T>()`)
1855 /// at least.
1856 /// So, it is responsible for you to prepare the `cipher` area big enough!
1857 /// - The padding bits are composed of the byte `0b_1000_0000` that
1858 /// indicates the delimiter one bit `1` followed by seven bits `0`s and
1859 /// all padding bits `0`s according to ISO 7816-4.
1860 /// - For more information about the padding bits according to ISO 7816-4,
1861 /// Read [here](https://en.wikipedia.org/wiki/Padding_(cryptography)#ISO/IEC_7816-4).
1862 ///
1863 /// # For Rijndael or AES, and its variants
1864 /// ## Example 1 for AES-128
1865 /// ```
1866 /// use std::io::Write;
1867 /// use std::fmt::Write as _;
1868 /// use cryptocol::symmetric::{ AES_128, CBC_ISO };
1869 ///
1870 /// let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1871 /// println!("K =\t{:#016X}", key);
1872 /// let mut a_aes = AES_128::new_with_key_u128(key);
1873 /// let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1874 /// println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1875 ///
1876 /// let message = "In the beginning God created the heavens and the earth.";
1877 /// println!("M =\t{}", message);
1878 /// let message = unsafe { message.to_string().as_mut_vec().clone() };
1879 /// let mut cipher = [0_u8; 64];
1880 /// a_aes.encrypt_vec_into_array(iv, &message, &mut cipher);
1881 /// print!("C =\t");
1882 /// for c in cipher.clone()
1883 /// { print!("{:02X} ", c); }
1884 /// println!();
1885 /// let mut txt = String::new();
1886 /// for c in cipher.clone()
1887 /// { write!(txt, "{:02X} ", c); }
1888 /// assert_eq!(txt, "C9 1C 27 CE 83 92 A1 CF 7D A4 64 35 16 48 01 72 CC E3 6D CD BB 19 FC D0 80 22 09 9F 23 32 73 27 58 37 F9 9B 3C 44 7B 03 B3 80 7E 99 DF 97 4E E9 EC D3 12 F1 1A 78 77 A6 A5 CB 73 AB 65 22 5B 7D ");
1889 /// ```
1890 ///
1891 /// ## For more examples,
1892 /// click [here](./documentation/rijndael_cbc_iso/struct.Rijndael_Generic.html#method.encrypt_vec_into_array)
1893 ///
1894 /// # For DES and its variants
1895 /// ## Example 1 for Normal case
1896 /// ```
1897 /// use std::io::Write;
1898 /// use std::fmt::Write as _;
1899 /// use cryptocol::symmetric::{ DES, CBC_ISO };
1900 ///
1901 /// let key = 0x_1234567890ABCDEF_u64;
1902 /// println!("K =\t{:#016X}", key);
1903 /// let mut a_des = DES::new_with_key_u64(key);
1904 ///
1905 /// let message = "In the beginning God created the heavens and the earth.";
1906 /// println!("M =\t{}", message);
1907 /// let message = unsafe { message.to_string().as_mut_vec().clone() };
1908 /// let iv = 0x_FEDCBA0987654321_u64;
1909 /// println!("IV = {}", iv);
1910 /// let mut cipher = [0_u8; 56];
1911 /// a_des.encrypt_vec_into_array(iv, &message, &mut cipher);
1912 /// print!("C (16 rounds) =\t");
1913 /// for c in cipher.clone()
1914 /// { print!("{:02X} ", c); }
1915 /// println!();
1916 /// let mut txt = String::new();
1917 /// for c in cipher.clone()
1918 /// { write!(txt, "{:02X} ", c); }
1919 /// assert_eq!(txt, "4B B5 ED DC A0 58 7E 6D 6C 3B A2 00 38 C3 D4 29 42 B1 CF 0D E9 FA EA 11 11 6B C8 30 73 39 DD B7 3F 96 9B A3 76 05 34 7E 64 2F D4 CC B2 68 33 64 11 78 69 FB 0B 32 CF 92 ");
1920 /// ```
1921 ///
1922 /// ## For more examples,
1923 /// click [here](./documentation/des_cbc_iso/struct.DES_Generic.html#method.encrypt_vec_into_array)
1924 ///
1925 /// # For BigCryptor128
1926 /// ## Example 1 for TAES case
1927 /// ```
1928 /// use std::io::Write;
1929 /// use std::fmt::Write as _;
1930 /// use cryptocol::symmetric::{ BigCryptor128, AES_128, CBC_ISO };
1931 ///
1932 /// let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
1933 /// + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
1934 /// + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
1935 /// let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
1936 /// println!("IV = {:#034X}", iv);
1937 /// let message = "In the beginning God created the heavens and the earth.";
1938 /// println!("M =\t{}", message);
1939 /// let message = unsafe { message.to_string().as_mut_vec().clone() };
1940 /// let mut cipher = [0_u8; 64];
1941 /// taes.encrypt_vec_into_array(iv, &message, &mut cipher);
1942 /// print!("C =\t");
1943 /// for c in cipher.clone()
1944 /// { print!("{:02X} ", c); }
1945 /// println!();
1946 /// let mut txt = String::new();
1947 /// for c in cipher.clone()
1948 /// { write!(txt, "{:02X} ", c); }
1949 /// assert_eq!(txt, "87 B7 B0 67 C9 41 CF C6 CF B8 69 0D 39 D3 9B EC 0C 18 28 9A FA 51 7D 2A E4 D1 0C 2F 2B A0 0E E3 D1 04 65 E4 B6 66 2B 23 04 42 66 31 7A C5 76 E0 73 1D B1 F8 BB 75 C5 FB C2 1B 73 26 67 57 02 A0 ");
1950 /// ```
1951 ///
1952 /// ## For more examples,
1953 /// click [here](./documentation/big_cryptor128_cbc_iso/struct.BigCryptor128.html#method.encrypt_vec_into_array)
1954 ///
1955 /// # For BigCryptor64
1956 /// ## Example 1 for TDES case
1957 /// ```
1958 /// use std::io::Write;
1959 /// use std::fmt::Write as _;
1960 /// use cryptocol::symmetric::{ BigCryptor64, DES, CBC_ISO };
1961 ///
1962 /// let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
1963 /// + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
1964 /// + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
1965 /// let iv = 0x_FEDCBA0987654321_u64;
1966 /// println!("IV = {:#018X}", iv);
1967 /// let message = "In the beginning God created the heavens and the earth.";
1968 /// println!("M =\t{}", message);
1969 /// let message = unsafe { message.to_string().as_mut_vec().clone() };
1970 /// let mut cipher = [0_u8; 56];
1971 /// tdes.encrypt_vec_into_array(iv, &message, &mut cipher);
1972 /// print!("C =\t");
1973 /// for c in cipher.clone()
1974 /// { print!("{:02X} ", c); }
1975 /// println!();
1976 /// let mut txt = String::new();
1977 /// for c in cipher.clone()
1978 /// { write!(txt, "{:02X} ", c); }
1979 /// assert_eq!(txt, "01 A5 7E BC ED 83 28 FB 7A 06 61 60 7D 8B BA AF 5C A7 76 0B FE 94 1C C4 C2 BC DD 15 98 A9 98 6C 85 18 92 6B 00 72 FB 72 DB 9D 46 BD B1 3C 56 C0 ED 3C 12 45 CA 1D 7E A3 ");
1980 /// ```
1981 ///
1982 /// ## For more examples,
1983 /// click [here](./documentation/big_cryptor64_cbc_iso/struct.BigCryptor64.html#method.encrypt_vec_into_array)
1984 #[inline]
1985 fn encrypt_vec_into_array<U, V, const N: usize>(&mut self, iv: T, message: &Vec<U>, cipher: &mut [V; N]) -> u64
1986 where U: SmallUInt + Copy + Clone, V: SmallUInt + Copy + Clone
1987 {
1988 self.encrypt_into_array(iv, message.as_ptr() as *const u8, (message.len() as u32 * U::size_in_bytes()) as u64, cipher)
1989 }
1990
1991 // fn encrypt_array<U, const N: usize>(&mut self, iv: T, message: &[U; N], cipher: *mut u8) -> u64
1992 /// Encrypts the data stored in an array `[U; N]` object with the padding
1993 /// defined according to ISO 7816-4 in CBC (Cipher-Block Chaining) mode.
1994 ///
1995 /// # Arguments
1996 /// - `iv` is an initialization vector for CBC mode.
1997 /// - `message` is an immutable reference to an array `[U; N]` object, and
1998 /// is the place where the plaintext to be encrypted is stored.
1999 /// - `cipher` is a mutable pointer to `u8` which is `*mut u8`, and
2000 /// is the place where the encrypted data will be stored.
2001 ///
2002 /// # Output
2003 /// - This method returns the size of ciphertext including padding bits
2004 /// in bytes.
2005 /// - The output will be at least `size_of::<T>()`,
2006 /// and cannot be other than a multiple of `size_of::<T>()`.
2007 /// - If this method failed in encryption, this method returns `zero`.
2008 ///
2009 /// # Features
2010 /// - You are not encouraged to use this method in pure Rust programming.
2011 /// Instead, use other safer methods such as encrypt_vec_into_*().
2012 /// - This method is useful to use in hybrid programming with C/C++.
2013 /// - The size of the memory area which starts at `cipher` is assumed to be
2014 /// enough to store the ciphertext.
2015 /// - The size of the area for ciphertext should be prepared to be
2016 /// (`size_of::<U>()` * `N` + `1`).next_multiple_of(`size_of::<T>()`)
2017 /// at least.
2018 /// So, it is responsible for you to prepare the `cipher` area big enough!
2019 /// - If `message` is an empty array `[U; 0]` object, only padding bytes
2020 /// will be encrypted, and stored in the memory area that starts from `cipher`.
2021 /// - The padding bits are composed of the byte `0b_1000_0000` that
2022 /// indicates the delimiter one bit `1` followed by seven bits `0`s and
2023 /// all padding bits `0`s according to ISO 7816-4.
2024 /// - For more information about the padding bits according to ISO 7816-4,
2025 /// Read [here](https://en.wikipedia.org/wiki/Padding_(cryptography)#ISO/IEC_7816-4).
2026 ///
2027 /// # For Rijndael or AES, and its variants
2028 /// ## Example 1 for AES-128
2029 /// ```
2030 /// use std::io::Write;
2031 /// use std::fmt::Write as _;
2032 /// use cryptocol::symmetric::{ AES_128, CBC_ISO };
2033 ///
2034 /// let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
2035 /// println!("K =\t{:#016X}", key);
2036 /// let mut a_aes = AES_128::new_with_key_u128(key);
2037 /// let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2038 /// println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2039 ///
2040 /// let mes = "In the beginning God created the heavens and the earth.";
2041 /// println!("M =\t{}", mes);
2042 /// let mut message = [0_u8; 55];
2043 /// message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2044 /// let mut cipher = [0_u8; 64];
2045 /// a_aes.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
2046 /// print!("C =\t");
2047 /// for c in cipher.clone()
2048 /// { print!("{:02X} ", c); }
2049 /// println!();
2050 /// let mut txt = String::new();
2051 /// for c in cipher.clone()
2052 /// { write!(txt, "{:02X} ", c); }
2053 /// assert_eq!(txt, "C9 1C 27 CE 83 92 A1 CF 7D A4 64 35 16 48 01 72 CC E3 6D CD BB 19 FC D0 80 22 09 9F 23 32 73 27 58 37 F9 9B 3C 44 7B 03 B3 80 7E 99 DF 97 4E E9 EC D3 12 F1 1A 78 77 A6 A5 CB 73 AB 65 22 5B 7D ");
2054 /// ```
2055 ///
2056 /// ## For more examples,
2057 /// click [here](./documentation/rijndael_cbc_iso/struct.Rijndael_Generic.html#method.encrypt_array)
2058 ///
2059 /// # For DES and its variants
2060 /// ## Example 1 for Normal case
2061 /// ```
2062 /// use std::io::Write;
2063 /// use std::fmt::Write as _;
2064 /// use cryptocol::symmetric::{ DES, CBC_ISO };
2065 ///
2066 /// let key = 0x_1234567890ABCDEF_u64;
2067 /// println!("K =\t{:#016X}", key);
2068 /// let mut a_des = DES::new_with_key_u64(key);
2069 ///
2070 /// let mes = "In the beginning God created the heavens and the earth.";
2071 /// println!("M =\t{}", mes);
2072 /// let mut message = [0_u8; 55];
2073 /// message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2074 /// let iv = 0x_FEDCBA0987654321_u64;
2075 /// println!("IV = {}", iv);
2076 /// let mut cipher = [0_u8; 56];
2077 /// a_des.encrypt_array(iv, &message, cipher.as_mut_ptr());
2078 /// print!("C (16 rounds) =\t");
2079 /// for c in cipher.clone()
2080 /// { print!("{:02X} ", c); }
2081 /// println!();
2082 /// let mut txt = String::new();
2083 /// for c in cipher.clone()
2084 /// { write!(txt, "{:02X} ", c); }
2085 /// assert_eq!(txt, "4B B5 ED DC A0 58 7E 6D 6C 3B A2 00 38 C3 D4 29 42 B1 CF 0D E9 FA EA 11 11 6B C8 30 73 39 DD B7 3F 96 9B A3 76 05 34 7E 64 2F D4 CC B2 68 33 64 11 78 69 FB 0B 32 CF 92 ");
2086 /// ```
2087 ///
2088 /// ## For more examples,
2089 /// click [here](./documentation/des_cbc_iso/struct.DES_Generic.html#method.encrypt_array)
2090 ///
2091 /// # For BigCryptor128
2092 /// ## Example 1 for TAES case
2093 /// ```
2094 /// use std::io::Write;
2095 /// use std::fmt::Write as _;
2096 /// use cryptocol::symmetric::{ BigCryptor128, AES_128, CBC_ISO };
2097 ///
2098 /// let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
2099 /// + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
2100 /// + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
2101 /// let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
2102 /// println!("IV = {:#034X}", iv);
2103 /// let mes = "In the beginning God created the heavens and the earth.";
2104 /// println!("M =\t{}", mes);
2105 /// let mut message = [0_u8; 55];
2106 /// message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2107 /// let mut cipher = [0_u8; 64];
2108 /// taes.encrypt_array(iv, &message, cipher.as_mut_ptr());
2109 /// print!("C =\t");
2110 /// for c in cipher.clone()
2111 /// { print!("{:02X} ", c); }
2112 /// println!();
2113 /// let mut txt = String::new();
2114 /// for c in cipher.clone()
2115 /// { write!(txt, "{:02X} ", c); }
2116 /// assert_eq!(txt, "87 B7 B0 67 C9 41 CF C6 CF B8 69 0D 39 D3 9B EC 0C 18 28 9A FA 51 7D 2A E4 D1 0C 2F 2B A0 0E E3 D1 04 65 E4 B6 66 2B 23 04 42 66 31 7A C5 76 E0 73 1D B1 F8 BB 75 C5 FB C2 1B 73 26 67 57 02 A0 ");
2117 /// ```
2118 ///
2119 /// ## For more examples,
2120 /// click [here](./documentation/big_cryptor128_cbc_iso/struct.BigCryptor128.html#method.encrypt_array)
2121 ///
2122 /// # For BigCryptor64
2123 /// ## Example 1 for TDES case
2124 /// ```
2125 /// use std::io::Write;
2126 /// use std::fmt::Write as _;
2127 /// use cryptocol::symmetric::{ BigCryptor64, DES, CBC_ISO };
2128 ///
2129 /// let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
2130 /// + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
2131 /// + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
2132 /// let iv = 0x_FEDCBA0987654321_u64;
2133 /// println!("IV = {:#018X}", iv);
2134 /// let mes = "In the beginning God created the heavens and the earth.";
2135 /// println!("M =\t{}", mes);
2136 /// let mut message = [0_u8; 55];
2137 /// message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2138 /// let mut cipher = [0_u8; 56];
2139 /// tdes.encrypt_array(iv, &message, cipher.as_mut_ptr());
2140 /// print!("C =\t");
2141 /// for c in cipher.clone()
2142 /// { print!("{:02X} ", c); }
2143 /// println!();
2144 /// let mut txt = String::new();
2145 /// for c in cipher.clone()
2146 /// { write!(txt, "{:02X} ", c); }
2147 /// assert_eq!(txt, "01 A5 7E BC ED 83 28 FB 7A 06 61 60 7D 8B BA AF 5C A7 76 0B FE 94 1C C4 C2 BC DD 15 98 A9 98 6C 85 18 92 6B 00 72 FB 72 DB 9D 46 BD B1 3C 56 C0 ED 3C 12 45 CA 1D 7E A3 ");
2148 /// ```
2149 ///
2150 /// ## For more examples,
2151 /// click [here](./documentation/big_cryptor64_cbc_iso/struct.BigCryptor64.html#method.encrypt_array)
2152 #[inline]
2153 fn encrypt_array<U, const N: usize>(&mut self, iv: T, message: &[U; N], cipher: *mut u8) -> u64
2154 where U: SmallUInt + Copy + Clone
2155 {
2156 self.encrypt(iv, message.as_ptr() as *const u8, (N as u32 * U::size_in_bytes()) as u64, cipher)
2157 }
2158
2159 // fn encrypt_array_into_vec<U, V, const N: usize>(&mut self, iv: T, message: &[U; N], cipher: &mut Vec<V>) -> u64
2160 /// Encrypts the data stored in an array `[U; N]` object with the padding
2161 /// according to ISO 7816-4 in CBC (Cipher-Block Chaining) mode, and stores the
2162 /// encrypted data in `Vec<V>`.
2163 ///
2164 /// # Arguments
2165 /// - `iv` is an initialization vector for CBC mode.
2166 /// - `message` is an immutable reference to an array `[U; N]` object, and
2167 /// is the place where the plaintext to be encrypted is stored.
2168 /// - `cipher` is a mutable reference to `Vec<U>` object, and
2169 /// is the place where the encrypted data will be stored.
2170 ///
2171 /// # Output
2172 /// - This method returns the size of ciphertext including padding bits
2173 /// in bytes.
2174 /// - The output will be at least `size_of::<T>()`,
2175 /// and cannot be other than a multiple of `size_of::<T>()`.
2176 /// - If this method failed in encryption, this method returns `zero`.
2177 ///
2178 /// # Features
2179 /// - If `message` is an empty array `[U; 0]` object, only padding bytes
2180 /// will be encrypted, and stored in the `Vec<U>` object `cipher`.
2181 /// - The padding bits are composed of the byte `0b_1000_0000` that
2182 /// indicates the delimiter one bit `1` followed by seven bits `0`s and
2183 /// all padding bits `0`s according to ISO 7816-4.
2184 /// - For more information about the padding bits according to ISO 7816-4,
2185 /// Read [here](https://en.wikipedia.org/wiki/Padding_(cryptography)#ISO/IEC_7816-4).
2186 /// - You don't have to worry about whether or not the size of the memory
2187 /// area where the ciphertext will be stored is enough.
2188 ///
2189 /// # For Rijndael or AES, and its variants
2190 /// ## Example 1 for AES-128
2191 /// ```
2192 /// use std::io::Write;
2193 /// use std::fmt::Write as _;
2194 /// use cryptocol::symmetric::{ AES_128, CBC_ISO };
2195 ///
2196 /// let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
2197 /// println!("K =\t{:#016X}", key);
2198 /// let mut a_aes = AES_128::new_with_key_u128(key);
2199 /// let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2200 /// println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2201 ///
2202 /// let mes = "In the beginning God created the heavens and the earth.";
2203 /// println!("M =\t{}", mes);
2204 /// let mut message = [0_u8; 55];
2205 /// message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2206 /// let mut cipher = Vec::<u8>::new();
2207 /// a_aes.encrypt_array_into_vec(iv, &message, &mut cipher);
2208 /// print!("C =\t");
2209 /// for c in cipher.clone()
2210 /// { print!("{:02X} ", c); }
2211 /// println!();
2212 /// let mut txt = String::new();
2213 /// for c in cipher.clone()
2214 /// { write!(txt, "{:02X} ", c); }
2215 /// assert_eq!(txt, "C9 1C 27 CE 83 92 A1 CF 7D A4 64 35 16 48 01 72 CC E3 6D CD BB 19 FC D0 80 22 09 9F 23 32 73 27 58 37 F9 9B 3C 44 7B 03 B3 80 7E 99 DF 97 4E E9 EC D3 12 F1 1A 78 77 A6 A5 CB 73 AB 65 22 5B 7D ");
2216 /// ```
2217 ///
2218 /// ## For more examples,
2219 /// click [here](./documentation/rijndael_cbc_iso/struct.Rijndael_Generic.html#method.encrypt_array_into_vec)
2220 ///
2221 /// # For DES and its variants
2222 /// ## Example 1 for Normal case
2223 /// ```
2224 /// use std::io::Write;
2225 /// use std::fmt::Write as _;
2226 /// use cryptocol::symmetric::{ DES, CBC_ISO };
2227 ///
2228 /// let key = 0x_1234567890ABCDEF_u64;
2229 /// println!("K =\t{:#016X}", key);
2230 /// let mut a_des = DES::new_with_key_u64(key);
2231 ///
2232 /// let mes = "In the beginning God created the heavens and the earth.";
2233 /// println!("M =\t{}", mes);
2234 /// let mut message = [0_u8; 55];
2235 /// message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2236 /// let iv = 0x_FEDCBA0987654321_u64;
2237 /// println!("IV = {}", iv);
2238 /// let mut cipher = Vec::<u8>::new();
2239 /// a_des.encrypt_array_into_vec(iv, &message, &mut cipher);
2240 /// print!("C (16 rounds) =\t");
2241 /// for c in cipher.clone()
2242 /// { print!("{:02X} ", c); }
2243 /// println!();
2244 /// let mut txt = String::new();
2245 /// for c in cipher.clone()
2246 /// { write!(txt, "{:02X} ", c); }
2247 /// assert_eq!(txt, "4B B5 ED DC A0 58 7E 6D 6C 3B A2 00 38 C3 D4 29 42 B1 CF 0D E9 FA EA 11 11 6B C8 30 73 39 DD B7 3F 96 9B A3 76 05 34 7E 64 2F D4 CC B2 68 33 64 11 78 69 FB 0B 32 CF 92 ");
2248 /// ```
2249 ///
2250 /// ## For more examples,
2251 /// click [here](./documentation/des_cbc_iso/struct.DES_Generic.html#method.encrypt_array_into_vec)
2252 ///
2253 /// # For BigCryptor128
2254 /// ## Example 1 for TAES case
2255 /// ```
2256 /// use std::io::Write;
2257 /// use std::fmt::Write as _;
2258 /// use cryptocol::symmetric::{ BigCryptor128, AES_128, CBC_ISO };
2259 ///
2260 /// let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
2261 /// + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
2262 /// + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
2263 /// let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
2264 /// println!("IV = {:#034X}", iv);
2265 /// let mes = "In the beginning God created the heavens and the earth.";
2266 /// println!("M =\t{}", mes);
2267 /// let mut message = [0_u8; 55];
2268 /// message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2269 /// let mut cipher = Vec::<u8>::new();
2270 /// taes.encrypt_array_into_vec(iv, &message, &mut cipher);
2271 /// print!("C =\t");
2272 /// for c in cipher.clone()
2273 /// { print!("{:02X} ", c); }
2274 /// println!();
2275 /// let mut txt = String::new();
2276 /// for c in cipher.clone()
2277 /// { write!(txt, "{:02X} ", c); }
2278 /// assert_eq!(txt, "87 B7 B0 67 C9 41 CF C6 CF B8 69 0D 39 D3 9B EC 0C 18 28 9A FA 51 7D 2A E4 D1 0C 2F 2B A0 0E E3 D1 04 65 E4 B6 66 2B 23 04 42 66 31 7A C5 76 E0 73 1D B1 F8 BB 75 C5 FB C2 1B 73 26 67 57 02 A0 ");
2279 /// ```
2280 ///
2281 /// ## For more examples,
2282 /// click [here](./documentation/big_cryptor128_cbc_iso/struct.BigCryptor128.html#method.encrypt_array_into_vec)
2283 ///
2284 /// # For BigCryptor64
2285 /// ## Example 1 for TDES case
2286 /// ```
2287 /// use std::io::Write;
2288 /// use std::fmt::Write as _;
2289 /// use cryptocol::symmetric::{ BigCryptor64, DES, CBC_ISO };
2290 ///
2291 /// let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
2292 /// + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
2293 /// + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
2294 /// let iv = 0x_FEDCBA0987654321_u64;
2295 /// println!("IV = {:#018X}", iv);
2296 /// let mes = "In the beginning God created the heavens and the earth.";
2297 /// println!("M =\t{}", mes);
2298 /// let mut message = [0_u8; 55];
2299 /// message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2300 /// let mut cipher = Vec::<u8>::new();
2301 /// tdes.encrypt_array_into_vec(iv, &message, &mut cipher);
2302 /// print!("C =\t");
2303 /// for c in cipher.clone()
2304 /// { print!("{:02X} ", c); }
2305 /// println!();
2306 /// let mut txt = String::new();
2307 /// for c in cipher.clone()
2308 /// { write!(txt, "{:02X} ", c); }
2309 /// assert_eq!(txt, "01 A5 7E BC ED 83 28 FB 7A 06 61 60 7D 8B BA AF 5C A7 76 0B FE 94 1C C4 C2 BC DD 15 98 A9 98 6C 85 18 92 6B 00 72 FB 72 DB 9D 46 BD B1 3C 56 C0 ED 3C 12 45 CA 1D 7E A3 ");
2310 /// ```
2311 ///
2312 /// ## For more examples,
2313 /// click [here](./documentation/big_cryptor64_cbc_iso/struct.BigCryptor64.html#method.encrypt_array_into_vec)
2314 #[inline]
2315 fn encrypt_array_into_vec<U, V, const N: usize>(&mut self, iv: T, message: &[U; N], cipher: &mut Vec<V>) -> u64
2316 where U: SmallUInt + Copy + Clone, V: SmallUInt + Copy + Clone
2317 {
2318 self.encrypt_into_vec(iv, message.as_ptr() as *const u8, (N as u32 * U::size_in_bytes()) as u64, cipher)
2319 }
2320
2321 // fn encrypt_array_into_array<U, V, const N: usize, const M: usize>(&mut self, iv: T, message: &[U; N], cipher: &mut [V; M]) -> u64
2322 /// Encrypts the data stored in an array `[U; N]` object with the padding
2323 /// according to ISO 7816-4 in CBC (Cipher-Block Chaining) mode, and stores the
2324 /// encrypted data in array `[V; M]`.
2325 ///
2326 /// # Arguments
2327 /// - `iv` is an initialization vector for CBC mode.
2328 /// - `message` is an immutable reference to an array `[U; N]` object, and
2329 /// is the place where the plaintext to be encrypted is stored.
2330 /// - `cipher` is a mutable reference to an array `[U; N]` object, and
2331 /// is the place where the encrypted data will be stored.
2332 ///
2333 /// # Output
2334 /// - This method returns the size of ciphertext including padding bits
2335 /// in bytes.
2336 /// - The output will be at least `size_of::<T>()`,
2337 /// and cannot be other than a multiple of `size_of::<T>()`.
2338 /// - If this method failed in encryption, this method returns `zero`.
2339 ///
2340 /// # Features
2341 /// - If `message` is an empty array `[U; 0]` object, only padding bytes
2342 /// will be encrypted, and stored in the array `[V; M]` object `cipher`.
2343 /// - If `V::size_in_bytes()` * `M` is less than
2344 /// `U::size_in_bytes()` * `N`'s next multiple of `size_of::<T>()`,
2345 /// this method does not perform encryption and returns `zero`.
2346 /// - If `V::size_in_bytes()` * `M` is equal to
2347 /// `U::size_in_bytes()` * `N`'s next multiple of `size_of::<T>()`,
2348 /// this method performs encryption, fills the array `cipher` with the
2349 /// encrypted ciphertext, and returns the size of the ciphertext including
2350 /// padding bits in bytes.
2351 /// - If `V::size_in_bytes()` * `M` is greater than
2352 /// `U::size_in_bytes()` * `N`'s next multiple of `size_of::<T>()`,
2353 /// this method performs encryption, fills the array `cipher` with the
2354 /// encrypted ciphertext, and then fills the rest of the elements of the
2355 /// array `cipher` with zeros, and returns the size of the ciphertext
2356 /// including padding bits in bytes.
2357 /// - The size of the area for ciphertext should be prepared to be
2358 /// (`size_of::<U>()` * `message.len()` + `1`).next_multiple_of(`size_of::<T>()`)
2359 /// at least.
2360 /// So, it is responsible for you to prepare the `cipher` area big enough!
2361 /// - The padding bits are composed of the byte `0b_1000_0000` that
2362 /// indicates the delimiter one bit `1` followed by seven bits `0`s and
2363 /// all padding bits `0`s according to ISO 7816-4.
2364 /// - For more information about the padding bits according to ISO 7816-4,
2365 /// Read [here](https://en.wikipedia.org/wiki/Padding_(cryptography)#ISO/IEC_7816-4).
2366 ///
2367 /// # For Rijndael or AES, and its variants
2368 /// ## Example 1 for AES-128
2369 /// ```
2370 /// use std::io::Write;
2371 /// use std::fmt::Write as _;
2372 /// use cryptocol::symmetric::{ AES_128, CBC_ISO };
2373 ///
2374 /// let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
2375 /// println!("K =\t{:#016X}", key);
2376 /// let mut a_aes = AES_128::new_with_key_u128(key);
2377 /// let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2378 /// println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2379 ///
2380 /// let mes = "In the beginning God created the heavens and the earth.";
2381 /// println!("M =\t{}", mes);
2382 /// let mut message = [0_u8; 55];
2383 /// message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2384 /// let mut cipher = [0_u8; 64];
2385 /// a_aes.encrypt_array_into_array(iv, &message, &mut cipher);
2386 /// print!("C =\t");
2387 /// for c in cipher.clone()
2388 /// { print!("{:02X} ", c); }
2389 /// println!();
2390 /// let mut txt = String::new();
2391 /// for c in cipher.clone()
2392 /// { write!(txt, "{:02X} ", c); }
2393 /// assert_eq!(txt, "C9 1C 27 CE 83 92 A1 CF 7D A4 64 35 16 48 01 72 CC E3 6D CD BB 19 FC D0 80 22 09 9F 23 32 73 27 58 37 F9 9B 3C 44 7B 03 B3 80 7E 99 DF 97 4E E9 EC D3 12 F1 1A 78 77 A6 A5 CB 73 AB 65 22 5B 7D ");
2394 /// ```
2395 ///
2396 /// ## For more examples,
2397 /// click [here](./documentation/rijndael_cbc_iso/struct.Rijndael_Generic.html#method.encrypt_array_into_array)
2398 ///
2399 /// # For DES and its variants
2400 /// ## Example 1 for Normal case
2401 /// ```
2402 /// use std::io::Write;
2403 /// use std::fmt::Write as _;
2404 /// use cryptocol::symmetric::{ DES, CBC_ISO };
2405 ///
2406 /// let key = 0x_1234567890ABCDEF_u64;
2407 /// println!("K =\t{:#016X}", key);
2408 /// let mut a_des = DES::new_with_key_u64(key);
2409 ///
2410 /// let mes = "In the beginning God created the heavens and the earth.";
2411 /// println!("M =\t{}", mes);
2412 /// let mut message = [0_u8; 55];
2413 /// message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2414 /// let iv = 0x_FEDCBA0987654321_u64;
2415 /// println!("IV = {}", iv);
2416 /// let mut cipher = [0_u8; 56];
2417 /// a_des.encrypt_array_into_array(iv, &message, &mut cipher);
2418 /// for c in cipher.clone()
2419 /// { print!("{:02X} ", c); }
2420 /// println!();
2421 /// let mut txt = String::new();
2422 /// for c in cipher.clone()
2423 /// { write!(txt, "{:02X} ", c); }
2424 /// assert_eq!(txt, "4B B5 ED DC A0 58 7E 6D 6C 3B A2 00 38 C3 D4 29 42 B1 CF 0D E9 FA EA 11 11 6B C8 30 73 39 DD B7 3F 96 9B A3 76 05 34 7E 64 2F D4 CC B2 68 33 64 11 78 69 FB 0B 32 CF 92 ");
2425 /// ```
2426 ///
2427 /// ## For more examples,
2428 /// click [here](./documentation/des_cbc_iso/struct.DES_Generic.html#method.encrypt_array_into_array)
2429 ///
2430 /// # For BigCryptor128
2431 /// ## Example 1 for TAES case
2432 /// ```
2433 /// use std::io::Write;
2434 /// use std::fmt::Write as _;
2435 /// use cryptocol::symmetric::{ BigCryptor128, AES_128, CBC_ISO };
2436 ///
2437 /// let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
2438 /// + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
2439 /// + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
2440 /// let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
2441 /// println!("IV = {:#034X}", iv);
2442 /// let mes = "In the beginning God created the heavens and the earth.";
2443 /// println!("M =\t{}", mes);
2444 /// let mut message = [0_u8; 55];
2445 /// message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2446 /// let mut cipher = [0_u8; 64];
2447 /// taes.encrypt_array_into_array(iv, &message, &mut cipher);
2448 /// for c in cipher.clone()
2449 /// { print!("{:02X} ", c); }
2450 /// println!();
2451 /// let mut txt = String::new();
2452 /// for c in cipher.clone()
2453 /// { write!(txt, "{:02X} ", c); }
2454 /// assert_eq!(txt, "87 B7 B0 67 C9 41 CF C6 CF B8 69 0D 39 D3 9B EC 0C 18 28 9A FA 51 7D 2A E4 D1 0C 2F 2B A0 0E E3 D1 04 65 E4 B6 66 2B 23 04 42 66 31 7A C5 76 E0 73 1D B1 F8 BB 75 C5 FB C2 1B 73 26 67 57 02 A0 ");
2455 /// ```
2456 ///
2457 /// ## For more examples,
2458 /// click [here](./documentation/big_cryptor128_cbc_iso/struct.BigCryptor128.html#method.encrypt_array_into_array)
2459 ///
2460 /// # For BigCryptor64
2461 /// ## Example 1 for TDES case
2462 /// ```
2463 /// use std::io::Write;
2464 /// use std::fmt::Write as _;
2465 /// use cryptocol::symmetric::{ BigCryptor64, DES, CBC_ISO };
2466 ///
2467 /// let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
2468 /// + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
2469 /// + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
2470 /// let iv = 0x_FEDCBA0987654321_u64;
2471 /// println!("IV = {:#018X}", iv);
2472 /// let mes = "In the beginning God created the heavens and the earth.";
2473 /// println!("M =\t{}", mes);
2474 /// let mut message = [0_u8; 55];
2475 /// message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2476 /// let mut cipher = [0_u8; 56];
2477 /// tdes.encrypt_array_into_array(iv, &message, &mut cipher);
2478 /// for c in cipher.clone()
2479 /// { print!("{:02X} ", c); }
2480 /// println!();
2481 /// let mut txt = String::new();
2482 /// for c in cipher.clone()
2483 /// { write!(txt, "{:02X} ", c); }
2484 /// assert_eq!(txt, "01 A5 7E BC ED 83 28 FB 7A 06 61 60 7D 8B BA AF 5C A7 76 0B FE 94 1C C4 C2 BC DD 15 98 A9 98 6C 85 18 92 6B 00 72 FB 72 DB 9D 46 BD B1 3C 56 C0 ED 3C 12 45 CA 1D 7E A3 ");
2485 /// ```
2486 ///
2487 /// ## For more examples,
2488 /// click [here](./documentation/big_cryptor64_cbc_iso/struct.BigCryptor64.html#method.encrypt_array_into_array)
2489 #[inline]
2490 fn encrypt_array_into_array<U, V, const N: usize, const M: usize>(&mut self, iv: T, message: &[U; N], cipher: &mut [V; M]) -> u64
2491 where U: SmallUInt + Copy + Clone, V: SmallUInt + Copy + Clone
2492 {
2493 self.encrypt_into_array(iv, message.as_ptr() as *const u8, (N as u32 * U::size_in_bytes()) as u64, cipher)
2494 }
2495
2496 // fn decrypt(&mut self, iv: T, cipher: *const u8, length_in_bytes: u64, message: *mut u8) -> u64;
2497 /// Decrypts the data with the padding defined according to ISO 7816-4
2498 /// in CBC (Cipher-Block Chaining) mode.
2499 ///
2500 /// # Arguments
2501 /// - `iv` is an initialization vector for CBC mode.
2502 /// - `cipher` is an immutable pointer to `u8` which is `*const u8`,
2503 /// and is the place where the ciphertext to be decrypted is stored.
2504 /// - `length_in_bytes` is of `u64`-type,
2505 /// and is the length of the ciphertext `cipher` in bytes.
2506 /// - `message` is a mutable pointer to `u8` which is `*mut u8`, and
2507 /// is the place where the decrypted data will be stored.
2508 ///
2509 /// # Output
2510 /// - This method returns the size of plaintext in bytes.
2511 /// - If this method failed in decryption, it always returns `zero`.
2512 /// - Even if this method succeeded in decryption, it returns `zero` when
2513 /// the original plaintext is zero-length empty data. Then, you will have
2514 /// to check whether or not it failed by using the method
2515 /// `is_successful()` or `is_failed()`.
2516 /// - If `length_in_bytes` is greater than `size_of::<T>()` (which means
2517 /// that the original plaintext is surely not empty data) and it returns
2518 /// `zero`, you can interpret it that this method surely failed in
2519 /// decryption.
2520 ///
2521 /// # Features
2522 /// - You are not encouraged to use this method in pure Rust programming.
2523 /// Instead, use other safer methods such as `decrypt_*_into_*()`.
2524 /// - This method is useful to use in hybrid programming with C/C++.
2525 /// - `length_in_bytes` cannot be other than any multiple of `size_of::<T>()`.
2526 /// - The size of the memory area which starts at `message` is assumed to
2527 /// be enough to store the plaintext. So, it is responsible for you to
2528 /// prepare the `message` area big enough!
2529 /// - The size of the area for plaintext does not have to be prepared more
2530 /// than `length_in_bytes` - `1`.
2531 /// - If the size of the area for plaintext is prepared more than
2532 /// `length_in_bytes` - `1`, the rest of the area will be filled with
2533 /// `0`s.
2534 /// - The padding bits are composed of the byte `0b_1000_0000` that
2535 /// indicates the delimiter one bit `1` followed by seven bits `0`s and
2536 /// all padding bits `0`s according to ISO 7816-4.
2537 /// - For more information about the padding bits according to ISO 7816-4,
2538 /// Read [here](https://en.wikipedia.org/wiki/Padding_(cryptography)#ISO/IEC_7816-4).
2539 ///
2540 /// # For Rijndael or AES, and its variants
2541 /// ## Example 1 for AES-128
2542 /// ```
2543 /// use std::io::Write;
2544 /// use std::fmt::Write as _;
2545 /// use cryptocol::symmetric::{ AES_128, CBC_ISO };
2546 ///
2547 /// let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
2548 /// println!("K =\t{:#016X}", key);
2549 /// let mut a_aes = AES_128::new_with_key_u128(key);
2550 /// let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2551 /// println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2552 ///
2553 /// let message = "In the beginning God created the heavens and the earth.";
2554 /// println!("M =\t{}", message);
2555 /// let mut cipher = [0_u8; 64];
2556 /// a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2557 /// print!("C =\t");
2558 /// for c in cipher.clone()
2559 /// { print!("{:02X} ", c); }
2560 /// println!();
2561 /// let mut txt = String::new();
2562 /// for c in cipher.clone()
2563 /// { write!(txt, "{:02X} ", c); }
2564 /// assert_eq!(txt, "C9 1C 27 CE 83 92 A1 CF 7D A4 64 35 16 48 01 72 CC E3 6D CD BB 19 FC D0 80 22 09 9F 23 32 73 27 58 37 F9 9B 3C 44 7B 03 B3 80 7E 99 DF 97 4E E9 EC D3 12 F1 1A 78 77 A6 A5 CB 73 AB 65 22 5B 7D ");
2565 ///
2566 /// let mut recovered = vec![0; 55];
2567 /// a_aes.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2568 /// print!("Ba =\t");
2569 /// for b in recovered.clone()
2570 /// { print!("{:02X} ", b); }
2571 /// println!();
2572 /// let mut txt = String::new();
2573 /// for c in recovered.clone()
2574 /// { write!(txt, "{:02X} ", c); }
2575 /// 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 ");
2576 ///
2577 /// let mut converted = String::new();
2578 /// unsafe { converted.as_mut_vec() }.append(&mut recovered);
2579 ///
2580 /// println!("Bb =\t{}", converted);
2581 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2582 /// assert_eq!(converted, message);
2583 /// ```
2584 ///
2585 /// ## For more examples,
2586 /// click [here](./documentation/rijndael_cbc_iso/struct.Rijndael_Generic.html#method.decrypt)
2587 ///
2588 /// # For DES and its variants
2589 /// ## Example 1 for Normal case
2590 /// ```
2591 /// use std::io::Write;
2592 /// use std::fmt::Write as _;
2593 /// use cryptocol::symmetric::{ DES, CBC_ISO };
2594 ///
2595 /// let key = 0x_1234567890ABCDEF_u64;
2596 /// println!("K =\t{:#016X}", key);
2597 /// let mut a_des = DES::new_with_key_u64(key);
2598 ///
2599 /// let message = "In the beginning God created the heavens and the earth.";
2600 /// println!("M =\t{}", message);
2601 /// let iv = 0x_FEDCBA0987654321_u64;
2602 /// println!("IV = {}", iv);
2603 /// let mut cipher = Vec::<u8>::new();
2604 /// a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
2605 /// print!("C (16 rounds) =\t");
2606 /// for c in cipher.clone()
2607 /// { print!("{:02X} ", c); }
2608 /// println!();
2609 /// let mut txt = String::new();
2610 /// for c in cipher.clone()
2611 /// { write!(txt, "{:02X} ", c); }
2612 /// assert_eq!(txt, "4B B5 ED DC A0 58 7E 6D 6C 3B A2 00 38 C3 D4 29 42 B1 CF 0D E9 FA EA 11 11 6B C8 30 73 39 DD B7 3F 96 9B A3 76 05 34 7E 64 2F D4 CC B2 68 33 64 11 78 69 FB 0B 32 CF 92 ");
2613 ///
2614 /// let mut recovered = vec![0; 55];
2615 /// a_des.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2616 /// print!("Ba (16 rounds) =\t");
2617 /// for b in recovered.clone()
2618 /// { print!("{:02X} ", b); }
2619 /// println!();
2620 /// let mut txt = String::new();
2621 /// for c in recovered.clone()
2622 /// { write!(txt, "{:02X} ", c); }
2623 /// 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 ");
2624 ///
2625 /// let mut converted = String::new();
2626 /// unsafe { converted.as_mut_vec() }.append(&mut recovered);
2627 ///
2628 /// println!("Bb (16 rounds) =\t{}", converted);
2629 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2630 /// assert_eq!(converted, message);
2631 /// ```
2632 ///
2633 /// ## For more examples,
2634 /// click [here](./documentation/des_cbc_iso/struct.DES_Generic.html#method.decrypt)
2635 ///
2636 /// # For BigCryptor128
2637 /// ## Example 1 for TAES case
2638 /// ```
2639 /// use std::io::Write;
2640 /// use std::fmt::Write as _;
2641 /// use cryptocol::symmetric::{ BigCryptor128, AES_128, CBC_ISO };
2642 ///
2643 /// let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
2644 /// + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
2645 /// + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
2646 /// let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
2647 /// println!("IV = {:#034X}", iv);
2648 /// let message = "In the beginning God created the heavens and the earth.";
2649 /// println!("M =\t{}", message);
2650 /// let mut cipher = Vec::<u8>::new();
2651 /// taes.encrypt_str_into_vec(iv, &message, &mut cipher);
2652 /// print!("C =\t");
2653 /// for c in cipher.clone()
2654 /// { print!("{:02X} ", c); }
2655 /// println!();
2656 /// let mut txt = String::new();
2657 /// for c in cipher.clone()
2658 /// { write!(txt, "{:02X} ", c); }
2659 /// assert_eq!(txt, "87 B7 B0 67 C9 41 CF C6 CF B8 69 0D 39 D3 9B EC 0C 18 28 9A FA 51 7D 2A E4 D1 0C 2F 2B A0 0E E3 D1 04 65 E4 B6 66 2B 23 04 42 66 31 7A C5 76 E0 73 1D B1 F8 BB 75 C5 FB C2 1B 73 26 67 57 02 A0 ");
2660 ///
2661 /// let mut recovered = vec![0; 55];
2662 /// taes.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2663 /// print!("Ba =\t");
2664 /// for b in recovered.clone()
2665 /// { print!("{:02X} ", b); }
2666 /// println!();
2667 /// let mut txt = String::new();
2668 /// for c in recovered.clone()
2669 /// { write!(txt, "{:02X} ", c); }
2670 /// 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 ");
2671 ///
2672 /// let mut converted = String::new();
2673 /// unsafe { converted.as_mut_vec() }.append(&mut recovered);
2674 ///
2675 /// println!("Bb =\t{}", converted);
2676 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2677 /// assert_eq!(converted, message);
2678 /// ```
2679 ///
2680 /// ## For more examples,
2681 /// click [here](./documentation/big_cryptor128_cbc_iso/struct.BigCryptor128.html#method.decrypt)
2682 ///
2683 /// # For BigCryptor64
2684 /// ## Example 1 for TDES case
2685 /// ```
2686 /// use std::io::Write;
2687 /// use std::fmt::Write as _;
2688 /// use cryptocol::symmetric::{ BigCryptor64, DES, CBC_ISO };
2689 ///
2690 /// let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
2691 /// + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
2692 /// + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
2693 /// let iv = 0x_FEDCBA0987654321_u64;
2694 /// println!("IV = {:#018X}", iv);
2695 /// let message = "In the beginning God created the heavens and the earth.";
2696 /// println!("M =\t{}", message);
2697 /// let mut cipher = Vec::<u8>::new();
2698 /// tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
2699 /// print!("C =\t");
2700 /// for c in cipher.clone()
2701 /// { print!("{:02X} ", c); }
2702 /// println!();
2703 /// let mut txt = String::new();
2704 /// for c in cipher.clone()
2705 /// { write!(txt, "{:02X} ", c); }
2706 /// assert_eq!(txt, "01 A5 7E BC ED 83 28 FB 7A 06 61 60 7D 8B BA AF 5C A7 76 0B FE 94 1C C4 C2 BC DD 15 98 A9 98 6C 85 18 92 6B 00 72 FB 72 DB 9D 46 BD B1 3C 56 C0 ED 3C 12 45 CA 1D 7E A3 ");
2707 ///
2708 /// let mut recovered = vec![0; 55];
2709 /// tdes.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2710 /// print!("Ba =\t");
2711 /// for b in recovered.clone()
2712 /// { print!("{:02X} ", b); }
2713 /// println!();
2714 /// let mut txt = String::new();
2715 /// for c in recovered.clone()
2716 /// { write!(txt, "{:02X} ", c); }
2717 /// 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 ");
2718 ///
2719 /// let mut converted = String::new();
2720 /// unsafe { converted.as_mut_vec() }.append(&mut recovered);
2721 ///
2722 /// println!("Bb =\t{}", converted);
2723 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2724 /// assert_eq!(converted, message);
2725 /// ```
2726 ///
2727 /// ## For more examples,
2728 /// click [here](./documentation/big_cryptor64_cbc_iso/struct.BigCryptor64.html#method.decrypt)
2729 fn decrypt(&mut self, iv: T, cipher: *const u8, length_in_bytes: u64, message: *mut u8) -> u64;
2730
2731 // fn decrypt_into_vec<U>(&mut self, iv: T, cipher: *const u8, length_in_bytes: u64, message: &mut Vec<U>) -> u64
2732 /// Decrypts the data with the padding defined according to ISO 7816-4
2733 /// in CBC (Cipher-Block Chaining) mode, and stores the decrypted data
2734 /// in `Vec<U>`.
2735 ///
2736 /// # Arguments
2737 /// - `iv` is an initialization vector for CBC mode.
2738 /// - `cipher` is an immutable pointer to `u8` which is `*const u8`,
2739 /// and is the place where the ciphertext to be decrypted is stored.
2740 /// - `length_in_bytes` is of `u64`-type,
2741 /// and is the length of the ciphertext `cipher` in bytes.
2742 /// - `message` is a mutable reference to `Vec<U>` object, and
2743 /// is the place where the decrypted data will be stored.
2744 ///
2745 /// # Output
2746 /// - This method returns the size of plaintext in bytes.
2747 /// - If this method failed in decryption, it always returns `zero`.
2748 /// - Even if this method succeeded in decryption, it returns `zero` when
2749 /// the original plaintext is zero-length empty data. Then, you will have
2750 /// to check whether or not it failed by using the method
2751 /// `is_successful()` or `is_failed()`.
2752 /// - If `length_in_bytes` is greater than `size_of::<T>()` (which means
2753 /// that the original plaintext is surely not empty data) and it returns
2754 /// `zero`, you can interpret it that this method surely failed in
2755 /// decryption.
2756 ///
2757 /// # Features
2758 /// - You are not encouraged to use this method in pure Rust programming.
2759 /// Instead, use other safer methods such as decrypt_*_into_vec().
2760 /// - This method is useful to use in hybrid programming with C/C++.
2761 /// - `length_in_bytes` cannot be other than any multiple of `size_of::<T>()`.
2762 /// - The padding bits are composed of the byte `0b_1000_0000` that
2763 /// indicates the delimiter one bit `1` followed by seven bits `0`s and
2764 /// all padding bits `0`s according to ISO 7816-4.
2765 /// - For more information about the padding bits according to ISO 7816-4,
2766 /// Read [here](https://en.wikipedia.org/wiki/Padding_(cryptography)#ISO/IEC_7816-4).
2767 /// - You don't have to worry about whether or not the size of the memory
2768 /// area where the plaintext will be stored is enough.
2769 ///
2770 /// # For Rijndael or AES, and its variants
2771 /// ## Example 1 for AES-128
2772 /// ```
2773 /// use std::io::Write;
2774 /// use std::fmt::Write as _;
2775 /// use cryptocol::symmetric::{ AES_128, CBC_ISO };
2776 ///
2777 /// let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
2778 /// println!("K =\t{:#016X}", key);
2779 /// let mut a_aes = AES_128::new_with_key_u128(key);
2780 /// let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2781 /// println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2782 ///
2783 /// let message = "In the beginning God created the heavens and the earth.";
2784 /// println!("M =\t{}", message);
2785 /// let mut cipher = [0_u8; 64];
2786 /// a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2787 /// print!("C =\t");
2788 /// for c in cipher.clone()
2789 /// { print!("{:02X} ", c); }
2790 /// println!();
2791 /// let mut txt = String::new();
2792 /// for c in cipher.clone()
2793 /// { write!(txt, "{:02X} ", c); }
2794 /// assert_eq!(txt, "C9 1C 27 CE 83 92 A1 CF 7D A4 64 35 16 48 01 72 CC E3 6D CD BB 19 FC D0 80 22 09 9F 23 32 73 27 58 37 F9 9B 3C 44 7B 03 B3 80 7E 99 DF 97 4E E9 EC D3 12 F1 1A 78 77 A6 A5 CB 73 AB 65 22 5B 7D ");
2795 ///
2796 /// let mut recovered = Vec::<u8>::new();
2797 /// a_aes.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2798 /// print!("Ba =\t");
2799 /// for b in recovered.clone()
2800 /// { print!("{:02X} ", b); }
2801 /// println!();
2802 /// let mut txt = String::new();
2803 /// for c in recovered.clone()
2804 /// { write!(txt, "{:02X} ", c); }
2805 /// 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 ");
2806 ///
2807 /// let mut converted = String::new();
2808 /// unsafe { converted.as_mut_vec() }.append(&mut recovered);
2809 ///
2810 /// println!("Bb =\t{}", converted);
2811 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2812 /// assert_eq!(converted, message);
2813 /// ```
2814 ///
2815 /// ## For more examples,
2816 /// click [here](./documentation/rijndael_cbc_iso/struct.Rijndael_Generic.html#method.decrypt_into_vec)
2817 ///
2818 /// # For DES and its variants
2819 /// ## Example 1 for Normal case
2820 /// ```
2821 /// use std::io::Write;
2822 /// use std::fmt::Write as _;
2823 /// use cryptocol::symmetric::{ DES, CBC_ISO };
2824 ///
2825 /// let key = 0x_1234567890ABCDEF_u64;
2826 /// println!("K =\t{:#016X}", key);
2827 /// let mut a_des = DES::new_with_key_u64(key);
2828 ///
2829 /// let message = "In the beginning God created the heavens and the earth.";
2830 /// println!("M =\t{}", message);
2831 /// let iv = 0x_FEDCBA0987654321_u64;
2832 /// println!("IV = {}", iv);
2833 /// let mut cipher = Vec::<u8>::new();
2834 /// a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
2835 /// print!("C (16 rounds) =\t");
2836 /// for c in cipher.clone()
2837 /// { print!("{:02X} ", c); }
2838 /// println!();
2839 /// let mut txt = String::new();
2840 /// for c in cipher.clone()
2841 /// { write!(txt, "{:02X} ", c); }
2842 /// assert_eq!(txt, "4B B5 ED DC A0 58 7E 6D 6C 3B A2 00 38 C3 D4 29 42 B1 CF 0D E9 FA EA 11 11 6B C8 30 73 39 DD B7 3F 96 9B A3 76 05 34 7E 64 2F D4 CC B2 68 33 64 11 78 69 FB 0B 32 CF 92 ");
2843 ///
2844 /// let mut recovered = Vec::<u8>::new();
2845 /// a_des.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2846 /// print!("Ba (16 rounds) =\t");
2847 /// for b in recovered.clone()
2848 /// { print!("{:02X} ", b); }
2849 /// println!();
2850 /// let mut txt = String::new();
2851 /// for c in recovered.clone()
2852 /// { write!(txt, "{:02X} ", c); }
2853 /// 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 ");
2854 ///
2855 /// let mut converted = String::new();
2856 /// unsafe { converted.as_mut_vec() }.append(&mut recovered);
2857 ///
2858 /// println!("Bb (16 rounds) =\t{}", converted);
2859 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2860 /// assert_eq!(converted, message);
2861 /// ```
2862 ///
2863 /// ## For more examples,
2864 /// click [here](./documentation/des_cbc_iso/struct.DES_Generic.html#method.decrypt_into_vec)
2865 ///
2866 /// # For BigCryptor128
2867 /// ## Example 1 for TAES case
2868 /// ```
2869 /// use std::io::Write;
2870 /// use std::fmt::Write as _;
2871 /// use cryptocol::symmetric::{ BigCryptor128, AES_128, CBC_ISO };
2872 ///
2873 /// let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
2874 /// + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
2875 /// + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
2876 /// let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
2877 /// println!("IV = {:#034X}", iv);
2878 /// let message = "In the beginning God created the heavens and the earth.";
2879 /// println!("M =\t{}", message);
2880 /// let mut cipher = Vec::<u8>::new();
2881 /// taes.encrypt_str_into_vec(iv, &message, &mut cipher);
2882 /// print!("C =\t");
2883 /// for c in cipher.clone()
2884 /// { print!("{:02X} ", c); }
2885 /// println!();
2886 /// let mut txt = String::new();
2887 /// for c in cipher.clone()
2888 /// { write!(txt, "{:02X} ", c); }
2889 /// assert_eq!(txt, "87 B7 B0 67 C9 41 CF C6 CF B8 69 0D 39 D3 9B EC 0C 18 28 9A FA 51 7D 2A E4 D1 0C 2F 2B A0 0E E3 D1 04 65 E4 B6 66 2B 23 04 42 66 31 7A C5 76 E0 73 1D B1 F8 BB 75 C5 FB C2 1B 73 26 67 57 02 A0 ");
2890 ///
2891 /// let mut recovered = Vec::<u8>::new();
2892 /// taes.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2893 /// print!("Ba =\t");
2894 /// for b in recovered.clone()
2895 /// { print!("{:02X} ", b); }
2896 /// println!();
2897 /// let mut txt = String::new();
2898 /// for c in recovered.clone()
2899 /// { write!(txt, "{:02X} ", c); }
2900 /// assert_eq!(txt, "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 ");
2901 ///
2902 /// let mut converted = String::new();
2903 /// unsafe { converted.as_mut_vec() }.append(&mut recovered);
2904 ///
2905 /// println!("Bb =\t{}", converted);
2906 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2907 /// assert_eq!(converted, message);
2908 /// ```
2909 ///
2910 /// ## For more examples,
2911 /// click [here](./documentation/big_cryptor128_cbc_iso/struct.BigCryptor128.html#method.decrypt_into_vec)
2912 ///
2913 /// # For BigCryptor64
2914 /// ## Example 1 for TDES case
2915 /// ```
2916 /// use std::io::Write;
2917 /// use std::fmt::Write as _;
2918 /// use cryptocol::symmetric::{ BigCryptor64, DES, CBC_ISO };
2919 ///
2920 /// let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
2921 /// + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
2922 /// + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
2923 /// let iv = 0x_FEDCBA0987654321_u64;
2924 /// println!("IV = {:#018X}", iv);
2925 /// let message = "In the beginning God created the heavens and the earth.";
2926 /// println!("M =\t{}", message);
2927 /// let mut cipher = Vec::<u8>::new();
2928 /// tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
2929 /// print!("C =\t");
2930 /// for c in cipher.clone()
2931 /// { print!("{:02X} ", c); }
2932 /// println!();
2933 /// let mut txt = String::new();
2934 /// for c in cipher.clone()
2935 /// { write!(txt, "{:02X} ", c); }
2936 /// assert_eq!(txt, "01 A5 7E BC ED 83 28 FB 7A 06 61 60 7D 8B BA AF 5C A7 76 0B FE 94 1C C4 C2 BC DD 15 98 A9 98 6C 85 18 92 6B 00 72 FB 72 DB 9D 46 BD B1 3C 56 C0 ED 3C 12 45 CA 1D 7E A3 ");
2937 ///
2938 /// let mut recovered = Vec::<u8>::new();
2939 /// tdes.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2940 /// print!("Ba =\t");
2941 /// for b in recovered.clone()
2942 /// { print!("{:02X} ", b); }
2943 /// println!();
2944 /// let mut txt = String::new();
2945 /// for c in recovered.clone()
2946 /// { write!(txt, "{:02X} ", c); }
2947 /// 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 ");
2948 ///
2949 /// let mut converted = String::new();
2950 /// unsafe { converted.as_mut_vec() }.append(&mut recovered);
2951 ///
2952 /// println!("Bb =\t{}", converted);
2953 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2954 /// assert_eq!(converted, message);
2955 /// ```
2956 ///
2957 /// ## For more examples,
2958 /// click [here](./documentation/big_cryptor64_cbc_iso/struct.BigCryptor64.html#method.decrypt_into_vec)
2959 fn decrypt_into_vec<U>(&mut self, iv: T, cipher: *const u8, length_in_bytes: u64, message: &mut Vec<U>) -> u64
2960 where U: SmallUInt + Copy + Clone
2961 {
2962 pre_decrypt_into_vec!(message, length_in_bytes, U);
2963 let len = self.decrypt(iv, cipher, length_in_bytes, message.as_mut_ptr() as *mut u8);
2964 message.truncate(len as usize);
2965 len
2966 }
2967
2968 // fn decrypt_into_array<U, const N: usize>(&mut self, iv: T, cipher: *const u8, length_in_bytes: u64, message: &mut [U; N]) -> u64
2969 /// Decrypts the data with the padding defined according to ISO 7816-4
2970 /// in CBC (Cipher-Block Chaining) mode, and stores the decrypted data
2971 /// in array `[U; N]`.
2972 ///
2973 /// # Arguments
2974 /// - `iv` is an initialization vector for CBC mode.
2975 /// - `cipher` is an immutable pointer to `u8` which is `*const u8`,
2976 /// and is the place where the ciphertext to be decrypted is stored.
2977 /// - `length_in_bytes` is of `u64`-type,
2978 /// and is the length of the ciphertext `cipher` in bytes.
2979 /// - `message` is a mutable reference to an array `[U; N]` object, and
2980 /// is the place where the decrypted data will be stored.
2981 ///
2982 /// # Output
2983 /// - This method returns the size of plaintext in bytes.
2984 /// - If this method failed in decryption, it always returns `zero`.
2985 /// - Even if this method succeeded in decryption, it returns `zero` when
2986 /// the original plaintext is zero-length empty data. Then, you will have
2987 /// to check whether or not it failed by using the method
2988 /// `is_successful()` or `is_failed()`.
2989 /// - If `length_in_bytes` is greater than `size_of::<T>()` (which means
2990 /// that the original plaintext is surely not empty data) and it returns
2991 /// `zero`, you can interpret it that this method surely failed in
2992 /// decryption.
2993 ///
2994 /// # Features
2995 /// - You are not encouraged to use this method in pure Rust programming.
2996 /// Instead, use other safer methods such as decrypt_*_into_array().
2997 /// - This method is useful to use in hybrid programming with C/C++.
2998 /// - `length_in_bytes` cannot be other than any multiple of `size_of::<T>()`.
2999 /// - If `U::size_in_bytes()` * `N` is less than `length_in_bytes` - `1`,
3000 /// this method does not perform decryption but returns `zero`.
3001 /// - If `U::size_in_bytes()` * `N` is equal to or greater than
3002 /// `length_in_bytes` - `1`, this method performs decryption, fills the
3003 /// array `message` with the decrypted data, and then fills the rest of
3004 /// the elements of the array `message` with zeros, and returns the size
3005 /// of the plaintext.
3006 /// - It is responsible for you to prepare the `message` area big enough!
3007 /// - The size of the area for plaintext does not have to be prepared more
3008 /// than `length_in_bytes` - `1`.
3009 /// - The padding bits are composed of the byte `0b_1000_0000` that
3010 /// indicates the delimiter one bit `1` followed by seven bits `0`s and
3011 /// all padding bits `0`s according to ISO 7816-4.
3012 /// - For more information about the padding bits according to ISO 7816-4,
3013 /// Read [here](https://en.wikipedia.org/wiki/Padding_(cryptography)#ISO/IEC_7816-4).
3014 ///
3015 /// # For Rijndael or AES, and its variants
3016 /// ## Example 1 for AES-128
3017 /// ```
3018 /// use std::io::Write;
3019 /// use std::fmt::Write as _;
3020 /// use cryptocol::symmetric::{ AES_128, CBC_ISO };
3021 ///
3022 /// let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
3023 /// println!("K =\t{:#016X}", key);
3024 /// let mut a_aes = AES_128::new_with_key_u128(key);
3025 /// let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3026 /// println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3027 ///
3028 /// let message = "In the beginning God created the heavens and the earth.";
3029 /// println!("M =\t{}", message);
3030 /// let mut cipher = [0_u8; 64];
3031 /// a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
3032 /// print!("C =\t");
3033 /// for c in cipher.clone()
3034 /// { print!("{:02X} ", c); }
3035 /// println!();
3036 /// let mut txt = String::new();
3037 /// for c in cipher.clone()
3038 /// { write!(txt, "{:02X} ", c); }
3039 /// assert_eq!(txt, "C9 1C 27 CE 83 92 A1 CF 7D A4 64 35 16 48 01 72 CC E3 6D CD BB 19 FC D0 80 22 09 9F 23 32 73 27 58 37 F9 9B 3C 44 7B 03 B3 80 7E 99 DF 97 4E E9 EC D3 12 F1 1A 78 77 A6 A5 CB 73 AB 65 22 5B 7D ");
3040 ///
3041 /// let mut recovered = [0; 64];
3042 /// let len = a_aes.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3043 /// print!("Ba =\t");
3044 /// for b in recovered.clone()
3045 /// { print!("{:02X} ", b); }
3046 /// println!();
3047 /// let mut txt = String::new();
3048 /// for c in recovered.clone()
3049 /// { write!(txt, "{:02X} ", c); }
3050 /// 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 00 00 00 00 00 00 00 00 ");
3051 ///
3052 /// let mut converted = String::new();
3053 /// unsafe { converted.as_mut_vec() }.write(&recovered);
3054 /// unsafe { converted.as_mut_vec() }.truncate(len as usize);
3055 ///
3056 /// println!("Bb =\t{}", converted);
3057 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3058 /// assert_eq!(converted, message);
3059 /// ```
3060 ///
3061 /// ## For more examples,
3062 /// click [here](./documentation/rijndael_cbc_iso/struct.Rijndael_Generic.html#method.decrypt_into_array)
3063 ///
3064 /// # For DES and its variants
3065 /// ## Example 1 for Normal case
3066 /// ```
3067 /// use std::io::Write;
3068 /// use std::fmt::Write as _;
3069 /// use cryptocol::symmetric::{ DES, CBC_ISO };
3070 ///
3071 /// let key = 0x_1234567890ABCDEF_u64;
3072 /// println!("K =\t{:#016X}", key);
3073 /// let mut a_des = DES::new_with_key_u64(key);
3074 ///
3075 /// let message = "In the beginning God created the heavens and the earth.";
3076 /// println!("M =\t{}", message);
3077 /// let iv = 0x_FEDCBA0987654321_u64;
3078 /// println!("IV = {}", iv);
3079 /// let mut cipher = Vec::<u8>::new();
3080 /// a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3081 /// print!("C (16 rounds) =\t");
3082 /// for c in cipher.clone()
3083 /// { print!("{:02X} ", c); }
3084 /// println!();
3085 /// let mut txt = String::new();
3086 /// for c in cipher.clone()
3087 /// { write!(txt, "{:02X} ", c); }
3088 /// assert_eq!(txt, "4B B5 ED DC A0 58 7E 6D 6C 3B A2 00 38 C3 D4 29 42 B1 CF 0D E9 FA EA 11 11 6B C8 30 73 39 DD B7 3F 96 9B A3 76 05 34 7E 64 2F D4 CC B2 68 33 64 11 78 69 FB 0B 32 CF 92 ");
3089 ///
3090 /// let mut recovered = [0u8; 56];
3091 /// let len = a_des.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3092 /// print!("Ba (16 rounds) =\t");
3093 /// for b in recovered.clone()
3094 /// { print!("{:02X} ", b); }
3095 /// println!();
3096 /// let mut txt = String::new();
3097 /// for c in recovered.clone()
3098 /// { write!(txt, "{:02X} ", c); }
3099 /// assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 ");
3100 ///
3101 /// let mut converted = String::new();
3102 /// unsafe { converted.as_mut_vec() }.write(&recovered);
3103 /// unsafe { converted.as_mut_vec() }.truncate(len as usize);
3104 /// println!("Bb (16 rounds) =\t{}", converted);
3105 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3106 /// assert_eq!(converted, message);
3107 /// ```
3108 ///
3109 /// ## For more examples,
3110 /// click [here](./documentation/des_cbc_iso/struct.DES_Generic.html#method.decrypt_into_array)
3111 ///
3112 /// # For BigCryptor128
3113 /// ## Example 1 for TAES case
3114 /// ```
3115 /// use std::io::Write;
3116 /// use std::fmt::Write as _;
3117 /// use cryptocol::symmetric::{ BigCryptor128, AES_128, CBC_ISO };
3118 ///
3119 /// let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
3120 /// + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
3121 /// + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
3122 /// let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
3123 /// println!("IV = {:#034X}", iv);
3124 /// let message = "In the beginning God created the heavens and the earth.";
3125 /// println!("M =\t{}", message);
3126 /// let mut cipher = Vec::<u8>::new();
3127 /// taes.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, "87 B7 B0 67 C9 41 CF C6 CF B8 69 0D 39 D3 9B EC 0C 18 28 9A FA 51 7D 2A E4 D1 0C 2F 2B A0 0E E3 D1 04 65 E4 B6 66 2B 23 04 42 66 31 7A C5 76 E0 73 1D B1 F8 BB 75 C5 FB C2 1B 73 26 67 57 02 A0 ");
3136 ///
3137 /// let mut recovered = [0u8; 56];
3138 /// let len = taes.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
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, "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 ");
3147 ///
3148 /// let mut converted = String::new();
3149 /// unsafe { converted.as_mut_vec() }.write(&recovered);
3150 /// unsafe { converted.as_mut_vec() }.truncate(len as usize);
3151 /// println!("Bb =\t{}", converted);
3152 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3153 /// assert_eq!(converted, message);
3154 /// ```
3155 ///
3156 /// ## For more examples,
3157 /// click [here](./documentation/big_cryptor128_cbc_iso/struct.BigCryptor128.html#method.decrypt_into_array)
3158 ///
3159 /// # For BigCryptor64
3160 /// ## Example 1 for TDES case
3161 /// ```
3162 /// use std::io::Write;
3163 /// use std::fmt::Write as _;
3164 /// use cryptocol::symmetric::{ BigCryptor64, DES, CBC_ISO };
3165 ///
3166 /// let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
3167 /// + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
3168 /// + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
3169 /// let iv = 0x_FEDCBA0987654321_u64;
3170 /// println!("IV = {:#018X}", iv);
3171 /// let message = "In the beginning God created the heavens and the earth.";
3172 /// println!("M =\t{}", message);
3173 /// let mut cipher = Vec::<u8>::new();
3174 /// tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
3175 /// print!("C =\t");
3176 /// for c in cipher.clone()
3177 /// { print!("{:02X} ", c); }
3178 /// println!();
3179 /// let mut txt = String::new();
3180 /// for c in cipher.clone()
3181 /// { write!(txt, "{:02X} ", c); }
3182 /// assert_eq!(txt, "01 A5 7E BC ED 83 28 FB 7A 06 61 60 7D 8B BA AF 5C A7 76 0B FE 94 1C C4 C2 BC DD 15 98 A9 98 6C 85 18 92 6B 00 72 FB 72 DB 9D 46 BD B1 3C 56 C0 ED 3C 12 45 CA 1D 7E A3 ");
3183 ///
3184 /// let mut recovered = [0u8; 56];
3185 /// let len = tdes.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3186 /// print!("Ba =\t");
3187 /// for b in recovered.clone()
3188 /// { print!("{:02X} ", b); }
3189 /// println!();
3190 /// let mut txt = String::new();
3191 /// for c in recovered.clone()
3192 /// { write!(txt, "{:02X} ", c); }
3193 /// 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 ");
3194 ///
3195 /// let mut converted = String::new();
3196 /// unsafe { converted.as_mut_vec() }.write(&recovered);
3197 /// unsafe { converted.as_mut_vec() }.truncate(len as usize);
3198 /// println!("Bb =\t{}", converted);
3199 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3200 /// assert_eq!(converted, message);
3201 /// ```
3202 ///
3203 /// ## For more examples,
3204 /// click [here](./documentation/big_cryptor64_cbc_iso/struct.BigCryptor64.html#method.decrypt_into_array)
3205 fn decrypt_into_array<U, const N: usize>(&mut self, iv: T, cipher: *const u8, length_in_bytes: u64, message: &mut [U; N]) -> u64
3206 where U: SmallUInt + Copy + Clone;
3207
3208 // fn decrypt_into_string(&mut self, iv: T, cipher: *const u8, length_in_bytes: u64, message: &mut String) -> u64
3209 /// Decrypts the data with the padding defined according to ISO 7816-4
3210 /// in CBC (Cipher-Block Chaining) mode, and stores the decrypted data
3211 /// in a `String`.
3212 ///
3213 /// # Arguments
3214 /// - `iv` is an initialization vector for CBC mode.
3215 /// - `cipher` is an immutable pointer to `u8` which is `*const u8`,
3216 /// and is the place where the ciphertext to be decrypted is stored.
3217 /// - `length_in_bytes` is of `u64`-type,
3218 /// and is the length of the ciphertext `cipher` in bytes.
3219 /// - `message` is a mutable reference to a `String` object, and
3220 /// is the place where the decrypted data will be stored.
3221 ///
3222 /// # Output
3223 /// - This method returns the size of plaintext in bytes.
3224 /// - If this method failed in decryption, it always returns `zero`.
3225 /// - Even if this method succeeded in decryption, it returns `zero` when
3226 /// the original plaintext is zero-length empty data. Then, you will have
3227 /// to check whether or not it failed by using the method
3228 /// `is_successful()` or `is_failed()`.
3229 /// - If `length_in_bytes` is greater than `size_of::<T>()` (which means
3230 /// that the original plaintext is surely not empty data) and it returns
3231 /// `zero`, you can interpret it that this method surely failed in
3232 /// decryption.
3233 ///
3234 /// # Features
3235 /// - You are not encouraged to use this method in pure Rust programming.
3236 /// Instead, use other safer methods such as decrypt_*_into_string().
3237 /// - This method is useful to use in hybrid programming with C/C++.
3238 /// - `length_in_bytes` cannot be other than any multiple of `size_of::<T>()`.
3239 /// - The padding bits are composed of the byte `0b_1000_0000` that
3240 /// indicates the delimiter one bit `1` followed by seven bits `0`s and
3241 /// all padding bits `0`s according to ISO 7816-4.
3242 /// - For more information about the padding bits according to ISO 7816-4,
3243 /// Read [here](https://en.wikipedia.org/wiki/Padding_(cryptography)#ISO/IEC_7816-4).
3244 /// - You don't have to worry about whether or not the size of the memory
3245 /// area where the ciphertext will be stored is enough.
3246 /// - This method assumes that the original plaintext is a string
3247 /// in the format of UTF-8.
3248 ///
3249 /// # For Rijndael or AES, and its variants
3250 /// ## Example 1 for AES-128
3251 /// ```
3252 /// use std::io::Write;
3253 /// use std::fmt::Write as _;
3254 /// use cryptocol::symmetric::{ AES_128, CBC_ISO };
3255 ///
3256 /// let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
3257 /// println!("K =\t{:#016X}", key);
3258 /// let mut a_aes = AES_128::new_with_key_u128(key);
3259 /// let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3260 /// println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3261 ///
3262 /// let message = "In the beginning God created the heavens and the earth.";
3263 /// println!("M =\t{}", message);
3264 /// let mut cipher = [0_u8; 64];
3265 /// a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
3266 /// print!("C =\t");
3267 /// for c in cipher.clone()
3268 /// { print!("{:02X} ", c); }
3269 /// println!();
3270 /// let mut txt = String::new();
3271 /// for c in cipher.clone()
3272 /// { write!(txt, "{:02X} ", c); }
3273 /// assert_eq!(txt, "C9 1C 27 CE 83 92 A1 CF 7D A4 64 35 16 48 01 72 CC E3 6D CD BB 19 FC D0 80 22 09 9F 23 32 73 27 58 37 F9 9B 3C 44 7B 03 B3 80 7E 99 DF 97 4E E9 EC D3 12 F1 1A 78 77 A6 A5 CB 73 AB 65 22 5B 7D ");
3274 /// println!();
3275 ///
3276 /// let mut converted= String::new();
3277 /// a_aes.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut converted);
3278 /// println!("B =\t{}", converted);
3279 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3280 /// assert_eq!(converted, message);
3281 /// ```
3282 ///
3283 /// ## For more examples,
3284 /// click [here](./documentation/rijndael_cbc_iso/struct.Rijndael_Generic.html#method.decrypt_into_string)
3285 ///
3286 /// # For DES and its variants
3287 /// ## Example 1 for Normal case
3288 /// ```
3289 /// use std::io::Write;
3290 /// use std::fmt::Write as _;
3291 /// use cryptocol::symmetric::{ DES, CBC_ISO };
3292 ///
3293 /// let key = 0x_1234567890ABCDEF_u64;
3294 /// println!("K =\t{:#016X}", key);
3295 /// let mut a_des = DES::new_with_key_u64(key);
3296 ///
3297 /// let message = "In the beginning God created the heavens and the earth.";
3298 /// println!("M =\t{}", message);
3299 /// let iv = 0x_FEDCBA0987654321_u64;
3300 /// println!("IV = {}", iv);
3301 /// let mut cipher = Vec::<u8>::new();
3302 /// a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3303 /// print!("C (16 rounds) =\t");
3304 /// for c in cipher.clone()
3305 /// { print!("{:02X} ", c); }
3306 /// println!();
3307 /// let mut txt = String::new();
3308 /// for c in cipher.clone()
3309 /// { write!(txt, "{:02X} ", c); }
3310 /// assert_eq!(txt, "4B B5 ED DC A0 58 7E 6D 6C 3B A2 00 38 C3 D4 29 42 B1 CF 0D E9 FA EA 11 11 6B C8 30 73 39 DD B7 3F 96 9B A3 76 05 34 7E 64 2F D4 CC B2 68 33 64 11 78 69 FB 0B 32 CF 92 ");
3311 ///
3312 /// let mut recovered = String::new();
3313 /// a_des.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3314 /// println!("B (16 rounds) =\t{}", recovered);
3315 /// assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
3316 /// assert_eq!(recovered, message);
3317 /// ```
3318 ///
3319 /// ## For more examples,
3320 /// click [here](./documentation/des_cbc_iso/struct.DES_Generic.html#method.decrypt_into_string)
3321 ///
3322 /// # For BigCryptor128
3323 /// ## Example 1 for TAES case
3324 /// ```
3325 /// use std::io::Write;
3326 /// use std::fmt::Write as _;
3327 /// use cryptocol::symmetric::{ BigCryptor128, AES_128, CBC_ISO };
3328 ///
3329 /// let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
3330 /// + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
3331 /// + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
3332 /// let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
3333 /// println!("IV = {:#034X}", iv);
3334 /// let message = "In the beginning God created the heavens and the earth.";
3335 /// println!("M =\t{}", message);
3336 /// let mut cipher = Vec::<u8>::new();
3337 /// taes.encrypt_str_into_vec(iv, &message, &mut cipher);
3338 /// print!("C =\t");
3339 /// for c in cipher.clone()
3340 /// { print!("{:02X} ", c); }
3341 /// println!();
3342 /// let mut txt = String::new();
3343 /// for c in cipher.clone()
3344 /// { write!(txt, "{:02X} ", c); }
3345 /// assert_eq!(txt, "87 B7 B0 67 C9 41 CF C6 CF B8 69 0D 39 D3 9B EC 0C 18 28 9A FA 51 7D 2A E4 D1 0C 2F 2B A0 0E E3 D1 04 65 E4 B6 66 2B 23 04 42 66 31 7A C5 76 E0 73 1D B1 F8 BB 75 C5 FB C2 1B 73 26 67 57 02 A0 ");
3346 ///
3347 /// let mut recovered = String::new();
3348 /// taes.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3349 /// println!("B =\t{}", recovered);
3350 /// assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
3351 /// assert_eq!(recovered, message);
3352 /// ```
3353 ///
3354 /// ## For more examples,
3355 /// click [here](./documentation/big_cryptor128_cbc_iso/struct.BigCryptor128.html#method.decrypt_into_string)
3356 ///
3357 /// # For BigCryptor64
3358 /// ## Example 1 for TDES case
3359 /// ```
3360 /// use std::io::Write;
3361 /// use std::fmt::Write as _;
3362 /// use cryptocol::symmetric::{ BigCryptor64, DES, CBC_ISO };
3363 ///
3364 /// let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
3365 /// + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
3366 /// + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
3367 /// let iv = 0x_FEDCBA0987654321_u64;
3368 /// println!("IV = {:#018X}", iv);
3369 /// let message = "In the beginning God created the heavens and the earth.";
3370 /// println!("M =\t{}", message);
3371 /// let mut cipher = Vec::<u8>::new();
3372 /// tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
3373 /// print!("C =\t");
3374 /// for c in cipher.clone()
3375 /// { print!("{:02X} ", c); }
3376 /// println!();
3377 /// let mut txt = String::new();
3378 /// for c in cipher.clone()
3379 /// { write!(txt, "{:02X} ", c); }
3380 /// assert_eq!(txt, "01 A5 7E BC ED 83 28 FB 7A 06 61 60 7D 8B BA AF 5C A7 76 0B FE 94 1C C4 C2 BC DD 15 98 A9 98 6C 85 18 92 6B 00 72 FB 72 DB 9D 46 BD B1 3C 56 C0 ED 3C 12 45 CA 1D 7E A3 ");
3381 ///
3382 /// let mut recovered = String::new();
3383 /// tdes.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3384 /// println!("B =\t{}", recovered);
3385 /// assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
3386 /// assert_eq!(recovered, message);
3387 /// ```
3388 ///
3389 /// ## For more examples,
3390 /// click [here](./documentation/big_cryptor64_cbc_iso/struct.BigCryptor64.html#method.decrypt_into_string)
3391 #[inline]
3392 fn decrypt_into_string(&mut self, iv: T, cipher: *const u8, length_in_bytes: u64, message: &mut String) -> u64
3393 {
3394 self.decrypt_into_vec(iv, cipher, length_in_bytes, unsafe { message.as_mut_vec() })
3395 }
3396
3397 // fn decrypt_vec<U>(&mut self, iv: T, cipher: &Vec<U>, message: *mut u8) -> u64
3398 /// Decrypts the data stored in a `Vec<U>` object with the padding defined
3399 /// according to ISO 7816-4 in CBC (Cipher-Block Chaining) mode.
3400 ///
3401 /// # Arguments
3402 /// - `iv` is an initialization vector for CBC mode.
3403 /// - `cipher` is an immutable reference to `Vec<U>` object, and
3404 /// is the place where the plaintext to be decrypted is stored.
3405 /// - `message` is a mutable pointer to `u8` which is `*mut u8`, and
3406 /// is the place where the decrypted data will be stored.
3407 ///
3408 /// # Output
3409 /// - This method returns the size of plaintext in bytes.
3410 /// - If this method failed in decryption, it always returns `zero`.
3411 /// - Even if this method succeeded in decryption, it returns `zero` when
3412 /// the original plaintext is zero-length empty data. Then, you will have
3413 /// to check whether or not it failed by using the method
3414 /// `is_successful()` or `is_failed()`.
3415 /// - If `size_of::<U>()` * `cipher.len()` is greater than `size_of::<T>()`
3416 /// (which means that the original plaintext is surely not empty data)
3417 /// and it returns `zero`, you can interpret it that this method surely
3418 /// failed in decryption.
3419 ///
3420 /// # Features
3421 /// - You are not encouraged to use this method in pure Rust programming.
3422 /// Instead, use other safer methods such as decrypt_vec_into_*().
3423 /// - This method is useful to use in hybrid programming with C/C++.
3424 /// - `size_of::<U>()` * `cipher.len()` cannot be other than any multiple
3425 /// of `size_of::<T>()`.
3426 /// - The size of the memory area which starts at `message` is assumed to
3427 /// be enough to store the plaintext. So, it is responsible for you to
3428 /// prepare the `message` area big enough!
3429 /// - The size of the area for plaintext does not have to be prepared more
3430 /// than `size_of::<U>()` * `cipher.len()` - `1`.
3431 /// - If the size of the area for plaintext is prepared more than
3432 /// `size_of::<U>()` * `cipher.len()` - `1`, the rest of the area will be
3433 /// filled with `0`s.
3434 /// - The padding bits are composed of the byte `0b_1000_0000` that
3435 /// indicates the delimiter one bit `1` followed by seven bits `0`s and
3436 /// all padding bits `0`s according to ISO 7816-4.
3437 /// - For more information about the padding bits according to ISO 7816-4,
3438 /// Read [here](https://en.wikipedia.org/wiki/Padding_(cryptography)#ISO/IEC_7816-4).
3439 ///
3440 /// # For Rijndael or AES, and its variants
3441 /// ## Example 1 for AES-128
3442 /// ```
3443 /// use std::io::Write;
3444 /// use std::fmt::Write as _;
3445 /// use cryptocol::symmetric::{ AES_128, CBC_ISO };
3446 ///
3447 /// let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
3448 /// println!("K =\t{:#016X}", key);
3449 /// let mut a_aes = AES_128::new_with_key_u128(key);
3450 /// let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3451 /// println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3452 ///
3453 /// let message = "In the beginning God created the heavens and the earth.";
3454 /// println!("M =\t{}", message);
3455 /// let mut cipher = Vec::<u8>::new();
3456 /// a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3457 /// print!("C =\t");
3458 /// for c in cipher.clone()
3459 /// { print!("{:02X} ", c); }
3460 /// println!();
3461 /// let mut txt = String::new();
3462 /// for c in cipher.clone()
3463 /// { write!(txt, "{:02X} ", c); }
3464 /// assert_eq!(txt, "C9 1C 27 CE 83 92 A1 CF 7D A4 64 35 16 48 01 72 CC E3 6D CD BB 19 FC D0 80 22 09 9F 23 32 73 27 58 37 F9 9B 3C 44 7B 03 B3 80 7E 99 DF 97 4E E9 EC D3 12 F1 1A 78 77 A6 A5 CB 73 AB 65 22 5B 7D ");
3465 ///
3466 /// let mut recovered = vec![0; 55];
3467 /// a_aes.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
3468 /// print!("Ba =\t");
3469 /// for b in recovered.clone()
3470 /// { print!("{:02X} ", b); }
3471 /// println!();
3472 /// let mut txt = String::new();
3473 /// for c in recovered.clone()
3474 /// { write!(txt, "{:02X} ", c); }
3475 /// 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 ");
3476 ///
3477 /// let mut converted = String::new();
3478 /// unsafe { converted.as_mut_vec() }.append(&mut recovered);
3479 ///
3480 /// println!("Bb =\t{}", converted);
3481 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3482 /// assert_eq!(converted, message);
3483 /// ```
3484 ///
3485 /// ## For more examples,
3486 /// click [here](./documentation/rijndael_cbc_iso/struct.Rijndael_Generic.html#method.decrypt_vec)
3487 ///
3488 /// # For DES and its variants
3489 /// ## Example 1 for Normal case
3490 /// ```
3491 /// use std::io::Write;
3492 /// use std::fmt::Write as _;
3493 /// use cryptocol::symmetric::{ DES, CBC_ISO };
3494 ///
3495 /// let key = 0x_1234567890ABCDEF_u64;
3496 /// println!("K =\t{:#016X}", key);
3497 /// let mut a_des = DES::new_with_key_u64(key);
3498 ///
3499 /// let message = "In the beginning God created the heavens and the earth.";
3500 /// println!("M =\t{}", message);
3501 /// let iv = 0x_FEDCBA0987654321_u64;
3502 /// println!("IV = {}", iv);
3503 /// let mut cipher = Vec::<u8>::new();
3504 /// a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3505 /// print!("C (16 rounds) =\t");
3506 /// for c in cipher.clone()
3507 /// { print!("{:02X} ", c); }
3508 /// println!();
3509 /// let mut txt = String::new();
3510 /// for c in cipher.clone()
3511 /// { write!(txt, "{:02X} ", c); }
3512 /// assert_eq!(txt, "4B B5 ED DC A0 58 7E 6D 6C 3B A2 00 38 C3 D4 29 42 B1 CF 0D E9 FA EA 11 11 6B C8 30 73 39 DD B7 3F 96 9B A3 76 05 34 7E 64 2F D4 CC B2 68 33 64 11 78 69 FB 0B 32 CF 92 ");
3513 ///
3514 /// let mut recovered = vec![0; 55];
3515 /// a_des.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
3516 /// print!("Ba (16 rounds) =\t");
3517 /// for b in recovered.clone()
3518 /// { print!("{:02X} ", b); }
3519 /// println!();
3520 /// let mut txt = String::new();
3521 /// for c in recovered.clone()
3522 /// { write!(txt, "{:02X} ", c); }
3523 /// 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 ");
3524 ///
3525 /// let mut converted = String::new();
3526 /// unsafe { converted.as_mut_vec() }.append(&mut recovered);
3527 ///
3528 /// println!("Bb (16 rounds) =\t{}", converted);
3529 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3530 /// assert_eq!(converted, message);
3531 /// ```
3532 ///
3533 /// ## For more examples,
3534 /// click [here](./documentation/des_cbc_iso/struct.DES_Generic.html#method.decrypt_vec)
3535 ///
3536 /// # For BigCryptor128
3537 /// ## Example 1 for TAES case
3538 /// ```
3539 /// use std::io::Write;
3540 /// use std::fmt::Write as _;
3541 /// use cryptocol::symmetric::{ BigCryptor128, AES_128, CBC_ISO };
3542 ///
3543 /// let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
3544 /// + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
3545 /// + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
3546 /// let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
3547 /// println!("IV = {:#034X}", iv);
3548 /// let message = "In the beginning God created the heavens and the earth.";
3549 /// println!("M =\t{}", message);
3550 /// let mut cipher = Vec::<u8>::new();
3551 /// taes.encrypt_str_into_vec(iv, &message, &mut cipher);
3552 /// print!("C =\t");
3553 /// for c in cipher.clone()
3554 /// { print!("{:02X} ", c); }
3555 /// println!();
3556 /// let mut txt = String::new();
3557 /// for c in cipher.clone()
3558 /// { write!(txt, "{:02X} ", c); }
3559 /// assert_eq!(txt, "87 B7 B0 67 C9 41 CF C6 CF B8 69 0D 39 D3 9B EC 0C 18 28 9A FA 51 7D 2A E4 D1 0C 2F 2B A0 0E E3 D1 04 65 E4 B6 66 2B 23 04 42 66 31 7A C5 76 E0 73 1D B1 F8 BB 75 C5 FB C2 1B 73 26 67 57 02 A0 ");
3560 ///
3561 /// let mut recovered = vec![0; 55];
3562 /// taes.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
3563 /// print!("Ba =\t");
3564 /// for b in recovered.clone()
3565 /// { print!("{:02X} ", b); }
3566 /// println!();
3567 /// let mut txt = String::new();
3568 /// for c in recovered.clone()
3569 /// { write!(txt, "{:02X} ", c); }
3570 /// 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 ");
3571 ///
3572 /// let mut converted = String::new();
3573 /// unsafe { converted.as_mut_vec() }.append(&mut recovered);
3574 ///
3575 /// println!("Bb =\t{}", converted);
3576 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3577 /// assert_eq!(converted, message);
3578 /// ```
3579 ///
3580 /// ## For more examples,
3581 /// click [here](./documentation/big_cryptor128_cbc_iso/struct.BigCryptor128.html#method.decrypt_vec)
3582 ///
3583 /// # For BigCryptor64
3584 /// ## Example 1 for TDES case
3585 /// ```
3586 /// use std::io::Write;
3587 /// use std::fmt::Write as _;
3588 /// use cryptocol::symmetric::{ BigCryptor64, DES, CBC_ISO };
3589 ///
3590 /// let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
3591 /// + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
3592 /// + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
3593 /// let iv = 0x_FEDCBA0987654321_u64;
3594 /// println!("IV = {:#018X}", iv);
3595 /// let message = "In the beginning God created the heavens and the earth.";
3596 /// println!("M =\t{}", message);
3597 /// let mut cipher = Vec::<u8>::new();
3598 /// tdes.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, "01 A5 7E BC ED 83 28 FB 7A 06 61 60 7D 8B BA AF 5C A7 76 0B FE 94 1C C4 C2 BC DD 15 98 A9 98 6C 85 18 92 6B 00 72 FB 72 DB 9D 46 BD B1 3C 56 C0 ED 3C 12 45 CA 1D 7E A3 ");
3607 ///
3608 /// let mut recovered = vec![0; 55];
3609 /// tdes.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
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, "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 ");
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, "In the beginning God created the heavens and the earth.");
3624 /// assert_eq!(converted, message);
3625 /// ```
3626 ///
3627 /// ## For more examples,
3628 /// click [here](./documentation/big_cryptor64_cbc_iso/struct.BigCryptor64.html#method.decrypt_vec)
3629 #[inline]
3630 fn decrypt_vec<U>(&mut self, iv: T, cipher: &Vec<U>, message: *mut u8) -> u64
3631 where U: SmallUInt + Copy + Clone
3632 {
3633 self.decrypt(iv, cipher.as_ptr() as *const u8, (cipher.len() as u32 * U::size_in_bytes()) as u64, message)
3634 }
3635
3636 // fn decrypt_vec_into_vec<U, V>(&mut self, iv: T, cipher: &Vec<U>, message: &mut Vec<V>) -> u64
3637 /// Decrypts the data stored in a `Vec<U>` object with the padding defined
3638 /// according to ISO 7816-4 in CBC (Cipher-Block Chaining) mode, and
3639 /// stores the decrypted data in `Vec<V>`.
3640 ///
3641 /// # Arguments
3642 /// - `iv` is an initialization vector for CBC mode.
3643 /// - `cipher` is an immutable reference to `Vec<U>` object, and
3644 /// is the place where the ciphertext to be decrypted is stored.
3645 /// - `message` is a mutable reference to `Vec<U>` object, and
3646 /// is the place where the decrypted data will be stored.
3647 ///
3648 /// # Output
3649 /// - This method returns the size of plaintext in bytes.
3650 /// - If this method failed in decryption, it always returns `zero`.
3651 /// - Even if this method succeeded in decryption, it returns `zero` when
3652 /// the original plaintext is zero-length empty data. Then, you will have
3653 /// to check whether or not it failed by using the method
3654 /// `is_successful()` or `is_failed()`.
3655 /// - If `size_of::<U>()` * `cipher.len()` is greater than `size_of::<T>()`
3656 /// (which means that the original plaintext is surely not empty data)
3657 /// and it returns `zero`, you can interpret it that this method surely
3658 /// failed in decryption.
3659 ///
3660 /// # Features
3661 /// - The padding bits are composed of the byte `0b_1000_0000` that
3662 /// indicates the delimiter one bit `1` followed by seven bits `0`s and
3663 /// all padding bits `0`s according to ISO 7816-4.
3664 /// - For more information about the padding bits according to ISO 7816-4,
3665 /// Read [here](https://en.wikipedia.org/wiki/Padding_(cryptography)#ISO/IEC_7816-4).
3666 /// - You don't have to worry about whether or not the size of the memory
3667 /// area where the ciphertext will be stored is enough.
3668 ///
3669 /// # For Rijndael or AES, and its variants
3670 /// ## Example 1 for AES-128
3671 /// ```
3672 /// use std::io::Write;
3673 /// use std::fmt::Write as _;
3674 /// use cryptocol::symmetric::{ AES_128, CBC_ISO };
3675 ///
3676 /// let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
3677 /// println!("K =\t{:#016X}", key);
3678 /// let mut a_aes = AES_128::new_with_key_u128(key);
3679 /// let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3680 /// println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3681 ///
3682 /// let message = "In the beginning God created the heavens and the earth.";
3683 /// println!("M =\t{}", message);
3684 /// let mut cipher = Vec::<u8>::new();
3685 /// a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3686 /// print!("C =\t");
3687 /// for c in cipher.clone()
3688 /// { print!("{:02X} ", c); }
3689 /// println!();
3690 /// let mut txt = String::new();
3691 /// for c in cipher.clone()
3692 /// { write!(txt, "{:02X} ", c); }
3693 /// assert_eq!(txt, "C9 1C 27 CE 83 92 A1 CF 7D A4 64 35 16 48 01 72 CC E3 6D CD BB 19 FC D0 80 22 09 9F 23 32 73 27 58 37 F9 9B 3C 44 7B 03 B3 80 7E 99 DF 97 4E E9 EC D3 12 F1 1A 78 77 A6 A5 CB 73 AB 65 22 5B 7D ");
3694 ///
3695 /// let mut recovered = Vec::<u8>::new();
3696 /// a_aes.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
3697 /// print!("Ba =\t");
3698 /// for b in recovered.clone()
3699 /// { print!("{:02X} ", b); }
3700 /// println!();
3701 /// let mut txt = String::new();
3702 /// for c in recovered.clone()
3703 /// { write!(txt, "{:02X} ", c); }
3704 /// assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
3705 ///
3706 /// let mut converted = String::new();
3707 /// unsafe { converted.as_mut_vec() }.append(&mut recovered);
3708 ///
3709 /// println!("Bb =\t{}", converted);
3710 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3711 /// assert_eq!(converted, message);
3712 /// ```
3713 ///
3714 /// ## For more examples,
3715 /// click [here](./documentation/rijndael_cbc_iso/struct.Rijndael_Generic.html#method.decrypt_vec_into_vec)
3716 ///
3717 /// # For DES and its variants
3718 /// ## Example 1 for Normal case
3719 /// ```
3720 /// use std::io::Write;
3721 /// use std::fmt::Write as _;
3722 /// use cryptocol::symmetric::{ DES, CBC_ISO };
3723 ///
3724 /// let key = 0x_1234567890ABCDEF_u64;
3725 /// println!("K =\t{:#016X}", key);
3726 /// let mut a_des = DES::new_with_key_u64(key);
3727 ///
3728 /// let message = "In the beginning God created the heavens and the earth.";
3729 /// println!("M =\t{}", message);
3730 /// let iv = 0x_FEDCBA0987654321_u64;
3731 /// println!("IV = {}", iv);
3732 /// let mut cipher = Vec::<u8>::new();
3733 /// a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3734 /// print!("C (16 rounds) =\t");
3735 /// for c in cipher.clone()
3736 /// { print!("{:02X} ", c); }
3737 /// println!();
3738 /// let mut txt = String::new();
3739 /// for c in cipher.clone()
3740 /// { write!(txt, "{:02X} ", c); }
3741 /// assert_eq!(txt, "4B B5 ED DC A0 58 7E 6D 6C 3B A2 00 38 C3 D4 29 42 B1 CF 0D E9 FA EA 11 11 6B C8 30 73 39 DD B7 3F 96 9B A3 76 05 34 7E 64 2F D4 CC B2 68 33 64 11 78 69 FB 0B 32 CF 92 ");
3742 ///
3743 /// let mut recovered = Vec::<u8>::new();
3744 /// a_des.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
3745 /// print!("Ba (16 rounds) =\t");
3746 /// for b in recovered.clone()
3747 /// { print!("{:02X} ", b); }
3748 /// println!();
3749 /// let mut txt = String::new();
3750 /// for c in recovered.clone()
3751 /// { write!(txt, "{:02X} ", c); }
3752 /// 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 ");
3753 ///
3754 /// let mut converted = String::new();
3755 /// unsafe { converted.as_mut_vec() }.append(&mut recovered);
3756 ///
3757 /// println!("Bb (16 rounds) =\t{}", converted);
3758 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3759 /// assert_eq!(converted, message);
3760 /// ```
3761 ///
3762 /// ## For more examples,
3763 /// click [here](./documentation/des_cbc_iso/struct.DES_Generic.html#method.decrypt_vec_into_vec)
3764 ///
3765 /// # For BigCryptor128
3766 /// ## Example 1 for TAES case
3767 /// ```
3768 /// use std::io::Write;
3769 /// use std::fmt::Write as _;
3770 /// use cryptocol::symmetric::{ BigCryptor128, AES_128, CBC_ISO };
3771 ///
3772 /// let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
3773 /// + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
3774 /// + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
3775 /// let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
3776 /// println!("IV = {:#034X}", iv);
3777 /// let message = "In the beginning God created the heavens and the earth.";
3778 /// println!("M =\t{}", message);
3779 /// let mut cipher = Vec::<u8>::new();
3780 /// taes.encrypt_str_into_vec(iv, &message, &mut cipher);
3781 /// print!("C =\t");
3782 /// for c in cipher.clone()
3783 /// { print!("{:02X} ", c); }
3784 /// println!();
3785 /// let mut txt = String::new();
3786 /// for c in cipher.clone()
3787 /// { write!(txt, "{:02X} ", c); }
3788 /// assert_eq!(txt, "87 B7 B0 67 C9 41 CF C6 CF B8 69 0D 39 D3 9B EC 0C 18 28 9A FA 51 7D 2A E4 D1 0C 2F 2B A0 0E E3 D1 04 65 E4 B6 66 2B 23 04 42 66 31 7A C5 76 E0 73 1D B1 F8 BB 75 C5 FB C2 1B 73 26 67 57 02 A0 ");
3789 ///
3790 /// let mut recovered = Vec::<u8>::new();
3791 /// taes.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
3792 /// print!("Ba =\t");
3793 /// for b in recovered.clone()
3794 /// { print!("{:02X} ", b); }
3795 /// println!();
3796 /// let mut txt = String::new();
3797 /// for c in recovered.clone()
3798 /// { write!(txt, "{:02X} ", c); }
3799 /// 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 ");
3800 ///
3801 /// let mut converted = String::new();
3802 /// unsafe { converted.as_mut_vec() }.append(&mut recovered);
3803 ///
3804 /// println!("Bb =\t{}", converted);
3805 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3806 /// assert_eq!(converted, message);
3807 /// ```
3808 ///
3809 /// ## For more examples,
3810 /// click [here](./documentation/big_cryptor128_cbc_iso/struct.BigCryptor128.html#method.decrypt_vec_into_vec)
3811 ///
3812 /// # For BigCryptor64
3813 /// ## Example 1 for TDES case
3814 /// ```
3815 /// use std::io::Write;
3816 /// use std::fmt::Write as _;
3817 /// use cryptocol::symmetric::{ BigCryptor64, DES, CBC_ISO };
3818 ///
3819 /// let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
3820 /// + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
3821 /// + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
3822 /// let iv = 0x_FEDCBA0987654321_u64;
3823 /// println!("IV = {:#018X}", iv);
3824 /// let message = "In the beginning God created the heavens and the earth.";
3825 /// println!("M =\t{}", message);
3826 /// let mut cipher = Vec::<u8>::new();
3827 /// tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
3828 /// print!("C =\t");
3829 /// for c in cipher.clone()
3830 /// { print!("{:02X} ", c); }
3831 /// println!();
3832 /// let mut txt = String::new();
3833 /// for c in cipher.clone()
3834 /// { write!(txt, "{:02X} ", c); }
3835 /// assert_eq!(txt, "01 A5 7E BC ED 83 28 FB 7A 06 61 60 7D 8B BA AF 5C A7 76 0B FE 94 1C C4 C2 BC DD 15 98 A9 98 6C 85 18 92 6B 00 72 FB 72 DB 9D 46 BD B1 3C 56 C0 ED 3C 12 45 CA 1D 7E A3 ");
3836 ///
3837 /// let mut recovered = Vec::<u8>::new();
3838 /// tdes.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
3839 /// print!("Ba =\t");
3840 /// for b in recovered.clone()
3841 /// { print!("{:02X} ", b); }
3842 /// println!();
3843 /// let mut txt = String::new();
3844 /// for c in recovered.clone()
3845 /// { write!(txt, "{:02X} ", c); }
3846 /// 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 ");
3847 ///
3848 /// let mut converted = String::new();
3849 /// unsafe { converted.as_mut_vec() }.append(&mut recovered);
3850 ///
3851 /// println!("Bb =\t{}", converted);
3852 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3853 /// assert_eq!(converted, message);
3854 /// ```
3855 ///
3856 /// ## For more examples,
3857 /// click [here](./documentation/big_cryptor64_cbc_iso/struct.BigCryptor64.html#method.decrypt_vec_into_vec)
3858 #[inline]
3859 fn decrypt_vec_into_vec<U, V>(&mut self, iv: T, cipher: &Vec<U>, message: &mut Vec<V>) -> u64
3860 where U: SmallUInt + Copy + Clone, V: SmallUInt + Copy + Clone
3861 {
3862 self.decrypt_into_vec(iv, cipher.as_ptr() as *const u8, (cipher.len() as u32 * U::size_in_bytes()) as u64, message)
3863 }
3864
3865 // fn decrypt_vec_into_array<U, V, const N: usize>(&mut self, iv: T, cipher: &Vec<U>, message: &mut [V; N]) -> u64
3866 /// Decrypts the data stored in a `Vec<U>` object with the padding defined
3867 /// according to ISO 7816-4 in CBC (Cipher-Block Chaining) mode, and
3868 /// stores the decrypted data in array `[V; N]`.
3869 ///
3870 /// # Arguments
3871 /// - `iv` is an initialization vector for CBC mode.
3872 /// - `cipher` is an immutable reference to `Vec<U>` object, and
3873 /// is the place where the ciphertext to be decrypted is stored.
3874 /// - `message` is a mutable reference to an array `[U; N]` object, and
3875 /// is the place where the decrypted data will be stored.
3876 ///
3877 /// # Output
3878 /// - This method returns the size of plaintext in bytes.
3879 /// - If this method failed in decryption, it always returns `zero`.
3880 /// - Even if this method succeeded in decryption, it returns `zero` when
3881 /// the original plaintext is zero-length empty data. Then, you will have
3882 /// to check whether or not it failed by using the method
3883 /// `is_successful()` or `is_failed()`.
3884 /// - If `size_of::<U>()` * `cipher.len()` is greater than `size_of::<T>()`
3885 /// (which means that the original plaintext is surely not empty data)
3886 /// and it returns `zero`, you can interpret it that this method surely
3887 /// failed in decryption.
3888 ///
3889 /// # Features
3890 /// - If `size_of::<V>()` * `N` is less than
3891 /// `size_of::<U>()` * `cipher.len()` - `1`, this method does not perform
3892 /// decryption but returns `zero`.
3893 /// - If `size_of::<V>()` * `N` is equal to or greater than
3894 /// `size_of::<U>()` * `cipher.len()` - `1`, this method performs
3895 /// decryption, fills the array `message` with the decrypted data, and then
3896 /// fills the rest of the elements of the array `message` with zeros, and
3897 /// returns the size of the plaintext.
3898 /// - It is responsible for you to prepare the `message` area big enough!
3899 /// - The size of the area for plaintext does not have to be prepared more
3900 /// than `size_of::<U>()` * `cipher.len()` - `1`.
3901 /// - The padding bits are composed of the byte `0b_1000_0000` that
3902 /// indicates the delimiter one bit `1` followed by seven bits `0`s and
3903 /// all padding bits `0`s according to ISO 7816-4.
3904 /// - For more information about the padding bits according to ISO 7816-4,
3905 /// Read [here](https://en.wikipedia.org/wiki/Padding_(cryptography)#ISO/IEC_7816-4).
3906 ///
3907 /// # For Rijndael or AES, and its variants
3908 /// ## Example 1 for AES-128
3909 /// ```
3910 /// use std::io::Write;
3911 /// use std::fmt::Write as _;
3912 /// use cryptocol::symmetric::{ AES_128, CBC_ISO };
3913 ///
3914 /// let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
3915 /// println!("K =\t{:#016X}", key);
3916 /// let mut a_aes = AES_128::new_with_key_u128(key);
3917 /// let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3918 /// println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3919 ///
3920 /// let message = "In the beginning God created the heavens and the earth.";
3921 /// println!("M =\t{}", message);
3922 /// let mut cipher = Vec::<u8>::new();
3923 /// a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3924 /// print!("C =\t");
3925 /// for c in cipher.clone()
3926 /// { print!("{:02X} ", c); }
3927 /// println!();
3928 /// let mut txt = String::new();
3929 /// for c in cipher.clone()
3930 /// { write!(txt, "{:02X} ", c); }
3931 /// assert_eq!(txt, "C9 1C 27 CE 83 92 A1 CF 7D A4 64 35 16 48 01 72 CC E3 6D CD BB 19 FC D0 80 22 09 9F 23 32 73 27 58 37 F9 9B 3C 44 7B 03 B3 80 7E 99 DF 97 4E E9 EC D3 12 F1 1A 78 77 A6 A5 CB 73 AB 65 22 5B 7D ");
3932 ///
3933 /// let mut recovered = [0; 64];
3934 /// let len = a_aes.decrypt_vec_into_array(iv, &cipher, &mut recovered);
3935 /// print!("Ba =\t");
3936 /// for b in recovered.clone()
3937 /// { print!("{:02X} ", b); }
3938 /// println!();
3939 /// let mut txt = String::new();
3940 /// for c in recovered.clone()
3941 /// { write!(txt, "{:02X} ", c); }
3942 /// 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 00 00 00 00 00 00 00 00 ");
3943 ///
3944 /// let mut converted = String::new();
3945 /// unsafe { converted.as_mut_vec() }.write(&recovered);
3946 /// unsafe { converted.as_mut_vec() }.truncate(len as usize);
3947 /// println!("Bb =\t{}", converted);
3948 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3949 /// assert_eq!(converted, message);
3950 /// ```
3951 ///
3952 /// ## For more examples,
3953 /// click [here](./documentation/rijndael_cbc_iso/struct.Rijndael_Generic.html#method.decrypt_vec_into_array)
3954 ///
3955 /// # For DES and its variants
3956 /// ## Example 1 for Normal case
3957 /// ```
3958 /// use std::io::Write;
3959 /// use std::fmt::Write as _;
3960 /// use cryptocol::symmetric::{ DES, CBC_ISO };
3961 ///
3962 /// let key = 0x_1234567890ABCDEF_u64;
3963 /// println!("K =\t{:#016X}", key);
3964 /// let mut a_des = DES::new_with_key_u64(key);
3965 ///
3966 /// let message = "In the beginning God created the heavens and the earth.";
3967 /// println!("M =\t{}", message);
3968 /// let iv = 0x_FEDCBA0987654321_u64;
3969 /// println!("IV = {}", iv);
3970 /// let mut cipher = Vec::<u8>::new();
3971 /// a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3972 /// print!("C (16 rounds) =\t");
3973 /// for c in cipher.clone()
3974 /// { print!("{:02X} ", c); }
3975 /// println!();
3976 /// let mut txt = String::new();
3977 /// for c in cipher.clone()
3978 /// { write!(txt, "{:02X} ", c); }
3979 /// assert_eq!(txt, "4B B5 ED DC A0 58 7E 6D 6C 3B A2 00 38 C3 D4 29 42 B1 CF 0D E9 FA EA 11 11 6B C8 30 73 39 DD B7 3F 96 9B A3 76 05 34 7E 64 2F D4 CC B2 68 33 64 11 78 69 FB 0B 32 CF 92 ");
3980 ///
3981 /// let mut recovered = [0u8; 56];
3982 /// let len = a_des.decrypt_vec_into_array(iv, &cipher, &mut recovered);
3983 /// print!("Ba (16 rounds) =\t");
3984 /// for b in recovered.clone()
3985 /// { print!("{:02X} ", b); }
3986 /// println!();
3987 /// let mut txt = String::new();
3988 /// for c in recovered.clone()
3989 /// { write!(txt, "{:02X} ", c); }
3990 /// 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 ");
3991 ///
3992 /// let mut converted = String::new();
3993 /// unsafe { converted.as_mut_vec() }.write(&recovered);
3994 /// unsafe { converted.as_mut_vec() }.truncate(len as usize);
3995 /// println!("Bb (16 rounds) =\t{}", converted);
3996 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3997 /// assert_eq!(converted, message);
3998 /// ```
3999 ///
4000 /// ## For more examples,
4001 /// click [here](./documentation/des_cbc_iso/struct.DES_Generic.html#method.decrypt_vec_into_array)
4002 ///
4003 /// # For BigCryptor128
4004 /// ## Example 1 for TAES case
4005 /// ```
4006 /// use std::io::Write;
4007 /// use std::fmt::Write as _;
4008 /// use cryptocol::symmetric::{ BigCryptor128, AES_128, CBC_ISO };
4009 ///
4010 /// let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
4011 /// + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
4012 /// + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
4013 /// let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
4014 /// println!("IV = {:#034X}", iv);
4015 /// let message = "In the beginning God created the heavens and the earth.";
4016 /// println!("M =\t{}", message);
4017 /// let mut cipher = Vec::<u8>::new();
4018 /// taes.encrypt_str_into_vec(iv, &message, &mut cipher);
4019 /// print!("C =\t");
4020 /// for c in cipher.clone()
4021 /// { print!("{:02X} ", c); }
4022 /// println!();
4023 /// let mut txt = String::new();
4024 /// for c in cipher.clone()
4025 /// { write!(txt, "{:02X} ", c); }
4026 /// assert_eq!(txt, "87 B7 B0 67 C9 41 CF C6 CF B8 69 0D 39 D3 9B EC 0C 18 28 9A FA 51 7D 2A E4 D1 0C 2F 2B A0 0E E3 D1 04 65 E4 B6 66 2B 23 04 42 66 31 7A C5 76 E0 73 1D B1 F8 BB 75 C5 FB C2 1B 73 26 67 57 02 A0 ");
4027 ///
4028 /// let mut recovered = [0u8; 56];
4029 /// let len = taes.decrypt_vec_into_array(iv, &cipher, &mut recovered);
4030 /// print!("Ba =\t");
4031 /// for b in recovered.clone()
4032 /// { print!("{:02X} ", b); }
4033 /// println!();
4034 /// let mut txt = String::new();
4035 /// for c in recovered.clone()
4036 /// { write!(txt, "{:02X} ", c); }
4037 /// 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 ");
4038 ///
4039 /// let mut converted = String::new();
4040 /// unsafe { converted.as_mut_vec() }.write(&recovered);
4041 /// unsafe { converted.as_mut_vec() }.truncate(len as usize);
4042 /// println!("Bb =\t{}", converted);
4043 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4044 /// ```
4045 ///
4046 /// ## For more examples,
4047 /// click [here](./documentation/big_cryptor128_cbc_iso/struct.BigCryptor128.html#method.decrypt_vec_into_array)
4048 ///
4049 /// # For BigCryptor64
4050 /// ## Example 1 for TDES case
4051 /// ```
4052 /// use std::io::Write;
4053 /// use std::fmt::Write as _;
4054 /// use cryptocol::symmetric::{ BigCryptor64, DES, CBC_ISO };
4055 ///
4056 /// let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
4057 /// + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
4058 /// + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
4059 /// let iv = 0x_FEDCBA0987654321_u64;
4060 /// println!("IV = {:#018X}", iv);
4061 /// let message = "In the beginning God created the heavens and the earth.";
4062 /// println!("M =\t{}", message);
4063 /// let mut cipher = Vec::<u8>::new();
4064 /// tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
4065 /// print!("C =\t");
4066 /// for c in cipher.clone()
4067 /// { print!("{:02X} ", c); }
4068 /// println!();
4069 /// let mut txt = String::new();
4070 /// for c in cipher.clone()
4071 /// { write!(txt, "{:02X} ", c); }
4072 /// assert_eq!(txt, "01 A5 7E BC ED 83 28 FB 7A 06 61 60 7D 8B BA AF 5C A7 76 0B FE 94 1C C4 C2 BC DD 15 98 A9 98 6C 85 18 92 6B 00 72 FB 72 DB 9D 46 BD B1 3C 56 C0 ED 3C 12 45 CA 1D 7E A3 ");
4073 ///
4074 /// let mut recovered = [0u8; 56];
4075 /// let len = tdes.decrypt_vec_into_array(iv, &cipher, &mut recovered);
4076 /// print!("Ba =\t");
4077 /// for b in recovered.clone()
4078 /// { print!("{:02X} ", b); }
4079 /// println!();
4080 /// let mut txt = String::new();
4081 /// for c in recovered.clone()
4082 /// { write!(txt, "{:02X} ", c); }
4083 /// assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 ");
4084 ///
4085 /// let mut converted = String::new();
4086 /// unsafe { converted.as_mut_vec() }.write(&recovered);
4087 /// unsafe { converted.as_mut_vec() }.truncate(len as usize);
4088 /// println!("Bb =\t{}", converted);
4089 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4090 /// ```
4091 ///
4092 /// ## For more examples,
4093 /// click [here](./documentation/big_cryptor64_cbc_iso/struct.BigCryptor64.html#method.decrypt_vec_into_array)
4094 #[inline]
4095 fn decrypt_vec_into_array<U, V, const N: usize>(&mut self, iv: T, cipher: &Vec<U>, message: &mut [V; N]) -> u64
4096 where U: SmallUInt + Copy + Clone, V: SmallUInt + Copy + Clone
4097 {
4098 self.decrypt_into_array(iv, cipher.as_ptr() as *const u8, (cipher.len() as u32 * U::size_in_bytes()) as u64, message)
4099 }
4100
4101 // fn decrypt_vec_into_string(&mut self, iv: T, cipher: &str, message: &mut String) -> u64
4102 /// Decrypts the data in `str` with the padding defined according to
4103 /// ISO 7816-4 in CBC (Cipher-Block Chaining) mode, and stores the
4104 /// decrypted data in `String`.
4105 ///
4106 /// # Arguments
4107 /// - `iv` is an initialization vector for CBC mode.
4108 /// - `cipher` is an immutable reference to `Vec<U>` object, and
4109 /// is the place where the ciphertext to be decrypted is stored.
4110 /// - `message` is a mutable reference to a `String` object, and
4111 /// is the place where the decrypted data will be stored.
4112 ///
4113 /// # Output
4114 /// - This method returns the size of plaintext in bytes.
4115 /// - If this method failed in decryption, it always returns `zero`.
4116 /// - Even if this method succeeded in decryption, it returns `zero` when
4117 /// the original plaintext is zero-length empty data. Then, you will have
4118 /// to check whether or not it failed by using the method
4119 /// `is_successful()` or `is_failed()`.
4120 /// - If `size_of::<U>()` * `cipher.len()` is greater than `size_of::<T>()`
4121 /// (which means that the original plaintext is surely not empty data)
4122 /// and it returns `zero`, you can interpret it that this method surely
4123 /// failed in decryption.
4124 ///
4125 /// # Features
4126 /// - The padding bits are composed of the byte `0b_1000_0000` that
4127 /// indicates the delimiter one bit `1` followed by seven bits `0`s and
4128 /// all padding bits `0`s according to ISO 7816-4.
4129 /// - For more information about the padding bits according to ISO 7816-4,
4130 /// Read [here](https://en.wikipedia.org/wiki/Padding_(cryptography)#ISO/IEC_7816-4).
4131 /// - You don't have to worry about whether or not the size of the memory
4132 /// area where the ciphertext will be stored is enough.
4133 /// - This method assumes that the original plaintext is a string
4134 /// in the format of UTF-8.
4135 ///
4136 /// # For Rijndael or AES, and its variants
4137 /// ## Example 1 for AES-128
4138 /// ```
4139 /// use std::io::Write;
4140 /// use std::fmt::Write as _;
4141 /// use cryptocol::symmetric::{ AES_128, CBC_ISO };
4142 ///
4143 /// let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
4144 /// println!("K =\t{:#016X}", key);
4145 /// let mut a_aes = AES_128::new_with_key_u128(key);
4146 /// let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
4147 /// println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
4148 ///
4149 /// let message = "In the beginning God created the heavens and the earth.";
4150 /// println!("M =\t{}", message);
4151 /// let mut cipher = Vec::<u8>::new();
4152 /// a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
4153 /// print!("C =\t");
4154 /// for c in cipher.clone()
4155 /// { print!("{:02X} ", c); }
4156 /// println!();
4157 /// let mut txt = String::new();
4158 /// for c in cipher.clone()
4159 /// { write!(txt, "{:02X} ", c); }
4160 /// assert_eq!(txt, "C9 1C 27 CE 83 92 A1 CF 7D A4 64 35 16 48 01 72 CC E3 6D CD BB 19 FC D0 80 22 09 9F 23 32 73 27 58 37 F9 9B 3C 44 7B 03 B3 80 7E 99 DF 97 4E E9 EC D3 12 F1 1A 78 77 A6 A5 CB 73 AB 65 22 5B 7D ");
4161 ///
4162 /// let mut converted= String::new();
4163 /// a_aes.decrypt_vec_into_string(iv, &cipher, &mut converted);
4164 /// println!("B =\t{}", converted);
4165 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4166 /// assert_eq!(converted, message);
4167 /// ```
4168 ///
4169 /// ## For more examples,
4170 /// click [here](./documentation/rijndael_cbc_iso/struct.Rijndael_Generic.html#method.decrypt_vec_into_string)
4171 ///
4172 /// # For DES and its variants
4173 /// ## Example 1 for Normal case
4174 /// ```
4175 /// use std::io::Write;
4176 /// use std::fmt::Write as _;
4177 /// use cryptocol::symmetric::{ DES, CBC_ISO };
4178 ///
4179 /// let key = 0x_1234567890ABCDEF_u64;
4180 /// println!("K =\t{:#016X}", key);
4181 /// let mut a_des = DES::new_with_key_u64(key);
4182 ///
4183 /// let message = "In the beginning God created the heavens and the earth.";
4184 /// println!("M =\t{}", message);
4185 /// let iv = 0x_FEDCBA0987654321_u64;
4186 /// println!("IV = {}", iv);
4187 /// let mut cipher = Vec::<u8>::new();
4188 /// a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4189 /// print!("C (16 rounds) =\t");
4190 /// for c in cipher.clone()
4191 /// { print!("{:02X} ", c); }
4192 /// println!();
4193 /// let mut txt = String::new();
4194 /// for c in cipher.clone()
4195 /// { write!(txt, "{:02X} ", c); }
4196 /// assert_eq!(txt, "4B B5 ED DC A0 58 7E 6D 6C 3B A2 00 38 C3 D4 29 42 B1 CF 0D E9 FA EA 11 11 6B C8 30 73 39 DD B7 3F 96 9B A3 76 05 34 7E 64 2F D4 CC B2 68 33 64 11 78 69 FB 0B 32 CF 92 ");
4197 ///
4198 /// let mut recovered = String::new();
4199 /// a_des.decrypt_vec_into_string(iv, &cipher, &mut recovered);
4200 /// println!("B (16 rounds) =\t{}", recovered);
4201 /// assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
4202 /// assert_eq!(recovered, message);
4203 /// ```
4204 ///
4205 /// ## For more examples,
4206 /// click [here](./documentation/des_cbc_iso/struct.DES_Generic.html#method.decrypt_vec_into_string)
4207 ///
4208 /// # For BigCryptor128
4209 /// ## Example 1 for TAES case
4210 /// ```
4211 /// use std::io::Write;
4212 /// use std::fmt::Write as _;
4213 /// use cryptocol::symmetric::{ BigCryptor128, AES_128, CBC_ISO };
4214 ///
4215 /// let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
4216 /// + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
4217 /// + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
4218 /// let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
4219 /// println!("IV = {:#034X}", iv);
4220 /// let message = "In the beginning God created the heavens and the earth.";
4221 /// println!("M =\t{}", message);
4222 /// let mut cipher = Vec::<u8>::new();
4223 /// taes.encrypt_str_into_vec(iv, &message, &mut cipher);
4224 /// print!("C =\t");
4225 /// for c in cipher.clone()
4226 /// { print!("{:02X} ", c); }
4227 /// println!();
4228 /// let mut txt = String::new();
4229 /// for c in cipher.clone()
4230 /// { write!(txt, "{:02X} ", c); }
4231 /// assert_eq!(txt, "87 B7 B0 67 C9 41 CF C6 CF B8 69 0D 39 D3 9B EC 0C 18 28 9A FA 51 7D 2A E4 D1 0C 2F 2B A0 0E E3 D1 04 65 E4 B6 66 2B 23 04 42 66 31 7A C5 76 E0 73 1D B1 F8 BB 75 C5 FB C2 1B 73 26 67 57 02 A0 ");
4232 ///
4233 /// let mut recovered = String::new();
4234 /// taes.decrypt_vec_into_string(iv, &cipher, &mut recovered);
4235 /// println!("B =\t{}", recovered);
4236 /// assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
4237 /// assert_eq!(recovered, message);
4238 /// ```
4239 ///
4240 /// ## For more examples,
4241 /// click [here](./documentation/big_cryptor128_cbc_iso/struct.BigCryptor128.html#method.decrypt_vec_into_string)
4242 ///
4243 /// # For BigCryptor64
4244 /// ## Example 1 for TDES case
4245 /// ```
4246 /// use std::io::Write;
4247 /// use std::fmt::Write as _;
4248 /// use cryptocol::symmetric::{ BigCryptor64, DES, CBC_ISO };
4249 ///
4250 /// let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
4251 /// + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
4252 /// + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
4253 /// let iv = 0x_FEDCBA0987654321_u64;
4254 /// println!("IV = {:#018X}", iv);
4255 /// let message = "In the beginning God created the heavens and the earth.";
4256 /// println!("M =\t{}", message);
4257 /// let mut cipher = Vec::<u8>::new();
4258 /// tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
4259 /// print!("C =\t");
4260 /// for c in cipher.clone()
4261 /// { print!("{:02X} ", c); }
4262 /// println!();
4263 /// let mut txt = String::new();
4264 /// for c in cipher.clone()
4265 /// { write!(txt, "{:02X} ", c); }
4266 /// assert_eq!(txt, "01 A5 7E BC ED 83 28 FB 7A 06 61 60 7D 8B BA AF 5C A7 76 0B FE 94 1C C4 C2 BC DD 15 98 A9 98 6C 85 18 92 6B 00 72 FB 72 DB 9D 46 BD B1 3C 56 C0 ED 3C 12 45 CA 1D 7E A3 ");
4267 ///
4268 /// let mut recovered = String::new();
4269 /// tdes.decrypt_vec_into_string(iv, &cipher, &mut recovered);
4270 /// println!("B =\t{}", recovered);
4271 /// assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
4272 /// assert_eq!(recovered, message);
4273 /// ```
4274 ///
4275 /// ## For more examples,
4276 /// click [here](./documentation/big_cryptor64_cbc_iso/struct.BigCryptor64.html#method.decrypt_vec_into_string)
4277 #[inline]
4278 fn decrypt_vec_into_string<U>(&mut self, iv: T, cipher: &Vec<U>, message: &mut String) -> u64
4279 where U: SmallUInt + Copy + Clone
4280 {
4281 self.decrypt_into_string(iv, cipher.as_ptr() as *const u8, (cipher.len() as u32 * U::size_in_bytes()) as u64, message)
4282 }
4283
4284 // fn decrypt_array<U, const N: usize>(&mut self, iv: T, cipher: &[U; N], message: *mut u8) -> u64
4285 /// Decrypts the data stored in an array `[U; N]` object with the padding
4286 /// defined according to ISO 7816-4 in CBC (Cipher-Block Chaining) mode.
4287 ///
4288 /// # Arguments
4289 /// - `iv` is an initialization vector for CBC mode.
4290 /// - `cipher` is an immutable reference to an array `[U; N]` object, and
4291 /// is the place where the plaintext to be decrypted is stored.
4292 /// - `message` is a mutable pointer to `u8` which is `*mut u8`, and
4293 /// is the place where the decrypted data will be stored.
4294 ///
4295 /// # Output
4296 /// - This method returns the size of plaintext in bytes.
4297 /// - If this method failed in decryption, it always returns `zero`.
4298 /// - Even if this method succeeded in decryption, it returns `zero` when
4299 /// the original plaintext is zero-length empty data. Then, you will have
4300 /// to check whether or not it failed by using the method
4301 /// `is_successful()` or `is_failed()`.
4302 /// - If `size_of::<U>()` * `N` is greater than `size_of::<T>()`
4303 /// (which means that the original plaintext is surely not empty data)
4304 /// and it returns `zero`, you can interpret it that this method surely
4305 /// failed in decryption.
4306 ///
4307 /// # Features
4308 /// - You are not encouraged to use this method in pure Rust programming.
4309 /// Instead, use other safer methods such as decrypt_array_into_*().
4310 /// - This method is useful to use in hybrid programming with C/C++.
4311 /// - `size_of::<U>()` * `N` cannot be other than any multiple
4312 /// of `size_of::<T>()`.
4313 /// - The size of the memory area which starts at `message` is assumed to
4314 /// be enough to store the plaintext. So, it is responsible for you to
4315 /// prepare the `message` area big enough!
4316 /// - The size of the area for plaintext does not have to be prepared more
4317 /// than `size_of::<U>()` * `N` - `1`.
4318 /// - If the size of the area for plaintext is prepared more than
4319 /// `size_of::<U>()` * `N` - `1`, the rest of the area will be
4320 /// filled with `0`s.
4321 /// - The padding bits are composed of the byte `0b_1000_0000` that
4322 /// indicates the delimiter one bit `1` followed by seven bits `0`s and
4323 /// all padding bits `0`s according to ISO 7816-4.
4324 /// - For more information about the padding bits according to ISO 7816-4,
4325 /// Read [here](https://en.wikipedia.org/wiki/Padding_(cryptography)#ISO/IEC_7816-4).
4326 ///
4327 /// # For Rijndael or AES, and its variants
4328 /// ## Example 1 for AES-128
4329 /// ```
4330 /// use std::io::Write;
4331 /// use std::fmt::Write as _;
4332 /// use cryptocol::symmetric::{ AES_128, CBC_ISO };
4333 ///
4334 /// let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
4335 /// println!("K =\t{:#016X}", key);
4336 /// let mut a_aes = AES_128::new_with_key_u128(key);
4337 /// let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
4338 /// println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
4339 ///
4340 /// let message = "In the beginning God created the heavens and the earth.";
4341 /// println!("M =\t{}", message);
4342 /// let mut cipher = [0_u8; 64];
4343 /// a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4344 /// print!("C =\t");
4345 /// for c in cipher.clone()
4346 /// { print!("{:02X} ", c); }
4347 /// println!();
4348 /// let mut txt = String::new();
4349 /// for c in cipher.clone()
4350 /// { write!(txt, "{:02X} ", c); }
4351 /// assert_eq!(txt, "C9 1C 27 CE 83 92 A1 CF 7D A4 64 35 16 48 01 72 CC E3 6D CD BB 19 FC D0 80 22 09 9F 23 32 73 27 58 37 F9 9B 3C 44 7B 03 B3 80 7E 99 DF 97 4E E9 EC D3 12 F1 1A 78 77 A6 A5 CB 73 AB 65 22 5B 7D ");
4352 /// println!();
4353 ///
4354 /// let mut recovered = vec![0; 55];
4355 /// a_aes.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
4356 /// print!("Ba =\t");
4357 /// for b in recovered.clone()
4358 /// { print!("{:02X} ", b); }
4359 /// println!();
4360 /// let mut txt = String::new();
4361 /// for c in recovered.clone()
4362 /// { write!(txt, "{:02X} ", c); }
4363 /// 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 ");
4364 ///
4365 /// let mut converted = String::new();
4366 /// unsafe { converted.as_mut_vec() }.append(&mut recovered);
4367 ///
4368 /// println!("Bb =\t{}", converted);
4369 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4370 /// assert_eq!(converted, message);
4371 /// ```
4372 ///
4373 /// ## For more examples,
4374 /// click [here](./documentation/rijndael_cbc_iso/struct.Rijndael_Generic.html#method.decrypt_array)
4375 ///
4376 /// # For DES and its variants
4377 /// ## Example 1 for Normal case
4378 /// ```
4379 /// use std::io::Write;
4380 /// use std::fmt::Write as _;
4381 /// use cryptocol::symmetric::{ DES, CBC_ISO };
4382 ///
4383 /// let key = 0x_1234567890ABCDEF_u64;
4384 /// println!("K =\t{:#016X}", key);
4385 /// let mut a_des = DES::new_with_key_u64(key);
4386 ///
4387 /// let message = "In the beginning God created the heavens and the earth.";
4388 /// println!("M =\t{}", message);
4389 /// let iv = 0x_FEDCBA0987654321_u64;
4390 /// println!("IV = {}", iv);
4391 /// let mut cipher = [0_u8; 56];
4392 /// a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
4393 /// print!("C (16 rounds) =\t");
4394 /// for c in cipher.clone()
4395 /// { print!("{:02X} ", c); }
4396 /// println!();
4397 /// let mut txt = String::new();
4398 /// for c in cipher.clone()
4399 /// { write!(txt, "{:02X} ", c); }
4400 /// assert_eq!(txt, "4B B5 ED DC A0 58 7E 6D 6C 3B A2 00 38 C3 D4 29 42 B1 CF 0D E9 FA EA 11 11 6B C8 30 73 39 DD B7 3F 96 9B A3 76 05 34 7E 64 2F D4 CC B2 68 33 64 11 78 69 FB 0B 32 CF 92 ");
4401 ///
4402 /// let mut recovered = vec![0; 55];
4403 /// let len = a_des.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
4404 /// recovered.truncate(len as usize);
4405 /// print!("Ba (16 rounds) =\t");
4406 /// for b in recovered.clone()
4407 /// { print!("{:02X} ", b); }
4408 /// println!();
4409 /// let mut txt = String::new();
4410 /// for c in recovered.clone()
4411 /// { write!(txt, "{:02X} ", c); }
4412 /// 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 ");
4413 ///
4414 /// let mut converted = String::new();
4415 /// unsafe { converted.as_mut_vec() }.append(&mut recovered);
4416 ///
4417 /// println!("Bb (16 rounds) =\t{}", converted);
4418 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4419 /// assert_eq!(converted, message);
4420 /// ```
4421 ///
4422 /// ## For more examples,
4423 /// click [here](./documentation/des_cbc_iso/struct.DES_Generic.html#method.decrypt_array)
4424 ///
4425 /// # For BigCryptor128
4426 /// ## Example 1 for TAES case
4427 /// ```
4428 /// use std::io::Write;
4429 /// use std::fmt::Write as _;
4430 /// use cryptocol::symmetric::{ BigCryptor128, AES_128, CBC_ISO };
4431 ///
4432 /// let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
4433 /// + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
4434 /// + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
4435 /// let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
4436 /// println!("IV = {:#034X}", iv);
4437 /// let message = "In the beginning God created the heavens and the earth.";
4438 /// println!("M =\t{}", message);
4439 /// let mut cipher = [0_u8; 64];
4440 /// taes.encrypt_str_into_array(iv, &message, &mut cipher);
4441 /// print!("C =\t");
4442 /// for c in cipher.clone()
4443 /// { print!("{:02X} ", c); }
4444 /// println!();
4445 /// let mut txt = String::new();
4446 /// for c in cipher.clone()
4447 /// { write!(txt, "{:02X} ", c); }
4448 /// assert_eq!(txt, "87 B7 B0 67 C9 41 CF C6 CF B8 69 0D 39 D3 9B EC 0C 18 28 9A FA 51 7D 2A E4 D1 0C 2F 2B A0 0E E3 D1 04 65 E4 B6 66 2B 23 04 42 66 31 7A C5 76 E0 73 1D B1 F8 BB 75 C5 FB C2 1B 73 26 67 57 02 A0 ");
4449 ///
4450 /// let mut recovered = vec![0; 55];
4451 /// let len = taes.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
4452 /// recovered.truncate(len as usize);
4453 /// print!("Ba =\t");
4454 /// for b in recovered.clone()
4455 /// { print!("{:02X} ", b); }
4456 /// println!();
4457 /// let mut txt = String::new();
4458 /// for c in recovered.clone()
4459 /// { write!(txt, "{:02X} ", c); }
4460 /// 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 ");
4461 ///
4462 /// let mut converted = String::new();
4463 /// unsafe { converted.as_mut_vec() }.append(&mut recovered);
4464 ///
4465 /// println!("Bb =\t{}", converted);
4466 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4467 /// assert_eq!(converted, message);
4468 /// ```
4469 ///
4470 /// ## For more examples,
4471 /// click [here](./documentation/big_cryptor128_cbc_iso/struct.BigCryptor128.html#method.decrypt_array)
4472 ///
4473 /// # For BigCryptor64
4474 /// ## Example 1 for TDES case
4475 /// ```
4476 /// use std::io::Write;
4477 /// use std::fmt::Write as _;
4478 /// use cryptocol::symmetric::{ BigCryptor64, DES, CBC_ISO };
4479 ///
4480 /// let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
4481 /// + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
4482 /// + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
4483 /// let iv = 0x_FEDCBA0987654321_u64;
4484 /// println!("IV = {:#018X}", iv);
4485 /// let message = "In the beginning God created the heavens and the earth.";
4486 /// println!("M =\t{}", message); let mut cipher = [0_u8; 56];
4487 /// tdes.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
4488 /// print!("C =\t");
4489 /// for c in cipher.clone()
4490 /// { print!("{:02X} ", c); }
4491 /// println!();
4492 /// let mut txt = String::new();
4493 /// for c in cipher.clone()
4494 /// { write!(txt, "{:02X} ", c); }
4495 /// assert_eq!(txt, "01 A5 7E BC ED 83 28 FB 7A 06 61 60 7D 8B BA AF 5C A7 76 0B FE 94 1C C4 C2 BC DD 15 98 A9 98 6C 85 18 92 6B 00 72 FB 72 DB 9D 46 BD B1 3C 56 C0 ED 3C 12 45 CA 1D 7E A3 ");
4496 ///
4497 /// let mut recovered = vec![0; 55];
4498 /// let len = tdes.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
4499 /// recovered.truncate(len as usize);
4500 /// print!("Ba =\t");
4501 /// for b in recovered.clone()
4502 /// { print!("{:02X} ", b); }
4503 /// println!();
4504 /// let mut txt = String::new();
4505 /// for c in recovered.clone()
4506 /// { write!(txt, "{:02X} ", c); }
4507 /// 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 ");
4508 ///
4509 /// let mut converted = String::new();
4510 /// unsafe { converted.as_mut_vec() }.append(&mut recovered);
4511 ///
4512 /// println!("Bb =\t{}", converted);
4513 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4514 /// assert_eq!(converted, message);
4515 /// ```
4516 ///
4517 /// ## For more examples,
4518 /// click [here](./documentation/big_cryptor64_cbc_iso/struct.BigCryptor64.html#method.decrypt_array)
4519 #[inline]
4520 fn decrypt_array<U, const N: usize>(&mut self, iv: T, cipher: &[U; N], message: *mut u8) -> u64
4521 where U: SmallUInt + Copy + Clone
4522 {
4523 self.decrypt(iv, cipher.as_ptr() as *const u8, (cipher.len() as u32 * U::size_in_bytes()) as u64, message)
4524 }
4525
4526 // fn decrypt_array_into_vec<U, V, const N: usize>(&mut self, iv: T, cipher: &[U; N], message: &mut Vec<V>) -> u64
4527 /// Decrypts the data stored in an array `[U; N]` object with the padding
4528 /// defined according to ISO 7816-4 in CBC (Cipher-Block Chaining) mode,
4529 /// and stores the decrypted data in `Vec<V>`.
4530 ///
4531 /// # Arguments
4532 /// - `iv` is an initialization vector for CBC mode.
4533 /// - `cipher` is an immutable reference to an array `[U; N]` object, and
4534 /// is the place where the plaintext to be decrypted is stored.
4535 /// - `message` is a mutable reference to `Vec<U>` object, and
4536 /// is the place where the decrypted data will be stored.
4537 ///
4538 /// # Output
4539 /// - This method returns the size of plaintext in bytes.
4540 /// - If this method failed in decryption, it always returns `zero`.
4541 /// - Even if this method succeeded in decryption, it returns `zero` when
4542 /// the original plaintext is zero-length empty data. Then, you will have
4543 /// to check whether or not it failed by using the method
4544 /// `is_successful()` or `is_failed()`.
4545 /// - If `size_of::<U>()` * `N` is greater than `size_of::<T>()`
4546 /// (which means that the original plaintext is surely not empty data)
4547 /// and it returns `zero`, you can interpret it that this method surely
4548 /// failed in decryption.
4549 ///
4550 /// # Features
4551 /// - The padding bits are composed of the byte `0b_1000_0000` that
4552 /// indicates the delimiter one bit `1` followed by seven bits `0`s and
4553 /// all padding bits `0`s according to ISO 7816-4.
4554 /// - For more information about the padding bits according to ISO 7816-4,
4555 /// Read [here](https://en.wikipedia.org/wiki/Padding_(cryptography)#ISO/IEC_7816-4).
4556 /// - You don't have to worry about whether or not the size of the memory
4557 /// area where the ciphertext will be stored is enough.
4558 ///
4559 /// # For Rijndael or AES, and its variants
4560 /// ## Example 1 for AES-128
4561 /// ```
4562 /// use std::io::Write;
4563 /// use std::fmt::Write as _;
4564 /// use cryptocol::symmetric::{ AES_128, CBC_ISO };
4565 ///
4566 /// let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
4567 /// println!("K =\t{:#016X}", key);
4568 /// let mut a_aes = AES_128::new_with_key_u128(key);
4569 /// let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
4570 /// println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
4571 ///
4572 /// let message = "In the beginning God created the heavens and the earth.";
4573 /// println!("M =\t{}", message);
4574 /// let mut cipher = [0_u8; 64];
4575 /// a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4576 /// print!("C =\t");
4577 /// for c in cipher.clone()
4578 /// { print!("{:02X} ", c); }
4579 /// println!();
4580 /// let mut txt = String::new();
4581 /// for c in cipher.clone()
4582 /// { write!(txt, "{:02X} ", c); }
4583 /// assert_eq!(txt, "C9 1C 27 CE 83 92 A1 CF 7D A4 64 35 16 48 01 72 CC E3 6D CD BB 19 FC D0 80 22 09 9F 23 32 73 27 58 37 F9 9B 3C 44 7B 03 B3 80 7E 99 DF 97 4E E9 EC D3 12 F1 1A 78 77 A6 A5 CB 73 AB 65 22 5B 7D ");
4584 /// println!();
4585 ///
4586 /// let mut recovered = vec![0; 55];
4587 /// a_aes.decrypt_array_into_vec(iv, &cipher, &mut recovered);
4588 /// print!("Ba =\t");
4589 /// for b in recovered.clone()
4590 /// { print!("{:02X} ", b); }
4591 /// println!();
4592 /// let mut txt = String::new();
4593 /// for c in recovered.clone()
4594 /// { write!(txt, "{:02X} ", c); }
4595 /// 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 ");
4596 ///
4597 /// let mut converted = String::new();
4598 /// unsafe { converted.as_mut_vec() }.append(&mut recovered);
4599 ///
4600 /// println!("Bb =\t{}", converted);
4601 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4602 /// assert_eq!(converted, message);
4603 /// ```
4604 ///
4605 /// ## For more examples,
4606 /// click [here](./documentation/rijndael_cbc_iso/struct.Rijndael_Generic.html#method.decrypt_array_into_vec)
4607 ///
4608 /// # For DES and its variants
4609 /// ## Example 1 for Normal case
4610 /// ```
4611 /// use std::io::Write;
4612 /// use std::fmt::Write as _;
4613 /// use cryptocol::symmetric::{ DES, CBC_ISO };
4614 ///
4615 /// let key = 0x_1234567890ABCDEF_u64;
4616 /// println!("K =\t{:#016X}", key);
4617 /// let mut a_des = DES::new_with_key_u64(key);
4618 ///
4619 /// let message = "In the beginning God created the heavens and the earth.";
4620 /// println!("M =\t{}", message);
4621 /// let iv = 0x_FEDCBA0987654321_u64;
4622 /// println!("IV = {}", iv);
4623 /// let mut cipher = [0_u8; 56];
4624 /// a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
4625 /// print!("C (16 rounds) =\t");
4626 /// for c in cipher.clone()
4627 /// { print!("{:02X} ", c); }
4628 /// println!();
4629 /// let mut txt = String::new();
4630 /// for c in cipher.clone()
4631 /// { write!(txt, "{:02X} ", c); }
4632 /// assert_eq!(txt, "4B B5 ED DC A0 58 7E 6D 6C 3B A2 00 38 C3 D4 29 42 B1 CF 0D E9 FA EA 11 11 6B C8 30 73 39 DD B7 3F 96 9B A3 76 05 34 7E 64 2F D4 CC B2 68 33 64 11 78 69 FB 0B 32 CF 92 ");
4633 ///
4634 /// let mut recovered = Vec::<u8>::new();
4635 /// a_des.decrypt_array_into_vec(iv, &cipher, &mut recovered);
4636 /// print!("Ba (16 rounds) =\t");
4637 /// for b in recovered.clone()
4638 /// { print!("{:02X} ", b); }
4639 /// println!();
4640 /// let mut txt = String::new();
4641 /// for c in recovered.clone()
4642 /// { write!(txt, "{:02X} ", c); }
4643 /// 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 ");
4644 ///
4645 /// let mut converted = String::new();
4646 /// unsafe { converted.as_mut_vec() }.append(&mut recovered);
4647 ///
4648 /// println!("Bb (16 rounds) =\t{}", converted);
4649 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4650 /// assert_eq!(converted, message);
4651 /// ```
4652 ///
4653 /// ## For more examples,
4654 /// click [here](./documentation/des_cbc_iso/struct.DES_Generic.html#method.decrypt_array_into_vec)
4655 ///
4656 /// # For BigCryptor128
4657 /// ## Example 1 for TAES case
4658 /// ```
4659 /// use std::io::Write;
4660 /// use std::fmt::Write as _;
4661 /// use cryptocol::symmetric::{ BigCryptor128, AES_128, CBC_ISO };
4662 ///
4663 /// let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
4664 /// + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
4665 /// + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
4666 /// let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
4667 /// println!("IV = {:#034X}", iv);
4668 /// let message = "In the beginning God created the heavens and the earth.";
4669 /// println!("M =\t{}", message);
4670 /// let mut cipher = [0_u8; 64];
4671 /// taes.encrypt_str_into_array(iv, &message, &mut cipher);
4672 /// print!("C =\t");
4673 /// for c in cipher.clone()
4674 /// { print!("{:02X} ", c); }
4675 /// println!();
4676 /// let mut txt = String::new();
4677 /// for c in cipher.clone()
4678 /// { write!(txt, "{:02X} ", c); }
4679 /// assert_eq!(txt, "87 B7 B0 67 C9 41 CF C6 CF B8 69 0D 39 D3 9B EC 0C 18 28 9A FA 51 7D 2A E4 D1 0C 2F 2B A0 0E E3 D1 04 65 E4 B6 66 2B 23 04 42 66 31 7A C5 76 E0 73 1D B1 F8 BB 75 C5 FB C2 1B 73 26 67 57 02 A0 ");
4680 ///
4681 /// let mut recovered = Vec::<u8>::new();
4682 /// taes.decrypt_array_into_vec(iv, &cipher, &mut recovered);
4683 /// print!("Ba =\t");
4684 /// for b in recovered.clone()
4685 /// { print!("{:02X} ", b); }
4686 /// println!();
4687 /// let mut txt = String::new();
4688 /// for c in recovered.clone()
4689 /// { write!(txt, "{:02X} ", c); }
4690 /// 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 ");
4691 ///
4692 /// let mut converted = String::new();
4693 /// unsafe { converted.as_mut_vec() }.append(&mut recovered);
4694 ///
4695 /// println!("Bb =\t{}", converted);
4696 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4697 /// assert_eq!(converted, message);
4698 /// ```
4699 ///
4700 /// ## For more examples,
4701 /// click [here](./documentation/big_cryptor128_cbc_iso/struct.BigCryptor128.html#method.decrypt_array_into_vec)
4702 ///
4703 /// # For BigCryptor64
4704 /// ## Example 1 for TDES case
4705 /// ```
4706 /// use std::io::Write;
4707 /// use std::fmt::Write as _;
4708 /// use cryptocol::symmetric::{ BigCryptor64, DES, CBC_ISO };
4709 ///
4710 /// let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
4711 /// + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
4712 /// + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
4713 /// let iv = 0x_FEDCBA0987654321_u64;
4714 /// println!("IV = {:#018X}", iv);
4715 /// let message = "In the beginning God created the heavens and the earth.";
4716 /// println!("M =\t{}", message); let mut cipher = [0_u8; 56];
4717 /// tdes.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
4718 /// print!("C =\t");
4719 /// for c in cipher.clone()
4720 /// { print!("{:02X} ", c); }
4721 /// println!();
4722 /// let mut txt = String::new();
4723 /// for c in cipher.clone()
4724 /// { write!(txt, "{:02X} ", c); }
4725 /// assert_eq!(txt, "01 A5 7E BC ED 83 28 FB 7A 06 61 60 7D 8B BA AF 5C A7 76 0B FE 94 1C C4 C2 BC DD 15 98 A9 98 6C 85 18 92 6B 00 72 FB 72 DB 9D 46 BD B1 3C 56 C0 ED 3C 12 45 CA 1D 7E A3 ");
4726 ///
4727 /// let mut recovered = Vec::<u8>::new();
4728 /// tdes.decrypt_array_into_vec(iv, &cipher, &mut recovered);
4729 /// print!("Ba =\t");
4730 /// for b in recovered.clone()
4731 /// { print!("{:02X} ", b); }
4732 /// println!();
4733 /// let mut txt = String::new();
4734 /// for c in recovered.clone()
4735 /// { write!(txt, "{:02X} ", c); }
4736 /// 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 ");
4737 ///
4738 /// let mut converted = String::new();
4739 /// unsafe { converted.as_mut_vec() }.append(&mut recovered);
4740 ///
4741 /// println!("Bb =\t{}", converted);
4742 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4743 /// assert_eq!(converted, message);
4744 /// ```
4745 ///
4746 /// ## For more examples,
4747 /// click [here](./documentation/big_cryptor64_cbc_iso/struct.BigCryptor64.html#method.decrypt_array_into_vec)
4748 #[inline]
4749 fn decrypt_array_into_vec<U, V, const N: usize>(&mut self, iv: T, cipher: &[U; N], message: &mut Vec<V>) -> u64
4750 where U: SmallUInt + Copy + Clone, V: SmallUInt + Copy + Clone
4751 {
4752 self.decrypt_into_vec(iv, cipher.as_ptr() as *const u8, (cipher.len() as u32 * U::size_in_bytes()) as u64, message)
4753 }
4754
4755 // fn decrypt_array_into_array<U, V, const N: usize, const M: usize>(&mut self, iv: T, cipher: &[U; N], message: &mut [V; M]) -> u64
4756 /// Decrypts the data stored in an array `[U; N]` object with the padding
4757 /// defined according to ISO 7816-4 in CBC (Cipher-Block Chaining) mode,
4758 /// and stores the decrypted data in array `[V; M]`.
4759 ///
4760 /// # Arguments
4761 /// - `iv` is an initialization vector for CBC mode.
4762 /// - `cipher` is an immutable reference to an array `[U; N]` object, and
4763 /// is the place where the plaintext to be decrypted is stored.
4764 /// - `message` is a mutable reference to an array `[U; N]` object, and
4765 /// is the place where the decrypted data will be stored.
4766 ///
4767 /// # Output
4768 /// - This method returns the size of plaintext in bytes.
4769 /// - If this method failed in decryption, it always returns `zero`.
4770 /// - Even if this method succeeded in decryption, it returns `zero` when
4771 /// the original plaintext is zero-length empty data. Then, you will have
4772 /// to check whether or not it failed by using the method
4773 /// `is_successful()` or `is_failed()`.
4774 /// - If `size_of::<U>()` * `N` is greater than `size_of::<T>()`
4775 /// (which means that the original plaintext is surely not empty data)
4776 /// and it returns `zero`, you can interpret it that this method surely
4777 /// failed in decryption.
4778 ///
4779 /// # Features
4780 /// - If `size_of::<V>()` * `M` is less than
4781 /// `size_of::<U>()` * `N` - `1`, this method does not perform
4782 /// decryption but returns `zero`.
4783 /// - If `size_of::<V>()` * `M` is equal to or greater than
4784 /// `size_of::<U>()` * `N` - `1`, this method performs decryption,
4785 /// fills the array `message` with the decrypted data, and then
4786 /// fills the rest of the elements of the array `message` with zeros, and
4787 /// returns the size of the plaintext.
4788 /// - It is responsible for you to prepare the `message` area big enough!
4789 /// - The size of the area for plaintext does not have to be prepared more
4790 /// than `size_of::<U>()` * `N` - `1`.
4791 /// - The padding bits are composed of the byte `0b_1000_0000` that
4792 /// indicates the delimiter one bit `1` followed by seven bits `0`s and
4793 /// all padding bits `0`s according to ISO 7816-4.
4794 /// - For more information about the padding bits according to ISO 7816-4,
4795 /// Read [here](https://en.wikipedia.org/wiki/Padding_(cryptography)#ISO/IEC_7816-4).
4796 ///
4797 /// # For Rijndael or AES, and its variants
4798 /// ## Example 1 for AES-128
4799 /// ```
4800 /// use std::io::Write;
4801 /// use std::fmt::Write as _;
4802 /// use cryptocol::symmetric::{ AES_128, CBC_ISO };
4803 ///
4804 /// let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
4805 /// println!("K =\t{:#016X}", key);
4806 /// let mut a_aes = AES_128::new_with_key_u128(key);
4807 /// let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
4808 /// println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
4809 ///
4810 /// let message = "In the beginning God created the heavens and the earth.";
4811 /// println!("M =\t{}", message);
4812 /// let mut cipher = [0_u8; 64];
4813 /// a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4814 /// print!("C =\t");
4815 /// for c in cipher.clone()
4816 /// { print!("{:02X} ", c); }
4817 /// println!();
4818 /// let mut txt = String::new();
4819 /// for c in cipher.clone()
4820 /// { write!(txt, "{:02X} ", c); }
4821 /// assert_eq!(txt, "C9 1C 27 CE 83 92 A1 CF 7D A4 64 35 16 48 01 72 CC E3 6D CD BB 19 FC D0 80 22 09 9F 23 32 73 27 58 37 F9 9B 3C 44 7B 03 B3 80 7E 99 DF 97 4E E9 EC D3 12 F1 1A 78 77 A6 A5 CB 73 AB 65 22 5B 7D ");
4822 /// println!();
4823 ///
4824 /// let mut recovered = [0; 64];
4825 /// let len = a_aes.decrypt_array_into_array(iv, &cipher, &mut recovered);
4826 /// print!("Ba =\t");
4827 /// for b in recovered.clone()
4828 /// { print!("{:02X} ", b); }
4829 /// println!();
4830 /// let mut txt = String::new();
4831 /// for c in recovered.clone()
4832 /// { write!(txt, "{:02X} ", c); }
4833 /// 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 00 00 00 00 00 00 00 00 ");
4834 ///
4835 /// let mut converted = String::new();
4836 /// unsafe { converted.as_mut_vec() }.write(&recovered);
4837 /// unsafe { converted.as_mut_vec() }.truncate(len as usize);
4838 /// println!("Bb =\t{}", converted);
4839 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4840 /// assert_eq!(converted, message);
4841 /// ```
4842 ///
4843 /// ## For more examples,
4844 /// click [here](./documentation/rijndael_cbc_iso/struct.Rijndael_Generic.html#method.decrypt_array_into_array)
4845 ///
4846 /// # For DES and its variants
4847 /// ## Example 1 for Normal case
4848 /// ```
4849 /// use std::io::Write;
4850 /// use std::fmt::Write as _;
4851 /// use cryptocol::symmetric::{ DES, CBC_ISO };
4852 ///
4853 /// let key = 0x_1234567890ABCDEF_u64;
4854 /// println!("K =\t{:#016X}", key);
4855 /// let mut a_des = DES::new_with_key_u64(key);
4856 ///
4857 /// let message = "In the beginning God created the heavens and the earth.";
4858 /// println!("M =\t{}", message);
4859 /// let iv = 0x_FEDCBA0987654321_u64;
4860 /// println!("IV = {}", iv);
4861 /// let mut cipher = [0_u8; 56];
4862 /// a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
4863 /// print!("C (16 rounds) =\t");
4864 /// for c in cipher.clone()
4865 /// { print!("{:02X} ", c); }
4866 /// println!();
4867 /// let mut txt = String::new();
4868 /// for c in cipher.clone()
4869 /// { write!(txt, "{:02X} ", c); }
4870 /// assert_eq!(txt, "4B B5 ED DC A0 58 7E 6D 6C 3B A2 00 38 C3 D4 29 42 B1 CF 0D E9 FA EA 11 11 6B C8 30 73 39 DD B7 3F 96 9B A3 76 05 34 7E 64 2F D4 CC B2 68 33 64 11 78 69 FB 0B 32 CF 92 ");
4871 ///
4872 /// let mut recovered = [0u8; 56];
4873 /// let len = a_des.decrypt_array_into_array(iv, &cipher, &mut recovered);
4874 /// print!("Ba (16 rounds) =\t");
4875 /// for b in recovered.clone()
4876 /// { print!("{:02X} ", b); }
4877 /// println!();
4878 /// let mut txt = String::new();
4879 /// for c in recovered.clone()
4880 /// { write!(txt, "{:02X} ", c); }
4881 /// assert_eq!(txt, "49 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 ");
4882 ///
4883 /// let mut converted = String::new();
4884 /// unsafe { converted.as_mut_vec() }.write(&recovered);
4885 /// unsafe { converted.as_mut_vec() }.truncate(len as usize);
4886 /// println!("Bb (16 rounds) =\t{}", converted);
4887 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4888 /// assert_eq!(converted, message);
4889 /// ```
4890 ///
4891 /// ## For more examples,
4892 /// click [here](./documentation/des_cbc_iso/struct.DES_Generic.html#method.decrypt_array_into_array)
4893 ///
4894 /// # For BigCryptor128
4895 /// ## Example 1 for TAES case
4896 /// ```
4897 /// use std::io::Write;
4898 /// use std::fmt::Write as _;
4899 /// use cryptocol::symmetric::{ BigCryptor128, AES_128, CBC_ISO };
4900 ///
4901 /// let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
4902 /// + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
4903 /// + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
4904 /// let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
4905 /// println!("IV = {:#034X}", iv);
4906 /// let message = "In the beginning God created the heavens and the earth.";
4907 /// println!("M =\t{}", message);
4908 /// let mut cipher = [0_u8; 64];
4909 /// taes.encrypt_str_into_array(iv, &message, &mut cipher);
4910 /// print!("C =\t");
4911 /// for c in cipher.clone()
4912 /// { print!("{:02X} ", c); }
4913 /// println!();
4914 /// let mut txt = String::new();
4915 /// for c in cipher.clone()
4916 /// { write!(txt, "{:02X} ", c); }
4917 /// assert_eq!(txt, "87 B7 B0 67 C9 41 CF C6 CF B8 69 0D 39 D3 9B EC 0C 18 28 9A FA 51 7D 2A E4 D1 0C 2F 2B A0 0E E3 D1 04 65 E4 B6 66 2B 23 04 42 66 31 7A C5 76 E0 73 1D B1 F8 BB 75 C5 FB C2 1B 73 26 67 57 02 A0 ");
4918 ///
4919 /// let mut recovered = [0u8; 56];
4920 /// let len = taes.decrypt_array_into_array(iv, &cipher, &mut recovered);
4921 /// print!("Ba =\t");
4922 /// for b in recovered.clone()
4923 /// { print!("{:02X} ", b); }
4924 /// println!();
4925 /// let mut txt = String::new();
4926 /// for c in recovered.clone()
4927 /// { write!(txt, "{:02X} ", c); }
4928 /// 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 ");
4929 ///
4930 /// let mut converted = String::new();
4931 /// unsafe { converted.as_mut_vec() }.write(&recovered);
4932 /// unsafe { converted.as_mut_vec() }.truncate(len as usize);
4933 /// println!("Bb =\t{}", converted);
4934 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4935 /// assert_eq!(converted, message);
4936 /// ```
4937 ///
4938 /// ## For more examples,
4939 /// click [here](./documentation/big_cryptor128_cbc_iso/struct.BigCryptor128.html#method.decrypt_array_into_array)
4940 ///
4941 /// # For BigCryptor64
4942 /// ## Example 1 for TDES case
4943 /// ```
4944 /// use std::io::Write;
4945 /// use std::fmt::Write as _;
4946 /// use cryptocol::symmetric::{ BigCryptor64, DES, CBC_ISO };
4947 ///
4948 /// let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
4949 /// + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
4950 /// + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
4951 /// let iv = 0x_FEDCBA0987654321_u64;
4952 /// println!("IV = {:#018X}", iv);
4953 /// let message = "In the beginning God created the heavens and the earth.";
4954 /// println!("M =\t{}", message); let mut cipher = [0_u8; 56];
4955 /// tdes.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
4956 /// print!("C =\t");
4957 /// for c in cipher.clone()
4958 /// { print!("{:02X} ", c); }
4959 /// println!();
4960 /// let mut txt = String::new();
4961 /// for c in cipher.clone()
4962 /// { write!(txt, "{:02X} ", c); }
4963 /// assert_eq!(txt, "01 A5 7E BC ED 83 28 FB 7A 06 61 60 7D 8B BA AF 5C A7 76 0B FE 94 1C C4 C2 BC DD 15 98 A9 98 6C 85 18 92 6B 00 72 FB 72 DB 9D 46 BD B1 3C 56 C0 ED 3C 12 45 CA 1D 7E A3 ");
4964 ///
4965 /// let mut recovered = [0u8; 56];
4966 /// let len = tdes.decrypt_array_into_array(iv, &cipher, &mut recovered);
4967 /// print!("Ba =\t");
4968 /// for b in recovered.clone()
4969 /// { print!("{:02X} ", b); }
4970 /// println!();
4971 /// let mut txt = String::new();
4972 /// for c in recovered.clone()
4973 /// { write!(txt, "{:02X} ", c); }
4974 /// 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 ");
4975 ///
4976 /// let mut converted = String::new();
4977 /// unsafe { converted.as_mut_vec() }.write(&recovered);
4978 /// unsafe { converted.as_mut_vec() }.truncate(len as usize);
4979 /// println!("Bb =\t{}", converted);
4980 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4981 /// assert_eq!(converted, message);
4982 /// ```
4983 ///
4984 /// ## For more examples,
4985 /// click [here](./documentation/big_cryptor64_cbc_iso/struct.BigCryptor64.html#method.decrypt_array_into_array)
4986 #[inline]
4987 fn decrypt_array_into_array<U, V, const N: usize, const M: usize>(&mut self, iv: T, cipher: &[U; N], message: &mut [V; M]) -> u64
4988 where U: SmallUInt + Copy + Clone, V: SmallUInt + Copy + Clone
4989 {
4990 self.decrypt_into_array(iv, cipher.as_ptr() as *const u8, (N as u32 * U::size_in_bytes()) as u64, message)
4991 }
4992
4993 // fn decrypt_array_into_string<U, const N: usize>(&mut self, iv: T, cipher: &[U; N], message: &mut String) -> u64
4994 /// Decrypts the data stored in an array `[U; N]` object with the padding
4995 /// defined according to ISO 7816-4 in CBC (Cipher-Block Chaining) mode,
4996 /// and stores the decrypted data in `String`.
4997 ///
4998 /// # Arguments
4999 /// - `iv` is an initialization vector for CBC mode.
5000 /// - `cipher` is an immutable reference to an array `[U; N]` object, and
5001 /// is the place where the plaintext to be decrypted is stored.
5002 /// - `message` is a mutable reference to a `String` object, and
5003 /// is the place where the decrypted data will be stored.
5004 ///
5005 /// # Output
5006 /// - This method returns the size of plaintext in bytes.
5007 /// - If this method failed in decryption, it always returns `zero`.
5008 /// - Even if this method succeeded in decryption, it returns `zero` when
5009 /// the original plaintext is zero-length empty data. Then, you will have
5010 /// to check whether or not it failed by using the method
5011 /// `is_successful()` or `is_failed()`.
5012 /// - If `size_of::<U>()` * `N` is greater than `size_of::<T>()`
5013 /// (which means that the original plaintext is surely not empty data)
5014 /// and it returns `zero`, you can interpret it that this method surely
5015 /// failed in decryption.
5016 ///
5017 /// # Features
5018 /// - The padding bits are composed of the byte `0b_1000_0000` that
5019 /// indicates the delimiter one bit `1` followed by seven bits `0`s and
5020 /// all padding bits `0`s according to ISO 7816-4.
5021 /// - For more information about the padding bits according to ISO 7816-4,
5022 /// Read [here](https://en.wikipedia.org/wiki/Padding_(cryptography)#ISO/IEC_7816-4).
5023 /// - You don't have to worry about whether or not the size of the memory
5024 /// area where the ciphertext will be stored is enough.
5025 /// - This method assumes that the original plaintext is a string
5026 /// in the format of UTF-8.
5027 ///
5028 /// # For Rijndael or AES, and its variants
5029 /// ## Example 1 for AES-128
5030 /// ```
5031 /// use std::io::Write;
5032 /// use std::fmt::Write as _;
5033 /// use cryptocol::symmetric::{ AES_128, CBC_ISO };
5034 ///
5035 /// let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
5036 /// println!("K =\t{:#016X}", key);
5037 /// let mut a_aes = AES_128::new_with_key_u128(key);
5038 /// let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
5039 /// println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
5040 ///
5041 /// let message = "In the beginning God created the heavens and the earth.";
5042 /// println!("M =\t{}", message);
5043 /// let mut cipher = [0_u8; 64];
5044 /// a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
5045 /// print!("C =\t");
5046 /// for c in cipher.clone()
5047 /// { print!("{:02X} ", c); }
5048 /// println!();
5049 /// let mut txt = String::new();
5050 /// for c in cipher.clone()
5051 /// { write!(txt, "{:02X} ", c); }
5052 /// assert_eq!(txt, "C9 1C 27 CE 83 92 A1 CF 7D A4 64 35 16 48 01 72 CC E3 6D CD BB 19 FC D0 80 22 09 9F 23 32 73 27 58 37 F9 9B 3C 44 7B 03 B3 80 7E 99 DF 97 4E E9 EC D3 12 F1 1A 78 77 A6 A5 CB 73 AB 65 22 5B 7D ");
5053 /// println!();
5054 ///
5055 /// let mut converted= String::new();
5056 /// a_aes.decrypt_array_into_string(iv, &cipher, &mut converted);
5057 /// println!("B =\t{}", converted);
5058 /// assert_eq!(converted, "In the beginning God created the heavens and the earth.");
5059 /// assert_eq!(converted, message);
5060 /// ```
5061 ///
5062 /// ## For more examples,
5063 /// click [here](./documentation/rijndael_cbc_iso/struct.Rijndael_Generic.html#method.decrypt_array_into_string)
5064 ///
5065 /// # For DES and its variants
5066 /// ## Example 1 for Normal case
5067 /// ```
5068 /// use std::io::Write;
5069 /// use std::fmt::Write as _;
5070 /// use cryptocol::symmetric::{ DES, CBC_ISO };
5071 ///
5072 /// let key = 0x_1234567890ABCDEF_u64;
5073 /// println!("K =\t{:#016X}", key);
5074 /// let mut a_des = DES::new_with_key_u64(key);
5075 ///
5076 /// let message = "In the beginning God created the heavens and the earth.";
5077 /// println!("M =\t{}", message);
5078 /// let iv = 0x_FEDCBA0987654321_u64;
5079 /// println!("IV = {}", iv);
5080 /// let mut cipher = [0_u8; 56];
5081 /// a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
5082 /// print!("C (16 rounds) =\t");
5083 /// for c in cipher.clone()
5084 /// { print!("{:02X} ", c); }
5085 /// println!();
5086 /// let mut txt = String::new();
5087 /// for c in cipher.clone()
5088 /// { write!(txt, "{:02X} ", c); }
5089 /// assert_eq!(txt, "4B B5 ED DC A0 58 7E 6D 6C 3B A2 00 38 C3 D4 29 42 B1 CF 0D E9 FA EA 11 11 6B C8 30 73 39 DD B7 3F 96 9B A3 76 05 34 7E 64 2F D4 CC B2 68 33 64 11 78 69 FB 0B 32 CF 92 ");
5090 ///
5091 /// let mut recovered = String::new();
5092 /// a_des.decrypt_array_into_string(iv, &cipher, &mut recovered);
5093 /// println!("B (16 rounds) =\t{}", recovered);
5094 /// assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
5095 /// assert_eq!(recovered, message);
5096 /// ```
5097 ///
5098 /// ## For more examples,
5099 /// click [here](./documentation/des_cbc_iso/struct.DES_Generic.html#method.decrypt_array_into_string)
5100 ///
5101 /// # For BigCryptor128
5102 /// ## Example 1 for TAES case
5103 /// ```
5104 /// use std::io::Write;
5105 /// use std::fmt::Write as _;
5106 /// use cryptocol::symmetric::{ BigCryptor128, AES_128, CBC_ISO };
5107 ///
5108 /// let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
5109 /// + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
5110 /// + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
5111 /// let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
5112 /// println!("IV = {:#034X}", iv);
5113 /// let message = "In the beginning God created the heavens and the earth.";
5114 /// println!("M =\t{}", message);
5115 /// let mut cipher = [0_u8; 64];
5116 /// taes.encrypt_str_into_array(iv, &message, &mut cipher);
5117 /// print!("C =\t");
5118 /// for c in cipher.clone()
5119 /// { print!("{:02X} ", c); }
5120 /// println!();
5121 /// let mut txt = String::new();
5122 /// for c in cipher.clone()
5123 /// { write!(txt, "{:02X} ", c); }
5124 /// assert_eq!(txt, "87 B7 B0 67 C9 41 CF C6 CF B8 69 0D 39 D3 9B EC 0C 18 28 9A FA 51 7D 2A E4 D1 0C 2F 2B A0 0E E3 D1 04 65 E4 B6 66 2B 23 04 42 66 31 7A C5 76 E0 73 1D B1 F8 BB 75 C5 FB C2 1B 73 26 67 57 02 A0 ");
5125 ///
5126 /// let mut recovered = String::new();
5127 /// taes.decrypt_array_into_string(iv, &cipher, &mut recovered);
5128 /// println!("B =\t{}", recovered);
5129 /// assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
5130 /// assert_eq!(recovered, message);
5131 /// ```
5132 ///
5133 /// ## For more examples,
5134 /// click [here](./documentation/big_cryptor128_cbc_iso/struct.BigCryptor128.html#method.decrypt_array_into_string)
5135 ///
5136 /// # For BigCryptor64
5137 /// ## Example 1 for TDES case
5138 /// ```
5139 /// use std::io::Write;
5140 /// use std::fmt::Write as _;
5141 /// use cryptocol::symmetric::{ BigCryptor64, DES, CBC_ISO };
5142 ///
5143 /// let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
5144 /// + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
5145 /// + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
5146 /// let iv = 0x_FEDCBA0987654321_u64;
5147 /// println!("IV = {:#018X}", iv);
5148 /// let message = "In the beginning God created the heavens and the earth.";
5149 /// println!("M =\t{}", message); let mut cipher = [0_u8; 56];
5150 /// tdes.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
5151 /// print!("C =\t");
5152 /// for c in cipher.clone()
5153 /// { print!("{:02X} ", c); }
5154 /// println!();
5155 /// let mut txt = String::new();
5156 /// for c in cipher.clone()
5157 /// { write!(txt, "{:02X} ", c); }
5158 /// assert_eq!(txt, "01 A5 7E BC ED 83 28 FB 7A 06 61 60 7D 8B BA AF 5C A7 76 0B FE 94 1C C4 C2 BC DD 15 98 A9 98 6C 85 18 92 6B 00 72 FB 72 DB 9D 46 BD B1 3C 56 C0 ED 3C 12 45 CA 1D 7E A3 ");
5159 ///
5160 /// let mut recovered = String::new();
5161 /// tdes.decrypt_array_into_string(iv, &cipher, &mut recovered);
5162 /// println!("B =\t{}", recovered);
5163 /// assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
5164 /// assert_eq!(recovered, message);
5165 /// ```
5166 ///
5167 /// ## For more examples,
5168 /// click [here](./documentation/big_cryptor64_cbc_iso/struct.BigCryptor64.html#method.decrypt_array_into_string)
5169 #[inline]
5170 fn decrypt_array_into_string<U, const N: usize>(&mut self, iv: T, cipher: &[U; N], message: &mut String) -> u64
5171 where U: SmallUInt + Copy + Clone
5172 {
5173 self.decrypt_into_string(iv, cipher.as_ptr() as *const u8, (cipher.len() as u32 * U::size_in_bytes()) as u64, message)
5174 }
5175}