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