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