pub trait ECB_ISO<T>: Sized {
Show 27 methods
// Required methods
fn encrypt(
&mut self,
message: *const u8,
length_in_bytes: u64,
cipher: *mut u8,
) -> u64;
fn encrypt_into_array<U, const N: usize>(
&mut self,
message: *const u8,
length_in_bytes: u64,
cipher: &mut [U; N],
) -> u64
where U: SmallUInt + Copy + Clone;
fn decrypt(
&mut self,
cipher: *const u8,
length_in_bytes: u64,
message: *mut u8,
) -> u64;
fn decrypt_into_array<U, const N: usize>(
&mut self,
cipher: *const u8,
length_in_bytes: u64,
message: &mut [U; N],
) -> u64
where U: SmallUInt + Copy + Clone;
// Provided methods
fn encrypt_into_vec<U>(
&mut self,
message: *const u8,
length_in_bytes: u64,
cipher: &mut Vec<U>,
) -> u64
where U: SmallUInt + Copy + Clone { ... }
fn encrypt_str(&mut self, message: &str, cipher: *mut u8) -> u64 { ... }
fn encrypt_str_into_vec<U>(
&mut self,
message: &str,
cipher: &mut Vec<U>,
) -> u64
where U: SmallUInt + Copy + Clone { ... }
fn encrypt_str_into_array<U, const N: usize>(
&mut self,
message: &str,
cipher: &mut [U; N],
) -> u64
where U: SmallUInt + Copy + Clone { ... }
fn encrypt_string(&mut self, message: &String, cipher: *mut u8) -> u64 { ... }
fn encrypt_string_into_vec<U>(
&mut self,
message: &String,
cipher: &mut Vec<U>,
) -> u64
where U: SmallUInt + Copy + Clone { ... }
fn encrypt_string_into_array<U, const N: usize>(
&mut self,
message: &String,
cipher: &mut [U; N],
) -> u64
where U: SmallUInt + Copy + Clone { ... }
fn encrypt_vec<U>(&mut self, message: &Vec<U>, cipher: *mut u8) -> u64
where U: SmallUInt + Copy + Clone { ... }
fn encrypt_vec_into_vec<U, V>(
&mut self,
message: &Vec<U>,
cipher: &mut Vec<V>,
) -> u64
where U: SmallUInt + Copy + Clone,
V: SmallUInt + Copy + Clone { ... }
fn encrypt_vec_into_array<U, V, const N: usize>(
&mut self,
message: &Vec<U>,
cipher: &mut [V; N],
) -> u64
where U: SmallUInt + Copy + Clone,
V: SmallUInt + Copy + Clone { ... }
fn encrypt_array<U, const N: usize>(
&mut self,
message: &[U; N],
cipher: *mut u8,
) -> u64
where U: SmallUInt + Copy + Clone { ... }
fn encrypt_array_into_vec<U, V, const N: usize>(
&mut self,
message: &[U; N],
cipher: &mut Vec<V>,
) -> u64
where U: SmallUInt + Copy + Clone,
V: SmallUInt + Copy + Clone { ... }
fn encrypt_array_into_array<U, V, const N: usize, const M: usize>(
&mut self,
message: &[U; N],
cipher: &mut [V; M],
) -> u64
where U: SmallUInt + Copy + Clone,
V: SmallUInt + Copy + Clone { ... }
fn decrypt_into_vec<U>(
&mut self,
cipher: *const u8,
length_in_bytes: u64,
message: &mut Vec<U>,
) -> u64
where U: SmallUInt + Copy + Clone { ... }
fn decrypt_into_string(
&mut self,
cipher: *const u8,
length_in_bytes: u64,
message: &mut String,
) -> u64 { ... }
fn decrypt_vec<U>(&mut self, cipher: &Vec<U>, message: *mut u8) -> u64
where U: SmallUInt + Copy + Clone { ... }
fn decrypt_vec_into_vec<U, V>(
&mut self,
cipher: &Vec<U>,
message: &mut Vec<V>,
) -> u64
where U: SmallUInt + Copy + Clone,
V: SmallUInt + Copy + Clone { ... }
fn decrypt_vec_into_array<U, V, const N: usize>(
&mut self,
cipher: &Vec<U>,
message: &mut [V; N],
) -> u64
where U: SmallUInt + Copy + Clone,
V: SmallUInt + Copy + Clone { ... }
fn decrypt_vec_into_string<U>(
&mut self,
cipher: &Vec<U>,
message: &mut String,
) -> u64
where U: SmallUInt + Copy + Clone { ... }
fn decrypt_array<U, const N: usize>(
&mut self,
cipher: &[U; N],
message: *mut u8,
) -> u64
where U: SmallUInt + Copy + Clone { ... }
fn decrypt_array_into_vec<U, V, const N: usize>(
&mut self,
cipher: &[U; N],
message: &mut Vec<V>,
) -> u64
where U: SmallUInt + Copy + Clone,
V: SmallUInt + Copy + Clone { ... }
fn decrypt_array_into_array<U, V, const N: usize, const M: usize>(
&mut self,
cipher: &[U; N],
message: &mut [V; M],
) -> u64
where U: SmallUInt + Copy + Clone,
V: SmallUInt + Copy + Clone { ... }
fn decrypt_array_into_string<U, const N: usize>(
&mut self,
cipher: &[U; N],
message: &mut String,
) -> u64
where U: SmallUInt + Copy + Clone { ... }
}
Required Methods§
Sourcefn encrypt(
&mut self,
message: *const u8,
length_in_bytes: u64,
cipher: *mut u8,
) -> u64
fn encrypt( &mut self, message: *const u8, length_in_bytes: u64, cipher: *mut u8, ) -> u64
Encrypts the data with the padding defined according to ISO 7816-4 in ECB (Electronic CodeBook) mode.
§Arguments
message
is a pointer to u8 which is*const u8
, and is the plaintext to be encrypted.length_in_bytes
is ofu64
-type, and is the length of the plaintextmessage
in bytes.cipher
is a pointer to u8 which is*mut u8
, and is the ciphertext to be stored.- The size of the memory area which starts at
cipher
and the ciphertext will be stored at is assumed to be enough. - The size of the area for ciphertext should be prepared to be:
(
length_in_bytes
+ 1).next_multiple_of(8) at least whenT
isu64
, and (length_in_bytes
+ 1).next_multiple_of(16) at least whenT
isu128
. So, it is responsible for you to prepare thecipher
area big enough!
§Output
- This method returns the size of ciphertext including padding bits in bytes.
- When
T
isu64
, the output should be at least8
, and will be only any multiple of8
. - When
T
isu128
, the output should be at least16
, and will be only any multiple of16
. - If this method returns
zero
, it means this method failed in encryption.
§Features
- You are not encouraged to use this method in pure Rust programming. Instead, use other safer methods such as encrypt_into().
- This method is useful to use in hybrid programming with C/C++.
- If
length_in_bytes
is0
, it means the message is null string. So, only padding bytes will be encrypted, and stored in the memory area that starts fromcipher
. - The padding bits are composed of the bytes that indicate the length of the padding bits in bytes according to ISO 7816-4 defined in RFC 5652.
- For more information about the padding bits according to ISO 7816-4, Read here.
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, ECB_ISO };
let key = 0x_1234567890ABCDEF_u64;
println!("K =\t{:#016X}", key);
let mut a_des = DES::new_with_key_u64(key);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = [0_u8; 56];
a_des.encrypt(message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
print!("C (16 rounds) =\t");
for c in cipher.clone()
{ print!("{:02X} ", c); }
println!();
let mut txt = String::new();
for c in cipher.clone()
{ write!(txt, "{:02X} ", c); }
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 F4 BE 6B A5 C5 7D F6 5D ");
§For more examples,
click here
Sourcefn encrypt_into_array<U, const N: usize>(
&mut self,
message: *const u8,
length_in_bytes: u64,
cipher: &mut [U; N],
) -> u64
fn encrypt_into_array<U, const N: usize>( &mut self, message: *const u8, length_in_bytes: u64, cipher: &mut [U; N], ) -> u64
Encrypts the data with the padding defined according to ISO 7816-4
in ECB (Electronic CodeBook) mode, and stores the encrypted data
in array [U; N]
.
§Arguments
message
is a pointer to u8 which is*const u8
, and is the plaintext to be encrypted.length_in_bytes
is ofu64
-type, and is the length of the plaintextmessage
in bytes.cipher
is an array[U; N]
object, and is the ciphertext to be stored.
§Output
- This method returns the size of ciphertext including padding bits in bytes.
- When
T
isu64
, the output should be at least8
, and will be only any multiple of8
. - When
T
isu128
, the output should be at least16
, and will be only any multiple of16
. - If this method returns
zero
, it means this method failed in encryption.
§Features
- This method is useful to use in hybrid programming with C/C++.
- If
length_in_bytes
is0
, it means the message is null data. So, only padding bytes will be encrypted, and stored in the array[U; N]
objectcipher
. - If
U::size_in_bytes() * N
is less thanlength_in_bytes
’s next multiple of 8, this method does not perform encryption and returnszero
. - If
U::size_in_bytes() * N
is equal tolength_in_bytes
’s next multiple of 8, this method performs encryption, fills the arraycipher
with the encrypted ciphertext, and returns the size of the ciphertext including padding bits in bytes. - If
U::size_in_bytes() * N
is greater thanlength_in_bytes
’s next multiple of 8, this method performs encryption, fills the arraycipher
with the encrypted ciphertext, and then fills the rest of the elements of the arraycipher
with zeros, and returns the size of the ciphertext including padding bits in bytes. - The padding bits are composed of the bytes that indicate the length of the padding bits in bytes according to ISO 7816-4 defined in RFC 5652.
- For more information about the padding bits according to ISO 7816-4, Read here.
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, ECB_ISO };
let key = 0x_1234567890ABCDEF_u64;
println!("K =\t{:#016X}", key);
let mut a_des = DES::new_with_key_u64(key);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = [0_u8; 56];
a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
print!("C (16 rounds) =\t");
for c in cipher.clone()
{ print!("{:02X} ", c); }
println!();
let mut txt = String::new();
for c in cipher.clone()
{ write!(txt, "{:02X} ", c); }
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 F4 BE 6B A5 C5 7D F6 5D ");
§For more examples,
click here
Sourcefn decrypt(
&mut self,
cipher: *const u8,
length_in_bytes: u64,
message: *mut u8,
) -> u64
fn decrypt( &mut self, cipher: *const u8, length_in_bytes: u64, message: *mut u8, ) -> u64
Decrypts the data with the padding defined according to ISO 7816-4 in ECB (Electronic CodeBook) mode.
§Arguments
cipher
is a pointer to u8 which is*const u8
, and is the ciphertext to be decrypted.length_in_bytes
is ofu64
-type, and is the length of the ciphertextcipher
in bytes.message
is a pointer to u8 which is*mut u8
, and is the plaintext to be stored.- The size of the memory area which starts at
message
and the plaintext will be stored at is assumed to be enough. - The size of the area for plaintext should be prepared to be:
length_in_bytes
- 1. So, it is responsible for you to prepare themessage
area big enough!
§Output
- This method returns the size of plaintext in bytes.
- If this method returns
zero
, andlength_in_bytes
is greater than8
forT
=u64
or16
forT
=u128
, it means that this method failed in decryption. - If this method returns
zero
, andlength_in_bytes
is8
forT
=u64
or16
forT
=u128
, it means either that this method failed in decryption or that the original plaintext is empty data. Then, you will have to check whether or not it failed by the methodis_successful()
oris_failed()
.
§Features
- You are not encouraged to use this method in pure Rust programming. Instead, use other safer methods such as decrypt_into().
- This method is useful to use in hybrid programming with C/C++.
- When
T
isu64
,length_in_bytes
can be only any multiple of8
. - When
T
isu128
,length_in_bytes
can be only any multiple of16
. - The padding bits are composed of the bytes that indicate the length of the padding bits in bytes according to ISO 7816-4 defined in RFC 5652.
- For more information about the padding bits according to ISO 7816-4, Read here.
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, ECB_ISO };
let key = 0x_1234567890ABCDEF_u64;
println!("K =\t{:#016X}", key);
let mut a_des = DES::new_with_key_u64(key);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
print!("C (16 rounds) =\t");
for c in cipher.clone()
{ print!("{:02X} ", c); }
println!();
let mut txt = String::new();
for c in cipher.clone()
{ write!(txt, "{:02X} ", c); }
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 F4 BE 6B A5 C5 7D F6 5D ");
let mut recovered = vec![0; 55];
a_des.decrypt(cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
print!("Ba (16 rounds) =\t");
for b in recovered.clone()
{ print!("{:02X} ", b); }
println!();
let mut txt = String::new();
for c in recovered.clone()
{ write!(txt, "{:02X} ", c); }
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 ");
let mut converted = String::new();
unsafe { converted.as_mut_vec() }.append(&mut recovered);
println!("Bb (16 rounds) =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");
assert_eq!(converted, message);
§For more examples,
click here
Sourcefn decrypt_into_array<U, const N: usize>(
&mut self,
cipher: *const u8,
length_in_bytes: u64,
message: &mut [U; N],
) -> u64
fn decrypt_into_array<U, const N: usize>( &mut self, cipher: *const u8, length_in_bytes: u64, message: &mut [U; N], ) -> u64
Decrypts the data with the padding defined according to ISO 7816-4
in ECB (Electronic CodeBook) mode, and stores the encrypted data
in array [U; N]
.
§Arguments
cipher
is a pointer to u8 which is*const u8
, and is the ciphertext to be encrypted.length_in_bytes
is ofu64
-type, and is the length of the ciphertextmessage
in bytes.message
is an array[U; N]
object, and is the plaintext to be stored.
§Output
- This method returns the size of plaintext in bytes.
- If this method returns
zero
, andlength_in_bytes
is greater than8
forT
=u64
or16
forT
=u128
, it means that this method failed in decryption. - If this method returns
zero
, andlength_in_bytes
is8
forT
=u64
or16
forT
=u128
, it means either that this method failed in decryption or that the original plaintext is empty data. Then, you will have to check whether or not it failed by the methodis_successful()
oris_failed()
.
§Features
- This method is useful to use in hybrid programming with C/C++.
- When
T
isu64
,length_in_bytes
can be only any multiple of8
. - When
T
isu128
,length_in_bytes
can be only any multiple of16
. - If
U::size_in_bytes() * N
is less thanlength_in_bytes
- 1, this method does not perform decryption and returnszero
. - If
U::size_in_bytes() * N
is greater than or equal tolength_in_bytes
- 1, this method performs decryption, fills the arraymessage
with the derypted plaintext, and then fills the rest of the elements of the arraymessage
with zeros if any, and returns the size of the plaintext. - The padding bits are composed of the bytes that indicate the length of the padding bits in bytes according to ISO 7816-4 defined in RFC 5652.
- For more information about the padding bits according to ISO 7816-4, Read here.
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, ECB_ISO };
let key = 0x_1234567890ABCDEF_u64;
println!("K =\t{:#016X}", key);
let mut a_des = DES::new_with_key_u64(key);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
print!("C (16 rounds) =\t");
for c in cipher.clone()
{ print!("{:02X} ", c); }
println!();
let mut txt = String::new();
for c in cipher.clone()
{ write!(txt, "{:02X} ", c); }
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 F4 BE 6B A5 C5 7D F6 5D ");
let mut recovered = [0u8; 56];
let len = a_des.decrypt_into_array(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
print!("Ba (16 rounds) =\t");
for b in recovered.clone()
{ print!("{:02X} ", b); }
println!();
let mut txt = String::new();
for c in recovered.clone()
{ write!(txt, "{:02X} ", c); }
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 ");
let mut converted = String::new();
unsafe { converted.as_mut_vec() }.write(&recovered);
unsafe { converted.as_mut_vec() }.truncate(len as usize);
println!("Bb (16 rounds) =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");
assert_eq!(converted, message);
§For more examples,
click here
Provided Methods§
Sourcefn encrypt_into_vec<U>(
&mut self,
message: *const u8,
length_in_bytes: u64,
cipher: &mut Vec<U>,
) -> u64
fn encrypt_into_vec<U>( &mut self, message: *const u8, length_in_bytes: u64, cipher: &mut Vec<U>, ) -> u64
Encrypts the data with the padding defined according to ISO 7816-4
in ECB (Electronic CodeBook) mode, and stores the encrypted data
in Vec<U>
.
§Arguments
message
is a pointer to u8 which is*const u8
, and is the plaintext to be encrypted.length_in_bytes
is ofu64
-type, and is the length of the plaintextmessage
in bytes.cipher
is aVec<U>
object, and is the ciphertext to be stored.
§Output
- This method returns the size of ciphertext including padding bits in bytes.
- When
T
isu64
, the output should be at least8
, and will be only any multiple of8
. - When
T
isu128
, the output should be at least16
, and will be only any multiple of16
. - If this method returns
zero
, it means this method failed in encryption.
§Features
- This method is useful to use in hybrid programming with C/C++.
- If
length_in_bytes
is0
, it means the message is null string. So, only padding bytes will be encrypted, and stored in theVec<U>
objectcipher
. - The padding bits are composed of the bytes that indicate the length of the padding bits in bytes according to ISO 7816-4 defined in RFC 5652.
- For more information about the padding bits according to ISO 7816-4, Read here.
- You don’t have to worry about whether or not the size of the memory area where the ciphertext will be stored is enough.
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, ECB_ISO };
let key = 0x_1234567890ABCDEF_u64;
println!("K =\t{:#016X}", key);
let mut a_des = DES::new_with_key_u64(key);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
print!("C (16 rounds) =\t");
for c in cipher.clone()
{ print!("{:02X} ", c); }
println!();
let mut txt = String::new();
for c in cipher.clone()
{ write!(txt, "{:02X} ", c); }
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 F4 BE 6B A5 C5 7D F6 5D ");
§For more examples,
click here
Examples found in repository?
227fn des_encrypt_with_padding_iso_ecb_into_vec()
228{
229 println!("des_encrypt_with_padding_iso_ecb_into_vec");
230 use std::io::Write;
231 use std::fmt::Write as _;
232 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
233
234 // Normal case
235 let key = 0x_1234567890ABCDEF_u64;
236 println!("K =\t{:#016X}", key);
237 let mut a_des = DES::new_with_key_u64(key);
238
239 let message = "In the beginning God created the heavens and the earth.";
240 println!("M =\t{}", message);
241 let mut cipher = Vec::<u8>::new();
242 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
243 print!("C (16 rounds) =\t");
244 for c in cipher.clone()
245 { print!("{:02X} ", c); }
246 println!();
247 let mut txt = String::new();
248 for c in cipher.clone()
249 { write!(txt, "{:02X} ", c); }
250 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 F4 BE 6B A5 C5 7D F6 5D ");
251 println!();
252
253 // Expanded case for 128 rounds
254 let key = 0x_1234567890ABCDEF_u64;
255 println!("K =\t{:#016X}", key);
256 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
257
258 let message = "In the beginning God created the heavens and the earth.";
259 println!("M =\t{}", message);
260 let mut cipher = Vec::<u8>::new();
261 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
262 print!("C (128 rounds) =\t");
263 for c in cipher.clone()
264 { print!("{:02X} ", c); }
265 println!();
266 let mut txt = String::new();
267 for c in cipher.clone()
268 { write!(txt, "{:02X} ", c); }
269 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
270 println!();
271
272 // Expanded case for 0 rounds which means that key is meaningless
273 let key1 = 0x_1234567890ABCDEF_u64;
274 let key2 = 0_u64;
275 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
276 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
277 println!("K =\t{:#016X}", key);
278
279 let message = "In the beginning God created the heavens and the earth.";
280 println!("M =\t{}", message);
281 let mut cipher1 = Vec::<u8>::new();
282 let mut cipher2 = Vec::<u8>::new();
283 c_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher1);
284 d_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher2);
285 print!("C (0 rounds) =\t");
286 for c in cipher1.clone()
287 { print!("{:02X} ", c); }
288 println!();
289 let mut txt = String::new();
290 for c in cipher1.clone()
291 { write!(txt, "{:02X} ", c); }
292 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
293 print!("D (0 rounds) =\t");
294 for c in cipher2.clone()
295 { print!("{:02X} ", c); }
296 println!();
297 let mut txt = String::new();
298 for c in cipher2.clone()
299 { write!(txt, "{:02X} ", c); }
300 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
301 println!();
302
303 // Normal case for the message of 0 bytes
304 let key = 0x_1234567890ABCDEF_u64;
305 println!("K =\t{:#016X}", key);
306 let mut a_des = DES::new_with_key_u64(key);
307
308 let message = "";
309 println!("M =\t{}", message);
310 let mut cipher = Vec::<u8>::new();
311 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
312 print!("C =\t");
313 for c in cipher.clone()
314 { print!("{:02X} ", c); }
315 println!();
316 let mut txt = String::new();
317 for c in cipher.clone()
318 { write!(txt, "{:02X} ", c); }
319 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
320 println!();
321
322 // Normal case for the message shorter than 8 bytes
323 let key = 0x_1234567890ABCDEF_u64;
324 println!("K =\t{:#016X}", key);
325 let mut a_des = DES::new_with_key_u64(key);
326
327 let message = "7 bytes";
328 println!("M =\t{}", message);
329 let mut cipher = Vec::<u8>::new();
330 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
331 print!("C =\t");
332 for c in cipher.clone()
333 { print!("{:02X} ", c); }
334 println!();
335 let mut txt = String::new();
336 for c in cipher.clone()
337 { write!(txt, "{:02X} ", c); }
338 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
339 println!();
340
341 // Normal case for the message of 8 bytes
342 let key = 0x_1234567890ABCDEF_u64;
343 println!("K =\t{:#016X}", key);
344 let mut a_des = DES::new_with_key_u64(key);
345
346 let message = "I am OK.";
347 println!("M =\t{}", message);
348 let mut cipher = Vec::<u8>::new();
349 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
350 print!("C =\t");
351 for c in cipher.clone()
352 { print!("{:02X} ", c); }
353 println!();
354 let mut txt = String::new();
355 for c in cipher.clone()
356 { write!(txt, "{:02X} ", c); }
357 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
358 println!();
359
360 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
361 let key = 0x_1234567890ABCDEF_u64;
362 println!("K =\t{:#016X}", key);
363 let mut a_des = DES::new_with_key_u64(key);
364
365 let message = "PARK Youngho";
366 println!("M =\t{}", message);
367 let mut cipher = Vec::<u8>::new();
368 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
369 print!("C =\t");
370 for c in cipher.clone()
371 { print!("{:02X} ", c); }
372 println!();
373 let mut txt = String::new();
374 for c in cipher.clone()
375 { write!(txt, "{:02X} ", c); }
376 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
377 println!();
378
379 // Normal case for the message of 16 bytes
380 let key = 0x_1234567890ABCDEF_u64;
381 println!("K =\t{:#016X}", key);
382 let mut a_des = DES::new_with_key_u64(key);
383
384 let message = "고맙습니다.";
385 println!("M =\t{}", message);
386 let mut cipher = Vec::<u8>::new();
387 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
388 print!("C =\t");
389 for c in cipher.clone()
390 { print!("{:02X} ", c); }
391 println!();
392 let mut txt = String::new();
393 for c in cipher.clone()
394 { write!(txt, "{:02X} ", c); }
395 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
396 println!("-------------------------------");
397}
398
399fn des_encrypt_with_padding_iso_ecb_into_array()
400{
401 println!("des_encrypt_with_padding_iso_ecb_into_array");
402 use std::io::Write;
403 use std::fmt::Write as _;
404 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
405
406 // Normal case
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 F4 BE 6B A5 C5 7D F6 5D ");
423 println!();
424
425 // Expanded case for 128 rounds
426 let key = 0x_1234567890ABCDEF_u64;
427 println!("K =\t{:#016X}", key);
428 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
429
430 let message = "In the beginning God created the heavens and the earth.";
431 println!("M =\t{}", message);
432 let mut cipher = [0_u8; 56];
433 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
434 print!("C (128 rounds) =\t");
435 for c in cipher.clone()
436 { print!("{:02X} ", c); }
437 println!();
438 let mut txt = String::new();
439 for c in cipher.clone()
440 { write!(txt, "{:02X} ", c); }
441 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
442 println!();
443
444 // Expanded case for 0 rounds which means that key is meaningless
445 let key1 = 0x_1234567890ABCDEF_u64;
446 let key2 = 0_u64;
447 println!("K =\t{:#016X}", key);
448 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
449 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
450
451 let message = "In the beginning God created the heavens and the earth.";
452 println!("M =\t{}", message);
453 let mut cipher1 = [0_u8; 56];
454 let mut cipher2 = [0_u8; 56];
455 c_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher1);
456 d_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher2);
457 print!("C (0 rounds) =\t");
458 for c in cipher1.clone()
459 { print!("{:02X} ", c); }
460 println!();
461 let mut txt = String::new();
462 for c in cipher1.clone()
463 { write!(txt, "{:02X} ", c); }
464 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
465 print!("D (0 rounds) =\t");
466 for c in cipher2.clone()
467 { print!("{:02X} ", c); }
468 println!();
469 let mut txt = String::new();
470 for c in cipher2.clone()
471 { write!(txt, "{:02X} ", c); }
472 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
473 println!();
474
475 // Normal case for the message of 0 bytes
476 let key = 0x_1234567890ABCDEF_u64;
477 println!("K =\t{:#016X}", key);
478 let mut a_des = DES::new_with_key_u64(key);
479
480 let message = "";
481 println!("M =\t{}", message);
482 let mut cipher = [0_u8; 8];
483 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
484 print!("C =\t");
485 for c in cipher.clone()
486 { print!("{:02X} ", c); }
487 println!();
488 let mut txt = String::new();
489 for c in cipher.clone()
490 { write!(txt, "{:02X} ", c); }
491 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
492 println!();
493
494 // Normal case for the message shorter than 8 bytes
495 let key = 0x_1234567890ABCDEF_u64;
496 println!("K =\t{:#016X}", key);
497 let mut a_des = DES::new_with_key_u64(key);
498
499 let message = "7 bytes";
500 println!("M =\t{}", message);
501 let mut cipher = [0_u8; 8];
502 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
503 print!("C =\t");
504 for c in cipher.clone()
505 { print!("{:02X} ", c); }
506 println!();
507 let mut txt = String::new();
508 for c in cipher.clone()
509 { write!(txt, "{:02X} ", c); }
510 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
511 println!();
512
513 // Normal case for the message of 8 bytes
514 let key = 0x_1234567890ABCDEF_u64;
515 println!("K =\t{:#016X}", key);
516 let mut a_des = DES::new_with_key_u64(key);
517
518 let message = "I am OK.";
519 println!("M =\t{}", message);
520 let mut cipher = [0_u8; 16];
521 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
522 print!("C =\t");
523 for c in cipher.clone()
524 { print!("{:02X} ", c); }
525 println!();
526 let mut txt = String::new();
527 for c in cipher.clone()
528 { write!(txt, "{:02X} ", c); }
529 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
530 println!();
531
532 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
533 let key = 0x_1234567890ABCDEF_u64;
534 println!("K =\t{:#016X}", key);
535 let mut a_des = DES::new_with_key_u64(key);
536
537 let message = "PARK Youngho";
538 println!("M =\t{}", message);
539 let mut cipher = [0_u8; 16];
540 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
541 print!("C =\t");
542 for c in cipher.clone()
543 { print!("{:02X} ", c); }
544 println!();
545 let mut txt = String::new();
546 for c in cipher.clone()
547 { write!(txt, "{:02X} ", c); }
548 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
549 println!();
550
551
552 // Normal case for the message of 16 bytes
553 let key = 0x_1234567890ABCDEF_u64;
554 println!("K =\t{:#016X}", key);
555 let mut a_des = DES::new_with_key_u64(key);
556
557 let message = "고맙습니다.";
558 println!("M =\t{}", message);
559 let mut cipher = [0_u8; 24];
560 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
561 print!("C =\t");
562 for c in cipher.clone()
563 { print!("{:02X} ", c); }
564 println!();
565 let mut txt = String::new();
566 for c in cipher.clone()
567 { write!(txt, "{:02X} ", c); }
568 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
569 println!("-------------------------------");
570}
571
572fn des_encrypt_str_with_padding_iso_ecb()
573{
574 println!("des_encrypt_str_with_padding_iso_ecb");
575 use std::io::Write;
576 use std::fmt::Write as _;
577 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
578
579 // Normal case
580 let key = 0x_1234567890ABCDEF_u64;
581 println!("K =\t{:#016X}", key);
582 let mut a_des = DES::new_with_key_u64(key);
583
584 let message = "In the beginning God created the heavens and the earth.";
585 println!("M =\t{}", message);
586 let mut cipher = [0_u8; 56];
587 a_des.encrypt_str(&message, cipher.as_mut_ptr());
588 print!("C (16 rounds) =\t");
589 for c in cipher.clone()
590 { print!("{:02X} ", c); }
591 println!();
592 let mut txt = String::new();
593 for c in cipher.clone()
594 { write!(txt, "{:02X} ", c); }
595 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 F4 BE 6B A5 C5 7D F6 5D ");
596 println!();
597
598 // Expanded case for 128 rounds
599 let key = 0x_1234567890ABCDEF_u64;
600 println!("K =\t{:#016X}", key);
601 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
602
603 let message = "In the beginning God created the heavens and the earth.";
604 println!("M =\t{}", message);
605 let mut cipher = [0_u8; 56];
606 a_des.encrypt_str(&message, cipher.as_mut_ptr());
607 print!("C (128 rounds) =\t");
608 for c in cipher.clone()
609 { print!("{:02X} ", c); }
610 println!();
611 let mut txt = String::new();
612 for c in cipher.clone()
613 { write!(txt, "{:02X} ", c); }
614 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
615 println!();
616
617 // Expanded case for 0 rounds which means that key is meaningless
618 let key1 = 0x_1234567890ABCDEF_u64;
619 let key2 = 0_u64;
620 println!("K =\t{:#016X}", key);
621 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
622 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
623
624 let message = "In the beginning God created the heavens and the earth.";
625 println!("M =\t{}", message);
626 let mut cipher1 = [0_u8; 56];
627 let mut cipher2 = [0_u8; 56];
628 c_des.encrypt_str(&message, cipher1.as_mut_ptr());
629 d_des.encrypt_str(&message, cipher2.as_mut_ptr());
630 print!("C (0 rounds) =\t");
631 for c in cipher1.clone()
632 { print!("{:02X} ", c); }
633 println!();
634 let mut txt = String::new();
635 for c in cipher1.clone()
636 { write!(txt, "{:02X} ", c); }
637 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
638 print!("D (0 rounds) =\t");
639 for c in cipher2.clone()
640 { print!("{:02X} ", c); }
641 println!();
642 let mut txt = String::new();
643 for c in cipher2.clone()
644 { write!(txt, "{:02X} ", c); }
645 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
646 println!();
647
648 // Normal case for the message of 0 bytes
649 let key = 0x_1234567890ABCDEF_u64;
650 println!("K =\t{:#016X}", key);
651 let mut a_des = DES::new_with_key_u64(key);
652
653 let message = "";
654 println!("M =\t{}", message);
655 let mut cipher = [0_u8; 8];
656 a_des.encrypt_str(&message, cipher.as_mut_ptr());
657 print!("C =\t");
658 for c in cipher.clone()
659 { print!("{:02X} ", c); }
660 println!();
661 let mut txt = String::new();
662 for c in cipher.clone()
663 { write!(txt, "{:02X} ", c); }
664 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
665 println!();
666
667 // Normal case for the message shorter than 8 bytes
668 let key = 0x_1234567890ABCDEF_u64;
669 println!("K =\t{:#016X}", key);
670 let mut a_des = DES::new_with_key_u64(key);
671
672 let message = "7 bytes";
673 println!("M =\t{}", message);
674 let mut cipher = [0_u8; 8];
675 a_des.encrypt_str(&message, cipher.as_mut_ptr());
676 print!("C =\t");
677 for c in cipher.clone()
678 { print!("{:02X} ", c); }
679 println!();
680 let mut txt = String::new();
681 for c in cipher.clone()
682 { write!(txt, "{:02X} ", c); }
683 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
684 println!();
685
686 // Normal case for the message of 8 bytes
687 let key = 0x_1234567890ABCDEF_u64;
688 println!("K =\t{:#016X}", key);
689 let mut a_des = DES::new_with_key_u64(key);
690
691 let message = "I am OK.";
692 println!("M =\t{}", message);
693 let mut cipher = [0_u8; 16];
694 a_des.encrypt_str(&message, cipher.as_mut_ptr());
695 print!("C =\t");
696 for c in cipher.clone()
697 { print!("{:02X} ", c); }
698 println!();
699 let mut txt = String::new();
700 for c in cipher.clone()
701 { write!(txt, "{:02X} ", c); }
702 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
703 println!();
704
705 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
706 let key = 0x_1234567890ABCDEF_u64;
707 println!("K =\t{:#016X}", key);
708 let mut a_des = DES::new_with_key_u64(key);
709
710 let message = "PARK Youngho";
711 println!("M =\t{}", message);
712 let mut cipher = [0_u8; 16];
713 a_des.encrypt_str(&message, cipher.as_mut_ptr());
714 print!("C =\t");
715 for c in cipher.clone()
716 { print!("{:02X} ", c); }
717 println!();
718 let mut txt = String::new();
719 for c in cipher.clone()
720 { write!(txt, "{:02X} ", c); }
721 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
722 println!();
723
724
725 // Normal case for the message of 16 bytes
726 let key = 0x_1234567890ABCDEF_u64;
727 println!("K =\t{:#016X}", key);
728 let mut a_des = DES::new_with_key_u64(key);
729
730 let message = "고맙습니다.";
731 println!("M =\t{}", message);
732 let mut cipher = [0_u8; 24];
733 a_des.encrypt_str(&message, cipher.as_mut_ptr());
734 print!("C =\t");
735 for c in cipher.clone()
736 { print!("{:02X} ", c); }
737 println!();
738 let mut txt = String::new();
739 for c in cipher.clone()
740 { write!(txt, "{:02X} ", c); }
741 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
742 println!("-------------------------------");
743}
744
745fn des_encrypt_str_with_padding_iso_ecb_into_vec()
746{
747 println!("des_encrypt_str_with_padding_iso_ecb_into_vec");
748 use std::io::Write;
749 use std::fmt::Write as _;
750 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
751
752 // Normal case
753 let key = 0x_1234567890ABCDEF_u64;
754 println!("K =\t{:#016X}", key);
755 let mut a_des = DES::new_with_key_u64(key);
756
757 let message = "In the beginning God created the heavens and the earth.";
758 println!("M =\t{}", message);
759 let mut cipher = Vec::<u8>::new();
760 a_des.encrypt_str_into_vec(&message, &mut cipher);
761 print!("C (16 rounds) =\t");
762 for c in cipher.clone()
763 { print!("{:02X} ", c); }
764 println!();
765 let mut txt = String::new();
766 for c in cipher.clone()
767 { write!(txt, "{:02X} ", c); }
768 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 F4 BE 6B A5 C5 7D F6 5D ");
769 println!();
770
771 // Expanded case for 128 rounds
772 let key = 0x_1234567890ABCDEF_u64;
773 println!("K =\t{:#016X}", key);
774 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
775
776 let message = "In the beginning God created the heavens and the earth.";
777 println!("M =\t{}", message);
778 let mut cipher = Vec::<u8>::new();
779 a_des.encrypt_str_into_vec(&message, &mut cipher);
780 print!("C (128 rounds) =\t");
781 for c in cipher.clone()
782 { print!("{:02X} ", c); }
783 println!();
784 let mut txt = String::new();
785 for c in cipher.clone()
786 { write!(txt, "{:02X} ", c); }
787 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
788 println!();
789
790 // Expanded case for 0 rounds which means that key is meaningless
791 let key1 = 0x_1234567890ABCDEF_u64;
792 let key2 = 0_u64;
793 println!("K =\t{:#016X}", key);
794 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
795 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
796
797 let message = "In the beginning God created the heavens and the earth.";
798 println!("M =\t{}", message);
799 let mut cipher1 = Vec::<u8>::new();
800 let mut cipher2 = Vec::<u8>::new();
801 c_des.encrypt_str_into_vec(&message, &mut cipher1);
802 d_des.encrypt_str_into_vec(&message, &mut cipher2);
803 print!("C (0 rounds) =\t");
804 for c in cipher1.clone()
805 { print!("{:02X} ", c); }
806 println!();
807 let mut txt = String::new();
808 for c in cipher1.clone()
809 { write!(txt, "{:02X} ", c); }
810 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
811 print!("D (0 rounds) =\t");
812 for c in cipher2.clone()
813 { print!("{:02X} ", c); }
814 println!();
815 let mut txt = String::new();
816 for c in cipher2.clone()
817 { write!(txt, "{:02X} ", c); }
818 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
819 println!();
820
821 // Normal case for the message of 0 bytes
822 let key = 0x_1234567890ABCDEF_u64;
823 println!("K =\t{:#016X}", key);
824 let mut a_des = DES::new_with_key_u64(key);
825
826 let message = "";
827 println!("M =\t{}", message);
828 let mut cipher = Vec::<u8>::new();
829 a_des.encrypt_str_into_vec(&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, "E2 22 32 FE 87 E8 0A 93 ");
838 println!();
839
840 // Normal case for the message shorter than 8 bytes
841 let key = 0x_1234567890ABCDEF_u64;
842 println!("K =\t{:#016X}", key);
843 let mut a_des = DES::new_with_key_u64(key);
844
845 let message = "7 bytes";
846 println!("M =\t{}", message);
847 let mut cipher = Vec::<u8>::new();
848 a_des.encrypt_str_into_vec(&message, &mut cipher);
849 print!("C =\t");
850 for c in cipher.clone()
851 { print!("{:02X} ", c); }
852 println!();
853 let mut txt = String::new();
854 for c in cipher.clone()
855 { write!(txt, "{:02X} ", c); }
856 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
857 println!();
858
859 // Normal case for the message of 8 bytes
860 let key = 0x_1234567890ABCDEF_u64;
861 println!("K =\t{:#016X}", key);
862 let mut a_des = DES::new_with_key_u64(key);
863
864 let message = "I am OK.";
865 println!("M =\t{}", message);
866 let mut cipher = Vec::<u8>::new();
867 a_des.encrypt_str_into_vec(&message, &mut cipher);
868 print!("C =\t");
869 for c in cipher.clone()
870 { print!("{:02X} ", c); }
871 println!();
872 let mut txt = String::new();
873 for c in cipher.clone()
874 { write!(txt, "{:02X} ", c); }
875 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
876 println!();
877
878 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
879 let key = 0x_1234567890ABCDEF_u64;
880 println!("K =\t{:#016X}", key);
881 let mut a_des = DES::new_with_key_u64(key);
882
883 let message = "PARK Youngho";
884 println!("M =\t{}", message);
885 let mut cipher = Vec::<u8>::new();
886 a_des.encrypt_str_into_vec(&message, &mut cipher);
887 print!("C =\t");
888 for c in cipher.clone()
889 { print!("{:02X} ", c); }
890 println!();
891 let mut txt = String::new();
892 for c in cipher.clone()
893 { write!(txt, "{:02X} ", c); }
894 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
895 println!();
896
897
898 // Normal case for the message of 16 bytes
899 let key = 0x_1234567890ABCDEF_u64;
900 println!("K =\t{:#016X}", key);
901 let mut a_des = DES::new_with_key_u64(key);
902
903 let message = "고맙습니다.";
904 println!("M =\t{}", message);
905 let mut cipher = Vec::<u8>::new();
906 a_des.encrypt_str_into_vec(&message, &mut cipher);
907 print!("C =\t");
908 for c in cipher.clone()
909 { print!("{:02X} ", c); }
910 println!();
911 let mut txt = String::new();
912 for c in cipher.clone()
913 { write!(txt, "{:02X} ", c); }
914 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
915 println!("-------------------------------");
916}
917
918fn des_encrypt_str_with_padding_iso_ecb_into_array()
919{
920 println!("des_encrypt_str_with_padding_iso_ecb_into_array");
921 use std::io::Write;
922 use std::fmt::Write as _;
923 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
924
925 // Normal case
926 let key = 0x_1234567890ABCDEF_u64;
927 println!("K =\t{:#016X}", key);
928 let mut a_des = DES::new_with_key_u64(key);
929
930 let message = "In the beginning God created the heavens and the earth.";
931 println!("M =\t{}", message);
932 let mut cipher = [0_u8; 56];
933 a_des.encrypt_str_into_array(&message, &mut cipher);
934 print!("C (16 rounds) =\t");
935 for c in cipher.clone()
936 { print!("{:02X} ", c); }
937 println!();
938 let mut txt = String::new();
939 for c in cipher.clone()
940 { write!(txt, "{:02X} ", c); }
941 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 F4 BE 6B A5 C5 7D F6 5D ");
942 println!();
943
944 // Expanded case for 128 rounds
945 let key = 0x_1234567890ABCDEF_u64;
946 println!("K =\t{:#016X}", key);
947 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
948
949 let message = "In the beginning God created the heavens and the earth.";
950 println!("M =\t{}", message);
951 let mut cipher = [0_u8; 56];
952 a_des.encrypt_str_into_array(&message, &mut cipher);
953 print!("C (128 rounds) =\t");
954 for c in cipher.clone()
955 { print!("{:02X} ", c); }
956 println!();
957 let mut txt = String::new();
958 for c in cipher.clone()
959 { write!(txt, "{:02X} ", c); }
960 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
961 println!();
962
963 // Expanded case for 0 rounds which means that key is meaningless
964 let key1 = 0x_1234567890ABCDEF_u64;
965 let key2 = 0_u64;
966 println!("K =\t{:#016X}", key);
967 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
968 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
969
970 let message = "In the beginning God created the heavens and the earth.";
971 println!("M =\t{}", message);
972 let mut cipher1 = [0_u8; 56];
973 let mut cipher2 = [0_u8; 56];
974 c_des.encrypt_str_into_array(&message, &mut cipher1);
975 d_des.encrypt_str_into_array(&message, &mut cipher2);
976 print!("C (0 rounds) =\t");
977 for c in cipher1.clone()
978 { print!("{:02X} ", c); }
979 println!();
980 let mut txt = String::new();
981 for c in cipher1.clone()
982 { write!(txt, "{:02X} ", c); }
983 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
984 print!("D (0 rounds) =\t");
985 for c in cipher2.clone()
986 { print!("{:02X} ", c); }
987 println!();
988 let mut txt = String::new();
989 for c in cipher2.clone()
990 { write!(txt, "{:02X} ", c); }
991 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
992 println!();
993
994 // Normal case for the message of 0 bytes
995 let key = 0x_1234567890ABCDEF_u64;
996 println!("K =\t{:#016X}", key);
997 let mut a_des = DES::new_with_key_u64(key);
998
999 let message = "";
1000 println!("M =\t{}", message);
1001 let mut cipher = [0_u8; 8];
1002 a_des.encrypt_str_into_array(&message, &mut cipher);
1003 print!("C =\t");
1004 for c in cipher.clone()
1005 { print!("{:02X} ", c); }
1006 println!();
1007 let mut txt = String::new();
1008 for c in cipher.clone()
1009 { write!(txt, "{:02X} ", c); }
1010 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
1011 println!();
1012
1013 // Normal case for the message shorter than 8 bytes
1014 let key = 0x_1234567890ABCDEF_u64;
1015 println!("K =\t{:#016X}", key);
1016 let mut a_des = DES::new_with_key_u64(key);
1017
1018 let message = "7 bytes";
1019 println!("M =\t{}", message);
1020 let mut cipher = [0_u8; 8];
1021 a_des.encrypt_str_into_array(&message, &mut cipher);
1022 print!("C =\t");
1023 for c in cipher.clone()
1024 { print!("{:02X} ", c); }
1025 println!();
1026 let mut txt = String::new();
1027 for c in cipher.clone()
1028 { write!(txt, "{:02X} ", c); }
1029 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
1030 println!();
1031
1032 // Normal case for the message of 8 bytes
1033 let key = 0x_1234567890ABCDEF_u64;
1034 println!("K =\t{:#016X}", key);
1035 let mut a_des = DES::new_with_key_u64(key);
1036
1037 let message = "I am OK.";
1038 println!("M =\t{}", message);
1039 let mut cipher = [0_u8; 16];
1040 a_des.encrypt_str_into_array(&message, &mut cipher);
1041 print!("C =\t");
1042 for c in cipher.clone()
1043 { print!("{:02X} ", c); }
1044 println!();
1045 let mut txt = String::new();
1046 for c in cipher.clone()
1047 { write!(txt, "{:02X} ", c); }
1048 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
1049 println!();
1050
1051 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
1052 let key = 0x_1234567890ABCDEF_u64;
1053 println!("K =\t{:#016X}", key);
1054 let mut a_des = DES::new_with_key_u64(key);
1055
1056 let message = "PARK Youngho";
1057 println!("M =\t{}", message);
1058 let mut cipher = [0_u8; 16];
1059 a_des.encrypt_str_into_array(&message, &mut cipher);
1060 print!("C =\t");
1061 for c in cipher.clone()
1062 { print!("{:02X} ", c); }
1063 println!();
1064 let mut txt = String::new();
1065 for c in cipher.clone()
1066 { write!(txt, "{:02X} ", c); }
1067 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
1068 println!();
1069
1070
1071 // Normal case for the message of 16 bytes
1072 let key = 0x_1234567890ABCDEF_u64;
1073 println!("K =\t{:#016X}", key);
1074 let mut a_des = DES::new_with_key_u64(key);
1075
1076 let message = "고맙습니다.";
1077 println!("M =\t{}", message);
1078 let mut cipher = [0_u8; 24];
1079 a_des.encrypt_str_into_array(&message, &mut cipher);
1080 print!("C =\t");
1081 for c in cipher.clone()
1082 { print!("{:02X} ", c); }
1083 println!();
1084 let mut txt = String::new();
1085 for c in cipher.clone()
1086 { write!(txt, "{:02X} ", c); }
1087 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
1088 println!("-------------------------------");
1089}
1090
1091fn des_encrypt_string_with_padding_iso_ecb()
1092{
1093 println!("des_encrypt_string_with_padding_iso_ecb");
1094 use std::io::Write;
1095 use std::fmt::Write as _;
1096 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
1097
1098 // Normal case
1099 let key = 0x_1234567890ABCDEF_u64;
1100 println!("K =\t{:#016X}", key);
1101 let mut a_des = DES::new_with_key_u64(key);
1102
1103 let message = "In the beginning God created the heavens and the earth.".to_string();
1104 println!("M =\t{}", message);
1105 let mut cipher = [0_u8; 56];
1106 a_des.encrypt_string(&message, cipher.as_mut_ptr());
1107 print!("C (16 rounds) =\t");
1108 for c in cipher.clone()
1109 { print!("{:02X} ", c); }
1110 println!();
1111 let mut txt = String::new();
1112 for c in cipher.clone()
1113 { write!(txt, "{:02X} ", c); }
1114 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 F4 BE 6B A5 C5 7D F6 5D ");
1115 println!();
1116
1117 // Expanded case for 128 rounds
1118 let key = 0x_1234567890ABCDEF_u64;
1119 println!("K =\t{:#016X}", key);
1120 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
1121
1122 let message = "In the beginning God created the heavens and the earth.".to_string();
1123 println!("M =\t{}", message);
1124 let mut cipher = [0_u8; 56];
1125 a_des.encrypt_string(&message, cipher.as_mut_ptr());
1126 print!("C (128 rounds) =\t");
1127 for c in cipher.clone()
1128 { print!("{:02X} ", c); }
1129 println!();
1130 let mut txt = String::new();
1131 for c in cipher.clone()
1132 { write!(txt, "{:02X} ", c); }
1133 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
1134 println!();
1135
1136 // Expanded case for 0 rounds which means that key is meaningless
1137 let key1 = 0x_1234567890ABCDEF_u64;
1138 let key2 = 0_u64;
1139 println!("K =\t{:#016X}", key);
1140 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
1141 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
1142
1143 let message = "In the beginning God created the heavens and the earth.".to_string();
1144 println!("M =\t{}", message);
1145 let mut cipher1 = [0_u8; 56];
1146 let mut cipher2 = [0_u8; 56];
1147 c_des.encrypt_string(&message, cipher1.as_mut_ptr());
1148 d_des.encrypt_string(&message, cipher2.as_mut_ptr());
1149 print!("C (0 rounds) =\t");
1150 for c in cipher1.clone()
1151 { print!("{:02X} ", c); }
1152 println!();
1153 let mut txt = String::new();
1154 for c in cipher1.clone()
1155 { write!(txt, "{:02X} ", c); }
1156 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
1157 print!("D (0 rounds) =\t");
1158 for c in cipher2.clone()
1159 { print!("{:02X} ", c); }
1160 println!();
1161 let mut txt = String::new();
1162 for c in cipher2.clone()
1163 { write!(txt, "{:02X} ", c); }
1164 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
1165 println!();
1166
1167 // Normal case for the message of 0 bytes
1168 let key = 0x_1234567890ABCDEF_u64;
1169 println!("K =\t{:#016X}", key);
1170 let mut a_des = DES::new_with_key_u64(key);
1171
1172 let message = "".to_string();
1173 println!("M =\t{}", message);
1174 let mut cipher = [0_u8; 8];
1175 a_des.encrypt_string(&message, cipher.as_mut_ptr());
1176 print!("C =\t");
1177 for c in cipher.clone()
1178 { print!("{:02X} ", c); }
1179 println!();
1180 let mut txt = String::new();
1181 for c in cipher.clone()
1182 { write!(txt, "{:02X} ", c); }
1183 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
1184 println!();
1185
1186 // Normal case for the message shorter than 8 bytes
1187 let key = 0x_1234567890ABCDEF_u64;
1188 println!("K =\t{:#016X}", key);
1189 let mut a_des = DES::new_with_key_u64(key);
1190
1191 let message = "7 bytes".to_string();
1192 println!("M =\t{}", message);
1193 let mut cipher = [0_u8; 8];
1194 a_des.encrypt_string(&message, cipher.as_mut_ptr());
1195 print!("C =\t");
1196 for c in cipher.clone()
1197 { print!("{:02X} ", c); }
1198 println!();
1199 let mut txt = String::new();
1200 for c in cipher.clone()
1201 { write!(txt, "{:02X} ", c); }
1202 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
1203 println!();
1204
1205 // Normal case for the message of 8 bytes
1206 let key = 0x_1234567890ABCDEF_u64;
1207 println!("K =\t{:#016X}", key);
1208 let mut a_des = DES::new_with_key_u64(key);
1209
1210 let message = "I am OK.".to_string();
1211 println!("M =\t{}", message);
1212 let mut cipher = [0_u8; 16];
1213 a_des.encrypt_string(&message, cipher.as_mut_ptr());
1214 print!("C =\t");
1215 for c in cipher.clone()
1216 { print!("{:02X} ", c); }
1217 println!();
1218 let mut txt = String::new();
1219 for c in cipher.clone()
1220 { write!(txt, "{:02X} ", c); }
1221 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
1222 println!();
1223
1224 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
1225 let key = 0x_1234567890ABCDEF_u64;
1226 println!("K =\t{:#016X}", key);
1227 let mut a_des = DES::new_with_key_u64(key);
1228
1229 let message = "PARK Youngho".to_string();
1230 println!("M =\t{}", message);
1231 let mut cipher = [0_u8; 16];
1232 a_des.encrypt_string(&message, cipher.as_mut_ptr());
1233 print!("C =\t");
1234 for c in cipher.clone()
1235 { print!("{:02X} ", c); }
1236 println!();
1237 let mut txt = String::new();
1238 for c in cipher.clone()
1239 { write!(txt, "{:02X} ", c); }
1240 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
1241 println!();
1242
1243
1244 // Normal case for the message of 16 bytes
1245 let key = 0x_1234567890ABCDEF_u64;
1246 println!("K =\t{:#016X}", key);
1247 let mut a_des = DES::new_with_key_u64(key);
1248
1249 let message = "고맙습니다.".to_string();
1250 println!("M =\t{}", message);
1251 let mut cipher = [0_u8; 24];
1252 a_des.encrypt_string(&message, cipher.as_mut_ptr());
1253 print!("C =\t");
1254 for c in cipher.clone()
1255 { print!("{:02X} ", c); }
1256 println!();
1257 let mut txt = String::new();
1258 for c in cipher.clone()
1259 { write!(txt, "{:02X} ", c); }
1260 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
1261 println!("-------------------------------");
1262}
1263
1264fn des_encrypt_string_with_padding_iso_ecb_into_vec()
1265{
1266 println!("des_encrypt_string_with_padding_iso_ecb_into_vec");
1267 use std::io::Write;
1268 use std::fmt::Write as _;
1269 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
1270
1271 // Normal case
1272 let key = 0x_1234567890ABCDEF_u64;
1273 println!("K =\t{:#016X}", key);
1274 let mut a_des = DES::new_with_key_u64(key);
1275
1276 let message = "In the beginning God created the heavens and the earth.".to_string();
1277 println!("M =\t{}", message);
1278 let mut cipher = Vec::<u8>::new();
1279 a_des.encrypt_string_into_vec(&message, &mut cipher);
1280 print!("C (16 rounds) =\t");
1281 for c in cipher.clone()
1282 { print!("{:02X} ", c); }
1283 println!();
1284 let mut txt = String::new();
1285 for c in cipher.clone()
1286 { write!(txt, "{:02X} ", c); }
1287 assert_eq!(txt, "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 F4 BE 6B A5 C5 7D F6 5D ");
1288 println!();
1289
1290 // Expanded case for 128 rounds
1291 let key = 0x_1234567890ABCDEF_u64;
1292 println!("K =\t{:#016X}", key);
1293 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
1294
1295 let message = "In the beginning God created the heavens and the earth.".to_string();
1296 println!("M =\t{}", message);
1297 let mut cipher = Vec::<u8>::new();
1298 a_des.encrypt_string_into_vec(&message, &mut cipher);
1299 print!("C (128 rounds) =\t");
1300 for c in cipher.clone()
1301 { print!("{:02X} ", c); }
1302 println!();
1303 let mut txt = String::new();
1304 for c in cipher.clone()
1305 { write!(txt, "{:02X} ", c); }
1306 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
1307 println!();
1308
1309 // Expanded case for 0 rounds which means that key is meaningless
1310 let key1 = 0x_1234567890ABCDEF_u64;
1311 let key2 = 0_u64;
1312 println!("K =\t{:#016X}", key);
1313 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
1314 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
1315
1316 let message = "In the beginning God created the heavens and the earth.".to_string();
1317 println!("M =\t{}", message);
1318 let mut cipher1 = Vec::<u8>::new();
1319 let mut cipher2 = Vec::<u8>::new();
1320 c_des.encrypt_string_into_vec(&message, &mut cipher1);
1321 d_des.encrypt_string_into_vec(&message, &mut cipher2);
1322 print!("C (0 rounds) =\t");
1323 for c in cipher1.clone()
1324 { print!("{:02X} ", c); }
1325 println!();
1326 let mut txt = String::new();
1327 for c in cipher1.clone()
1328 { write!(txt, "{:02X} ", c); }
1329 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
1330 print!("D (0 rounds) =\t");
1331 for c in cipher2.clone()
1332 { print!("{:02X} ", c); }
1333 println!();
1334 let mut txt = String::new();
1335 for c in cipher2.clone()
1336 { write!(txt, "{:02X} ", c); }
1337 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
1338 println!();
1339
1340 // Normal case for the message of 0 bytes
1341 let key = 0x_1234567890ABCDEF_u64;
1342 println!("K =\t{:#016X}", key);
1343 let mut a_des = DES::new_with_key_u64(key);
1344
1345 let message = "".to_string();
1346 println!("M =\t{}", message);
1347 let mut cipher = Vec::<u8>::new();
1348 a_des.encrypt_string_into_vec(&message, &mut cipher);
1349 print!("C =\t");
1350 for c in cipher.clone()
1351 { print!("{:02X} ", c); }
1352 println!();
1353 let mut txt = String::new();
1354 for c in cipher.clone()
1355 { write!(txt, "{:02X} ", c); }
1356 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
1357 println!();
1358
1359 // Normal case for the message shorter than 8 bytes
1360 let key = 0x_1234567890ABCDEF_u64;
1361 println!("K =\t{:#016X}", key);
1362 let mut a_des = DES::new_with_key_u64(key);
1363
1364 let message = "7 bytes".to_string();
1365 println!("M =\t{}", message);
1366 let mut cipher = Vec::<u8>::new();
1367 a_des.encrypt_string_into_vec(&message, &mut cipher);
1368 print!("C =\t");
1369 for c in cipher.clone()
1370 { print!("{:02X} ", c); }
1371 println!();
1372 let mut txt = String::new();
1373 for c in cipher.clone()
1374 { write!(txt, "{:02X} ", c); }
1375 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
1376 println!();
1377
1378 // Normal case for the message of 8 bytes
1379 let key = 0x_1234567890ABCDEF_u64;
1380 println!("K =\t{:#016X}", key);
1381 let mut a_des = DES::new_with_key_u64(key);
1382
1383 let message = "I am OK.".to_string();
1384 println!("M =\t{}", message);
1385 let mut cipher = Vec::<u8>::new();
1386 a_des.encrypt_string_into_vec(&message, &mut cipher);
1387 print!("C =\t");
1388 for c in cipher.clone()
1389 { print!("{:02X} ", c); }
1390 println!();
1391 let mut txt = String::new();
1392 for c in cipher.clone()
1393 { write!(txt, "{:02X} ", c); }
1394 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
1395 println!();
1396
1397 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
1398 let key = 0x_1234567890ABCDEF_u64;
1399 println!("K =\t{:#016X}", key);
1400 let mut a_des = DES::new_with_key_u64(key);
1401
1402 let message = "PARK Youngho".to_string();
1403 println!("M =\t{}", message);
1404 let mut cipher = Vec::<u8>::new();
1405 a_des.encrypt_string_into_vec(&message, &mut cipher);
1406 print!("C =\t");
1407 for c in cipher.clone()
1408 { print!("{:02X} ", c); }
1409 println!();
1410 let mut txt = String::new();
1411 for c in cipher.clone()
1412 { write!(txt, "{:02X} ", c); }
1413 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
1414 println!();
1415
1416
1417 // Normal case for the message of 16 bytes
1418 let key = 0x_1234567890ABCDEF_u64;
1419 println!("K =\t{:#016X}", key);
1420 let mut a_des = DES::new_with_key_u64(key);
1421
1422 let message = "고맙습니다.".to_string();
1423 println!("M =\t{}", message);
1424 let mut cipher = Vec::<u8>::new();
1425 a_des.encrypt_string_into_vec(&message, &mut cipher);
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, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
1434 println!("-------------------------------");
1435}
1436
1437fn des_encrypt_string_with_padding_iso_ecb_into_array()
1438{
1439 println!("des_encrypt_string_with_padding_iso_ecb_into_array");
1440 use std::io::Write;
1441 use std::fmt::Write as _;
1442 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
1443
1444 // Normal case
1445 let key = 0x_1234567890ABCDEF_u64;
1446 println!("K =\t{:#016X}", key);
1447 let mut a_des = DES::new_with_key_u64(key);
1448
1449 let message = "In the beginning God created the heavens and the earth.".to_string();
1450 println!("M =\t{}", message);
1451 let mut cipher = [0_u8; 56];
1452 a_des.encrypt_string_into_array(&message, &mut cipher);
1453 print!("C (16 rounds) =\t");
1454 for c in cipher.clone()
1455 { print!("{:02X} ", c); }
1456 println!();
1457 let mut txt = String::new();
1458 for c in cipher.clone()
1459 { write!(txt, "{:02X} ", c); }
1460 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 F4 BE 6B A5 C5 7D F6 5D ");
1461 println!();
1462
1463 // Expanded case for 128 rounds
1464 let key = 0x_1234567890ABCDEF_u64;
1465 println!("K =\t{:#016X}", key);
1466 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
1467
1468 let message = "In the beginning God created the heavens and the earth.".to_string();
1469 println!("M =\t{}", message);
1470 let mut cipher = [0_u8; 56];
1471 a_des.encrypt_string_into_array(&message, &mut cipher);
1472 print!("C (128 rounds) =\t");
1473 for c in cipher.clone()
1474 { print!("{:02X} ", c); }
1475 println!();
1476 let mut txt = String::new();
1477 for c in cipher.clone()
1478 { write!(txt, "{:02X} ", c); }
1479 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
1480 println!();
1481
1482 // Expanded case for 0 rounds which means that key is meaningless
1483 let key1 = 0x_1234567890ABCDEF_u64;
1484 let key2 = 0_u64;
1485 println!("K =\t{:#016X}", key);
1486 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
1487 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
1488
1489 let message = "In the beginning God created the heavens and the earth.".to_string();
1490 println!("M =\t{}", message);
1491 let mut cipher1 = [0_u8; 56];
1492 let mut cipher2 = [0_u8; 56];
1493 c_des.encrypt_string_into_array(&message, &mut cipher1);
1494 d_des.encrypt_string_into_array(&message, &mut cipher2);
1495 print!("C (0 rounds) =\t");
1496 for c in cipher1.clone()
1497 { print!("{:02X} ", c); }
1498 println!();
1499 let mut txt = String::new();
1500 for c in cipher1.clone()
1501 { write!(txt, "{:02X} ", c); }
1502 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
1503 print!("D (0 rounds) =\t");
1504 for c in cipher2.clone()
1505 { print!("{:02X} ", c); }
1506 println!();
1507 let mut txt = String::new();
1508 for c in cipher2.clone()
1509 { write!(txt, "{:02X} ", c); }
1510 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
1511 println!();
1512
1513 // Normal case for the message of 0 bytes
1514 let key = 0x_1234567890ABCDEF_u64;
1515 println!("K =\t{:#016X}", key);
1516 let mut a_des = DES::new_with_key_u64(key);
1517
1518 let message = "".to_string();
1519 println!("M =\t{}", message);
1520 let mut cipher = [0_u8; 8];
1521 a_des.encrypt_string_into_array(&message, &mut cipher);
1522 print!("C =\t");
1523 for c in cipher.clone()
1524 { print!("{:02X} ", c); }
1525 println!();
1526 let mut txt = String::new();
1527 for c in cipher.clone()
1528 { write!(txt, "{:02X} ", c); }
1529 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
1530 println!();
1531
1532 // Normal case for the message shorter than 8 bytes
1533 let key = 0x_1234567890ABCDEF_u64;
1534 println!("K =\t{:#016X}", key);
1535 let mut a_des = DES::new_with_key_u64(key);
1536
1537 let message = "7 bytes".to_string();
1538 println!("M =\t{}", message);
1539 let mut cipher = [0_u8; 8];
1540 a_des.encrypt_string_into_array(&message, &mut cipher);
1541 print!("C =\t");
1542 for c in cipher.clone()
1543 { print!("{:02X} ", c); }
1544 println!();
1545 let mut txt = String::new();
1546 for c in cipher.clone()
1547 { write!(txt, "{:02X} ", c); }
1548 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
1549 println!();
1550
1551 // Normal case for the message of 8 bytes
1552 let key = 0x_1234567890ABCDEF_u64;
1553 println!("K =\t{:#016X}", key);
1554 let mut a_des = DES::new_with_key_u64(key);
1555
1556 let message = "I am OK.".to_string();
1557 println!("M =\t{}", message);
1558 let mut cipher = [0_u8; 16];
1559 a_des.encrypt_string_into_array(&message, &mut cipher);
1560 print!("C =\t");
1561 for c in cipher.clone()
1562 { print!("{:02X} ", c); }
1563 println!();
1564 let mut txt = String::new();
1565 for c in cipher.clone()
1566 { write!(txt, "{:02X} ", c); }
1567 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
1568 println!();
1569
1570 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
1571 let key = 0x_1234567890ABCDEF_u64;
1572 println!("K =\t{:#016X}", key);
1573 let mut a_des = DES::new_with_key_u64(key);
1574
1575 let message = "PARK Youngho".to_string();
1576 println!("M =\t{}", message);
1577 let mut cipher = [0_u8; 16];
1578 a_des.encrypt_string_into_array(&message, &mut cipher);
1579 print!("C =\t");
1580 for c in cipher.clone()
1581 { print!("{:02X} ", c); }
1582 println!();
1583 let mut txt = String::new();
1584 for c in cipher.clone()
1585 { write!(txt, "{:02X} ", c); }
1586 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
1587 println!();
1588
1589 // Normal case for the message of 16 bytes
1590 let key = 0x_1234567890ABCDEF_u64;
1591 println!("K =\t{:#016X}", key);
1592 let mut a_des = DES::new_with_key_u64(key);
1593
1594 let message = "고맙습니다.".to_string();
1595 println!("M =\t{}", message);
1596 let mut cipher = [0_u8; 24];
1597 a_des.encrypt_string_into_array(&message, &mut cipher);
1598 print!("C =\t");
1599 for c in cipher.clone()
1600 { print!("{:02X} ", c); }
1601 println!();
1602 let mut txt = String::new();
1603 for c in cipher.clone()
1604 { write!(txt, "{:02X} ", c); }
1605 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
1606 println!("-------------------------------");
1607}
1608
1609fn des_encrypt_vec_with_padding_iso_ecb()
1610{
1611 println!("des_encrypt_vec_with_padding_iso_ecb");
1612 use std::io::Write;
1613 use std::fmt::Write as _;
1614 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
1615
1616 // Normal case
1617 let key = 0x_1234567890ABCDEF_u64;
1618 println!("K =\t{:#016X}", key);
1619 let mut a_des = DES::new_with_key_u64(key);
1620
1621 let message = "In the beginning God created the heavens and the earth.";
1622 println!("M =\t{}", message);
1623 let message = unsafe { message.to_string().as_mut_vec().clone() };
1624 let mut cipher = [0_u8; 56];
1625 a_des.encrypt_vec(&message, cipher.as_mut_ptr());
1626 print!("C (16 rounds) =\t");
1627 for c in cipher.clone()
1628 { print!("{:02X} ", c); }
1629 println!();
1630 let mut txt = String::new();
1631 for c in cipher.clone()
1632 { write!(txt, "{:02X} ", c); }
1633 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 F4 BE 6B A5 C5 7D F6 5D ");
1634 println!();
1635
1636 // Expanded case for 128 rounds
1637 let key = 0x_1234567890ABCDEF_u64;
1638 println!("K =\t{:#016X}", key);
1639 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
1640
1641 let message = "In the beginning God created the heavens and the earth.";
1642 println!("M =\t{}", message);
1643 let message = unsafe { message.to_string().as_mut_vec().clone() };
1644 let mut cipher = [0_u8; 56];
1645 a_des.encrypt_vec(&message, cipher.as_mut_ptr());
1646 print!("C (128 rounds) =\t");
1647 for c in cipher.clone()
1648 { print!("{:02X} ", c); }
1649 println!();
1650 let mut txt = String::new();
1651 for c in cipher.clone()
1652 { write!(txt, "{:02X} ", c); }
1653 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
1654 println!();
1655
1656 // Expanded case for 0 rounds which means that key is meaningless
1657 let key1 = 0x_1234567890ABCDEF_u64;
1658 let key2 = 0_u64;
1659 println!("K1 =\t{:#016X}", key1);
1660 println!("K2 =\t{:#016X}", key2);
1661 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
1662 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
1663
1664 let message = "In the beginning God created the heavens and the earth.";
1665 println!("M =\t{}", message);
1666 let message = unsafe { message.to_string().as_mut_vec().clone() };
1667 let mut cipher1 = [0_u8; 56];
1668 let mut cipher2 = [0_u8; 56];
1669 c_des.encrypt_vec(&message, cipher1.as_mut_ptr());
1670 d_des.encrypt_vec(&message, cipher2.as_mut_ptr());
1671 print!("C (0 rounds) =\t");
1672 for c in cipher1.clone()
1673 { print!("{:02X} ", c); }
1674 println!();
1675 let mut txt = String::new();
1676 for c in cipher1.clone()
1677 { write!(txt, "{:02X} ", c); }
1678 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
1679 print!("D (0 rounds) =\t");
1680 for c in cipher2.clone()
1681 { print!("{:02X} ", c); }
1682 println!();
1683 let mut txt = String::new();
1684 for c in cipher2.clone()
1685 { write!(txt, "{:02X} ", c); }
1686 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
1687 println!();
1688
1689 // Normal case for the message of 0 bytes
1690 let key = 0x_1234567890ABCDEF_u64;
1691 println!("K =\t{:#016X}", key);
1692 let mut a_des = DES::new_with_key_u64(key);
1693
1694 let message = "";
1695 println!("M =\t{}", message);
1696 let message = unsafe { message.to_string().as_mut_vec().clone() };
1697 let mut cipher = [0_u8; 8];
1698 a_des.encrypt_vec(&message, cipher.as_mut_ptr());
1699 print!("C =\t");
1700 for c in cipher.clone()
1701 { print!("{:02X} ", c); }
1702 println!();
1703 let mut txt = String::new();
1704 for c in cipher.clone()
1705 { write!(txt, "{:02X} ", c); }
1706 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
1707 println!();
1708
1709 // Normal case for the message shorter than 8 bytes
1710 let key = 0x_1234567890ABCDEF_u64;
1711 println!("K =\t{:#016X}", key);
1712 let mut a_des = DES::new_with_key_u64(key);
1713
1714 let message = "7 bytes";
1715 println!("M =\t{}", message);
1716 let message = unsafe { message.to_string().as_mut_vec().clone() };
1717 let mut cipher = [0_u8; 8];
1718 a_des.encrypt_vec(&message, cipher.as_mut_ptr());
1719 print!("C =\t");
1720 for c in cipher.clone()
1721 { print!("{:02X} ", c); }
1722 println!();
1723 let mut txt = String::new();
1724 for c in cipher.clone()
1725 { write!(txt, "{:02X} ", c); }
1726 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
1727 println!();
1728
1729 // Normal case for the message of 8 bytes
1730 let key = 0x_1234567890ABCDEF_u64;
1731 println!("K =\t{:#016X}", key);
1732 let mut a_des = DES::new_with_key_u64(key);
1733
1734 let message = "I am OK.";
1735 println!("M =\t{}", message);
1736 let message = unsafe { message.to_string().as_mut_vec().clone() };
1737 let mut cipher = [0_u8; 16];
1738 a_des.encrypt_vec(&message, cipher.as_mut_ptr());
1739 print!("C =\t");
1740 for c in cipher.clone()
1741 { print!("{:02X} ", c); }
1742 println!();
1743 let mut txt = String::new();
1744 for c in cipher.clone()
1745 { write!(txt, "{:02X} ", c); }
1746 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
1747 println!();
1748
1749 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
1750 let key = 0x_1234567890ABCDEF_u64;
1751 println!("K =\t{:#016X}", key);
1752 let mut a_des = DES::new_with_key_u64(key);
1753
1754 let message = "PARK Youngho";
1755 println!("M =\t{}", message);
1756 let message = unsafe { message.to_string().as_mut_vec().clone() };
1757 let mut cipher = [0_u8; 16];
1758 a_des.encrypt_vec(&message, cipher.as_mut_ptr());
1759 print!("C =\t");
1760 for c in cipher.clone()
1761 { print!("{:02X} ", c); }
1762 println!();
1763 let mut txt = String::new();
1764 for c in cipher.clone()
1765 { write!(txt, "{:02X} ", c); }
1766 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
1767 println!();
1768
1769
1770 // Normal case for the message of 16 bytes
1771 let key = 0x_1234567890ABCDEF_u64;
1772 println!("K =\t{:#016X}", key);
1773 let mut a_des = DES::new_with_key_u64(key);
1774
1775 let message = "고맙습니다.";
1776 println!("M =\t{}", message);
1777 let message = unsafe { message.to_string().as_mut_vec().clone() };
1778 let mut cipher = [0_u8; 24];
1779 a_des.encrypt_vec(&message, cipher.as_mut_ptr());
1780 print!("C =\t");
1781 for c in cipher.clone()
1782 { print!("{:02X} ", c); }
1783 println!();
1784 let mut txt = String::new();
1785 for c in cipher.clone()
1786 { write!(txt, "{:02X} ", c); }
1787 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
1788 println!("-------------------------------");
1789}
1790
1791fn des_encrypt_vec_with_padding_iso_ecb_into_vec()
1792{
1793 println!("des_encrypt_vec_with_padding_iso_ecb_into_vec");
1794 use std::io::Write;
1795 use std::fmt::Write as _;
1796 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
1797
1798 // Normal case
1799 let key = 0x_1234567890ABCDEF_u64;
1800 println!("K =\t{:#016X}", key);
1801 let mut a_des = DES::new_with_key_u64(key);
1802
1803 let message = "In the beginning God created the heavens and the earth.";
1804 println!("M =\t{}", message);
1805 let message = unsafe { message.to_string().as_mut_vec().clone() };
1806 let mut cipher = Vec::<u8>::new();
1807 a_des.encrypt_vec_into_vec(&message, &mut cipher);
1808 print!("C (16 rounds) =\t");
1809 for c in cipher.clone()
1810 { print!("{:02X} ", c); }
1811 println!();
1812 let mut txt = String::new();
1813 for c in cipher.clone()
1814 { write!(txt, "{:02X} ", c); }
1815 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 F4 BE 6B A5 C5 7D F6 5D ");
1816 println!();
1817
1818 // Expanded case for 128 rounds
1819 let key = 0x_1234567890ABCDEF_u64;
1820 println!("K =\t{:#016X}", key);
1821 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
1822
1823 let message = "In the beginning God created the heavens and the earth.";
1824 println!("M =\t{}", message);
1825 let message = unsafe { message.to_string().as_mut_vec().clone() };
1826 let mut cipher = Vec::<u8>::new();
1827 a_des.encrypt_vec_into_vec(&message, &mut cipher);
1828 print!("C (128 rounds) =\t");
1829 for c in cipher.clone()
1830 { print!("{:02X} ", c); }
1831 println!();
1832 let mut txt = String::new();
1833 for c in cipher.clone()
1834 { write!(txt, "{:02X} ", c); }
1835 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
1836 println!();
1837
1838 // Expanded case for 0 rounds which means that key is meaningless
1839 let key1 = 0x_1234567890ABCDEF_u64;
1840 let key2 = 0_u64;
1841 println!("K1 =\t{:#016X}", key1);
1842 println!("K2 =\t{:#016X}", key2);
1843 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
1844 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
1845
1846 let message = "In the beginning God created the heavens and the earth.";
1847 println!("M =\t{}", message);
1848 let message = unsafe { message.to_string().as_mut_vec().clone() };
1849
1850 let mut cipher1 = Vec::<u8>::new();
1851 let mut cipher2 = Vec::<u8>::new();
1852 c_des.encrypt_vec_into_vec(&message, &mut cipher1);
1853 d_des.encrypt_vec_into_vec(&message, &mut cipher2);
1854 print!("C (0 rounds) =\t");
1855 for c in cipher1.clone()
1856 { print!("{:02X} ", c); }
1857 println!();
1858 let mut txt = String::new();
1859 for c in cipher1.clone()
1860 { write!(txt, "{:02X} ", c); }
1861 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
1862 print!("D (0 rounds) =\t");
1863 for c in cipher2.clone()
1864 { print!("{:02X} ", c); }
1865 println!();
1866 let mut txt = String::new();
1867 for c in cipher2.clone()
1868 { write!(txt, "{:02X} ", c); }
1869 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
1870 println!();
1871
1872 // Normal case for the message of 0 bytes
1873 let key = 0x_1234567890ABCDEF_u64;
1874 println!("K =\t{:#016X}", key);
1875 let mut a_des = DES::new_with_key_u64(key);
1876
1877 let message = "";
1878 println!("M =\t{}", message);
1879 let message = unsafe { message.to_string().as_mut_vec().clone() };
1880 let mut cipher = Vec::<u8>::new();
1881 a_des.encrypt_vec_into_vec(&message, &mut cipher);
1882 print!("C =\t");
1883 for c in cipher.clone()
1884 { print!("{:02X} ", c); }
1885 println!();
1886 let mut txt = String::new();
1887 for c in cipher.clone()
1888 { write!(txt, "{:02X} ", c); }
1889 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
1890 println!();
1891
1892 // Normal case for the message shorter than 8 bytes
1893 let key = 0x_1234567890ABCDEF_u64;
1894 println!("K =\t{:#016X}", key);
1895 let mut a_des = DES::new_with_key_u64(key);
1896
1897 let message = "7 bytes";
1898 println!("M =\t{}", message);
1899 let message = unsafe { message.to_string().as_mut_vec().clone() };
1900 let mut cipher = Vec::<u8>::new();
1901 a_des.encrypt_vec_into_vec(&message, &mut cipher);
1902 print!("C =\t");
1903 for c in cipher.clone()
1904 { print!("{:02X} ", c); }
1905 println!();
1906 let mut txt = String::new();
1907 for c in cipher.clone()
1908 { write!(txt, "{:02X} ", c); }
1909 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
1910 println!();
1911
1912 // Normal case for the message of 8 bytes
1913 let key = 0x_1234567890ABCDEF_u64;
1914 println!("K =\t{:#016X}", key);
1915 let mut a_des = DES::new_with_key_u64(key);
1916
1917 let message = "I am OK.";
1918 println!("M =\t{}", message);
1919 let message = unsafe { message.to_string().as_mut_vec().clone() };
1920 let mut cipher = Vec::<u8>::new();
1921 a_des.encrypt_vec_into_vec(&message, &mut cipher);
1922 print!("C =\t");
1923 for c in cipher.clone()
1924 { print!("{:02X} ", c); }
1925 println!();
1926 let mut txt = String::new();
1927 for c in cipher.clone()
1928 { write!(txt, "{:02X} ", c); }
1929 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
1930 println!();
1931
1932 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
1933 let key = 0x_1234567890ABCDEF_u64;
1934 println!("K =\t{:#016X}", key);
1935 let mut a_des = DES::new_with_key_u64(key);
1936
1937 let message = "PARK Youngho";
1938 println!("M =\t{}", message);
1939 let message = unsafe { message.to_string().as_mut_vec().clone() };
1940 let mut cipher = Vec::<u8>::new();
1941 a_des.encrypt_vec_into_vec(&message, &mut cipher);
1942 print!("C =\t");
1943 for c in cipher.clone()
1944 { print!("{:02X} ", c); }
1945 println!();
1946 let mut txt = String::new();
1947 for c in cipher.clone()
1948 { write!(txt, "{:02X} ", c); }
1949 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
1950 println!();
1951
1952
1953 // Normal case for the message of 16 bytes
1954 let key = 0x_1234567890ABCDEF_u64;
1955 println!("K =\t{:#016X}", key);
1956 let mut a_des = DES::new_with_key_u64(key);
1957
1958 let message = "고맙습니다.";
1959 println!("M =\t{}", message);
1960 let message = unsafe { message.to_string().as_mut_vec().clone() };
1961 let mut cipher = Vec::<u8>::new();
1962 a_des.encrypt_vec_into_vec(&message, &mut cipher);
1963 print!("C =\t");
1964 for c in cipher.clone()
1965 { print!("{:02X} ", c); }
1966 println!();
1967 let mut txt = String::new();
1968 for c in cipher.clone()
1969 { write!(txt, "{:02X} ", c); }
1970 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
1971 println!("-------------------------------");
1972}
1973
1974fn des_encrypt_vec_with_padding_iso_ecb_into_array()
1975{
1976 println!("des_encrypt_vec_with_padding_iso_ecb_into_array");
1977 use std::io::Write;
1978 use std::fmt::Write as _;
1979 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
1980
1981 // Normal case
1982 let key = 0x_1234567890ABCDEF_u64;
1983 println!("K =\t{:#016X}", key);
1984 let mut a_des = DES::new_with_key_u64(key);
1985
1986 let message = "In the beginning God created the heavens and the earth.";
1987 println!("M =\t{}", message);
1988 let message = unsafe { message.to_string().as_mut_vec().clone() };
1989 let mut cipher = [0_u8; 56];
1990 a_des.encrypt_vec_into_array(&message, &mut cipher);
1991 print!("C (16 rounds) =\t");
1992 for c in cipher.clone()
1993 { print!("{:02X} ", c); }
1994 println!();
1995 let mut txt = String::new();
1996 for c in cipher.clone()
1997 { write!(txt, "{:02X} ", c); }
1998 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 F4 BE 6B A5 C5 7D F6 5D ");
1999 println!();
2000
2001 // Expanded case for 128 rounds
2002 let key = 0x_1234567890ABCDEF_u64;
2003 println!("K =\t{:#016X}", key);
2004 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
2005
2006 let message = "In the beginning God created the heavens and the earth.";
2007 println!("M =\t{}", message);
2008 let message = unsafe { message.to_string().as_mut_vec().clone() };
2009 let mut cipher = [0_u8; 56];
2010 a_des.encrypt_vec_into_array(&message, &mut cipher);
2011 print!("C (128 rounds) =\t");
2012 for c in cipher.clone()
2013 { print!("{:02X} ", c); }
2014 println!();
2015 let mut txt = String::new();
2016 for c in cipher.clone()
2017 { write!(txt, "{:02X} ", c); }
2018 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
2019 println!();
2020
2021 // Expanded case for 0 rounds which means that key is meaningless
2022 let key1 = 0x_1234567890ABCDEF_u64;
2023 let key2 = 0_u64;
2024 println!("K1 =\t{:#016X}", key1);
2025 println!("K2 =\t{:#016X}", key2);
2026 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
2027 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
2028
2029 let message = "In the beginning God created the heavens and the earth.";
2030 println!("M =\t{}", message);
2031 let message = unsafe { message.to_string().as_mut_vec().clone() };
2032 let mut cipher1 = [0_u8; 56];
2033 let mut cipher2 = [0_u8; 56];
2034 c_des.encrypt_vec_into_array(&message, &mut cipher1);
2035 d_des.encrypt_vec_into_array(&message, &mut cipher2);
2036 print!("C (0 rounds) =\t");
2037 for c in cipher1.clone()
2038 { print!("{:02X} ", c); }
2039 println!();
2040 let mut txt = String::new();
2041 for c in cipher1.clone()
2042 { write!(txt, "{:02X} ", c); }
2043 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
2044 print!("D (0 rounds) =\t");
2045 for c in cipher2.clone()
2046 { print!("{:02X} ", c); }
2047 println!();
2048 let mut txt = String::new();
2049 for c in cipher2.clone()
2050 { write!(txt, "{:02X} ", c); }
2051 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
2052 println!();
2053
2054 // Normal case for the message of 0 bytes
2055 let key = 0x_1234567890ABCDEF_u64;
2056 println!("K =\t{:#016X}", key);
2057 let mut a_des = DES::new_with_key_u64(key);
2058
2059 let message = "";
2060 println!("M =\t{}", message);
2061 let message = unsafe { message.to_string().as_mut_vec().clone() };
2062 let mut cipher = [0_u8; 8];
2063 a_des.encrypt_vec_into_array(&message, &mut cipher);
2064 print!("C =\t");
2065 for c in cipher.clone()
2066 { print!("{:02X} ", c); }
2067 println!();
2068 let mut txt = String::new();
2069 for c in cipher.clone()
2070 { write!(txt, "{:02X} ", c); }
2071 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
2072 println!();
2073
2074 // Normal case for the message shorter than 8 bytes
2075 let key = 0x_1234567890ABCDEF_u64;
2076 println!("K =\t{:#016X}", key);
2077 let mut a_des = DES::new_with_key_u64(key);
2078
2079 let message = "7 bytes";
2080 println!("M =\t{}", message);
2081 let message = unsafe { message.to_string().as_mut_vec().clone() };
2082 let mut cipher = [0_u8; 8];
2083 a_des.encrypt_vec_into_array(&message, &mut cipher);
2084 print!("C =\t");
2085 for c in cipher.clone()
2086 { print!("{:02X} ", c); }
2087 println!();
2088 let mut txt = String::new();
2089 for c in cipher.clone()
2090 { write!(txt, "{:02X} ", c); }
2091 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
2092 println!();
2093
2094 // Normal case for the message of 8 bytes
2095 let key = 0x_1234567890ABCDEF_u64;
2096 println!("K =\t{:#016X}", key);
2097 let mut a_des = DES::new_with_key_u64(key);
2098
2099 let message = "I am OK.";
2100 println!("M =\t{}", message);
2101 let message = unsafe { message.to_string().as_mut_vec().clone() };
2102 let mut cipher = [0_u8; 16];
2103 a_des.encrypt_vec_into_array(&message, &mut cipher);
2104 print!("C =\t");
2105 for c in cipher.clone()
2106 { print!("{:02X} ", c); }
2107 println!();
2108 let mut txt = String::new();
2109 for c in cipher.clone()
2110 { write!(txt, "{:02X} ", c); }
2111 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
2112 println!();
2113
2114 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
2115 let key = 0x_1234567890ABCDEF_u64;
2116 println!("K =\t{:#016X}", key);
2117 let mut a_des = DES::new_with_key_u64(key);
2118
2119 let message = "PARK Youngho";
2120 println!("M =\t{}", message);
2121 let message = unsafe { message.to_string().as_mut_vec().clone() };
2122 let mut cipher = [0_u8; 16];
2123 a_des.encrypt_vec_into_array(&message, &mut cipher);
2124 print!("C =\t");
2125 for c in cipher.clone()
2126 { print!("{:02X} ", c); }
2127 println!();
2128 let mut txt = String::new();
2129 for c in cipher.clone()
2130 { write!(txt, "{:02X} ", c); }
2131 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
2132 println!();
2133
2134
2135 // Normal case for the message of 16 bytes
2136 let key = 0x_1234567890ABCDEF_u64;
2137 println!("K =\t{:#016X}", key);
2138 let mut a_des = DES::new_with_key_u64(key);
2139
2140 let message = "고맙습니다.";
2141 println!("M =\t{}", message);
2142 let message = unsafe { message.to_string().as_mut_vec().clone() };
2143 let mut cipher = [0_u8; 24];
2144 a_des.encrypt_vec_into_array(&message, &mut cipher);
2145 print!("C =\t");
2146 for c in cipher.clone()
2147 { print!("{:02X} ", c); }
2148 println!();
2149 let mut txt = String::new();
2150 for c in cipher.clone()
2151 { write!(txt, "{:02X} ", c); }
2152 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
2153 println!("-------------------------------");
2154}
2155
2156fn des_encrypt_array_with_padding_iso_ecb()
2157{
2158 println!("des_encrypt_array_with_padding_iso_ecb");
2159 use std::io::Write;
2160 use std::fmt::Write as _;
2161 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
2162
2163 // Normal case
2164 let key = 0x_1234567890ABCDEF_u64;
2165 println!("K =\t{:#016X}", key);
2166 let mut a_des = DES::new_with_key_u64(key);
2167
2168 let mes = "In the beginning God created the heavens and the earth.";
2169 println!("M =\t{}", mes);
2170 let mut message = [0_u8; 55];
2171 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2172 let mut cipher = [0_u8; 56];
2173 a_des.encrypt_array(&message, cipher.as_mut_ptr());
2174 print!("C (16 rounds) =\t");
2175 for c in cipher.clone()
2176 { print!("{:02X} ", c); }
2177 println!();
2178 let mut txt = String::new();
2179 for c in cipher.clone()
2180 { write!(txt, "{:02X} ", c); }
2181 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 F4 BE 6B A5 C5 7D F6 5D ");
2182 println!();
2183
2184 // Expanded case for 128 rounds
2185 let key = 0x_1234567890ABCDEF_u64;
2186 println!("K =\t{:#016X}", key);
2187 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
2188
2189 let mes = "In the beginning God created the heavens and the earth.";
2190 println!("M =\t{}", mes);
2191 let mut message = [0_u8; 55];
2192 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2193 let mut cipher = [0_u8; 56];
2194 a_des.encrypt_array(&message, cipher.as_mut_ptr());
2195 print!("C (128 rounds) =\t");
2196 for c in cipher.clone()
2197 { print!("{:02X} ", c); }
2198 println!();
2199 let mut txt = String::new();
2200 for c in cipher.clone()
2201 { write!(txt, "{:02X} ", c); }
2202 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
2203 println!();
2204
2205 // Expanded case for 0 rounds which means that key is meaningless
2206 let key1 = 0x_1234567890ABCDEF_u64;
2207 let key2 = 0_u64;
2208 println!("K1 =\t{:#016X}", key1);
2209 println!("K2 =\t{:#016X}", key2);
2210 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
2211 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
2212
2213 let mes = "In the beginning God created the heavens and the earth.";
2214 println!("M =\t{}", mes);
2215 let mut message = [0_u8; 55];
2216 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2217 let mut cipher1 = [0_u8; 56];
2218 let mut cipher2 = [0_u8; 56];
2219 c_des.encrypt_array(&message, cipher1.as_mut_ptr());
2220 d_des.encrypt_array(&message, cipher2.as_mut_ptr());
2221 print!("C (0 rounds) =\t");
2222 for c in cipher1.clone()
2223 { print!("{:02X} ", c); }
2224 println!();
2225 let mut txt = String::new();
2226 for c in cipher1.clone()
2227 { write!(txt, "{:02X} ", c); }
2228 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
2229 print!("D (0 rounds) =\t");
2230 for c in cipher2.clone()
2231 { print!("{:02X} ", c); }
2232 println!();
2233 let mut txt = String::new();
2234 for c in cipher2.clone()
2235 { write!(txt, "{:02X} ", c); }
2236 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
2237 println!();
2238
2239 // Normal case for the message of 0 bytes
2240 let key = 0x_1234567890ABCDEF_u64;
2241 println!("K =\t{:#016X}", key);
2242 let mut a_des = DES::new_with_key_u64(key);
2243
2244 let mes = "";
2245 println!("M =\t{}", mes);
2246 let mut message = [0_u8; 0];
2247 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2248 let mut cipher = [0_u8; 8];
2249 a_des.encrypt_array(&message, cipher.as_mut_ptr());
2250 print!("C =\t");
2251 for c in cipher.clone()
2252 { print!("{:02X} ", c); }
2253 println!();
2254 let mut txt = String::new();
2255 for c in cipher.clone()
2256 { write!(txt, "{:02X} ", c); }
2257 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
2258 println!();
2259
2260 // Normal case for the message shorter than 8 bytes
2261 let key = 0x_1234567890ABCDEF_u64;
2262 println!("K =\t{:#016X}", key);
2263 let mut a_des = DES::new_with_key_u64(key);
2264
2265 let mes = "7 bytes";
2266 println!("M =\t{}", mes);
2267 let mut message = [0_u8; 7];
2268 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2269 let mut cipher = [0_u8; 8];
2270 a_des.encrypt_array(&message, cipher.as_mut_ptr());
2271 print!("C =\t");
2272 for c in cipher.clone()
2273 { print!("{:02X} ", c); }
2274 println!();
2275 let mut txt = String::new();
2276 for c in cipher.clone()
2277 { write!(txt, "{:02X} ", c); }
2278 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
2279 println!();
2280
2281 // Normal case for the message of 8 bytes
2282 let key = 0x_1234567890ABCDEF_u64;
2283 println!("K =\t{:#016X}", key);
2284 let mut a_des = DES::new_with_key_u64(key);
2285
2286 let mes = "I am OK.";
2287 println!("M =\t{}", mes);
2288 let mut message = [0_u8; 8];
2289 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2290 let mut cipher = [0_u8; 16];
2291 a_des.encrypt_array(&message, cipher.as_mut_ptr());
2292 print!("C =\t");
2293 for c in cipher.clone()
2294 { print!("{:02X} ", c); }
2295 println!();
2296 let mut txt = String::new();
2297 for c in cipher.clone()
2298 { write!(txt, "{:02X} ", c); }
2299 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
2300 println!();
2301
2302 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
2303 let key = 0x_1234567890ABCDEF_u64;
2304 println!("K =\t{:#016X}", key);
2305 let mut a_des = DES::new_with_key_u64(key);
2306
2307 let mes = "PARK Youngho";
2308 println!("M =\t{}", mes);
2309 let mut message = [0_u8; 12];
2310 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2311 let mut cipher = [0_u8; 16];
2312 a_des.encrypt_array(&message, cipher.as_mut_ptr());
2313 print!("C =\t");
2314 for c in cipher.clone()
2315 { print!("{:02X} ", c); }
2316 println!();
2317 let mut txt = String::new();
2318 for c in cipher.clone()
2319 { write!(txt, "{:02X} ", c); }
2320 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
2321 println!();
2322
2323
2324 // Normal case for the message of 16 bytes
2325 let key = 0x_1234567890ABCDEF_u64;
2326 println!("K =\t{:#016X}", key);
2327 let mut a_des = DES::new_with_key_u64(key);
2328
2329 let mes = "고맙습니다.";
2330 println!("M =\t{}", mes);
2331 let mut message = [0_u8; 16];
2332 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2333 let mut cipher = [0_u8; 24];
2334 a_des.encrypt_array(&message, cipher.as_mut_ptr());
2335 print!("C =\t");
2336 for c in cipher.clone()
2337 { print!("{:02X} ", c); }
2338 println!();
2339 let mut txt = String::new();
2340 for c in cipher.clone()
2341 { write!(txt, "{:02X} ", c); }
2342 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
2343 println!("-------------------------------");
2344}
2345
2346fn des_encrypt_array_with_padding_iso_ecb_into_vec()
2347{
2348 println!("des_encrypt_array_with_padding_iso_ecb_into_vec");
2349 use std::io::Write;
2350 use std::fmt::Write as _;
2351 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
2352
2353 // Normal case
2354 let key = 0x_1234567890ABCDEF_u64;
2355 println!("K =\t{:#016X}", key);
2356 let mut a_des = DES::new_with_key_u64(key);
2357
2358 let mes = "In the beginning God created the heavens and the earth.";
2359 println!("M =\t{}", mes);
2360 let mut message = [0_u8; 55];
2361 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2362 let mut cipher = Vec::<u8>::new();
2363 a_des.encrypt_array_into_vec(&message, &mut cipher);
2364 print!("C (16 rounds) =\t");
2365 for c in cipher.clone()
2366 { print!("{:02X} ", c); }
2367 println!();
2368 let mut txt = String::new();
2369 for c in cipher.clone()
2370 { write!(txt, "{:02X} ", c); }
2371 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 F4 BE 6B A5 C5 7D F6 5D ");
2372 println!();
2373
2374 // Expanded case for 128 rounds
2375 let key = 0x_1234567890ABCDEF_u64;
2376 println!("K =\t{:#016X}", key);
2377 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
2378
2379 let mes = "In the beginning God created the heavens and the earth.";
2380 println!("M =\t{}", mes);
2381 let mut message = [0_u8; 55];
2382 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2383 let mut cipher = Vec::<u8>::new();
2384 a_des.encrypt_array_into_vec(&message, &mut cipher);
2385 print!("C (128 rounds) =\t");
2386 for c in cipher.clone()
2387 { print!("{:02X} ", c); }
2388 println!();
2389 let mut txt = String::new();
2390 for c in cipher.clone()
2391 { write!(txt, "{:02X} ", c); }
2392 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
2393 println!();
2394
2395 // Expanded case for 0 rounds which means that key is meaningless
2396 let key1 = 0x_1234567890ABCDEF_u64;
2397 let key2 = 0_u64;
2398 println!("K1 =\t{:#016X}", key1);
2399 println!("K2 =\t{:#016X}", key2);
2400 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
2401 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
2402
2403 let mes = "In the beginning God created the heavens and the earth.";
2404 println!("M =\t{}", mes);
2405 let mut message = [0_u8; 55];
2406 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2407
2408 let mut cipher1 = Vec::<u8>::new();
2409 let mut cipher2 = Vec::<u8>::new();
2410 c_des.encrypt_array_into_vec(&message, &mut cipher1);
2411 d_des.encrypt_array_into_vec(&message, &mut cipher2);
2412 print!("C (0 rounds) =\t");
2413 for c in cipher1.clone()
2414 { print!("{:02X} ", c); }
2415 println!();
2416 let mut txt = String::new();
2417 for c in cipher1.clone()
2418 { write!(txt, "{:02X} ", c); }
2419 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
2420 print!("D (0 rounds) =\t");
2421 for c in cipher2.clone()
2422 { print!("{:02X} ", c); }
2423 println!();
2424 let mut txt = String::new();
2425 for c in cipher2.clone()
2426 { write!(txt, "{:02X} ", c); }
2427 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
2428 println!();
2429
2430 // Normal case for the message of 0 bytes
2431 let key = 0x_1234567890ABCDEF_u64;
2432 println!("K =\t{:#016X}", key);
2433 let mut a_des = DES::new_with_key_u64(key);
2434
2435 let mes = "";
2436 println!("M =\t{}", mes);
2437 let mut message = [0_u8; 0];
2438 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2439 let mut cipher = Vec::<u8>::new();
2440 a_des.encrypt_array_into_vec(&message, &mut cipher);
2441 print!("C =\t");
2442 for c in cipher.clone()
2443 { print!("{:02X} ", c); }
2444 println!();
2445 let mut txt = String::new();
2446 for c in cipher.clone()
2447 { write!(txt, "{:02X} ", c); }
2448 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
2449 println!();
2450
2451 // Normal case for the message shorter than 8 bytes
2452 let key = 0x_1234567890ABCDEF_u64;
2453 println!("K =\t{:#016X}", key);
2454 let mut a_des = DES::new_with_key_u64(key);
2455
2456 let mes = "7 bytes";
2457 println!("M =\t{}", mes);
2458 let mut message = [0_u8; 7];
2459 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2460 let mut cipher = Vec::<u8>::new();
2461 a_des.encrypt_array_into_vec(&message, &mut cipher);
2462 print!("C =\t");
2463 for c in cipher.clone()
2464 { print!("{:02X} ", c); }
2465 println!();
2466 let mut txt = String::new();
2467 for c in cipher.clone()
2468 { write!(txt, "{:02X} ", c); }
2469 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
2470 println!();
2471
2472 // Normal case for the message of 8 bytes
2473 let key = 0x_1234567890ABCDEF_u64;
2474 println!("K =\t{:#016X}", key);
2475 let mut a_des = DES::new_with_key_u64(key);
2476
2477 let mes = "I am OK.";
2478 println!("M =\t{}", mes);
2479 let mut message = [0_u8; 8];
2480 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2481 let mut cipher = Vec::<u8>::new();
2482 a_des.encrypt_array_into_vec(&message, &mut cipher);
2483 print!("C =\t");
2484 for c in cipher.clone()
2485 { print!("{:02X} ", c); }
2486 println!();
2487 let mut txt = String::new();
2488 for c in cipher.clone()
2489 { write!(txt, "{:02X} ", c); }
2490 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
2491 println!();
2492
2493 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
2494 let key = 0x_1234567890ABCDEF_u64;
2495 println!("K =\t{:#016X}", key);
2496 let mut a_des = DES::new_with_key_u64(key);
2497
2498 let mes = "PARK Youngho";
2499 println!("M =\t{}", mes);
2500 let mut message = [0_u8; 12];
2501 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2502 let mut cipher = Vec::<u8>::new();
2503 a_des.encrypt_array_into_vec(&message, &mut cipher);
2504 print!("C =\t");
2505 for c in cipher.clone()
2506 { print!("{:02X} ", c); }
2507 println!();
2508 let mut txt = String::new();
2509 for c in cipher.clone()
2510 { write!(txt, "{:02X} ", c); }
2511 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
2512 println!();
2513
2514
2515 // Normal case for the message of 16 bytes
2516 let key = 0x_1234567890ABCDEF_u64;
2517 println!("K =\t{:#016X}", key);
2518 let mut a_des = DES::new_with_key_u64(key);
2519
2520 let mes = "고맙습니다.";
2521 println!("M =\t{}", mes);
2522 let mut message = [0_u8; 16];
2523 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2524 let mut cipher = Vec::<u8>::new();
2525 a_des.encrypt_array_into_vec(&message, &mut cipher);
2526 print!("C =\t");
2527 for c in cipher.clone()
2528 { print!("{:02X} ", c); }
2529 println!();
2530 let mut txt = String::new();
2531 for c in cipher.clone()
2532 { write!(txt, "{:02X} ", c); }
2533 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
2534 println!("-------------------------------");
2535}
2536
2537fn des_encrypt_array_with_padding_iso_ecb_into_array()
2538{
2539 println!("des_encrypt_array_with_padding_iso_ecb_into_array");
2540 use std::io::Write;
2541 use std::fmt::Write as _;
2542 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
2543
2544 // Normal case
2545 let key = 0x_1234567890ABCDEF_u64;
2546 println!("K =\t{:#016X}", key);
2547 let mut a_des = DES::new_with_key_u64(key);
2548
2549 let mes = "In the beginning God created the heavens and the earth.";
2550 println!("M =\t{}", mes);
2551 let mut message = [0_u8; 55];
2552 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2553 let mut cipher = [0_u8; 56];
2554 a_des.encrypt_array_into_array(&message, &mut cipher);
2555 for c in cipher.clone()
2556 { print!("{:02X} ", c); }
2557 println!();
2558 let mut txt = String::new();
2559 for c in cipher.clone()
2560 { write!(txt, "{:02X} ", c); }
2561 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 F4 BE 6B A5 C5 7D F6 5D ");
2562 println!();
2563
2564 // Expanded case for 128 rounds
2565 let key = 0x_1234567890ABCDEF_u64;
2566 println!("K =\t{:#016X}", key);
2567 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
2568
2569 let mes = "In the beginning God created the heavens and the earth.";
2570 println!("M =\t{}", mes);
2571 let mut message = [0_u8; 55];
2572 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2573 let mut cipher = [0_u8; 56];
2574 a_des.encrypt_array_into_array(&message, &mut cipher);
2575 print!("C (128 rounds) =\t");
2576 for c in cipher.clone()
2577 { print!("{:02X} ", c); }
2578 println!();
2579 let mut txt = String::new();
2580 for c in cipher.clone()
2581 { write!(txt, "{:02X} ", c); }
2582 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
2583 println!();
2584
2585 // Expanded case for 0 rounds which means that key is meaningless
2586 let key1 = 0x_1234567890ABCDEF_u64;
2587 let key2 = 0_u64;
2588 println!("K1 =\t{:#016X}", key1);
2589 println!("K2 =\t{:#016X}", key2);
2590 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
2591 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
2592
2593 let mes = "In the beginning God created the heavens and the earth.";
2594 println!("M =\t{}", mes);
2595 let mut message = [0_u8; 55];
2596 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2597 let mut cipher1 = [0_u8; 56];
2598 let mut cipher2 = [0_u8; 56];
2599 c_des.encrypt_array_into_array(&message, &mut cipher1);
2600 d_des.encrypt_array_into_array(&message, &mut cipher2);
2601 print!("C (0 rounds) =\t");
2602 for c in cipher1.clone()
2603 { print!("{:02X} ", c); }
2604 println!();
2605 let mut txt = String::new();
2606 for c in cipher1.clone()
2607 { write!(txt, "{:02X} ", c); }
2608 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
2609 print!("D (0 rounds) =\t");
2610 for c in cipher2.clone()
2611 { print!("{:02X} ", c); }
2612 println!();
2613 let mut txt = String::new();
2614 for c in cipher2.clone()
2615 { write!(txt, "{:02X} ", c); }
2616 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
2617 println!();
2618
2619 // Normal case for the message of 0 bytes
2620 let key = 0x_1234567890ABCDEF_u64;
2621 println!("K =\t{:#016X}", key);
2622 let mut a_des = DES::new_with_key_u64(key);
2623
2624 let mes = "";
2625 println!("M =\t{}", mes);
2626 let mut message = [0_u8; 0];
2627 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2628 let mut cipher = [0_u8; 8];
2629 a_des.encrypt_array_into_array(&message, &mut cipher);
2630 print!("C =\t");
2631 for c in cipher.clone()
2632 { print!("{:02X} ", c); }
2633 println!();
2634 let mut txt = String::new();
2635 for c in cipher.clone()
2636 { write!(txt, "{:02X} ", c); }
2637 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
2638 println!();
2639
2640 // Normal case for the message shorter than 8 bytes
2641 let key = 0x_1234567890ABCDEF_u64;
2642 println!("K =\t{:#016X}", key);
2643 let mut a_des = DES::new_with_key_u64(key);
2644
2645 let mes = "7 bytes";
2646 println!("M =\t{}", mes);
2647 let mut message = [0_u8; 7];
2648 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2649 let mut cipher = [0_u8; 8];
2650 a_des.encrypt_array_into_array(&message, &mut cipher);
2651 print!("C =\t");
2652 for c in cipher.clone()
2653 { print!("{:02X} ", c); }
2654 println!();
2655 let mut txt = String::new();
2656 for c in cipher.clone()
2657 { write!(txt, "{:02X} ", c); }
2658 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
2659 println!();
2660
2661 // Normal case for the message of 8 bytes
2662 let key = 0x_1234567890ABCDEF_u64;
2663 println!("K =\t{:#016X}", key);
2664 let mut a_des = DES::new_with_key_u64(key);
2665
2666 let mes = "I am OK.";
2667 println!("M =\t{}", mes);
2668 let mut message = [0_u8; 8];
2669 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2670 let mut cipher = [0_u8; 16];
2671 a_des.encrypt_array_into_array(&message, &mut cipher);
2672 print!("C =\t");
2673 for c in cipher.clone()
2674 { print!("{:02X} ", c); }
2675 println!();
2676 let mut txt = String::new();
2677 for c in cipher.clone()
2678 { write!(txt, "{:02X} ", c); }
2679 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
2680 println!();
2681
2682 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
2683 let key = 0x_1234567890ABCDEF_u64;
2684 println!("K =\t{:#016X}", key);
2685 let mut a_des = DES::new_with_key_u64(key);
2686
2687 let mes = "PARK Youngho";
2688 println!("M =\t{}", mes);
2689 let mut message = [0_u8; 12];
2690 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2691 let mut cipher = [0_u8; 16];
2692 a_des.encrypt_array_into_array(&message, &mut cipher);
2693 print!("C =\t");
2694 for c in cipher.clone()
2695 { print!("{:02X} ", c); }
2696 println!();
2697 let mut txt = String::new();
2698 for c in cipher.clone()
2699 { write!(txt, "{:02X} ", c); }
2700 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
2701 println!();
2702
2703
2704 // Normal case for the message of 16 bytes
2705 let key = 0x_1234567890ABCDEF_u64;
2706 println!("K =\t{:#016X}", key);
2707 let mut a_des = DES::new_with_key_u64(key);
2708
2709 let mes = "고맙습니다.";
2710 println!("M =\t{}", mes);
2711 let mut message = [0_u8; 16];
2712 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2713 let mut cipher = [0_u8; 24];
2714 a_des.encrypt_array_into_array(&message, &mut cipher);
2715 print!("C =\t");
2716 for c in cipher.clone()
2717 { print!("{:02X} ", c); }
2718 println!();
2719 let mut txt = String::new();
2720 for c in cipher.clone()
2721 { write!(txt, "{:02X} ", c); }
2722 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
2723 println!("-------------------------------");
2724}
2725
2726fn des_decrypt_with_padding_iso_ecb()
2727{
2728 println!("des_decrypt_with_padding_iso_ecb");
2729 use std::io::Write;
2730 use std::fmt::Write as _;
2731 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
2732
2733 // Normal case
2734 let key = 0x_1234567890ABCDEF_u64;
2735 println!("K =\t{:#016X}", key);
2736 let mut a_des = DES::new_with_key_u64(key);
2737
2738 let message = "In the beginning God created the heavens and the earth.";
2739 println!("M =\t{}", message);
2740 let mut cipher = Vec::<u8>::new();
2741 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
2742 print!("C (16 rounds) =\t");
2743 for c in cipher.clone()
2744 { print!("{:02X} ", c); }
2745 println!();
2746 let mut txt = String::new();
2747 for c in cipher.clone()
2748 { write!(txt, "{:02X} ", c); }
2749 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 F4 BE 6B A5 C5 7D F6 5D ");
2750
2751 let mut recovered = vec![0; 55];
2752 a_des.decrypt(cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2753 print!("Ba (16 rounds) =\t");
2754 for b in recovered.clone()
2755 { print!("{:02X} ", b); }
2756 println!();
2757 let mut txt = String::new();
2758 for c in recovered.clone()
2759 { write!(txt, "{:02X} ", c); }
2760 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 ");
2761
2762 let mut converted = String::new();
2763 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2764
2765 println!("Bb (16 rounds) =\t{}", converted);
2766 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2767 assert_eq!(converted, message);
2768 println!();
2769
2770 // Expanded case for 128 rounds
2771 let key = 0x_1234567890ABCDEF_u64;
2772 println!("K =\t{:#016X}", key);
2773 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
2774
2775 let message = "In the beginning God created the heavens and the earth.";
2776 println!("M =\t{}", message);
2777 let mut cipher = Vec::<u8>::new();
2778 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
2779 print!("C (128 rounds) =\t");
2780 for c in cipher.clone()
2781 { print!("{:02X} ", c); }
2782 println!();
2783 let mut txt = String::new();
2784 for c in cipher.clone()
2785 { write!(txt, "{:02X} ", c); }
2786 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
2787
2788 let mut recovered = vec![0; 55];
2789 a_des.decrypt(cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2790 print!("Ba (128 rounds) =\t");
2791 for b in recovered.clone()
2792 { print!("{:02X} ", b); }
2793 println!();
2794 let mut txt = String::new();
2795 for c in recovered.clone()
2796 { write!(txt, "{:02X} ", c); }
2797 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 ");
2798
2799 let mut converted = String::new();
2800 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2801
2802 println!("Bb (128 rounds) =\t{}", converted);
2803 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2804 assert_eq!(converted, message);
2805 println!();
2806
2807 // Expanded case for 0 rounds which means that key is meaningless
2808 let key1 = 0x_1234567890ABCDEF_u64;
2809 let key2 = 0_u64;
2810 println!("K =\t{:#016X}", key);
2811 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
2812 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
2813
2814 let message = "In the beginning God created the heavens and the earth.";
2815 println!("M =\t{}", message);
2816 let mut cipher1 = Vec::<u8>::new();
2817 let mut cipher2 = Vec::<u8>::new();
2818 c_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher1);
2819 d_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher2);
2820 print!("C (0 rounds) =\t");
2821 for c in cipher1.clone()
2822 { print!("{:02X} ", c); }
2823 println!();
2824 let mut txt = String::new();
2825 for c in cipher1.clone()
2826 { write!(txt, "{:02X} ", c); }
2827 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
2828 print!("D (0 rounds) =\t");
2829 for c in cipher2.clone()
2830 { print!("{:02X} ", c); }
2831 println!();
2832 let mut txt = String::new();
2833 for c in cipher2.clone()
2834 { write!(txt, "{:02X} ", c); }
2835 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
2836
2837 let mut recovered1 = vec![0; 55];
2838 let mut recovered2 = vec![0; 55];
2839 c_des.decrypt(cipher1.as_ptr(), cipher1.len() as u64, recovered1.as_mut_ptr());
2840 d_des.decrypt(cipher2.as_ptr(), cipher2.len() as u64, recovered2.as_mut_ptr());
2841 print!("B1a (0 rounds) =\t");
2842 for b in recovered1.clone()
2843 { print!("{:02X} ", b); }
2844 println!();
2845 let mut txt = String::new();
2846 for c in recovered1.clone()
2847 { write!(txt, "{:02X} ", c); }
2848 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 ");
2849 print!("B2a (0 rounds) =\t");
2850 for b in recovered2.clone()
2851 { print!("{:02X} ", b); }
2852 println!();
2853 let mut txt = String::new();
2854 for c in recovered2.clone()
2855 { write!(txt, "{:02X} ", c); }
2856 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 ");
2857
2858 let mut converted1 = String::new();
2859 let mut converted2 = String::new();
2860 unsafe { converted1.as_mut_vec() }.append(&mut recovered1);
2861 unsafe { converted2.as_mut_vec() }.append(&mut recovered2);
2862
2863 println!("B1b (0 rounds) =\t{}", converted1);
2864 assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
2865 assert_eq!(converted1, message);
2866 println!("B2b (0 rounds) =\t{}", converted2);
2867 assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
2868 assert_eq!(converted2, message);
2869 assert_eq!(converted1, converted1);
2870 println!();
2871
2872 // Normal case for the message of 0 bytes
2873 let key = 0x_1234567890ABCDEF_u64;
2874 println!("K =\t{:#016X}", key);
2875 let mut a_des = DES::new_with_key_u64(key);
2876
2877 let message = "";
2878 println!("M =\t{}", message);
2879 let mut cipher = Vec::<u8>::new();
2880 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
2881 print!("C =\t");
2882 for c in cipher.clone()
2883 { print!("{:02X} ", c); }
2884 println!();
2885 let mut txt = String::new();
2886 for c in cipher.clone()
2887 { write!(txt, "{:02X} ", c); }
2888 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
2889
2890 let mut recovered = vec![0; 8];
2891 let len = a_des.decrypt(cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2892 print!("Ba =\t");
2893 for b in recovered.clone()
2894 { print!("{:02X} ", b); }
2895 println!();
2896 let mut txt = String::new();
2897 for c in recovered.clone()
2898 { write!(txt, "{:02X} ", c); }
2899 assert_eq!(txt, "00 00 00 00 00 00 00 00 ");
2900
2901 let mut converted = String::new();
2902 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2903 converted.truncate(len as usize);
2904
2905 println!("Bb =\t{}", converted);
2906 assert_eq!(converted, "");
2907 assert_eq!(converted, message);
2908 println!();
2909
2910 // Normal case for the message shorter than 8 bytes
2911 let key = 0x_1234567890ABCDEF_u64;
2912 println!("K =\t{:#016X}", key);
2913 let mut a_des = DES::new_with_key_u64(key);
2914
2915 let message = "7 bytes";
2916 println!("M =\t{}", message);
2917 let mut cipher = Vec::<u8>::new();
2918 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
2919 print!("C =\t");
2920 for c in cipher.clone()
2921 { print!("{:02X} ", c); }
2922 println!();
2923 let mut txt = String::new();
2924 for c in cipher.clone()
2925 { write!(txt, "{:02X} ", c); }
2926 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
2927
2928 let mut recovered = vec![0; 8];
2929 let len = a_des.decrypt(cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2930 print!("Ba =\t");
2931 for b in recovered.clone()
2932 { print!("{:02X} ", b); }
2933 println!();
2934 let mut txt = String::new();
2935 for c in recovered.clone()
2936 { write!(txt, "{:02X} ", c); }
2937 assert_eq!(txt, "37 20 62 79 74 65 73 00 ");
2938
2939 let mut converted = String::new();
2940 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2941 converted.truncate(len as usize);
2942
2943 println!("Bb =\t{}", converted);
2944 assert_eq!(converted, "7 bytes");
2945 assert_eq!(converted, message);
2946 println!();
2947
2948 // Normal case for the message of 8 bytes
2949 let key = 0x_1234567890ABCDEF_u64;
2950 println!("K =\t{:#016X}", key);
2951 let mut a_des = DES::new_with_key_u64(key);
2952
2953 let message = "I am OK.";
2954 println!("M =\t{}", message);
2955 let mut cipher = Vec::<u8>::new();
2956 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
2957 print!("C =\t");
2958 for c in cipher.clone()
2959 { print!("{:02X} ", c); }
2960 println!();
2961 let mut txt = String::new();
2962 for c in cipher.clone()
2963 { write!(txt, "{:02X} ", c); }
2964 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
2965
2966 let mut recovered = vec![0; 16];
2967 let len = a_des.decrypt(cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2968 print!("Ba =\t");
2969 for b in recovered.clone()
2970 { print!("{:02X} ", b); }
2971 println!();
2972 let mut txt = String::new();
2973 for c in recovered.clone()
2974 { write!(txt, "{:02X} ", c); }
2975 assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E 00 00 00 00 00 00 00 00 ");
2976
2977 let mut converted = String::new();
2978 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2979 converted.truncate(len as usize);
2980
2981 println!("Bb =\t{}", converted);
2982 assert_eq!(converted, "I am OK.");
2983 assert_eq!(converted, message);
2984 println!();
2985
2986 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
2987 let key = 0x_1234567890ABCDEF_u64;
2988 println!("K =\t{:#016X}", key);
2989 let mut a_des = DES::new_with_key_u64(key);
2990
2991 let message = "PARK Youngho";
2992 println!("M =\t{}", message);
2993 let mut cipher = Vec::<u8>::new();
2994 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
2995 print!("C =\t");
2996 for c in cipher.clone()
2997 { print!("{:02X} ", c); }
2998 println!();
2999 let mut txt = String::new();
3000 for c in cipher.clone()
3001 { write!(txt, "{:02X} ", c); }
3002 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
3003
3004 let mut recovered = vec![0; 16];
3005 let len = a_des.decrypt(cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
3006 print!("Ba =\t");
3007 for b in recovered.clone()
3008 { print!("{:02X} ", b); }
3009 println!();
3010 let mut txt = String::new();
3011 for c in recovered.clone()
3012 { write!(txt, "{:02X} ", c); }
3013 assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F 00 00 00 00 ");
3014
3015 let mut converted = String::new();
3016 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3017 converted.truncate(len as usize);
3018
3019 println!("Bb =\t{}", converted);
3020 assert_eq!(converted, "PARK Youngho");
3021 assert_eq!(converted, message);
3022 println!();
3023
3024
3025 // Normal case for the message of 16 bytes
3026 let key = 0x_1234567890ABCDEF_u64;
3027 println!("K =\t{:#016X}", key);
3028 let mut a_des = DES::new_with_key_u64(key);
3029
3030 let message = "고맙습니다.";
3031 println!("M =\t{}", message);
3032 let mut cipher = Vec::<u8>::new();
3033 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3034 print!("C =\t");
3035 for c in cipher.clone()
3036 { print!("{:02X} ", c); }
3037 println!();
3038 let mut txt = String::new();
3039 for c in cipher.clone()
3040 { write!(txt, "{:02X} ", c); }
3041 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
3042
3043 let mut recovered = vec![0; 24];
3044 let len = a_des.decrypt(cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
3045 print!("Ba =\t");
3046 for b in recovered.clone()
3047 { print!("{:02X} ", b); }
3048 println!();
3049 let mut txt = String::new();
3050 for c in recovered.clone()
3051 { write!(txt, "{:02X} ", c); }
3052 assert_eq!(txt, "EA B3 A0 EB A7 99 EC 8A B5 EB 8B 88 EB 8B A4 2E 00 00 00 00 00 00 00 00 ");
3053
3054 let mut converted = String::new();
3055 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3056 converted.truncate(len as usize);
3057
3058 println!("Bb =\t{}", converted);
3059 assert_eq!(converted, "고맙습니다.");
3060 assert_eq!(converted, message);
3061 println!("-------------------------------");
3062}
3063
3064fn des_decrypt_with_padding_iso_ecb_into_vec()
3065{
3066 println!("des_decrypt_with_padding_iso_ecb_into_vec");
3067 use std::io::Write;
3068 use std::fmt::Write as _;
3069 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
3070
3071 // Normal case
3072 let key = 0x_1234567890ABCDEF_u64;
3073 println!("K =\t{:#016X}", key);
3074 let mut a_des = DES::new_with_key_u64(key);
3075
3076 let message = "In the beginning God created the heavens and the earth.";
3077 println!("M =\t{}", message);
3078 let mut cipher = Vec::<u8>::new();
3079 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3080 print!("C (16 rounds) =\t");
3081 for c in cipher.clone()
3082 { print!("{:02X} ", c); }
3083 println!();
3084 let mut txt = String::new();
3085 for c in cipher.clone()
3086 { write!(txt, "{:02X} ", c); }
3087 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 F4 BE 6B A5 C5 7D F6 5D ");
3088
3089 let mut recovered = Vec::<u8>::new();
3090 a_des.decrypt_into_vec(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3091 print!("Ba (16 rounds) =\t");
3092 for b in recovered.clone()
3093 { print!("{:02X} ", b); }
3094 println!();
3095 let mut txt = String::new();
3096 for c in recovered.clone()
3097 { write!(txt, "{:02X} ", c); }
3098 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 ");
3099
3100 let mut converted = String::new();
3101 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3102
3103 println!("Bb (16 rounds) =\t{}", converted);
3104 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3105 assert_eq!(converted, message);
3106 println!();
3107
3108 // Expanded case for 128 rounds
3109 let key = 0x_1234567890ABCDEF_u64;
3110 println!("K =\t{:#016X}", key);
3111 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
3112
3113 let message = "In the beginning God created the heavens and the earth.";
3114 println!("M =\t{}", message);
3115 let mut cipher = Vec::<u8>::new();
3116 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3117 print!("C (128 rounds) =\t");
3118 for c in cipher.clone()
3119 { print!("{:02X} ", c); }
3120 println!();
3121 let mut txt = String::new();
3122 for c in cipher.clone()
3123 { write!(txt, "{:02X} ", c); }
3124 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
3125
3126 let mut recovered = Vec::<u8>::new();
3127 a_des.decrypt_into_vec(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3128 print!("Ba (128 rounds) =\t");
3129 for b in recovered.clone()
3130 { print!("{:02X} ", b); }
3131 println!();
3132 let mut txt = String::new();
3133 for c in recovered.clone()
3134 { write!(txt, "{:02X} ", c); }
3135 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 ");
3136
3137 let mut converted = String::new();
3138 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3139
3140 println!("Bb (128 rounds) =\t{}", converted);
3141 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3142 assert_eq!(converted, message);
3143 println!();
3144
3145 // Expanded case for 0 rounds which means that key is meaningless
3146 let key1 = 0x_1234567890ABCDEF_u64;
3147 let key2 = 0_u64;
3148 println!("K =\t{:#016X}", key);
3149 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
3150 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
3151
3152 let message = "In the beginning God created the heavens and the earth.";
3153 println!("M =\t{}", message);
3154 let mut cipher1 = Vec::<u8>::new();
3155 let mut cipher2 = Vec::<u8>::new();
3156 c_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher1);
3157 d_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher2);
3158 print!("C (0 rounds) =\t");
3159 for c in cipher1.clone()
3160 { print!("{:02X} ", c); }
3161 println!();
3162 let mut txt = String::new();
3163 for c in cipher1.clone()
3164 { write!(txt, "{:02X} ", c); }
3165 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
3166 print!("D (0 rounds) =\t");
3167 for c in cipher2.clone()
3168 { print!("{:02X} ", c); }
3169 println!();
3170 let mut txt = String::new();
3171 for c in cipher2.clone()
3172 { write!(txt, "{:02X} ", c); }
3173 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
3174
3175 let mut recovered1 = Vec::<u8>::new();
3176 let mut recovered2 = Vec::<u8>::new();
3177 c_des.decrypt_into_vec(cipher1.as_ptr(), cipher1.len() as u64, &mut recovered1);
3178 d_des.decrypt_into_vec(cipher2.as_ptr(), cipher2.len() as u64, &mut recovered2);
3179 print!("B1a (0 rounds) =\t");
3180 for b in recovered1.clone()
3181 { print!("{:02X} ", b); }
3182 println!();
3183 let mut txt = String::new();
3184 for c in recovered1.clone()
3185 { write!(txt, "{:02X} ", c); }
3186 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 ");
3187 print!("B2a (0 rounds) =\t");
3188 for b in recovered2.clone()
3189 { print!("{:02X} ", b); }
3190 println!();
3191 let mut txt = String::new();
3192 for c in recovered2.clone()
3193 { write!(txt, "{:02X} ", c); }
3194 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 ");
3195
3196 let mut converted1 = String::new();
3197 let mut converted2 = String::new();
3198 unsafe { converted1.as_mut_vec() }.append(&mut recovered1);
3199 unsafe { converted2.as_mut_vec() }.append(&mut recovered2);
3200
3201 println!("B1b (0 rounds) =\t{}", converted1);
3202 assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
3203 assert_eq!(converted1, message);
3204 println!("B2b (0 rounds) =\t{}", converted2);
3205 assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
3206 assert_eq!(converted2, message);
3207 assert_eq!(converted1, converted1);
3208 println!();
3209
3210 // Normal case for the message of 0 bytes
3211 let key = 0x_1234567890ABCDEF_u64;
3212 println!("K =\t{:#016X}", key);
3213 let mut a_des = DES::new_with_key_u64(key);
3214
3215 let message = "";
3216 println!("M =\t{}", message);
3217 let mut cipher = Vec::<u8>::new();
3218 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3219 print!("C =\t");
3220 for c in cipher.clone()
3221 { print!("{:02X} ", c); }
3222 println!();
3223 let mut txt = String::new();
3224 for c in cipher.clone()
3225 { write!(txt, "{:02X} ", c); }
3226 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
3227
3228 let mut recovered = Vec::<u8>::new();
3229 a_des.decrypt_into_vec(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3230 print!("Ba =\t");
3231 for b in recovered.clone()
3232 { print!("{:02X} ", b); }
3233 println!();
3234 let mut txt = String::new();
3235 for c in recovered.clone()
3236 { write!(txt, "{:02X} ", c); }
3237 assert_eq!(txt, "");
3238
3239 let mut converted = String::new();
3240 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3241
3242 println!("Bb =\t{}", converted);
3243 assert_eq!(converted, "");
3244 assert_eq!(converted, message);
3245 println!();
3246
3247 // Normal case for the message shorter than 8 bytes
3248 let key = 0x_1234567890ABCDEF_u64;
3249 println!("K =\t{:#016X}", key);
3250 let mut a_des = DES::new_with_key_u64(key);
3251
3252 let message = "7 bytes";
3253 println!("M =\t{}", message);
3254 let mut cipher = Vec::<u8>::new();
3255 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3256 print!("C =\t");
3257 for c in cipher.clone()
3258 { print!("{:02X} ", c); }
3259 println!();
3260 let mut txt = String::new();
3261 for c in cipher.clone()
3262 { write!(txt, "{:02X} ", c); }
3263 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
3264
3265 let mut recovered = Vec::<u8>::new();
3266 a_des.decrypt_into_vec(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3267 print!("Ba =\t");
3268 for b in recovered.clone()
3269 { print!("{:02X} ", b); }
3270 println!();
3271 let mut txt = String::new();
3272 for c in recovered.clone()
3273 { write!(txt, "{:02X} ", c); }
3274 assert_eq!(txt, "37 20 62 79 74 65 73 ");
3275
3276 let mut converted = String::new();
3277 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3278
3279 println!("Bb =\t{}", converted);
3280 assert_eq!(converted, "7 bytes");
3281 assert_eq!(converted, message);
3282 println!();
3283
3284 // Normal case for the message of 8 bytes
3285 let key = 0x_1234567890ABCDEF_u64;
3286 println!("K =\t{:#016X}", key);
3287 let mut a_des = DES::new_with_key_u64(key);
3288
3289 let message = "I am OK.";
3290 println!("M =\t{}", message);
3291 let mut cipher = Vec::<u8>::new();
3292 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3293 print!("C =\t");
3294 for c in cipher.clone()
3295 { print!("{:02X} ", c); }
3296 println!();
3297 let mut txt = String::new();
3298 for c in cipher.clone()
3299 { write!(txt, "{:02X} ", c); }
3300 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
3301
3302 let mut recovered = Vec::<u8>::new();
3303 a_des.decrypt_into_vec(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3304 print!("Ba =\t");
3305 for b in recovered.clone()
3306 { print!("{:02X} ", b); }
3307 println!();
3308 let mut txt = String::new();
3309 for c in recovered.clone()
3310 { write!(txt, "{:02X} ", c); }
3311 assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E ");
3312
3313 let mut converted = String::new();
3314 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3315
3316 println!("Bb =\t{}", converted);
3317 assert_eq!(converted, "I am OK.");
3318 assert_eq!(converted, message);
3319 println!();
3320
3321 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
3322 let key = 0x_1234567890ABCDEF_u64;
3323 println!("K =\t{:#016X}", key);
3324 let mut a_des = DES::new_with_key_u64(key);
3325
3326 let message = "PARK Youngho";
3327 println!("M =\t{}", message);
3328 let mut cipher = Vec::<u8>::new();
3329 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3330 print!("C =\t");
3331 for c in cipher.clone()
3332 { print!("{:02X} ", c); }
3333 println!();
3334 let mut txt = String::new();
3335 for c in cipher.clone()
3336 { write!(txt, "{:02X} ", c); }
3337 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
3338
3339 let mut recovered = Vec::<u8>::new();
3340 a_des.decrypt_into_vec(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3341 print!("Ba =\t");
3342 for b in recovered.clone()
3343 { print!("{:02X} ", b); }
3344 println!();
3345 let mut txt = String::new();
3346 for c in recovered.clone()
3347 { write!(txt, "{:02X} ", c); }
3348 assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F ");
3349
3350 let mut converted = String::new();
3351 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3352
3353 println!("Bb =\t{}", converted);
3354 assert_eq!(converted, "PARK Youngho");
3355 assert_eq!(converted, message);
3356 println!();
3357
3358 // Normal case for the message of 16 bytes
3359 let key = 0x_1234567890ABCDEF_u64;
3360 println!("K =\t{:#016X}", key);
3361 let mut a_des = DES::new_with_key_u64(key);
3362
3363 let message = "고맙습니다.";
3364 println!("M =\t{}", message);
3365 let mut cipher = Vec::<u8>::new();
3366 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3367 print!("C =\t");
3368 for c in cipher.clone()
3369 { print!("{:02X} ", c); }
3370 println!();
3371 let mut txt = String::new();
3372 for c in cipher.clone()
3373 { write!(txt, "{:02X} ", c); }
3374 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
3375
3376 let mut recovered = Vec::<u8>::new();
3377 a_des.decrypt_into_vec(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3378 print!("Ba =\t");
3379 for b in recovered.clone()
3380 { print!("{:02X} ", b); }
3381 println!();
3382 let mut txt = String::new();
3383 for c in recovered.clone()
3384 { write!(txt, "{:02X} ", c); }
3385 assert_eq!(txt, "EA B3 A0 EB A7 99 EC 8A B5 EB 8B 88 EB 8B A4 2E ");
3386
3387 let mut converted = String::new();
3388 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3389
3390 println!("Bb =\t{}", converted);
3391 assert_eq!(converted, "고맙습니다.");
3392 assert_eq!(converted, message);
3393 println!("-------------------------------");
3394}
3395
3396fn des_decrypt_with_padding_iso_ecb_into_array()
3397{
3398 println!("des_decrypt_with_padding_iso_ecb_into_array");
3399 use std::io::Write;
3400 use std::fmt::Write as _;
3401 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
3402
3403 // Normal case
3404 let key = 0x_1234567890ABCDEF_u64;
3405 println!("K =\t{:#016X}", key);
3406 let mut a_des = DES::new_with_key_u64(key);
3407
3408 let message = "In the beginning God created the heavens and the earth.";
3409 println!("M =\t{}", message);
3410 let mut cipher = Vec::<u8>::new();
3411 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3412 print!("C (16 rounds) =\t");
3413 for c in cipher.clone()
3414 { print!("{:02X} ", c); }
3415 println!();
3416 let mut txt = String::new();
3417 for c in cipher.clone()
3418 { write!(txt, "{:02X} ", c); }
3419 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 F4 BE 6B A5 C5 7D F6 5D ");
3420
3421 let mut recovered = [0u8; 56];
3422 let len = a_des.decrypt_into_array(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3423 print!("Ba (16 rounds) =\t");
3424 for b in recovered.clone()
3425 { print!("{:02X} ", b); }
3426 println!();
3427 let mut txt = String::new();
3428 for c in recovered.clone()
3429 { write!(txt, "{:02X} ", c); }
3430 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 ");
3431
3432 let mut converted = String::new();
3433 unsafe { converted.as_mut_vec() }.write(&recovered);
3434 unsafe { converted.as_mut_vec() }.truncate(len as usize);
3435 println!("Bb (16 rounds) =\t{}", converted);
3436 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3437 assert_eq!(converted, message);
3438 println!();
3439
3440 // Expanded case for 128 rounds
3441 let key = 0x_1234567890ABCDEF_u64;
3442 println!("K =\t{:#016X}", key);
3443 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
3444
3445 let message = "In the beginning God created the heavens and the earth.";
3446 println!("M =\t{}", message);
3447 let mut cipher = Vec::<u8>::new();
3448 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3449 print!("C (128 rounds) =\t");
3450 for c in cipher.clone()
3451 { print!("{:02X} ", c); }
3452 println!();
3453 let mut txt = String::new();
3454 for c in cipher.clone()
3455 { write!(txt, "{:02X} ", c); }
3456 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
3457
3458 let mut recovered = [0u8; 56];
3459 let len = a_des.decrypt_into_array(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3460 print!("Ba (16 rounds) =\t");
3461 for b in recovered.clone()
3462 { print!("{:02X} ", b); }
3463 println!();
3464 let mut txt = String::new();
3465 for c in recovered.clone()
3466 { write!(txt, "{:02X} ", c); }
3467 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 ");
3468
3469 let mut converted = String::new();
3470 unsafe { converted.as_mut_vec() }.write(&recovered);
3471 unsafe { converted.as_mut_vec() }.truncate(len as usize);
3472 println!("Bb (16 rounds) =\t{}", converted);
3473 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3474 assert_eq!(converted, message);
3475 println!();
3476
3477 // Expanded case for 0 rounds which means that key is meaningless
3478 let key1 = 0x_1234567890ABCDEF_u64;
3479 let key2 = 0_u64;
3480 println!("K =\t{:#016X}", key);
3481 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
3482 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
3483
3484 let message = "In the beginning God created the heavens and the earth.";
3485 println!("M =\t{}", message);
3486 let mut cipher1 = Vec::<u8>::new();
3487 let mut cipher2 = Vec::<u8>::new();
3488 c_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher1);
3489 d_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher2);
3490 print!("C (0 rounds) =\t");
3491 for c in cipher1.clone()
3492 { print!("{:02X} ", c); }
3493 println!();
3494 let mut txt = String::new();
3495 for c in cipher1.clone()
3496 { write!(txt, "{:02X} ", c); }
3497 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
3498 print!("D (0 rounds) =\t");
3499 for c in cipher2.clone()
3500 { print!("{:02X} ", c); }
3501 println!();
3502 let mut txt = String::new();
3503 for c in cipher2.clone()
3504 { write!(txt, "{:02X} ", c); }
3505 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
3506
3507 let mut recovered1 = [0u8; 56];
3508 let mut recovered2 = [0u8; 56];
3509 let len1 = c_des.decrypt_into_array(cipher1.as_ptr(), cipher1.len() as u64, &mut recovered1);
3510 let len2 = d_des.decrypt_into_array(cipher2.as_ptr(), cipher2.len() as u64, &mut recovered2);
3511 print!("B1a (0 rounds) =\t");
3512 for b in recovered1.clone()
3513 { print!("{:02X} ", b); }
3514 println!();
3515 let mut txt = String::new();
3516 for c in recovered1.clone()
3517 { write!(txt, "{:02X} ", c); }
3518 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 ");
3519 print!("B2a (0 rounds) =\t");
3520 for b in recovered2.clone()
3521 { print!("{:02X} ", b); }
3522 println!();
3523 let mut txt = String::new();
3524 for c in recovered.clone()
3525 { write!(txt, "{:02X} ", c); }
3526 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 ");
3527
3528 let mut converted1 = String::new();
3529 let mut converted2 = String::new();
3530 unsafe { converted1.as_mut_vec() }.write(&recovered1);
3531 unsafe { converted2.as_mut_vec() }.write(&recovered2);
3532 unsafe { converted1.as_mut_vec() }.truncate(len1 as usize);
3533 unsafe { converted2.as_mut_vec() }.truncate(len2 as usize);
3534 println!("B1b (0 rounds) =\t{}", converted1);
3535 println!("B2b (0 rounds) =\t{}", converted2);
3536 assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
3537 assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
3538 assert_eq!(converted1, message);
3539 assert_eq!(converted2, message);
3540 assert_eq!(converted1, converted2);
3541 println!();
3542
3543 // Normal case for the message of 0 bytes
3544 let key = 0x_1234567890ABCDEF_u64;
3545 println!("K =\t{:#016X}", key);
3546 let mut a_des = DES::new_with_key_u64(key);
3547
3548 let message = "";
3549 println!("M =\t{}", message);
3550 let mut cipher = Vec::<u8>::new();
3551 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3552 print!("C =\t");
3553 for c in cipher.clone()
3554 { print!("{:02X} ", c); }
3555 println!();
3556 let mut txt = String::new();
3557 for c in cipher.clone()
3558 { write!(txt, "{:02X} ", c); }
3559 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
3560
3561 let mut recovered = [0u8; 8];
3562 let len = a_des.decrypt_into_array(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3563
3564 print!("Ba =\t");
3565 for b in recovered.clone()
3566 { print!("{:02X} ", b); }
3567 println!();
3568 let mut txt = String::new();
3569 for c in recovered.clone()
3570 { write!(txt, "{:02X} ", c); }
3571 assert_eq!(txt, "00 00 00 00 00 00 00 00 ");
3572
3573 let mut converted = String::new();
3574 unsafe { converted.as_mut_vec() }.write(&recovered);
3575 unsafe { converted.as_mut_vec() }.truncate(len as usize);
3576 println!("Bb =\t{}", converted);
3577 assert_eq!(converted, "");
3578 assert_eq!(converted, message);
3579 println!();
3580
3581 // Normal case for the message shorter than 8 bytes
3582 let key = 0x_1234567890ABCDEF_u64;
3583 println!("K =\t{:#016X}", key);
3584 let mut a_des = DES::new_with_key_u64(key);
3585
3586 let message = "7 bytes";
3587 println!("M =\t{}", message);
3588 let mut cipher = Vec::<u8>::new();
3589 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3590 print!("C =\t");
3591 for c in cipher.clone()
3592 { print!("{:02X} ", c); }
3593 println!();
3594 let mut txt = String::new();
3595 for c in cipher.clone()
3596 { write!(txt, "{:02X} ", c); }
3597 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
3598
3599 let mut recovered = [0u8; 8];
3600 let len = a_des.decrypt_into_array(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3601
3602 print!("Ba =\t");
3603 for b in recovered.clone()
3604 { print!("{:02X} ", b); }
3605 println!();
3606 let mut txt = String::new();
3607 for c in recovered.clone()
3608 { write!(txt, "{:02X} ", c); }
3609 assert_eq!(txt, "37 20 62 79 74 65 73 00 ");
3610
3611 let mut converted = String::new();
3612 unsafe { converted.as_mut_vec() }.write(&recovered);
3613 unsafe { converted.as_mut_vec() }.truncate(len as usize);
3614 println!("Bb =\t{}", converted);
3615 assert_eq!(converted, "7 bytes");
3616 assert_eq!(converted, message);
3617 println!();
3618
3619 // Normal case for the message of 8 bytes
3620 let key = 0x_1234567890ABCDEF_u64;
3621 println!("K =\t{:#016X}", key);
3622 let mut a_des = DES::new_with_key_u64(key);
3623
3624 let message = "I am OK.";
3625 println!("M =\t{}", message);
3626 let mut cipher = Vec::<u8>::new();
3627 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3628 print!("C =\t");
3629 for c in cipher.clone()
3630 { print!("{:02X} ", c); }
3631 println!();
3632 let mut txt = String::new();
3633 for c in cipher.clone()
3634 { write!(txt, "{:02X} ", c); }
3635 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
3636
3637 let mut recovered = [0u8; 16];
3638 let len = a_des.decrypt_into_array(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3639
3640 print!("Ba =\t");
3641 for b in recovered.clone()
3642 { print!("{:02X} ", b); }
3643 println!();
3644 let mut txt = String::new();
3645 for c in recovered.clone()
3646 { write!(txt, "{:02X} ", c); }
3647 assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E 00 00 00 00 00 00 00 00 ");
3648
3649 let mut converted = String::new();
3650 unsafe { converted.as_mut_vec() }.write(&recovered);
3651 unsafe { converted.as_mut_vec() }.truncate(len as usize);
3652 println!("Bb =\t{}", converted);
3653 assert_eq!(converted, "I am OK.");
3654 assert_eq!(converted, message);
3655 println!();
3656
3657 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
3658 let key = 0x_1234567890ABCDEF_u64;
3659 println!("K =\t{:#016X}", key);
3660 let mut a_des = DES::new_with_key_u64(key);
3661
3662 let message = "PARK Youngho";
3663 println!("M =\t{}", message);
3664 let mut cipher = Vec::<u8>::new();
3665 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3666 print!("C =\t");
3667 for c in cipher.clone()
3668 { print!("{:02X} ", c); }
3669 println!();
3670 let mut txt = String::new();
3671 for c in cipher.clone()
3672 { write!(txt, "{:02X} ", c); }
3673 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
3674
3675 let mut recovered = [0u8; 16];
3676 let len = a_des.decrypt_into_array(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3677
3678 print!("Ba =\t");
3679 for b in recovered.clone()
3680 { print!("{:02X} ", b); }
3681 println!();
3682 let mut txt = String::new();
3683 for c in recovered.clone()
3684 { write!(txt, "{:02X} ", c); }
3685 assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F 00 00 00 00 ");
3686
3687 let mut converted = String::new();
3688 unsafe { converted.as_mut_vec() }.write(&recovered);
3689 unsafe { converted.as_mut_vec() }.truncate(len as usize);
3690 println!("Bb =\t{}", converted);
3691 assert_eq!(converted, "PARK Youngho");
3692 assert_eq!(converted, message);
3693 println!();
3694
3695 // Normal case for the message of 16 bytes
3696 let key = 0x_1234567890ABCDEF_u64;
3697 println!("K =\t{:#016X}", key);
3698 let mut a_des = DES::new_with_key_u64(key);
3699
3700 let message = "고맙습니다.";
3701 println!("M =\t{}", message);
3702 let mut cipher = Vec::<u8>::new();
3703 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3704 print!("C =\t");
3705 for c in cipher.clone()
3706 { print!("{:02X} ", c); }
3707 println!();
3708 let mut txt = String::new();
3709 for c in cipher.clone()
3710 { write!(txt, "{:02X} ", c); }
3711 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
3712
3713 let mut recovered = [0u8; 24];
3714 let len = a_des.decrypt_into_array(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3715
3716 print!("Ba =\t");
3717 for b in recovered.clone()
3718 { print!("{:02X} ", b); }
3719 println!();
3720 let mut txt = String::new();
3721 for c in recovered.clone()
3722 { write!(txt, "{:02X} ", c); }
3723 assert_eq!(txt, "EA B3 A0 EB A7 99 EC 8A B5 EB 8B 88 EB 8B A4 2E 00 00 00 00 00 00 00 00 ");
3724
3725 let mut converted = String::new();
3726 unsafe { converted.as_mut_vec() }.write(&recovered);
3727 unsafe { converted.as_mut_vec() }.truncate(len as usize);
3728 println!("Bb =\t{}", converted);
3729 assert_eq!(converted, "고맙습니다.");
3730 assert_eq!(converted, message);
3731 println!("-------------------------------");
3732}
3733
3734fn des_decrypt_with_padding_iso_ecb_into_string()
3735{
3736 println!("des_decrypt_with_padding_iso_ecb_into_string");
3737 use std::io::Write;
3738 use std::fmt::Write as _;
3739 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
3740
3741 // Normal case
3742 let key = 0x_1234567890ABCDEF_u64;
3743 println!("K =\t{:#016X}", key);
3744 let mut a_des = DES::new_with_key_u64(key);
3745
3746 let message = "In the beginning God created the heavens and the earth.";
3747 println!("M =\t{}", message);
3748 let mut cipher = Vec::<u8>::new();
3749 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3750 print!("C (16 rounds) =\t");
3751 for c in cipher.clone()
3752 { print!("{:02X} ", c); }
3753 println!();
3754 let mut txt = String::new();
3755 for c in cipher.clone()
3756 { write!(txt, "{:02X} ", c); }
3757 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 F4 BE 6B A5 C5 7D F6 5D ");
3758
3759 let mut recovered = String::new();
3760 a_des.decrypt_into_string(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3761 println!("B (16 rounds) =\t{}", recovered);
3762 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
3763 assert_eq!(recovered, message);
3764 println!();
3765
3766 // Expanded case for 128 rounds
3767 let key = 0x_1234567890ABCDEF_u64;
3768 println!("K =\t{:#016X}", key);
3769 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
3770
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 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3775 print!("C (128 rounds) =\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, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
3783
3784 let mut recovered = String::new();
3785 a_des.decrypt_into_string(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3786 println!("B (128 rounds) =\t{}", recovered);
3787 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
3788 assert_eq!(recovered, message);
3789 println!();
3790
3791 // Expanded case for 0 rounds which means that key is meaningless
3792 let key1 = 0x_1234567890ABCDEF_u64;
3793 let key2 = 0_u64;
3794 println!("K =\t{:#016X}", key);
3795 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
3796 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
3797
3798 let message = "In the beginning God created the heavens and the earth.";
3799 println!("M =\t{}", message);
3800 let mut cipher1 = Vec::<u8>::new();
3801 let mut cipher2 = Vec::<u8>::new();
3802 c_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher1);
3803 d_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher2);
3804 print!("C (0 rounds) =\t");
3805 for c in cipher1.clone()
3806 { print!("{:02X} ", c); }
3807 println!();
3808 let mut txt = String::new();
3809 for c in cipher1.clone()
3810 { write!(txt, "{:02X} ", c); }
3811 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
3812 print!("D (0 rounds) =\t");
3813 for c in cipher2.clone()
3814 { print!("{:02X} ", c); }
3815 println!();
3816 let mut txt = String::new();
3817 for c in cipher2.clone()
3818 { write!(txt, "{:02X} ", c); }
3819 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
3820
3821 let mut recovered1 = String::new();
3822 let mut recovered2 = String::new();
3823 c_des.decrypt_into_string(cipher1.as_ptr(), cipher1.len() as u64, &mut recovered1);
3824 d_des.decrypt_into_string(cipher2.as_ptr(), cipher2.len() as u64, &mut recovered2);
3825 println!("B1 (0 rounds) =\t{}", recovered1);
3826 println!("B2 (0 rounds) =\t{}", recovered2);
3827 assert_eq!(recovered1, "In the beginning God created the heavens and the earth.");
3828 assert_eq!(recovered2, "In the beginning God created the heavens and the earth.");
3829 assert_eq!(recovered1, message);
3830 assert_eq!(recovered2, message);
3831 assert_eq!(recovered1, recovered2);
3832 println!();
3833
3834 // Normal case for the message of 0 bytes
3835 let key = 0x_1234567890ABCDEF_u64;
3836 println!("K =\t{:#016X}", key);
3837 let mut a_des = DES::new_with_key_u64(key);
3838
3839 let message = "";
3840 println!("M =\t{}", message);
3841 let mut cipher = Vec::<u8>::new();
3842 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3843 print!("C =\t");
3844 for c in cipher.clone()
3845 { print!("{:02X} ", c); }
3846 println!();
3847 let mut txt = String::new();
3848 for c in cipher.clone()
3849 { write!(txt, "{:02X} ", c); }
3850 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
3851
3852 let mut recovered = String::new();
3853 a_des.decrypt_into_string(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3854 println!("B =\t{}", recovered);
3855 assert_eq!(recovered, "");
3856 assert_eq!(recovered, message);
3857 println!();
3858
3859 // Normal case for the message shorter than 8 bytes
3860 let key = 0x_1234567890ABCDEF_u64;
3861 println!("K =\t{:#016X}", key);
3862 let mut a_des = DES::new_with_key_u64(key);
3863
3864 let message = "7 bytes";
3865 println!("M =\t{}", message);
3866 let mut cipher = Vec::<u8>::new();
3867 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3868 print!("C =\t");
3869 for c in cipher.clone()
3870 { print!("{:02X} ", c); }
3871 println!();
3872 let mut txt = String::new();
3873 for c in cipher.clone()
3874 { write!(txt, "{:02X} ", c); }
3875 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
3876
3877 let mut recovered = String::new();
3878 a_des.decrypt_into_string(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3879 println!("B =\t{}", recovered);
3880 assert_eq!(recovered, "7 bytes");
3881 assert_eq!(recovered, message);
3882 println!();
3883
3884 // Normal case for the message of 8 bytes
3885 let key = 0x_1234567890ABCDEF_u64;
3886 println!("K =\t{:#016X}", key);
3887 let mut a_des = DES::new_with_key_u64(key);
3888
3889 let message = "I am OK.";
3890 println!("M =\t{}", message);
3891 let mut cipher = Vec::<u8>::new();
3892 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3893 print!("C =\t");
3894 for c in cipher.clone()
3895 { print!("{:02X} ", c); }
3896 println!();
3897 let mut txt = String::new();
3898 for c in cipher.clone()
3899 { write!(txt, "{:02X} ", c); }
3900 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
3901
3902 let mut recovered = String::new();
3903 a_des.decrypt_into_string(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3904 println!("B =\t{}", recovered);
3905 assert_eq!(recovered, "I am OK.");
3906 assert_eq!(recovered, message);
3907 println!();
3908
3909 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
3910 let key = 0x_1234567890ABCDEF_u64;
3911 println!("K =\t{:#016X}", key);
3912 let mut a_des = DES::new_with_key_u64(key);
3913
3914 let message = "PARK Youngho";
3915 println!("M =\t{}", message);
3916 let mut cipher = Vec::<u8>::new();
3917 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3918 print!("C =\t");
3919 for c in cipher.clone()
3920 { print!("{:02X} ", c); }
3921 println!();
3922 let mut txt = String::new();
3923 for c in cipher.clone()
3924 { write!(txt, "{:02X} ", c); }
3925 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
3926
3927 let mut recovered = String::new();
3928 a_des.decrypt_into_string(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3929 println!("B =\t{}", recovered);
3930 assert_eq!(recovered, "PARK Youngho");
3931 assert_eq!(recovered, message);
3932 println!();
3933
3934 // Normal case for the message of 16 bytes
3935 let key = 0x_1234567890ABCDEF_u64;
3936 println!("K =\t{:#016X}", key);
3937 let mut a_des = DES::new_with_key_u64(key);
3938
3939 let message = "고맙습니다.";
3940 println!("M =\t{}", message);
3941 let mut cipher = Vec::<u8>::new();
3942 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3943 print!("C =\t");
3944 for c in cipher.clone()
3945 { print!("{:02X} ", c); }
3946 println!();
3947 let mut txt = String::new();
3948 for c in cipher.clone()
3949 { write!(txt, "{:02X} ", c); }
3950 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
3951
3952 let mut recovered = String::new();
3953 a_des.decrypt_into_string(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3954 println!("B =\t{}", recovered);
3955 assert_eq!(recovered, "고맙습니다.");
3956 assert_eq!(recovered, message);
3957 println!("-------------------------------");
3958}
3959
3960fn des_decrypt_vec_with_padding_iso_ecb()
3961{
3962 println!("des_decrypt_vec_with_padding_iso_ecb");
3963 use std::io::Write;
3964 use std::fmt::Write as _;
3965 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
3966
3967 // Normal case
3968 let key = 0x_1234567890ABCDEF_u64;
3969 println!("K =\t{:#016X}", key);
3970 let mut a_des = DES::new_with_key_u64(key);
3971
3972 let message = "In the beginning God created the heavens and the earth.";
3973 println!("M =\t{}", message);
3974 let mut cipher = Vec::<u8>::new();
3975 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3976 print!("C (16 rounds) =\t");
3977 for c in cipher.clone()
3978 { print!("{:02X} ", c); }
3979 println!();
3980 let mut txt = String::new();
3981 for c in cipher.clone()
3982 { write!(txt, "{:02X} ", c); }
3983 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 F4 BE 6B A5 C5 7D F6 5D ");
3984
3985 let mut recovered = vec![0; 55];
3986 a_des.decrypt_vec(&cipher, recovered.as_mut_ptr());
3987 print!("Ba (16 rounds) =\t");
3988 for b in recovered.clone()
3989 { print!("{:02X} ", b); }
3990 println!();
3991 let mut txt = String::new();
3992 for c in recovered.clone()
3993 { write!(txt, "{:02X} ", c); }
3994 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 ");
3995
3996 let mut converted = String::new();
3997 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3998
3999 println!("Bb (16 rounds) =\t{}", converted);
4000 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4001 assert_eq!(converted, message);
4002 println!();
4003
4004 // Expanded case for 128 rounds
4005 let key = 0x_1234567890ABCDEF_u64;
4006 println!("K =\t{:#016X}", key);
4007 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
4008
4009 let message = "In the beginning God created the heavens and the earth.";
4010 println!("M =\t{}", message);
4011 let mut cipher = Vec::<u8>::new();
4012 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4013 print!("C (128 rounds) =\t");
4014 for c in cipher.clone()
4015 { print!("{:02X} ", c); }
4016 println!();
4017 let mut txt = String::new();
4018 for c in cipher.clone()
4019 { write!(txt, "{:02X} ", c); }
4020 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
4021
4022 let mut recovered = vec![0; 55];
4023 a_des.decrypt_vec(&cipher, recovered.as_mut_ptr());
4024 print!("Ba (128 rounds) =\t");
4025 for b in recovered.clone()
4026 { print!("{:02X} ", b); }
4027 println!();
4028 let mut txt = String::new();
4029 for c in recovered.clone()
4030 { write!(txt, "{:02X} ", c); }
4031 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 ");
4032
4033 let mut converted = String::new();
4034 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4035
4036 println!("Bb (128 rounds) =\t{}", converted);
4037 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4038 assert_eq!(converted, message);
4039 println!();
4040
4041 // Expanded case for 0 rounds which means that key is meaningless
4042 let key1 = 0x_1234567890ABCDEF_u64;
4043 let key2 = 0_u64;
4044 println!("K =\t{:#016X}", key);
4045 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
4046 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
4047
4048 let message = "In the beginning God created the heavens and the earth.";
4049 println!("M =\t{}", message);
4050 let mut cipher1 = Vec::<u8>::new();
4051 let mut cipher2 = Vec::<u8>::new();
4052 c_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher1);
4053 d_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher2);
4054 print!("C (0 rounds) =\t");
4055 for c in cipher1.clone()
4056 { print!("{:02X} ", c); }
4057 println!();
4058 let mut txt = String::new();
4059 for c in cipher1.clone()
4060 { write!(txt, "{:02X} ", c); }
4061 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
4062 print!("D (0 rounds) =\t");
4063 for c in cipher2.clone()
4064 { print!("{:02X} ", c); }
4065 println!();
4066 let mut txt = String::new();
4067 for c in cipher2.clone()
4068 { write!(txt, "{:02X} ", c); }
4069 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
4070
4071 let mut recovered1 = vec![0; 55];
4072 let mut recovered2 = vec![0; 55];
4073 c_des.decrypt_vec(&cipher1, recovered1.as_mut_ptr());
4074 d_des.decrypt_vec(&cipher2, recovered2.as_mut_ptr());
4075 print!("B1a (0 rounds) =\t");
4076 for b in recovered1.clone()
4077 { print!("{:02X} ", b); }
4078 println!();
4079 let mut txt = String::new();
4080 for c in recovered1.clone()
4081 { write!(txt, "{:02X} ", c); }
4082 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 ");
4083 print!("B2a (0 rounds) =\t");
4084 for b in recovered2.clone()
4085 { print!("{:02X} ", b); }
4086 println!();
4087 let mut txt = String::new();
4088 for c in recovered2.clone()
4089 { write!(txt, "{:02X} ", c); }
4090 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 ");
4091
4092 let mut converted1 = String::new();
4093 let mut converted2 = String::new();
4094 unsafe { converted1.as_mut_vec() }.append(&mut recovered1);
4095 unsafe { converted2.as_mut_vec() }.append(&mut recovered2);
4096
4097 println!("B1b (0 rounds) =\t{}", converted1);
4098 assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
4099 assert_eq!(converted1, message);
4100 println!("B2b (0 rounds) =\t{}", converted2);
4101 assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
4102 assert_eq!(converted2, message);
4103 assert_eq!(converted1, converted1);
4104 println!();
4105
4106 // Normal case for the message of 0 bytes
4107 let key = 0x_1234567890ABCDEF_u64;
4108 println!("K =\t{:#016X}", key);
4109 let mut a_des = DES::new_with_key_u64(key);
4110
4111 let message = "";
4112 println!("M =\t{}", message);
4113 let mut cipher = Vec::<u8>::new();
4114 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4115 print!("C =\t");
4116 for c in cipher.clone()
4117 { print!("{:02X} ", c); }
4118 println!();
4119 let mut txt = String::new();
4120 for c in cipher.clone()
4121 { write!(txt, "{:02X} ", c); }
4122 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
4123
4124 let mut recovered = vec![0; 8];
4125 let len = a_des.decrypt_vec(&cipher, recovered.as_mut_ptr());
4126 print!("Ba =\t");
4127 for b in recovered.clone()
4128 { print!("{:02X} ", b); }
4129 println!();
4130 let mut txt = String::new();
4131 for c in recovered.clone()
4132 { write!(txt, "{:02X} ", c); }
4133 assert_eq!(txt, "00 00 00 00 00 00 00 00 ");
4134
4135 let mut converted = String::new();
4136 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4137 converted.truncate(len as usize);
4138
4139 println!("Bb =\t{}", converted);
4140 assert_eq!(converted, "");
4141 assert_eq!(converted, message);
4142 println!();
4143
4144 // Normal case for the message shorter than 8 bytes
4145 let key = 0x_1234567890ABCDEF_u64;
4146 println!("K =\t{:#016X}", key);
4147 let mut a_des = DES::new_with_key_u64(key);
4148
4149 let message = "7 bytes";
4150 println!("M =\t{}", message);
4151 let mut cipher = Vec::<u8>::new();
4152 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4153 print!("C =\t");
4154 for c in cipher.clone()
4155 { print!("{:02X} ", c); }
4156 println!();
4157 let mut txt = String::new();
4158 for c in cipher.clone()
4159 { write!(txt, "{:02X} ", c); }
4160 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
4161
4162 let mut recovered = vec![0; 8];
4163 let len = a_des.decrypt_vec(&cipher, recovered.as_mut_ptr());
4164 print!("Ba =\t");
4165 for b in recovered.clone()
4166 { print!("{:02X} ", b); }
4167 println!();
4168 let mut txt = String::new();
4169 for c in recovered.clone()
4170 { write!(txt, "{:02X} ", c); }
4171 assert_eq!(txt, "37 20 62 79 74 65 73 00 ");
4172
4173 let mut converted = String::new();
4174 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4175 converted.truncate(len as usize);
4176
4177 println!("Bb =\t{}", converted);
4178 assert_eq!(converted, "7 bytes");
4179 assert_eq!(converted, message);
4180 println!();
4181
4182 // Normal case for the message of 8 bytes
4183 let key = 0x_1234567890ABCDEF_u64;
4184 println!("K =\t{:#016X}", key);
4185 let mut a_des = DES::new_with_key_u64(key);
4186
4187 let message = "I am OK.";
4188 println!("M =\t{}", message);
4189 let mut cipher = Vec::<u8>::new();
4190 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4191 print!("C =\t");
4192 for c in cipher.clone()
4193 { print!("{:02X} ", c); }
4194 println!();
4195 let mut txt = String::new();
4196 for c in cipher.clone()
4197 { write!(txt, "{:02X} ", c); }
4198 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
4199
4200 let mut recovered = vec![0; 16];
4201 let len = a_des.decrypt_vec(&cipher, recovered.as_mut_ptr());
4202 print!("Ba =\t");
4203 for b in recovered.clone()
4204 { print!("{:02X} ", b); }
4205 println!();
4206 let mut txt = String::new();
4207 for c in recovered.clone()
4208 { write!(txt, "{:02X} ", c); }
4209 assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E 00 00 00 00 00 00 00 00 ");
4210
4211 let mut converted = String::new();
4212 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4213 converted.truncate(len as usize);
4214
4215 println!("Bb =\t{}", converted);
4216 assert_eq!(converted, "I am OK.");
4217 assert_eq!(converted, message);
4218 println!();
4219
4220 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
4221 let key = 0x_1234567890ABCDEF_u64;
4222 println!("K =\t{:#016X}", key);
4223 let mut a_des = DES::new_with_key_u64(key);
4224
4225 let message = "PARK Youngho";
4226 println!("M =\t{}", message);
4227 let mut cipher = Vec::<u8>::new();
4228 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4229 print!("C =\t");
4230 for c in cipher.clone()
4231 { print!("{:02X} ", c); }
4232 println!();
4233 let mut txt = String::new();
4234 for c in cipher.clone()
4235 { write!(txt, "{:02X} ", c); }
4236 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
4237
4238 let mut recovered = vec![0; 16];
4239 let len = a_des.decrypt_vec(&cipher, recovered.as_mut_ptr());
4240 print!("Ba =\t");
4241 for b in recovered.clone()
4242 { print!("{:02X} ", b); }
4243 println!();
4244 let mut txt = String::new();
4245 for c in recovered.clone()
4246 { write!(txt, "{:02X} ", c); }
4247 assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F 00 00 00 00 ");
4248
4249 let mut converted = String::new();
4250 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4251 converted.truncate(len as usize);
4252
4253 println!("Bb =\t{}", converted);
4254 assert_eq!(converted, "PARK Youngho");
4255 assert_eq!(converted, message);
4256 println!();
4257
4258 // Normal case for the message of 16 bytes
4259 let key = 0x_1234567890ABCDEF_u64;
4260 println!("K =\t{:#016X}", key);
4261 let mut a_des = DES::new_with_key_u64(key);
4262
4263 let message = "고맙습니다.";
4264 println!("M =\t{}", message);
4265 let mut cipher = Vec::<u8>::new();
4266 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4267 print!("C =\t");
4268 for c in cipher.clone()
4269 { print!("{:02X} ", c); }
4270 println!();
4271 let mut txt = String::new();
4272 for c in cipher.clone()
4273 { write!(txt, "{:02X} ", c); }
4274 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
4275
4276 let mut recovered = vec![0; 24];
4277 let len = a_des.decrypt_vec(&cipher, recovered.as_mut_ptr());
4278 print!("Ba =\t");
4279 for b in recovered.clone()
4280 { print!("{:02X} ", b); }
4281 println!();
4282 let mut txt = String::new();
4283 for c in recovered.clone()
4284 { write!(txt, "{:02X} ", c); }
4285 assert_eq!(txt, "EA B3 A0 EB A7 99 EC 8A B5 EB 8B 88 EB 8B A4 2E 00 00 00 00 00 00 00 00 ");
4286
4287 let mut converted = String::new();
4288 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4289 converted.truncate(len as usize);
4290
4291 println!("Bb =\t{}", converted);
4292 assert_eq!(converted, "고맙습니다.");
4293 assert_eq!(converted, message);
4294 println!("-------------------------------");
4295}
4296
4297fn des_decrypt_vec_with_padding_iso_ecb_into_vec()
4298{
4299 println!("des_decrypt_vec_with_padding_iso_ecb_into_vec");
4300 use std::io::Write;
4301 use std::fmt::Write as _;
4302 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
4303
4304 // Normal case
4305 let key = 0x_1234567890ABCDEF_u64;
4306 println!("K =\t{:#016X}", key);
4307 let mut a_des = DES::new_with_key_u64(key);
4308
4309 let message = "In the beginning God created the heavens and the earth.";
4310 println!("M =\t{}", message);
4311 let mut cipher = Vec::<u8>::new();
4312 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4313 print!("C (16 rounds) =\t");
4314 for c in cipher.clone()
4315 { print!("{:02X} ", c); }
4316 println!();
4317 let mut txt = String::new();
4318 for c in cipher.clone()
4319 { write!(txt, "{:02X} ", c); }
4320 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 F4 BE 6B A5 C5 7D F6 5D ");
4321
4322 let mut recovered = Vec::<u8>::new();
4323 a_des.decrypt_vec_into_vec(&cipher, &mut recovered);
4324 print!("Ba (16 rounds) =\t");
4325 for b in recovered.clone()
4326 { print!("{:02X} ", b); }
4327 println!();
4328 let mut txt = String::new();
4329 for c in recovered.clone()
4330 { write!(txt, "{:02X} ", c); }
4331 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 ");
4332
4333 let mut converted = String::new();
4334 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4335
4336 println!("Bb (16 rounds) =\t{}", converted);
4337 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4338 assert_eq!(converted, message);
4339 println!();
4340
4341 // Expanded case for 128 rounds
4342 let key = 0x_1234567890ABCDEF_u64;
4343 println!("K =\t{:#016X}", key);
4344 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::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 = Vec::<u8>::new();
4349 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4350 print!("C (128 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, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
4358
4359 let mut recovered = Vec::<u8>::new();
4360 a_des.decrypt_vec_into_vec(&cipher, &mut recovered);
4361 print!("Ba (128 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 (128 rounds) =\t{}", converted);
4374 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4375 assert_eq!(converted, message);
4376 println!();
4377
4378 // Expanded case for 0 rounds which means that key is meaningless
4379 let key1 = 0x_1234567890ABCDEF_u64;
4380 let key2 = 0_u64;
4381 println!("K =\t{:#016X}", key);
4382 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
4383 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
4384
4385 let message = "In the beginning God created the heavens and the earth.";
4386 println!("M =\t{}", message);
4387 let mut cipher1 = Vec::<u8>::new();
4388 let mut cipher2 = Vec::<u8>::new();
4389 c_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher1);
4390 d_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher2);
4391 print!("C (0 rounds) =\t");
4392 for c in cipher1.clone()
4393 { print!("{:02X} ", c); }
4394 println!();
4395 let mut txt = String::new();
4396 for c in cipher1.clone()
4397 { write!(txt, "{:02X} ", c); }
4398 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
4399 print!("D (0 rounds) =\t");
4400 for c in cipher2.clone()
4401 { print!("{:02X} ", c); }
4402 println!();
4403 let mut txt = String::new();
4404 for c in cipher2.clone()
4405 { write!(txt, "{:02X} ", c); }
4406 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
4407
4408 let mut recovered1 = Vec::<u8>::new();
4409 let mut recovered2 = Vec::<u8>::new();
4410 c_des.decrypt_vec_into_vec(&cipher1, &mut recovered1);
4411 d_des.decrypt_vec_into_vec(&cipher2, &mut recovered2);
4412 print!("B1a (0 rounds) =\t");
4413 for b in recovered1.clone()
4414 { print!("{:02X} ", b); }
4415 println!();
4416 let mut txt = String::new();
4417 for c in recovered1.clone()
4418 { write!(txt, "{:02X} ", c); }
4419 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 ");
4420 print!("B2a (0 rounds) =\t");
4421 for b in recovered2.clone()
4422 { print!("{:02X} ", b); }
4423 println!();
4424 let mut txt = String::new();
4425 for c in recovered2.clone()
4426 { write!(txt, "{:02X} ", c); }
4427 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 ");
4428
4429 let mut converted1 = String::new();
4430 let mut converted2 = String::new();
4431 unsafe { converted1.as_mut_vec() }.append(&mut recovered1);
4432 unsafe { converted2.as_mut_vec() }.append(&mut recovered2);
4433
4434 println!("B1b (0 rounds) =\t{}", converted1);
4435 assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
4436 assert_eq!(converted1, message);
4437 println!("B2b (0 rounds) =\t{}", converted2);
4438 assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
4439 assert_eq!(converted2, message);
4440 assert_eq!(converted1, converted1);
4441 println!();
4442
4443 // Normal case for the message of 0 bytes
4444 let key = 0x_1234567890ABCDEF_u64;
4445 println!("K =\t{:#016X}", key);
4446 let mut a_des = DES::new_with_key_u64(key);
4447
4448 let message = "";
4449 println!("M =\t{}", message);
4450 let mut cipher = Vec::<u8>::new();
4451 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4452 print!("C =\t");
4453 for c in cipher.clone()
4454 { print!("{:02X} ", c); }
4455 println!();
4456 let mut txt = String::new();
4457 for c in cipher.clone()
4458 { write!(txt, "{:02X} ", c); }
4459 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
4460
4461 let mut recovered = Vec::<u8>::new();
4462 a_des.decrypt_vec_into_vec(&cipher, &mut recovered);
4463 print!("Ba =\t");
4464 for b in recovered.clone()
4465 { print!("{:02X} ", b); }
4466 println!();
4467 let mut txt = String::new();
4468 for c in recovered.clone()
4469 { write!(txt, "{:02X} ", c); }
4470 assert_eq!(txt, "");
4471
4472 let mut converted = String::new();
4473 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4474
4475 println!("Bb =\t{}", converted);
4476 assert_eq!(converted, "");
4477 assert_eq!(converted, message);
4478 println!();
4479
4480 // Normal case for the message shorter than 8 bytes
4481 let key = 0x_1234567890ABCDEF_u64;
4482 println!("K =\t{:#016X}", key);
4483 let mut a_des = DES::new_with_key_u64(key);
4484
4485 let message = "7 bytes";
4486 println!("M =\t{}", message);
4487 let mut cipher = Vec::<u8>::new();
4488 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4489 print!("C =\t");
4490 for c in cipher.clone()
4491 { print!("{:02X} ", c); }
4492 println!();
4493 let mut txt = String::new();
4494 for c in cipher.clone()
4495 { write!(txt, "{:02X} ", c); }
4496 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
4497
4498 let mut recovered = Vec::<u8>::new();
4499 a_des.decrypt_vec_into_vec(&cipher, &mut recovered);
4500 print!("Ba =\t");
4501 for b in recovered.clone()
4502 { print!("{:02X} ", b); }
4503 println!();
4504 let mut txt = String::new();
4505 for c in recovered.clone()
4506 { write!(txt, "{:02X} ", c); }
4507 assert_eq!(txt, "37 20 62 79 74 65 73 ");
4508
4509 let mut converted = String::new();
4510 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4511
4512 println!("Bb =\t{}", converted);
4513 assert_eq!(converted, "7 bytes");
4514 assert_eq!(converted, message);
4515 println!();
4516
4517 // Normal case for the message of 8 bytes
4518 let key = 0x_1234567890ABCDEF_u64;
4519 println!("K =\t{:#016X}", key);
4520 let mut a_des = DES::new_with_key_u64(key);
4521
4522 let message = "I am OK.";
4523 println!("M =\t{}", message);
4524 let mut cipher = Vec::<u8>::new();
4525 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4526 print!("C =\t");
4527 for c in cipher.clone()
4528 { print!("{:02X} ", c); }
4529 println!();
4530 let mut txt = String::new();
4531 for c in cipher.clone()
4532 { write!(txt, "{:02X} ", c); }
4533 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
4534
4535 let mut recovered = Vec::<u8>::new();
4536 a_des.decrypt_vec_into_vec(&cipher, &mut recovered);
4537 print!("Ba =\t");
4538 for b in recovered.clone()
4539 { print!("{:02X} ", b); }
4540 println!();
4541 let mut txt = String::new();
4542 for c in recovered.clone()
4543 { write!(txt, "{:02X} ", c); }
4544 assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E ");
4545
4546 let mut converted = String::new();
4547 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4548
4549 println!("Bb =\t{}", converted);
4550 assert_eq!(converted, "I am OK.");
4551 assert_eq!(converted, message);
4552 println!();
4553
4554 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
4555 let key = 0x_1234567890ABCDEF_u64;
4556 println!("K =\t{:#016X}", key);
4557 let mut a_des = DES::new_with_key_u64(key);
4558
4559 let message = "PARK Youngho";
4560 println!("M =\t{}", message);
4561 let mut cipher = Vec::<u8>::new();
4562 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4563 print!("C =\t");
4564 for c in cipher.clone()
4565 { print!("{:02X} ", c); }
4566 println!();
4567 let mut txt = String::new();
4568 for c in cipher.clone()
4569 { write!(txt, "{:02X} ", c); }
4570 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
4571
4572 let mut recovered = Vec::<u8>::new();
4573 a_des.decrypt_vec_into_vec(&cipher, &mut recovered);
4574 print!("Ba =\t");
4575 for b in recovered.clone()
4576 { print!("{:02X} ", b); }
4577 println!();
4578 let mut txt = String::new();
4579 for c in recovered.clone()
4580 { write!(txt, "{:02X} ", c); }
4581 assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F ");
4582
4583 let mut converted = String::new();
4584 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4585
4586 println!("Bb =\t{}", converted);
4587 assert_eq!(converted, "PARK Youngho");
4588 assert_eq!(converted, message);
4589 println!();
4590
4591 // Normal case for the message of 16 bytes
4592 let key = 0x_1234567890ABCDEF_u64;
4593 println!("K =\t{:#016X}", key);
4594 let mut a_des = DES::new_with_key_u64(key);
4595
4596 let message = "고맙습니다.";
4597 println!("M =\t{}", message);
4598 let mut cipher = Vec::<u8>::new();
4599 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4600 print!("C =\t");
4601 for c in cipher.clone()
4602 { print!("{:02X} ", c); }
4603 println!();
4604 let mut txt = String::new();
4605 for c in cipher.clone()
4606 { write!(txt, "{:02X} ", c); }
4607 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
4608
4609 let mut recovered = Vec::<u8>::new();
4610 a_des.decrypt_vec_into_vec(&cipher, &mut recovered);
4611 print!("Ba =\t");
4612 for b in recovered.clone()
4613 { print!("{:02X} ", b); }
4614 println!();
4615 let mut txt = String::new();
4616 for c in recovered.clone()
4617 { write!(txt, "{:02X} ", c); }
4618 assert_eq!(txt, "EA B3 A0 EB A7 99 EC 8A B5 EB 8B 88 EB 8B A4 2E ");
4619
4620 let mut converted = String::new();
4621 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4622
4623 println!("Bb =\t{}", converted);
4624 assert_eq!(converted, "고맙습니다.");
4625 assert_eq!(converted, message);
4626 println!("-------------------------------");
4627}
4628
4629fn des_decrypt_vec_with_padding_iso_ecb_into_array()
4630{
4631 println!("des_decrypt_vec_with_padding_iso_ecb_into_array");
4632 use std::io::Write;
4633 use std::fmt::Write as _;
4634 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
4635
4636 // Normal case
4637 let key = 0x_1234567890ABCDEF_u64;
4638 println!("K =\t{:#016X}", key);
4639 let mut a_des = DES::new_with_key_u64(key);
4640
4641 let message = "In the beginning God created the heavens and the earth.";
4642 println!("M =\t{}", message);
4643 let mut cipher = Vec::<u8>::new();
4644 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4645 print!("C (16 rounds) =\t");
4646 for c in cipher.clone()
4647 { print!("{:02X} ", c); }
4648 println!();
4649 let mut txt = String::new();
4650 for c in cipher.clone()
4651 { write!(txt, "{:02X} ", c); }
4652 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 F4 BE 6B A5 C5 7D F6 5D ");
4653
4654 let mut recovered = [0u8; 56];
4655 let len = a_des.decrypt_vec_into_array(&cipher, &mut recovered);
4656 print!("Ba (16 rounds) =\t");
4657 for b in recovered.clone()
4658 { print!("{:02X} ", b); }
4659 println!();
4660 let mut txt = String::new();
4661 for c in recovered.clone()
4662 { write!(txt, "{:02X} ", c); }
4663 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 ");
4664
4665 let mut converted = String::new();
4666 unsafe { converted.as_mut_vec() }.write(&recovered);
4667 unsafe { converted.as_mut_vec() }.truncate(len as usize);
4668 println!("Bb (16 rounds) =\t{}", converted);
4669 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4670 assert_eq!(converted, message);
4671 println!();
4672
4673 // Expanded case for 128 rounds
4674 let key = 0x_1234567890ABCDEF_u64;
4675 println!("K =\t{:#016X}", key);
4676 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
4677
4678 let message = "In the beginning God created the heavens and the earth.";
4679 println!("M =\t{}", message);
4680 let mut cipher = Vec::<u8>::new();
4681 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4682 print!("C (128 rounds) =\t");
4683 for c in cipher.clone()
4684 { print!("{:02X} ", c); }
4685 println!();
4686 let mut txt = String::new();
4687 for c in cipher.clone()
4688 { write!(txt, "{:02X} ", c); }
4689 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
4690
4691 let mut recovered = [0u8; 56];
4692 let len = a_des.decrypt_vec_into_array(&cipher, &mut recovered);
4693 print!("Ba (16 rounds) =\t");
4694 for b in recovered.clone()
4695 { print!("{:02X} ", b); }
4696 println!();
4697 let mut txt = String::new();
4698 for c in recovered.clone()
4699 { write!(txt, "{:02X} ", c); }
4700 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 ");
4701
4702 let mut converted = String::new();
4703 unsafe { converted.as_mut_vec() }.write(&recovered);
4704 unsafe { converted.as_mut_vec() }.truncate(len as usize);
4705 println!("Bb (16 rounds) =\t{}", converted);
4706 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4707 assert_eq!(converted, message);
4708 println!();
4709
4710 // Expanded case for 0 rounds which means that key is meaningless
4711 let key1 = 0x_1234567890ABCDEF_u64;
4712 let key2 = 0_u64;
4713 println!("K =\t{:#016X}", key);
4714 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
4715 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
4716
4717 let message = "In the beginning God created the heavens and the earth.";
4718 println!("M =\t{}", message);
4719 let mut cipher1 = Vec::<u8>::new();
4720 let mut cipher2 = Vec::<u8>::new();
4721 c_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher1);
4722 d_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher2);
4723 print!("C (0 rounds) =\t");
4724 for c in cipher1.clone()
4725 { print!("{:02X} ", c); }
4726 println!();
4727 let mut txt = String::new();
4728 for c in cipher1.clone()
4729 { write!(txt, "{:02X} ", c); }
4730 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
4731 print!("D (0 rounds) =\t");
4732 for c in cipher2.clone()
4733 { print!("{:02X} ", c); }
4734 println!();
4735 let mut txt = String::new();
4736 for c in cipher2.clone()
4737 { write!(txt, "{:02X} ", c); }
4738 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
4739
4740 let mut recovered1 = [0u8; 56];
4741 let mut recovered2 = [0u8; 56];
4742 let len1 = c_des.decrypt_vec_into_array(&cipher1, &mut recovered1);
4743 let len2 = d_des.decrypt_vec_into_array(&cipher2, &mut recovered2);
4744 print!("B1a (0 rounds) =\t");
4745 for b in recovered1.clone()
4746 { print!("{:02X} ", b); }
4747 println!();
4748 let mut txt = String::new();
4749 for c in recovered1.clone()
4750 { write!(txt, "{:02X} ", c); }
4751 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 ");
4752 print!("B2a (0 rounds) =\t");
4753 for b in recovered2.clone()
4754 { print!("{:02X} ", b); }
4755 println!();
4756 let mut txt = String::new();
4757 for c in recovered.clone()
4758 { write!(txt, "{:02X} ", c); }
4759 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 ");
4760
4761 let mut converted1 = String::new();
4762 let mut converted2 = String::new();
4763 unsafe { converted1.as_mut_vec() }.write(&recovered1);
4764 unsafe { converted2.as_mut_vec() }.write(&recovered2);
4765 unsafe { converted1.as_mut_vec() }.truncate(len1 as usize);
4766 unsafe { converted2.as_mut_vec() }.truncate(len2 as usize);
4767 println!("B1b (0 rounds) =\t{}", converted1);
4768 println!("B2b (0 rounds) =\t{}", converted2);
4769 assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
4770 assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
4771 assert_eq!(converted1, message);
4772 assert_eq!(converted2, message);
4773 assert_eq!(converted1, converted2);
4774 println!();
4775
4776 // Normal case for the message of 0 bytes
4777 let key = 0x_1234567890ABCDEF_u64;
4778 println!("K =\t{:#016X}", key);
4779 let mut a_des = DES::new_with_key_u64(key);
4780
4781 let message = "";
4782 println!("M =\t{}", message);
4783 let mut cipher = Vec::<u8>::new();
4784 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4785 print!("C =\t");
4786 for c in cipher.clone()
4787 { print!("{:02X} ", c); }
4788 println!();
4789 let mut txt = String::new();
4790 for c in cipher.clone()
4791 { write!(txt, "{:02X} ", c); }
4792 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
4793
4794 let mut recovered = [0u8; 8];
4795 let len = a_des.decrypt_vec_into_array(&cipher, &mut recovered);
4796
4797 print!("Ba =\t");
4798 for b in recovered.clone()
4799 { print!("{:02X} ", b); }
4800 println!();
4801 let mut txt = String::new();
4802 for c in recovered.clone()
4803 { write!(txt, "{:02X} ", c); }
4804 assert_eq!(txt, "00 00 00 00 00 00 00 00 ");
4805
4806 let mut converted = String::new();
4807 unsafe { converted.as_mut_vec() }.write(&recovered);
4808 unsafe { converted.as_mut_vec() }.truncate(len as usize);
4809 println!("Bb =\t{}", converted);
4810 assert_eq!(converted, "");
4811 assert_eq!(converted, message);
4812 println!();
4813
4814 // Normal case for the message shorter than 8 bytes
4815 let key = 0x_1234567890ABCDEF_u64;
4816 println!("K =\t{:#016X}", key);
4817 let mut a_des = DES::new_with_key_u64(key);
4818
4819 let message = "7 bytes";
4820 println!("M =\t{}", message);
4821 let mut cipher = Vec::<u8>::new();
4822 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4823 print!("C =\t");
4824 for c in cipher.clone()
4825 { print!("{:02X} ", c); }
4826 println!();
4827 let mut txt = String::new();
4828 for c in cipher.clone()
4829 { write!(txt, "{:02X} ", c); }
4830 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
4831
4832 let mut recovered = [0u8; 8];
4833 let len = a_des.decrypt_vec_into_array(&cipher, &mut recovered);
4834
4835 print!("Ba =\t");
4836 for b in recovered.clone()
4837 { print!("{:02X} ", b); }
4838 println!();
4839 let mut txt = String::new();
4840 for c in recovered.clone()
4841 { write!(txt, "{:02X} ", c); }
4842 assert_eq!(txt, "37 20 62 79 74 65 73 00 ");
4843
4844 let mut converted = String::new();
4845 unsafe { converted.as_mut_vec() }.write(&recovered);
4846 unsafe { converted.as_mut_vec() }.truncate(len as usize);
4847 println!("Bb =\t{}", converted);
4848 assert_eq!(converted, "7 bytes");
4849 assert_eq!(converted, message);
4850 println!();
4851
4852 // Normal case for the message of 8 bytes
4853 let key = 0x_1234567890ABCDEF_u64;
4854 println!("K =\t{:#016X}", key);
4855 let mut a_des = DES::new_with_key_u64(key);
4856
4857 let message = "I am OK.";
4858 println!("M =\t{}", message);
4859 let mut cipher = Vec::<u8>::new();
4860 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4861 print!("C =\t");
4862 for c in cipher.clone()
4863 { print!("{:02X} ", c); }
4864 println!();
4865 let mut txt = String::new();
4866 for c in cipher.clone()
4867 { write!(txt, "{:02X} ", c); }
4868 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
4869
4870 let mut recovered = [0u8; 16];
4871 let len = a_des.decrypt_vec_into_array(&cipher, &mut recovered);
4872
4873 print!("Ba =\t");
4874 for b in recovered.clone()
4875 { print!("{:02X} ", b); }
4876 println!();
4877 let mut txt = String::new();
4878 for c in recovered.clone()
4879 { write!(txt, "{:02X} ", c); }
4880 assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E 00 00 00 00 00 00 00 00 ");
4881
4882 let mut converted = String::new();
4883 unsafe { converted.as_mut_vec() }.write(&recovered);
4884 unsafe { converted.as_mut_vec() }.truncate(len as usize);
4885 println!("Bb =\t{}", converted);
4886 assert_eq!(converted, "I am OK.");
4887 assert_eq!(converted, message);
4888 println!();
4889
4890 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
4891 let key = 0x_1234567890ABCDEF_u64;
4892 println!("K =\t{:#016X}", key);
4893 let mut a_des = DES::new_with_key_u64(key);
4894
4895 let message = "PARK Youngho";
4896 println!("M =\t{}", message);
4897 let mut cipher = Vec::<u8>::new();
4898 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4899 print!("C =\t");
4900 for c in cipher.clone()
4901 { print!("{:02X} ", c); }
4902 println!();
4903 let mut txt = String::new();
4904 for c in cipher.clone()
4905 { write!(txt, "{:02X} ", c); }
4906 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
4907
4908 let mut recovered = [0u8; 16];
4909 let len = a_des.decrypt_vec_into_array(&cipher, &mut recovered);
4910
4911 print!("Ba =\t");
4912 for b in recovered.clone()
4913 { print!("{:02X} ", b); }
4914 println!();
4915 let mut txt = String::new();
4916 for c in recovered.clone()
4917 { write!(txt, "{:02X} ", c); }
4918 assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F 00 00 00 00 ");
4919
4920 let mut converted = String::new();
4921 unsafe { converted.as_mut_vec() }.write(&recovered);
4922 unsafe { converted.as_mut_vec() }.truncate(len as usize);
4923 println!("Bb =\t{}", converted);
4924 assert_eq!(converted, "PARK Youngho");
4925 assert_eq!(converted, message);
4926 println!();
4927
4928 // Normal case for the message of 16 bytes
4929 let key = 0x_1234567890ABCDEF_u64;
4930 println!("K =\t{:#016X}", key);
4931 let mut a_des = DES::new_with_key_u64(key);
4932
4933 let message = "고맙습니다.";
4934 println!("M =\t{}", message);
4935 let mut cipher = Vec::<u8>::new();
4936 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4937 print!("C =\t");
4938 for c in cipher.clone()
4939 { print!("{:02X} ", c); }
4940 println!();
4941 let mut txt = String::new();
4942 for c in cipher.clone()
4943 { write!(txt, "{:02X} ", c); }
4944 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
4945
4946 let mut recovered = [0u8; 24];
4947 let len = a_des.decrypt_vec_into_array(&cipher, &mut recovered);
4948
4949 print!("Ba =\t");
4950 for b in recovered.clone()
4951 { print!("{:02X} ", b); }
4952 println!();
4953 let mut txt = String::new();
4954 for c in recovered.clone()
4955 { write!(txt, "{:02X} ", c); }
4956 assert_eq!(txt, "EA B3 A0 EB A7 99 EC 8A B5 EB 8B 88 EB 8B A4 2E 00 00 00 00 00 00 00 00 ");
4957
4958 let mut converted = String::new();
4959 unsafe { converted.as_mut_vec() }.write(&recovered);
4960 unsafe { converted.as_mut_vec() }.truncate(len as usize);
4961 println!("Bb =\t{}", converted);
4962 assert_eq!(converted, "고맙습니다.");
4963 assert_eq!(converted, message);
4964 println!("-------------------------------");
4965}
4966
4967fn des_decrypt_vec_with_padding_iso_ecb_into_string()
4968{
4969 println!("des_decrypt_vec_with_padding_iso_ecb_into_string");
4970 use std::io::Write;
4971 use std::fmt::Write as _;
4972 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
4973
4974 // Normal case
4975 let key = 0x_1234567890ABCDEF_u64;
4976 println!("K =\t{:#016X}", key);
4977 let mut a_des = DES::new_with_key_u64(key);
4978
4979 let message = "In the beginning God created the heavens and the earth.";
4980 println!("M =\t{}", message);
4981 let mut cipher = Vec::<u8>::new();
4982 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4983 print!("C (16 rounds) =\t");
4984 for c in cipher.clone()
4985 { print!("{:02X} ", c); }
4986 println!();
4987 let mut txt = String::new();
4988 for c in cipher.clone()
4989 { write!(txt, "{:02X} ", c); }
4990 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 F4 BE 6B A5 C5 7D F6 5D ");
4991
4992 let mut recovered = String::new();
4993 a_des.decrypt_vec_into_string(&cipher, &mut recovered);
4994 println!("B (16 rounds) =\t{}", recovered);
4995 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
4996 assert_eq!(recovered, message);
4997 println!();
4998
4999 // Expanded case for 128 rounds
5000 let key = 0x_1234567890ABCDEF_u64;
5001 println!("K =\t{:#016X}", key);
5002 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
5003
5004 let message = "In the beginning God created the heavens and the earth.";
5005 println!("M =\t{}", message);
5006 let mut cipher = Vec::<u8>::new();
5007 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
5008 print!("C (128 rounds) =\t");
5009 for c in cipher.clone()
5010 { print!("{:02X} ", c); }
5011 println!();
5012 let mut txt = String::new();
5013 for c in cipher.clone()
5014 { write!(txt, "{:02X} ", c); }
5015 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
5016
5017 let mut recovered = String::new();
5018 a_des.decrypt_vec_into_string(&cipher, &mut recovered);
5019 println!("B (128 rounds) =\t{}", recovered);
5020 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
5021 assert_eq!(recovered, message);
5022 println!();
5023
5024 // Expanded case for 0 rounds which means that key is meaningless
5025 let key1 = 0x_1234567890ABCDEF_u64;
5026 let key2 = 0_u64;
5027 println!("K =\t{:#016X}", key);
5028 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
5029 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
5030
5031 let message = "In the beginning God created the heavens and the earth.";
5032 println!("M =\t{}", message);
5033 let mut cipher1 = Vec::<u8>::new();
5034 let mut cipher2 = Vec::<u8>::new();
5035 c_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher1);
5036 d_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher2);
5037 print!("C (0 rounds) =\t");
5038 for c in cipher1.clone()
5039 { print!("{:02X} ", c); }
5040 println!();
5041 let mut txt = String::new();
5042 for c in cipher1.clone()
5043 { write!(txt, "{:02X} ", c); }
5044 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
5045 print!("D (0 rounds) =\t");
5046 for c in cipher2.clone()
5047 { print!("{:02X} ", c); }
5048 println!();
5049 let mut txt = String::new();
5050 for c in cipher2.clone()
5051 { write!(txt, "{:02X} ", c); }
5052 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
5053
5054 let mut recovered1 = String::new();
5055 let mut recovered2 = String::new();
5056 c_des.decrypt_vec_into_string(&cipher1, &mut recovered1);
5057 d_des.decrypt_vec_into_string(&cipher2, &mut recovered2);
5058 println!("B1 (0 rounds) =\t{}", recovered1);
5059 println!("B2 (0 rounds) =\t{}", recovered2);
5060 assert_eq!(recovered1, "In the beginning God created the heavens and the earth.");
5061 assert_eq!(recovered2, "In the beginning God created the heavens and the earth.");
5062 assert_eq!(recovered1, message);
5063 assert_eq!(recovered2, message);
5064 assert_eq!(recovered1, recovered2);
5065 println!();
5066
5067 // Normal case for the message of 0 bytes
5068 let key = 0x_1234567890ABCDEF_u64;
5069 println!("K =\t{:#016X}", key);
5070 let mut a_des = DES::new_with_key_u64(key);
5071
5072 let message = "";
5073 println!("M =\t{}", message);
5074 let mut cipher = Vec::<u8>::new();
5075 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
5076 print!("C =\t");
5077 for c in cipher.clone()
5078 { print!("{:02X} ", c); }
5079 println!();
5080 let mut txt = String::new();
5081 for c in cipher.clone()
5082 { write!(txt, "{:02X} ", c); }
5083 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
5084
5085 let mut recovered = String::new();
5086 a_des.decrypt_vec_into_string(&cipher, &mut recovered);
5087 println!("B =\t{}", recovered);
5088 assert_eq!(recovered, "");
5089 assert_eq!(recovered, message);
5090 println!();
5091
5092 // Normal case for the message shorter than 8 bytes
5093 let key = 0x_1234567890ABCDEF_u64;
5094 println!("K =\t{:#016X}", key);
5095 let mut a_des = DES::new_with_key_u64(key);
5096
5097 let message = "7 bytes";
5098 println!("M =\t{}", message);
5099 let mut cipher = Vec::<u8>::new();
5100 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
5101 print!("C =\t");
5102 for c in cipher.clone()
5103 { print!("{:02X} ", c); }
5104 println!();
5105 let mut txt = String::new();
5106 for c in cipher.clone()
5107 { write!(txt, "{:02X} ", c); }
5108 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
5109
5110 let mut recovered = String::new();
5111 a_des.decrypt_vec_into_string(&cipher, &mut recovered);
5112 println!("B =\t{}", recovered);
5113 assert_eq!(recovered, "7 bytes");
5114 assert_eq!(recovered, message);
5115 println!();
5116
5117 // Normal case for the message of 8 bytes
5118 let key = 0x_1234567890ABCDEF_u64;
5119 println!("K =\t{:#016X}", key);
5120 let mut a_des = DES::new_with_key_u64(key);
5121
5122 let message = "I am OK.";
5123 println!("M =\t{}", message);
5124 let mut cipher = Vec::<u8>::new();
5125 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
5126 print!("C =\t");
5127 for c in cipher.clone()
5128 { print!("{:02X} ", c); }
5129 println!();
5130 let mut txt = String::new();
5131 for c in cipher.clone()
5132 { write!(txt, "{:02X} ", c); }
5133 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
5134
5135 let mut recovered = String::new();
5136 a_des.decrypt_vec_into_string(&cipher, &mut recovered);
5137 println!("B =\t{}", recovered);
5138 assert_eq!(recovered, "I am OK.");
5139 assert_eq!(recovered, message);
5140 println!();
5141
5142 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
5143 let key = 0x_1234567890ABCDEF_u64;
5144 println!("K =\t{:#016X}", key);
5145 let mut a_des = DES::new_with_key_u64(key);
5146
5147 let message = "PARK Youngho";
5148 println!("M =\t{}", message);
5149 let mut cipher = Vec::<u8>::new();
5150 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
5151 print!("C =\t");
5152 for c in cipher.clone()
5153 { print!("{:02X} ", c); }
5154 println!();
5155 let mut txt = String::new();
5156 for c in cipher.clone()
5157 { write!(txt, "{:02X} ", c); }
5158 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
5159
5160 let mut recovered = String::new();
5161 a_des.decrypt_vec_into_string(&cipher, &mut recovered);
5162 println!("B =\t{}", recovered);
5163 assert_eq!(recovered, "PARK Youngho");
5164 assert_eq!(recovered, message);
5165 println!();
5166
5167 // Normal case for the message of 16 bytes
5168 let key = 0x_1234567890ABCDEF_u64;
5169 println!("K =\t{:#016X}", key);
5170 let mut a_des = DES::new_with_key_u64(key);
5171
5172 let message = "고맙습니다.";
5173 println!("M =\t{}", message);
5174 let mut cipher = Vec::<u8>::new();
5175 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
5176 print!("C =\t");
5177 for c in cipher.clone()
5178 { print!("{:02X} ", c); }
5179 println!();
5180 let mut txt = String::new();
5181 for c in cipher.clone()
5182 { write!(txt, "{:02X} ", c); }
5183 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
5184
5185 let mut recovered = String::new();
5186 a_des.decrypt_vec_into_string(&cipher, &mut recovered);
5187 println!("B =\t{}", recovered);
5188 assert_eq!(recovered, "고맙습니다.");
5189 assert_eq!(recovered, message);
5190 println!("-------------------------------");
5191}
Sourcefn encrypt_str(&mut self, message: &str, cipher: *mut u8) -> u64
fn encrypt_str(&mut self, message: &str, cipher: *mut u8) -> u64
Encrypts the data in str
with the padding defined
according to ISO 7816-4 in ECB (Electronic CodeBook) mode.
§Arguments
message
is astr
object, and is the plaintext to be encrypted.cipher
is a pointer to u8 which is*mut u8
, and is the ciphertext to be stored.- The size of the memory area which starts at
cipher
and the ciphertext will be stored at is assumed to be enough. - The size of the area for ciphertext should be prepared to be:
(
length_in_bytes
+ 1).next_multiple_of(8) at least whenT
isu64
, and (length_in_bytes
+ 1).next_multiple_of(16) at least whenT
isu128
. So, it is responsible for you to prepare thecipher
area big enough!
§Output
- This method returns the size of ciphertext including padding bits in bytes.
- When
T
isu64
, the output should be at least8
, and will be only any multiple of8
. - When
T
isu128
, the output should be at least16
, and will be only any multiple of16
. - If this method returns
zero
, it means this method failed in encryption.
§Features
- You are not encouraged to use this method in pure Rust programming. Instead, use other safer methods such as encrypt_str_into_*().
- This method is useful to use in hybrid programming with C/C++.
- If
message
is a null string “”, only padding bytes will be encrypted, and stored in the memory area that starts fromcipher
. - The padding bits are composed of the bytes that indicate the length of the padding bits in bytes according to ISO 7816-4 defined in RFC 5652.
- For more information about the padding bits according to ISO 7816-4, Read here.
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, ECB_ISO };
let key = 0x_1234567890ABCDEF_u64;
println!("K =\t{:#016X}", key);
let mut a_des = DES::new_with_key_u64(key);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = [0_u8; 56];
a_des.encrypt_str(&message, cipher.as_mut_ptr());
print!("C (16 rounds) =\t");
for c in cipher.clone()
{ print!("{:02X} ", c); }
println!();
let mut txt = String::new();
for c in cipher.clone()
{ write!(txt, "{:02X} ", c); }
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 F4 BE 6B A5 C5 7D F6 5D ");
§For more examples,
click here
Examples found in repository?
572fn des_encrypt_str_with_padding_iso_ecb()
573{
574 println!("des_encrypt_str_with_padding_iso_ecb");
575 use std::io::Write;
576 use std::fmt::Write as _;
577 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
578
579 // Normal case
580 let key = 0x_1234567890ABCDEF_u64;
581 println!("K =\t{:#016X}", key);
582 let mut a_des = DES::new_with_key_u64(key);
583
584 let message = "In the beginning God created the heavens and the earth.";
585 println!("M =\t{}", message);
586 let mut cipher = [0_u8; 56];
587 a_des.encrypt_str(&message, cipher.as_mut_ptr());
588 print!("C (16 rounds) =\t");
589 for c in cipher.clone()
590 { print!("{:02X} ", c); }
591 println!();
592 let mut txt = String::new();
593 for c in cipher.clone()
594 { write!(txt, "{:02X} ", c); }
595 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 F4 BE 6B A5 C5 7D F6 5D ");
596 println!();
597
598 // Expanded case for 128 rounds
599 let key = 0x_1234567890ABCDEF_u64;
600 println!("K =\t{:#016X}", key);
601 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
602
603 let message = "In the beginning God created the heavens and the earth.";
604 println!("M =\t{}", message);
605 let mut cipher = [0_u8; 56];
606 a_des.encrypt_str(&message, cipher.as_mut_ptr());
607 print!("C (128 rounds) =\t");
608 for c in cipher.clone()
609 { print!("{:02X} ", c); }
610 println!();
611 let mut txt = String::new();
612 for c in cipher.clone()
613 { write!(txt, "{:02X} ", c); }
614 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
615 println!();
616
617 // Expanded case for 0 rounds which means that key is meaningless
618 let key1 = 0x_1234567890ABCDEF_u64;
619 let key2 = 0_u64;
620 println!("K =\t{:#016X}", key);
621 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
622 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
623
624 let message = "In the beginning God created the heavens and the earth.";
625 println!("M =\t{}", message);
626 let mut cipher1 = [0_u8; 56];
627 let mut cipher2 = [0_u8; 56];
628 c_des.encrypt_str(&message, cipher1.as_mut_ptr());
629 d_des.encrypt_str(&message, cipher2.as_mut_ptr());
630 print!("C (0 rounds) =\t");
631 for c in cipher1.clone()
632 { print!("{:02X} ", c); }
633 println!();
634 let mut txt = String::new();
635 for c in cipher1.clone()
636 { write!(txt, "{:02X} ", c); }
637 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
638 print!("D (0 rounds) =\t");
639 for c in cipher2.clone()
640 { print!("{:02X} ", c); }
641 println!();
642 let mut txt = String::new();
643 for c in cipher2.clone()
644 { write!(txt, "{:02X} ", c); }
645 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
646 println!();
647
648 // Normal case for the message of 0 bytes
649 let key = 0x_1234567890ABCDEF_u64;
650 println!("K =\t{:#016X}", key);
651 let mut a_des = DES::new_with_key_u64(key);
652
653 let message = "";
654 println!("M =\t{}", message);
655 let mut cipher = [0_u8; 8];
656 a_des.encrypt_str(&message, cipher.as_mut_ptr());
657 print!("C =\t");
658 for c in cipher.clone()
659 { print!("{:02X} ", c); }
660 println!();
661 let mut txt = String::new();
662 for c in cipher.clone()
663 { write!(txt, "{:02X} ", c); }
664 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
665 println!();
666
667 // Normal case for the message shorter than 8 bytes
668 let key = 0x_1234567890ABCDEF_u64;
669 println!("K =\t{:#016X}", key);
670 let mut a_des = DES::new_with_key_u64(key);
671
672 let message = "7 bytes";
673 println!("M =\t{}", message);
674 let mut cipher = [0_u8; 8];
675 a_des.encrypt_str(&message, cipher.as_mut_ptr());
676 print!("C =\t");
677 for c in cipher.clone()
678 { print!("{:02X} ", c); }
679 println!();
680 let mut txt = String::new();
681 for c in cipher.clone()
682 { write!(txt, "{:02X} ", c); }
683 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
684 println!();
685
686 // Normal case for the message of 8 bytes
687 let key = 0x_1234567890ABCDEF_u64;
688 println!("K =\t{:#016X}", key);
689 let mut a_des = DES::new_with_key_u64(key);
690
691 let message = "I am OK.";
692 println!("M =\t{}", message);
693 let mut cipher = [0_u8; 16];
694 a_des.encrypt_str(&message, cipher.as_mut_ptr());
695 print!("C =\t");
696 for c in cipher.clone()
697 { print!("{:02X} ", c); }
698 println!();
699 let mut txt = String::new();
700 for c in cipher.clone()
701 { write!(txt, "{:02X} ", c); }
702 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
703 println!();
704
705 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
706 let key = 0x_1234567890ABCDEF_u64;
707 println!("K =\t{:#016X}", key);
708 let mut a_des = DES::new_with_key_u64(key);
709
710 let message = "PARK Youngho";
711 println!("M =\t{}", message);
712 let mut cipher = [0_u8; 16];
713 a_des.encrypt_str(&message, cipher.as_mut_ptr());
714 print!("C =\t");
715 for c in cipher.clone()
716 { print!("{:02X} ", c); }
717 println!();
718 let mut txt = String::new();
719 for c in cipher.clone()
720 { write!(txt, "{:02X} ", c); }
721 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
722 println!();
723
724
725 // Normal case for the message of 16 bytes
726 let key = 0x_1234567890ABCDEF_u64;
727 println!("K =\t{:#016X}", key);
728 let mut a_des = DES::new_with_key_u64(key);
729
730 let message = "고맙습니다.";
731 println!("M =\t{}", message);
732 let mut cipher = [0_u8; 24];
733 a_des.encrypt_str(&message, cipher.as_mut_ptr());
734 print!("C =\t");
735 for c in cipher.clone()
736 { print!("{:02X} ", c); }
737 println!();
738 let mut txt = String::new();
739 for c in cipher.clone()
740 { write!(txt, "{:02X} ", c); }
741 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
742 println!("-------------------------------");
743}
Sourcefn encrypt_str_into_vec<U>(&mut self, message: &str, cipher: &mut Vec<U>) -> u64
fn encrypt_str_into_vec<U>(&mut self, message: &str, cipher: &mut Vec<U>) -> u64
Encrypts the data in str
with the padding defined according to ISO 7816-4
in ECB (Electronic CodeBook) mode, and stores the encrypted data
in Vec<U>
.
§Arguments
message
is astr
object, and is the plaintext to be encrypted.cipher
is aVec<U>
object, and is the ciphertext to be stored.
§Output
- This method returns the size of ciphertext including padding bits in bytes.
- When
T
isu64
, the output should be at least8
, and will be only any multiple of8
. - When
T
isu128
, the output should be at least16
, and will be only any multiple of16
. - If this method returns
zero
, it means this method failed in encryption.
§Features
- If
message
is a null string “”, only padding bytes will be encrypted, and stored in theVec<U>
objectcipher
. - The padding bits are composed of the bytes that indicate the length of the padding bits in bytes according to ISO 7816-4 defined in RFC 5652.
- For more information about the padding bits according to ISO 7816-4, Read here.
- You don’t have to worry about whether or not the size of the memory area where the ciphertext will be stored is enough.
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, ECB_ISO };
let key = 0x_1234567890ABCDEF_u64;
println!("K =\t{:#016X}", key);
let mut a_des = DES::new_with_key_u64(key);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
a_des.encrypt_str_into_vec(&message, &mut cipher);
print!("C (16 rounds) =\t");
for c in cipher.clone()
{ print!("{:02X} ", c); }
println!();
let mut txt = String::new();
for c in cipher.clone()
{ write!(txt, "{:02X} ", c); }
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 F4 BE 6B A5 C5 7D F6 5D ");
§For more examples,
click here
Examples found in repository?
745fn des_encrypt_str_with_padding_iso_ecb_into_vec()
746{
747 println!("des_encrypt_str_with_padding_iso_ecb_into_vec");
748 use std::io::Write;
749 use std::fmt::Write as _;
750 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
751
752 // Normal case
753 let key = 0x_1234567890ABCDEF_u64;
754 println!("K =\t{:#016X}", key);
755 let mut a_des = DES::new_with_key_u64(key);
756
757 let message = "In the beginning God created the heavens and the earth.";
758 println!("M =\t{}", message);
759 let mut cipher = Vec::<u8>::new();
760 a_des.encrypt_str_into_vec(&message, &mut cipher);
761 print!("C (16 rounds) =\t");
762 for c in cipher.clone()
763 { print!("{:02X} ", c); }
764 println!();
765 let mut txt = String::new();
766 for c in cipher.clone()
767 { write!(txt, "{:02X} ", c); }
768 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 F4 BE 6B A5 C5 7D F6 5D ");
769 println!();
770
771 // Expanded case for 128 rounds
772 let key = 0x_1234567890ABCDEF_u64;
773 println!("K =\t{:#016X}", key);
774 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
775
776 let message = "In the beginning God created the heavens and the earth.";
777 println!("M =\t{}", message);
778 let mut cipher = Vec::<u8>::new();
779 a_des.encrypt_str_into_vec(&message, &mut cipher);
780 print!("C (128 rounds) =\t");
781 for c in cipher.clone()
782 { print!("{:02X} ", c); }
783 println!();
784 let mut txt = String::new();
785 for c in cipher.clone()
786 { write!(txt, "{:02X} ", c); }
787 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
788 println!();
789
790 // Expanded case for 0 rounds which means that key is meaningless
791 let key1 = 0x_1234567890ABCDEF_u64;
792 let key2 = 0_u64;
793 println!("K =\t{:#016X}", key);
794 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
795 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
796
797 let message = "In the beginning God created the heavens and the earth.";
798 println!("M =\t{}", message);
799 let mut cipher1 = Vec::<u8>::new();
800 let mut cipher2 = Vec::<u8>::new();
801 c_des.encrypt_str_into_vec(&message, &mut cipher1);
802 d_des.encrypt_str_into_vec(&message, &mut cipher2);
803 print!("C (0 rounds) =\t");
804 for c in cipher1.clone()
805 { print!("{:02X} ", c); }
806 println!();
807 let mut txt = String::new();
808 for c in cipher1.clone()
809 { write!(txt, "{:02X} ", c); }
810 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
811 print!("D (0 rounds) =\t");
812 for c in cipher2.clone()
813 { print!("{:02X} ", c); }
814 println!();
815 let mut txt = String::new();
816 for c in cipher2.clone()
817 { write!(txt, "{:02X} ", c); }
818 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
819 println!();
820
821 // Normal case for the message of 0 bytes
822 let key = 0x_1234567890ABCDEF_u64;
823 println!("K =\t{:#016X}", key);
824 let mut a_des = DES::new_with_key_u64(key);
825
826 let message = "";
827 println!("M =\t{}", message);
828 let mut cipher = Vec::<u8>::new();
829 a_des.encrypt_str_into_vec(&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, "E2 22 32 FE 87 E8 0A 93 ");
838 println!();
839
840 // Normal case for the message shorter than 8 bytes
841 let key = 0x_1234567890ABCDEF_u64;
842 println!("K =\t{:#016X}", key);
843 let mut a_des = DES::new_with_key_u64(key);
844
845 let message = "7 bytes";
846 println!("M =\t{}", message);
847 let mut cipher = Vec::<u8>::new();
848 a_des.encrypt_str_into_vec(&message, &mut cipher);
849 print!("C =\t");
850 for c in cipher.clone()
851 { print!("{:02X} ", c); }
852 println!();
853 let mut txt = String::new();
854 for c in cipher.clone()
855 { write!(txt, "{:02X} ", c); }
856 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
857 println!();
858
859 // Normal case for the message of 8 bytes
860 let key = 0x_1234567890ABCDEF_u64;
861 println!("K =\t{:#016X}", key);
862 let mut a_des = DES::new_with_key_u64(key);
863
864 let message = "I am OK.";
865 println!("M =\t{}", message);
866 let mut cipher = Vec::<u8>::new();
867 a_des.encrypt_str_into_vec(&message, &mut cipher);
868 print!("C =\t");
869 for c in cipher.clone()
870 { print!("{:02X} ", c); }
871 println!();
872 let mut txt = String::new();
873 for c in cipher.clone()
874 { write!(txt, "{:02X} ", c); }
875 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
876 println!();
877
878 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
879 let key = 0x_1234567890ABCDEF_u64;
880 println!("K =\t{:#016X}", key);
881 let mut a_des = DES::new_with_key_u64(key);
882
883 let message = "PARK Youngho";
884 println!("M =\t{}", message);
885 let mut cipher = Vec::<u8>::new();
886 a_des.encrypt_str_into_vec(&message, &mut cipher);
887 print!("C =\t");
888 for c in cipher.clone()
889 { print!("{:02X} ", c); }
890 println!();
891 let mut txt = String::new();
892 for c in cipher.clone()
893 { write!(txt, "{:02X} ", c); }
894 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
895 println!();
896
897
898 // Normal case for the message of 16 bytes
899 let key = 0x_1234567890ABCDEF_u64;
900 println!("K =\t{:#016X}", key);
901 let mut a_des = DES::new_with_key_u64(key);
902
903 let message = "고맙습니다.";
904 println!("M =\t{}", message);
905 let mut cipher = Vec::<u8>::new();
906 a_des.encrypt_str_into_vec(&message, &mut cipher);
907 print!("C =\t");
908 for c in cipher.clone()
909 { print!("{:02X} ", c); }
910 println!();
911 let mut txt = String::new();
912 for c in cipher.clone()
913 { write!(txt, "{:02X} ", c); }
914 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
915 println!("-------------------------------");
916}
Sourcefn encrypt_str_into_array<U, const N: usize>(
&mut self,
message: &str,
cipher: &mut [U; N],
) -> u64
fn encrypt_str_into_array<U, const N: usize>( &mut self, message: &str, cipher: &mut [U; N], ) -> u64
Encrypts the data in str
with the padding defined according to ISO 7816-4
in ECB (Electronic CodeBook) mode, and stores the encrypted data
in array [U; N]
.
§Arguments
message
is astr
object, and is the plaintext to be encrypted.cipher
is an array[U; N]
object, and is the ciphertext to be stored.
§Output
- This method returns the size of ciphertext including padding bits in bytes.
- When
T
isu64
, the output should be at least8
, and will be only any multiple of8
. - When
T
isu128
, the output should be at least16
, and will be only any multiple of16
. - If this method returns
zero
, it means this method failed in encryption.
§Features
- If
message
is a null string “”, only padding bytes will be encrypted, and stored in the array[U; N]
objectcipher
. - If
U::size_in_bytes() * N
is less thanmessage.len()
’s next multiple of 8, this method does not perform encryption and returnszero
. - If
U::size_in_bytes() * N
is equal tomessage.len()
’s next multiple of 8, this method performs encryption, fills the arraycipher
with the encrypted ciphertext, and returns the size of the ciphertext including padding bits in bytes. - If
U::size_in_bytes() * N
is greater thanmessage.len()
’s next multiple of 8, this method performs encryption, fills the arraycipher
with the encrypted ciphertext, and then fills the rest of the elements of the arraycipher
with zeros, and returns the size of the ciphertext including padding bits in bytes. - The padding bits are composed of the bytes that indicate the length of the padding bits in bytes according to ISO 7816-4 defined in RFC 5652.
- For more information about the padding bits according to ISO 7816-4, Read here.
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, ECB_ISO };
let key = 0x_1234567890ABCDEF_u64;
println!("K =\t{:#016X}", key);
let mut a_des = DES::new_with_key_u64(key);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = [0_u8; 56];
a_des.encrypt_str_into_array(&message, &mut cipher);
print!("C (16 rounds) =\t");
for c in cipher.clone()
{ print!("{:02X} ", c); }
println!();
let mut txt = String::new();
for c in cipher.clone()
{ write!(txt, "{:02X} ", c); }
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 F4 BE 6B A5 C5 7D F6 5D ");
§For more examples,
click here
Examples found in repository?
918fn des_encrypt_str_with_padding_iso_ecb_into_array()
919{
920 println!("des_encrypt_str_with_padding_iso_ecb_into_array");
921 use std::io::Write;
922 use std::fmt::Write as _;
923 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
924
925 // Normal case
926 let key = 0x_1234567890ABCDEF_u64;
927 println!("K =\t{:#016X}", key);
928 let mut a_des = DES::new_with_key_u64(key);
929
930 let message = "In the beginning God created the heavens and the earth.";
931 println!("M =\t{}", message);
932 let mut cipher = [0_u8; 56];
933 a_des.encrypt_str_into_array(&message, &mut cipher);
934 print!("C (16 rounds) =\t");
935 for c in cipher.clone()
936 { print!("{:02X} ", c); }
937 println!();
938 let mut txt = String::new();
939 for c in cipher.clone()
940 { write!(txt, "{:02X} ", c); }
941 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 F4 BE 6B A5 C5 7D F6 5D ");
942 println!();
943
944 // Expanded case for 128 rounds
945 let key = 0x_1234567890ABCDEF_u64;
946 println!("K =\t{:#016X}", key);
947 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
948
949 let message = "In the beginning God created the heavens and the earth.";
950 println!("M =\t{}", message);
951 let mut cipher = [0_u8; 56];
952 a_des.encrypt_str_into_array(&message, &mut cipher);
953 print!("C (128 rounds) =\t");
954 for c in cipher.clone()
955 { print!("{:02X} ", c); }
956 println!();
957 let mut txt = String::new();
958 for c in cipher.clone()
959 { write!(txt, "{:02X} ", c); }
960 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
961 println!();
962
963 // Expanded case for 0 rounds which means that key is meaningless
964 let key1 = 0x_1234567890ABCDEF_u64;
965 let key2 = 0_u64;
966 println!("K =\t{:#016X}", key);
967 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
968 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
969
970 let message = "In the beginning God created the heavens and the earth.";
971 println!("M =\t{}", message);
972 let mut cipher1 = [0_u8; 56];
973 let mut cipher2 = [0_u8; 56];
974 c_des.encrypt_str_into_array(&message, &mut cipher1);
975 d_des.encrypt_str_into_array(&message, &mut cipher2);
976 print!("C (0 rounds) =\t");
977 for c in cipher1.clone()
978 { print!("{:02X} ", c); }
979 println!();
980 let mut txt = String::new();
981 for c in cipher1.clone()
982 { write!(txt, "{:02X} ", c); }
983 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
984 print!("D (0 rounds) =\t");
985 for c in cipher2.clone()
986 { print!("{:02X} ", c); }
987 println!();
988 let mut txt = String::new();
989 for c in cipher2.clone()
990 { write!(txt, "{:02X} ", c); }
991 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
992 println!();
993
994 // Normal case for the message of 0 bytes
995 let key = 0x_1234567890ABCDEF_u64;
996 println!("K =\t{:#016X}", key);
997 let mut a_des = DES::new_with_key_u64(key);
998
999 let message = "";
1000 println!("M =\t{}", message);
1001 let mut cipher = [0_u8; 8];
1002 a_des.encrypt_str_into_array(&message, &mut cipher);
1003 print!("C =\t");
1004 for c in cipher.clone()
1005 { print!("{:02X} ", c); }
1006 println!();
1007 let mut txt = String::new();
1008 for c in cipher.clone()
1009 { write!(txt, "{:02X} ", c); }
1010 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
1011 println!();
1012
1013 // Normal case for the message shorter than 8 bytes
1014 let key = 0x_1234567890ABCDEF_u64;
1015 println!("K =\t{:#016X}", key);
1016 let mut a_des = DES::new_with_key_u64(key);
1017
1018 let message = "7 bytes";
1019 println!("M =\t{}", message);
1020 let mut cipher = [0_u8; 8];
1021 a_des.encrypt_str_into_array(&message, &mut cipher);
1022 print!("C =\t");
1023 for c in cipher.clone()
1024 { print!("{:02X} ", c); }
1025 println!();
1026 let mut txt = String::new();
1027 for c in cipher.clone()
1028 { write!(txt, "{:02X} ", c); }
1029 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
1030 println!();
1031
1032 // Normal case for the message of 8 bytes
1033 let key = 0x_1234567890ABCDEF_u64;
1034 println!("K =\t{:#016X}", key);
1035 let mut a_des = DES::new_with_key_u64(key);
1036
1037 let message = "I am OK.";
1038 println!("M =\t{}", message);
1039 let mut cipher = [0_u8; 16];
1040 a_des.encrypt_str_into_array(&message, &mut cipher);
1041 print!("C =\t");
1042 for c in cipher.clone()
1043 { print!("{:02X} ", c); }
1044 println!();
1045 let mut txt = String::new();
1046 for c in cipher.clone()
1047 { write!(txt, "{:02X} ", c); }
1048 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
1049 println!();
1050
1051 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
1052 let key = 0x_1234567890ABCDEF_u64;
1053 println!("K =\t{:#016X}", key);
1054 let mut a_des = DES::new_with_key_u64(key);
1055
1056 let message = "PARK Youngho";
1057 println!("M =\t{}", message);
1058 let mut cipher = [0_u8; 16];
1059 a_des.encrypt_str_into_array(&message, &mut cipher);
1060 print!("C =\t");
1061 for c in cipher.clone()
1062 { print!("{:02X} ", c); }
1063 println!();
1064 let mut txt = String::new();
1065 for c in cipher.clone()
1066 { write!(txt, "{:02X} ", c); }
1067 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
1068 println!();
1069
1070
1071 // Normal case for the message of 16 bytes
1072 let key = 0x_1234567890ABCDEF_u64;
1073 println!("K =\t{:#016X}", key);
1074 let mut a_des = DES::new_with_key_u64(key);
1075
1076 let message = "고맙습니다.";
1077 println!("M =\t{}", message);
1078 let mut cipher = [0_u8; 24];
1079 a_des.encrypt_str_into_array(&message, &mut cipher);
1080 print!("C =\t");
1081 for c in cipher.clone()
1082 { print!("{:02X} ", c); }
1083 println!();
1084 let mut txt = String::new();
1085 for c in cipher.clone()
1086 { write!(txt, "{:02X} ", c); }
1087 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
1088 println!("-------------------------------");
1089}
Sourcefn encrypt_string(&mut self, message: &String, cipher: *mut u8) -> u64
fn encrypt_string(&mut self, message: &String, cipher: *mut u8) -> u64
Encrypts the data stored in a String object with the padding according to ISO 7816-4 in ECB (Electronic CodeBook) mode.
§Arguments
message
is a String object, and is the plaintext to be encrypted.cipher
is a pointer to u8 which is*mut u8
, and is the ciphertext to be stored.- The size of the memory area which starts at
cipher
and the ciphertext will be stored at is assumed to be enough. - The size of the area for ciphertext should be prepared to be:
(
length_in_bytes
+ 1).next_multiple_of(8) at least whenT
isu64
, and (length_in_bytes
+ 1).next_multiple_of(16) at least whenT
isu128
. So, it is responsible for you to prepare thecipher
area big enough!
§Output
- This method returns the size of ciphertext including padding bits in bytes.
- When
T
isu64
, the output should be at least8
, and will be only any multiple of8
. - When
T
isu128
, the output should be at least16
, and will be only any multiple of16
. - If this method returns
zero
, it means this method failed in encryption.
§Features
- You are not encouraged to use this method in pure Rust programming. Instead, use other safer methods such as encrypt_string_into_*().
- This method is useful to use in hybrid programming with C/C++.
- If
message
is a null string String::new(), only padding bytes will be encrypted, and stored in the memory area that starts fromcipher
. - The padding bits are composed of the bytes that indicate the length of the padding bits in bytes according to ISO 7816-4 defined in RFC 5652.
- For more information about the padding bits according to ISO 7816-4, Read here.
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, ECB_ISO };
let key = 0x_1234567890ABCDEF_u64;
println!("K =\t{:#016X}", key);
let mut a_des = DES::new_with_key_u64(key);
let message = "In the beginning God created the heavens and the earth.".to_string();
println!("M =\t{}", message);
let mut cipher = [0_u8; 56];
a_des.encrypt_string(&message, cipher.as_mut_ptr());
print!("C (16 rounds) =\t");
for c in cipher.clone()
{ print!("{:02X} ", c); }
println!();
let mut txt = String::new();
for c in cipher.clone()
{ write!(txt, "{:02X} ", c); }
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 F4 BE 6B A5 C5 7D F6 5D ");
§For more examples,
click here
Examples found in repository?
1091fn des_encrypt_string_with_padding_iso_ecb()
1092{
1093 println!("des_encrypt_string_with_padding_iso_ecb");
1094 use std::io::Write;
1095 use std::fmt::Write as _;
1096 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
1097
1098 // Normal case
1099 let key = 0x_1234567890ABCDEF_u64;
1100 println!("K =\t{:#016X}", key);
1101 let mut a_des = DES::new_with_key_u64(key);
1102
1103 let message = "In the beginning God created the heavens and the earth.".to_string();
1104 println!("M =\t{}", message);
1105 let mut cipher = [0_u8; 56];
1106 a_des.encrypt_string(&message, cipher.as_mut_ptr());
1107 print!("C (16 rounds) =\t");
1108 for c in cipher.clone()
1109 { print!("{:02X} ", c); }
1110 println!();
1111 let mut txt = String::new();
1112 for c in cipher.clone()
1113 { write!(txt, "{:02X} ", c); }
1114 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 F4 BE 6B A5 C5 7D F6 5D ");
1115 println!();
1116
1117 // Expanded case for 128 rounds
1118 let key = 0x_1234567890ABCDEF_u64;
1119 println!("K =\t{:#016X}", key);
1120 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
1121
1122 let message = "In the beginning God created the heavens and the earth.".to_string();
1123 println!("M =\t{}", message);
1124 let mut cipher = [0_u8; 56];
1125 a_des.encrypt_string(&message, cipher.as_mut_ptr());
1126 print!("C (128 rounds) =\t");
1127 for c in cipher.clone()
1128 { print!("{:02X} ", c); }
1129 println!();
1130 let mut txt = String::new();
1131 for c in cipher.clone()
1132 { write!(txt, "{:02X} ", c); }
1133 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
1134 println!();
1135
1136 // Expanded case for 0 rounds which means that key is meaningless
1137 let key1 = 0x_1234567890ABCDEF_u64;
1138 let key2 = 0_u64;
1139 println!("K =\t{:#016X}", key);
1140 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
1141 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
1142
1143 let message = "In the beginning God created the heavens and the earth.".to_string();
1144 println!("M =\t{}", message);
1145 let mut cipher1 = [0_u8; 56];
1146 let mut cipher2 = [0_u8; 56];
1147 c_des.encrypt_string(&message, cipher1.as_mut_ptr());
1148 d_des.encrypt_string(&message, cipher2.as_mut_ptr());
1149 print!("C (0 rounds) =\t");
1150 for c in cipher1.clone()
1151 { print!("{:02X} ", c); }
1152 println!();
1153 let mut txt = String::new();
1154 for c in cipher1.clone()
1155 { write!(txt, "{:02X} ", c); }
1156 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
1157 print!("D (0 rounds) =\t");
1158 for c in cipher2.clone()
1159 { print!("{:02X} ", c); }
1160 println!();
1161 let mut txt = String::new();
1162 for c in cipher2.clone()
1163 { write!(txt, "{:02X} ", c); }
1164 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
1165 println!();
1166
1167 // Normal case for the message of 0 bytes
1168 let key = 0x_1234567890ABCDEF_u64;
1169 println!("K =\t{:#016X}", key);
1170 let mut a_des = DES::new_with_key_u64(key);
1171
1172 let message = "".to_string();
1173 println!("M =\t{}", message);
1174 let mut cipher = [0_u8; 8];
1175 a_des.encrypt_string(&message, cipher.as_mut_ptr());
1176 print!("C =\t");
1177 for c in cipher.clone()
1178 { print!("{:02X} ", c); }
1179 println!();
1180 let mut txt = String::new();
1181 for c in cipher.clone()
1182 { write!(txt, "{:02X} ", c); }
1183 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
1184 println!();
1185
1186 // Normal case for the message shorter than 8 bytes
1187 let key = 0x_1234567890ABCDEF_u64;
1188 println!("K =\t{:#016X}", key);
1189 let mut a_des = DES::new_with_key_u64(key);
1190
1191 let message = "7 bytes".to_string();
1192 println!("M =\t{}", message);
1193 let mut cipher = [0_u8; 8];
1194 a_des.encrypt_string(&message, cipher.as_mut_ptr());
1195 print!("C =\t");
1196 for c in cipher.clone()
1197 { print!("{:02X} ", c); }
1198 println!();
1199 let mut txt = String::new();
1200 for c in cipher.clone()
1201 { write!(txt, "{:02X} ", c); }
1202 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
1203 println!();
1204
1205 // Normal case for the message of 8 bytes
1206 let key = 0x_1234567890ABCDEF_u64;
1207 println!("K =\t{:#016X}", key);
1208 let mut a_des = DES::new_with_key_u64(key);
1209
1210 let message = "I am OK.".to_string();
1211 println!("M =\t{}", message);
1212 let mut cipher = [0_u8; 16];
1213 a_des.encrypt_string(&message, cipher.as_mut_ptr());
1214 print!("C =\t");
1215 for c in cipher.clone()
1216 { print!("{:02X} ", c); }
1217 println!();
1218 let mut txt = String::new();
1219 for c in cipher.clone()
1220 { write!(txt, "{:02X} ", c); }
1221 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
1222 println!();
1223
1224 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
1225 let key = 0x_1234567890ABCDEF_u64;
1226 println!("K =\t{:#016X}", key);
1227 let mut a_des = DES::new_with_key_u64(key);
1228
1229 let message = "PARK Youngho".to_string();
1230 println!("M =\t{}", message);
1231 let mut cipher = [0_u8; 16];
1232 a_des.encrypt_string(&message, cipher.as_mut_ptr());
1233 print!("C =\t");
1234 for c in cipher.clone()
1235 { print!("{:02X} ", c); }
1236 println!();
1237 let mut txt = String::new();
1238 for c in cipher.clone()
1239 { write!(txt, "{:02X} ", c); }
1240 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
1241 println!();
1242
1243
1244 // Normal case for the message of 16 bytes
1245 let key = 0x_1234567890ABCDEF_u64;
1246 println!("K =\t{:#016X}", key);
1247 let mut a_des = DES::new_with_key_u64(key);
1248
1249 let message = "고맙습니다.".to_string();
1250 println!("M =\t{}", message);
1251 let mut cipher = [0_u8; 24];
1252 a_des.encrypt_string(&message, cipher.as_mut_ptr());
1253 print!("C =\t");
1254 for c in cipher.clone()
1255 { print!("{:02X} ", c); }
1256 println!();
1257 let mut txt = String::new();
1258 for c in cipher.clone()
1259 { write!(txt, "{:02X} ", c); }
1260 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
1261 println!("-------------------------------");
1262}
Sourcefn encrypt_string_into_vec<U>(
&mut self,
message: &String,
cipher: &mut Vec<U>,
) -> u64
fn encrypt_string_into_vec<U>( &mut self, message: &String, cipher: &mut Vec<U>, ) -> u64
Encrypts the data stored in a String object with the padding according
to ISO 7816-4 in ECB (Electronic CodeBook) mode, and stores the encrypted
data in Vec<U>
.
§Arguments
message
is a String object, and is the plaintext to be encrypted.cipher
is aVec<U>
object, and is the ciphertext to be stored.
§Output
- This method returns the size of ciphertext including padding bits in bytes.
- When
T
isu64
, the output should be at least8
, and will be only any multiple of8
. - When
T
isu128
, the output should be at least16
, and will be only any multiple of16
. - If this method returns
zero
, it means this method failed in encryption.
§Features
- If
message
is a null string String::new(), only padding bytes will be encrypted, and stored in theVec<U>
objectcipher
. - The padding bits are composed of the bytes that indicate the length of the padding bits in bytes according to ISO 7816-4 defined in RFC 5652.
- For more information about the padding bits according to ISO 7816-4, Read here.
- You don’t have to worry about whether or not the size of the memory area where the ciphertext will be stored is enough.
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, ECB_ISO };
let key = 0x_1234567890ABCDEF_u64;
println!("K =\t{:#016X}", key);
let mut a_des = DES::new_with_key_u64(key);
let message = "In the beginning God created the heavens and the earth.".to_string();
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
a_des.encrypt_string_into_vec(&message, &mut cipher);
print!("C (16 rounds) =\t");
for c in cipher.clone()
{ print!("{:02X} ", c); }
println!();
let mut txt = String::new();
for c in cipher.clone()
{ write!(txt, "{:02X} ", c); }
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 F4 BE 6B A5 C5 7D F6 5D ");
§For more examples,
click here
Examples found in repository?
1264fn des_encrypt_string_with_padding_iso_ecb_into_vec()
1265{
1266 println!("des_encrypt_string_with_padding_iso_ecb_into_vec");
1267 use std::io::Write;
1268 use std::fmt::Write as _;
1269 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
1270
1271 // Normal case
1272 let key = 0x_1234567890ABCDEF_u64;
1273 println!("K =\t{:#016X}", key);
1274 let mut a_des = DES::new_with_key_u64(key);
1275
1276 let message = "In the beginning God created the heavens and the earth.".to_string();
1277 println!("M =\t{}", message);
1278 let mut cipher = Vec::<u8>::new();
1279 a_des.encrypt_string_into_vec(&message, &mut cipher);
1280 print!("C (16 rounds) =\t");
1281 for c in cipher.clone()
1282 { print!("{:02X} ", c); }
1283 println!();
1284 let mut txt = String::new();
1285 for c in cipher.clone()
1286 { write!(txt, "{:02X} ", c); }
1287 assert_eq!(txt, "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 F4 BE 6B A5 C5 7D F6 5D ");
1288 println!();
1289
1290 // Expanded case for 128 rounds
1291 let key = 0x_1234567890ABCDEF_u64;
1292 println!("K =\t{:#016X}", key);
1293 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
1294
1295 let message = "In the beginning God created the heavens and the earth.".to_string();
1296 println!("M =\t{}", message);
1297 let mut cipher = Vec::<u8>::new();
1298 a_des.encrypt_string_into_vec(&message, &mut cipher);
1299 print!("C (128 rounds) =\t");
1300 for c in cipher.clone()
1301 { print!("{:02X} ", c); }
1302 println!();
1303 let mut txt = String::new();
1304 for c in cipher.clone()
1305 { write!(txt, "{:02X} ", c); }
1306 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
1307 println!();
1308
1309 // Expanded case for 0 rounds which means that key is meaningless
1310 let key1 = 0x_1234567890ABCDEF_u64;
1311 let key2 = 0_u64;
1312 println!("K =\t{:#016X}", key);
1313 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
1314 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
1315
1316 let message = "In the beginning God created the heavens and the earth.".to_string();
1317 println!("M =\t{}", message);
1318 let mut cipher1 = Vec::<u8>::new();
1319 let mut cipher2 = Vec::<u8>::new();
1320 c_des.encrypt_string_into_vec(&message, &mut cipher1);
1321 d_des.encrypt_string_into_vec(&message, &mut cipher2);
1322 print!("C (0 rounds) =\t");
1323 for c in cipher1.clone()
1324 { print!("{:02X} ", c); }
1325 println!();
1326 let mut txt = String::new();
1327 for c in cipher1.clone()
1328 { write!(txt, "{:02X} ", c); }
1329 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
1330 print!("D (0 rounds) =\t");
1331 for c in cipher2.clone()
1332 { print!("{:02X} ", c); }
1333 println!();
1334 let mut txt = String::new();
1335 for c in cipher2.clone()
1336 { write!(txt, "{:02X} ", c); }
1337 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
1338 println!();
1339
1340 // Normal case for the message of 0 bytes
1341 let key = 0x_1234567890ABCDEF_u64;
1342 println!("K =\t{:#016X}", key);
1343 let mut a_des = DES::new_with_key_u64(key);
1344
1345 let message = "".to_string();
1346 println!("M =\t{}", message);
1347 let mut cipher = Vec::<u8>::new();
1348 a_des.encrypt_string_into_vec(&message, &mut cipher);
1349 print!("C =\t");
1350 for c in cipher.clone()
1351 { print!("{:02X} ", c); }
1352 println!();
1353 let mut txt = String::new();
1354 for c in cipher.clone()
1355 { write!(txt, "{:02X} ", c); }
1356 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
1357 println!();
1358
1359 // Normal case for the message shorter than 8 bytes
1360 let key = 0x_1234567890ABCDEF_u64;
1361 println!("K =\t{:#016X}", key);
1362 let mut a_des = DES::new_with_key_u64(key);
1363
1364 let message = "7 bytes".to_string();
1365 println!("M =\t{}", message);
1366 let mut cipher = Vec::<u8>::new();
1367 a_des.encrypt_string_into_vec(&message, &mut cipher);
1368 print!("C =\t");
1369 for c in cipher.clone()
1370 { print!("{:02X} ", c); }
1371 println!();
1372 let mut txt = String::new();
1373 for c in cipher.clone()
1374 { write!(txt, "{:02X} ", c); }
1375 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
1376 println!();
1377
1378 // Normal case for the message of 8 bytes
1379 let key = 0x_1234567890ABCDEF_u64;
1380 println!("K =\t{:#016X}", key);
1381 let mut a_des = DES::new_with_key_u64(key);
1382
1383 let message = "I am OK.".to_string();
1384 println!("M =\t{}", message);
1385 let mut cipher = Vec::<u8>::new();
1386 a_des.encrypt_string_into_vec(&message, &mut cipher);
1387 print!("C =\t");
1388 for c in cipher.clone()
1389 { print!("{:02X} ", c); }
1390 println!();
1391 let mut txt = String::new();
1392 for c in cipher.clone()
1393 { write!(txt, "{:02X} ", c); }
1394 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
1395 println!();
1396
1397 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
1398 let key = 0x_1234567890ABCDEF_u64;
1399 println!("K =\t{:#016X}", key);
1400 let mut a_des = DES::new_with_key_u64(key);
1401
1402 let message = "PARK Youngho".to_string();
1403 println!("M =\t{}", message);
1404 let mut cipher = Vec::<u8>::new();
1405 a_des.encrypt_string_into_vec(&message, &mut cipher);
1406 print!("C =\t");
1407 for c in cipher.clone()
1408 { print!("{:02X} ", c); }
1409 println!();
1410 let mut txt = String::new();
1411 for c in cipher.clone()
1412 { write!(txt, "{:02X} ", c); }
1413 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
1414 println!();
1415
1416
1417 // Normal case for the message of 16 bytes
1418 let key = 0x_1234567890ABCDEF_u64;
1419 println!("K =\t{:#016X}", key);
1420 let mut a_des = DES::new_with_key_u64(key);
1421
1422 let message = "고맙습니다.".to_string();
1423 println!("M =\t{}", message);
1424 let mut cipher = Vec::<u8>::new();
1425 a_des.encrypt_string_into_vec(&message, &mut cipher);
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, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
1434 println!("-------------------------------");
1435}
Sourcefn encrypt_string_into_array<U, const N: usize>(
&mut self,
message: &String,
cipher: &mut [U; N],
) -> u64
fn encrypt_string_into_array<U, const N: usize>( &mut self, message: &String, cipher: &mut [U; N], ) -> u64
Encrypts the data stored in a String object with the padding according
to ISO 7816-4 in ECB (Electronic CodeBook) mode, and stores the encrypted
data in array [U; N]
.
§Arguments
message
is a String object, and is the plaintext to be encrypted.cipher
is an array[U; N]
object, and is the ciphertext to be stored.
§Output
- This method returns the size of ciphertext including padding bits in bytes.
- When
T
isu64
, the output should be at least8
, and will be only any multiple of8
. - When
T
isu128
, the output should be at least16
, and will be only any multiple of16
. - If this method returns
zero
, it means this method failed in encryption.
§Features
- If
message
is a null string String::new(), only padding bytes will be encrypted, and stored in the array[U; N]
objectcipher
. - If
U::size_in_bytes() * N
is less thanmessage.len()
’s next multiple of 8, this method does not perform encryption and returnszero
. - If
U::size_in_bytes() * N
is equal tomessage.len()
’s next multiple of 8, this method performs encryption, fills the arraycipher
with the encrypted ciphertext, and returns the size of the ciphertext including padding bits in bytes. - If
U::size_in_bytes() * N
is greater thanmessage.len()
’s next multiple of 8, this method performs encryption, fills the arraycipher
with the encrypted ciphertext, and then fills the rest of the elements of the arraycipher
with zeros, and returns the size of the ciphertext including padding bits in bytes. - The padding bits are composed of the bytes that indicate the length of the padding bits in bytes according to ISO 7816-4 defined in RFC 5652.
- For more information about the padding bits according to ISO 7816-4, Read here.
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, ECB_ISO };
let key = 0x_1234567890ABCDEF_u64;
println!("K =\t{:#016X}", key);
let mut a_des = DES::new_with_key_u64(key);
let message = "In the beginning God created the heavens and the earth.".to_string();
println!("M =\t{}", message);
let mut cipher = [0_u8; 56];
a_des.encrypt_string_into_array(&message, &mut cipher);
print!("C (16 rounds) =\t");
for c in cipher.clone()
{ print!("{:02X} ", c); }
println!();
let mut txt = String::new();
for c in cipher.clone()
{ write!(txt, "{:02X} ", c); }
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 F4 BE 6B A5 C5 7D F6 5D ");
println!();
§For more examples,
click here
Examples found in repository?
1437fn des_encrypt_string_with_padding_iso_ecb_into_array()
1438{
1439 println!("des_encrypt_string_with_padding_iso_ecb_into_array");
1440 use std::io::Write;
1441 use std::fmt::Write as _;
1442 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
1443
1444 // Normal case
1445 let key = 0x_1234567890ABCDEF_u64;
1446 println!("K =\t{:#016X}", key);
1447 let mut a_des = DES::new_with_key_u64(key);
1448
1449 let message = "In the beginning God created the heavens and the earth.".to_string();
1450 println!("M =\t{}", message);
1451 let mut cipher = [0_u8; 56];
1452 a_des.encrypt_string_into_array(&message, &mut cipher);
1453 print!("C (16 rounds) =\t");
1454 for c in cipher.clone()
1455 { print!("{:02X} ", c); }
1456 println!();
1457 let mut txt = String::new();
1458 for c in cipher.clone()
1459 { write!(txt, "{:02X} ", c); }
1460 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 F4 BE 6B A5 C5 7D F6 5D ");
1461 println!();
1462
1463 // Expanded case for 128 rounds
1464 let key = 0x_1234567890ABCDEF_u64;
1465 println!("K =\t{:#016X}", key);
1466 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
1467
1468 let message = "In the beginning God created the heavens and the earth.".to_string();
1469 println!("M =\t{}", message);
1470 let mut cipher = [0_u8; 56];
1471 a_des.encrypt_string_into_array(&message, &mut cipher);
1472 print!("C (128 rounds) =\t");
1473 for c in cipher.clone()
1474 { print!("{:02X} ", c); }
1475 println!();
1476 let mut txt = String::new();
1477 for c in cipher.clone()
1478 { write!(txt, "{:02X} ", c); }
1479 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
1480 println!();
1481
1482 // Expanded case for 0 rounds which means that key is meaningless
1483 let key1 = 0x_1234567890ABCDEF_u64;
1484 let key2 = 0_u64;
1485 println!("K =\t{:#016X}", key);
1486 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
1487 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
1488
1489 let message = "In the beginning God created the heavens and the earth.".to_string();
1490 println!("M =\t{}", message);
1491 let mut cipher1 = [0_u8; 56];
1492 let mut cipher2 = [0_u8; 56];
1493 c_des.encrypt_string_into_array(&message, &mut cipher1);
1494 d_des.encrypt_string_into_array(&message, &mut cipher2);
1495 print!("C (0 rounds) =\t");
1496 for c in cipher1.clone()
1497 { print!("{:02X} ", c); }
1498 println!();
1499 let mut txt = String::new();
1500 for c in cipher1.clone()
1501 { write!(txt, "{:02X} ", c); }
1502 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
1503 print!("D (0 rounds) =\t");
1504 for c in cipher2.clone()
1505 { print!("{:02X} ", c); }
1506 println!();
1507 let mut txt = String::new();
1508 for c in cipher2.clone()
1509 { write!(txt, "{:02X} ", c); }
1510 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
1511 println!();
1512
1513 // Normal case for the message of 0 bytes
1514 let key = 0x_1234567890ABCDEF_u64;
1515 println!("K =\t{:#016X}", key);
1516 let mut a_des = DES::new_with_key_u64(key);
1517
1518 let message = "".to_string();
1519 println!("M =\t{}", message);
1520 let mut cipher = [0_u8; 8];
1521 a_des.encrypt_string_into_array(&message, &mut cipher);
1522 print!("C =\t");
1523 for c in cipher.clone()
1524 { print!("{:02X} ", c); }
1525 println!();
1526 let mut txt = String::new();
1527 for c in cipher.clone()
1528 { write!(txt, "{:02X} ", c); }
1529 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
1530 println!();
1531
1532 // Normal case for the message shorter than 8 bytes
1533 let key = 0x_1234567890ABCDEF_u64;
1534 println!("K =\t{:#016X}", key);
1535 let mut a_des = DES::new_with_key_u64(key);
1536
1537 let message = "7 bytes".to_string();
1538 println!("M =\t{}", message);
1539 let mut cipher = [0_u8; 8];
1540 a_des.encrypt_string_into_array(&message, &mut cipher);
1541 print!("C =\t");
1542 for c in cipher.clone()
1543 { print!("{:02X} ", c); }
1544 println!();
1545 let mut txt = String::new();
1546 for c in cipher.clone()
1547 { write!(txt, "{:02X} ", c); }
1548 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
1549 println!();
1550
1551 // Normal case for the message of 8 bytes
1552 let key = 0x_1234567890ABCDEF_u64;
1553 println!("K =\t{:#016X}", key);
1554 let mut a_des = DES::new_with_key_u64(key);
1555
1556 let message = "I am OK.".to_string();
1557 println!("M =\t{}", message);
1558 let mut cipher = [0_u8; 16];
1559 a_des.encrypt_string_into_array(&message, &mut cipher);
1560 print!("C =\t");
1561 for c in cipher.clone()
1562 { print!("{:02X} ", c); }
1563 println!();
1564 let mut txt = String::new();
1565 for c in cipher.clone()
1566 { write!(txt, "{:02X} ", c); }
1567 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
1568 println!();
1569
1570 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
1571 let key = 0x_1234567890ABCDEF_u64;
1572 println!("K =\t{:#016X}", key);
1573 let mut a_des = DES::new_with_key_u64(key);
1574
1575 let message = "PARK Youngho".to_string();
1576 println!("M =\t{}", message);
1577 let mut cipher = [0_u8; 16];
1578 a_des.encrypt_string_into_array(&message, &mut cipher);
1579 print!("C =\t");
1580 for c in cipher.clone()
1581 { print!("{:02X} ", c); }
1582 println!();
1583 let mut txt = String::new();
1584 for c in cipher.clone()
1585 { write!(txt, "{:02X} ", c); }
1586 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
1587 println!();
1588
1589 // Normal case for the message of 16 bytes
1590 let key = 0x_1234567890ABCDEF_u64;
1591 println!("K =\t{:#016X}", key);
1592 let mut a_des = DES::new_with_key_u64(key);
1593
1594 let message = "고맙습니다.".to_string();
1595 println!("M =\t{}", message);
1596 let mut cipher = [0_u8; 24];
1597 a_des.encrypt_string_into_array(&message, &mut cipher);
1598 print!("C =\t");
1599 for c in cipher.clone()
1600 { print!("{:02X} ", c); }
1601 println!();
1602 let mut txt = String::new();
1603 for c in cipher.clone()
1604 { write!(txt, "{:02X} ", c); }
1605 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
1606 println!("-------------------------------");
1607}
Sourcefn encrypt_vec<U>(&mut self, message: &Vec<U>, cipher: *mut u8) -> u64
fn encrypt_vec<U>(&mut self, message: &Vec<U>, cipher: *mut u8) -> u64
Encrypts the data stored in a Vec<U>
object with the padding defined
according to ISO 7816-4 in ECB (Electronic CodeBook) mode.
§Arguments
message
is aVec<U>
object, and is the plaintext to be encrypted.cipher
is a pointer to u8 which is*mut u8
, and is the ciphertext to be stored.- The size of the memory area which starts at
cipher
and the ciphertext will be stored at is assumed to be enough. - The size of the area for ciphertext should be prepared to be:
(
length_in_bytes
+ 1).next_multiple_of(8) at least whenT
isu64
, and (length_in_bytes
+ 1).next_multiple_of(16) at least whenT
isu128
. So, it is responsible for you to prepare thecipher
area big enough!
§Output
- This method returns the size of ciphertext including padding bits in bytes.
- When
T
isu64
, the output should be at least8
, and will be only any multiple of8
. - When
T
isu128
, the output should be at least16
, and will be only any multiple of16
. - If this method returns
zero
, it means this method failed in encryption.
§Features
- You are not encouraged to use this method in pure Rust programming. Instead, use other safer methods such as encrypt_vec_into_*().
- This method is useful to use in hybrid programming with C/C++.
- If
message
is an emptyVec<U>
objectVec::<U>::new()
, only padding bytes will be encrypted, and stored in the memory area that starts fromcipher
. - The padding bits are composed of the bytes that indicate the length of the padding bits in bytes according to ISO 7816-4 defined in RFC 5652.
- For more information about the padding bits according to ISO 7816-4, Read here.
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, ECB_ISO };
let key = 0x_1234567890ABCDEF_u64;
println!("K =\t{:#016X}", key);
let mut a_des = DES::new_with_key_u64(key);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let message = unsafe { message.to_string().as_mut_vec().clone() };
let mut cipher = [0_u8; 56];
a_des.encrypt_vec(&message, cipher.as_mut_ptr());
print!("C (16 rounds) =\t");
for c in cipher.clone()
{ print!("{:02X} ", c); }
println!();
let mut txt = String::new();
for c in cipher.clone()
{ write!(txt, "{:02X} ", c); }
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 F4 BE 6B A5 C5 7D F6 5D ");
§For more examples,
click here
Examples found in repository?
1609fn des_encrypt_vec_with_padding_iso_ecb()
1610{
1611 println!("des_encrypt_vec_with_padding_iso_ecb");
1612 use std::io::Write;
1613 use std::fmt::Write as _;
1614 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
1615
1616 // Normal case
1617 let key = 0x_1234567890ABCDEF_u64;
1618 println!("K =\t{:#016X}", key);
1619 let mut a_des = DES::new_with_key_u64(key);
1620
1621 let message = "In the beginning God created the heavens and the earth.";
1622 println!("M =\t{}", message);
1623 let message = unsafe { message.to_string().as_mut_vec().clone() };
1624 let mut cipher = [0_u8; 56];
1625 a_des.encrypt_vec(&message, cipher.as_mut_ptr());
1626 print!("C (16 rounds) =\t");
1627 for c in cipher.clone()
1628 { print!("{:02X} ", c); }
1629 println!();
1630 let mut txt = String::new();
1631 for c in cipher.clone()
1632 { write!(txt, "{:02X} ", c); }
1633 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 F4 BE 6B A5 C5 7D F6 5D ");
1634 println!();
1635
1636 // Expanded case for 128 rounds
1637 let key = 0x_1234567890ABCDEF_u64;
1638 println!("K =\t{:#016X}", key);
1639 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
1640
1641 let message = "In the beginning God created the heavens and the earth.";
1642 println!("M =\t{}", message);
1643 let message = unsafe { message.to_string().as_mut_vec().clone() };
1644 let mut cipher = [0_u8; 56];
1645 a_des.encrypt_vec(&message, cipher.as_mut_ptr());
1646 print!("C (128 rounds) =\t");
1647 for c in cipher.clone()
1648 { print!("{:02X} ", c); }
1649 println!();
1650 let mut txt = String::new();
1651 for c in cipher.clone()
1652 { write!(txt, "{:02X} ", c); }
1653 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
1654 println!();
1655
1656 // Expanded case for 0 rounds which means that key is meaningless
1657 let key1 = 0x_1234567890ABCDEF_u64;
1658 let key2 = 0_u64;
1659 println!("K1 =\t{:#016X}", key1);
1660 println!("K2 =\t{:#016X}", key2);
1661 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
1662 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
1663
1664 let message = "In the beginning God created the heavens and the earth.";
1665 println!("M =\t{}", message);
1666 let message = unsafe { message.to_string().as_mut_vec().clone() };
1667 let mut cipher1 = [0_u8; 56];
1668 let mut cipher2 = [0_u8; 56];
1669 c_des.encrypt_vec(&message, cipher1.as_mut_ptr());
1670 d_des.encrypt_vec(&message, cipher2.as_mut_ptr());
1671 print!("C (0 rounds) =\t");
1672 for c in cipher1.clone()
1673 { print!("{:02X} ", c); }
1674 println!();
1675 let mut txt = String::new();
1676 for c in cipher1.clone()
1677 { write!(txt, "{:02X} ", c); }
1678 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
1679 print!("D (0 rounds) =\t");
1680 for c in cipher2.clone()
1681 { print!("{:02X} ", c); }
1682 println!();
1683 let mut txt = String::new();
1684 for c in cipher2.clone()
1685 { write!(txt, "{:02X} ", c); }
1686 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
1687 println!();
1688
1689 // Normal case for the message of 0 bytes
1690 let key = 0x_1234567890ABCDEF_u64;
1691 println!("K =\t{:#016X}", key);
1692 let mut a_des = DES::new_with_key_u64(key);
1693
1694 let message = "";
1695 println!("M =\t{}", message);
1696 let message = unsafe { message.to_string().as_mut_vec().clone() };
1697 let mut cipher = [0_u8; 8];
1698 a_des.encrypt_vec(&message, cipher.as_mut_ptr());
1699 print!("C =\t");
1700 for c in cipher.clone()
1701 { print!("{:02X} ", c); }
1702 println!();
1703 let mut txt = String::new();
1704 for c in cipher.clone()
1705 { write!(txt, "{:02X} ", c); }
1706 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
1707 println!();
1708
1709 // Normal case for the message shorter than 8 bytes
1710 let key = 0x_1234567890ABCDEF_u64;
1711 println!("K =\t{:#016X}", key);
1712 let mut a_des = DES::new_with_key_u64(key);
1713
1714 let message = "7 bytes";
1715 println!("M =\t{}", message);
1716 let message = unsafe { message.to_string().as_mut_vec().clone() };
1717 let mut cipher = [0_u8; 8];
1718 a_des.encrypt_vec(&message, cipher.as_mut_ptr());
1719 print!("C =\t");
1720 for c in cipher.clone()
1721 { print!("{:02X} ", c); }
1722 println!();
1723 let mut txt = String::new();
1724 for c in cipher.clone()
1725 { write!(txt, "{:02X} ", c); }
1726 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
1727 println!();
1728
1729 // Normal case for the message of 8 bytes
1730 let key = 0x_1234567890ABCDEF_u64;
1731 println!("K =\t{:#016X}", key);
1732 let mut a_des = DES::new_with_key_u64(key);
1733
1734 let message = "I am OK.";
1735 println!("M =\t{}", message);
1736 let message = unsafe { message.to_string().as_mut_vec().clone() };
1737 let mut cipher = [0_u8; 16];
1738 a_des.encrypt_vec(&message, cipher.as_mut_ptr());
1739 print!("C =\t");
1740 for c in cipher.clone()
1741 { print!("{:02X} ", c); }
1742 println!();
1743 let mut txt = String::new();
1744 for c in cipher.clone()
1745 { write!(txt, "{:02X} ", c); }
1746 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
1747 println!();
1748
1749 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
1750 let key = 0x_1234567890ABCDEF_u64;
1751 println!("K =\t{:#016X}", key);
1752 let mut a_des = DES::new_with_key_u64(key);
1753
1754 let message = "PARK Youngho";
1755 println!("M =\t{}", message);
1756 let message = unsafe { message.to_string().as_mut_vec().clone() };
1757 let mut cipher = [0_u8; 16];
1758 a_des.encrypt_vec(&message, cipher.as_mut_ptr());
1759 print!("C =\t");
1760 for c in cipher.clone()
1761 { print!("{:02X} ", c); }
1762 println!();
1763 let mut txt = String::new();
1764 for c in cipher.clone()
1765 { write!(txt, "{:02X} ", c); }
1766 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
1767 println!();
1768
1769
1770 // Normal case for the message of 16 bytes
1771 let key = 0x_1234567890ABCDEF_u64;
1772 println!("K =\t{:#016X}", key);
1773 let mut a_des = DES::new_with_key_u64(key);
1774
1775 let message = "고맙습니다.";
1776 println!("M =\t{}", message);
1777 let message = unsafe { message.to_string().as_mut_vec().clone() };
1778 let mut cipher = [0_u8; 24];
1779 a_des.encrypt_vec(&message, cipher.as_mut_ptr());
1780 print!("C =\t");
1781 for c in cipher.clone()
1782 { print!("{:02X} ", c); }
1783 println!();
1784 let mut txt = String::new();
1785 for c in cipher.clone()
1786 { write!(txt, "{:02X} ", c); }
1787 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
1788 println!("-------------------------------");
1789}
Sourcefn encrypt_vec_into_vec<U, V>(
&mut self,
message: &Vec<U>,
cipher: &mut Vec<V>,
) -> u64
fn encrypt_vec_into_vec<U, V>( &mut self, message: &Vec<U>, cipher: &mut Vec<V>, ) -> u64
Encrypts the data stored in a Vec<U>
object with the padding according
to ISO 7816-4 in ECB (Electronic CodeBook) mode, and stores the encrypted
data in Vec<V>
.
§Arguments
message
is aVec<U>
object, and is the plaintext to be encrypted.cipher
is aVec<V>
object, and is the ciphertext to be stored.
§Output
- This method returns the size of ciphertext including padding bits in bytes.
- When
T
isu64
, the output should be at least8
, and will be only any multiple of8
. - When
T
isu128
, the output should be at least16
, and will be only any multiple of16
. - If this method returns
zero
, it means this method failed in encryption.
§Features
- If
message
is an emptyVec<U>
objectVec::<U>::new()
, only padding bytes will be encrypted, and stored in theVec<V>
objectcipher
. - The padding bits are composed of the bytes that indicate the length of the padding bits in bytes according to ISO 7816-4 defined in RFC 5652.
- For more information about the padding bits according to ISO 7816-4, Read here.
- You don’t have to worry about whether or not the size of the memory area where the ciphertext will be stored is enough.
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, ECB_ISO };
let key = 0x_1234567890ABCDEF_u64;
println!("K =\t{:#016X}", key);
let mut a_des = DES::new_with_key_u64(key);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let message = unsafe { message.to_string().as_mut_vec().clone() };
let mut cipher = Vec::<u8>::new();
a_des.encrypt_vec_into_vec(&message, &mut cipher);
print!("C (16 rounds) =\t");
for c in cipher.clone()
{ print!("{:02X} ", c); }
println!();
let mut txt = String::new();
for c in cipher.clone()
{ write!(txt, "{:02X} ", c); }
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 F4 BE 6B A5 C5 7D F6 5D ");
§For more examples,
click here
Examples found in repository?
1791fn des_encrypt_vec_with_padding_iso_ecb_into_vec()
1792{
1793 println!("des_encrypt_vec_with_padding_iso_ecb_into_vec");
1794 use std::io::Write;
1795 use std::fmt::Write as _;
1796 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
1797
1798 // Normal case
1799 let key = 0x_1234567890ABCDEF_u64;
1800 println!("K =\t{:#016X}", key);
1801 let mut a_des = DES::new_with_key_u64(key);
1802
1803 let message = "In the beginning God created the heavens and the earth.";
1804 println!("M =\t{}", message);
1805 let message = unsafe { message.to_string().as_mut_vec().clone() };
1806 let mut cipher = Vec::<u8>::new();
1807 a_des.encrypt_vec_into_vec(&message, &mut cipher);
1808 print!("C (16 rounds) =\t");
1809 for c in cipher.clone()
1810 { print!("{:02X} ", c); }
1811 println!();
1812 let mut txt = String::new();
1813 for c in cipher.clone()
1814 { write!(txt, "{:02X} ", c); }
1815 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 F4 BE 6B A5 C5 7D F6 5D ");
1816 println!();
1817
1818 // Expanded case for 128 rounds
1819 let key = 0x_1234567890ABCDEF_u64;
1820 println!("K =\t{:#016X}", key);
1821 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
1822
1823 let message = "In the beginning God created the heavens and the earth.";
1824 println!("M =\t{}", message);
1825 let message = unsafe { message.to_string().as_mut_vec().clone() };
1826 let mut cipher = Vec::<u8>::new();
1827 a_des.encrypt_vec_into_vec(&message, &mut cipher);
1828 print!("C (128 rounds) =\t");
1829 for c in cipher.clone()
1830 { print!("{:02X} ", c); }
1831 println!();
1832 let mut txt = String::new();
1833 for c in cipher.clone()
1834 { write!(txt, "{:02X} ", c); }
1835 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
1836 println!();
1837
1838 // Expanded case for 0 rounds which means that key is meaningless
1839 let key1 = 0x_1234567890ABCDEF_u64;
1840 let key2 = 0_u64;
1841 println!("K1 =\t{:#016X}", key1);
1842 println!("K2 =\t{:#016X}", key2);
1843 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
1844 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
1845
1846 let message = "In the beginning God created the heavens and the earth.";
1847 println!("M =\t{}", message);
1848 let message = unsafe { message.to_string().as_mut_vec().clone() };
1849
1850 let mut cipher1 = Vec::<u8>::new();
1851 let mut cipher2 = Vec::<u8>::new();
1852 c_des.encrypt_vec_into_vec(&message, &mut cipher1);
1853 d_des.encrypt_vec_into_vec(&message, &mut cipher2);
1854 print!("C (0 rounds) =\t");
1855 for c in cipher1.clone()
1856 { print!("{:02X} ", c); }
1857 println!();
1858 let mut txt = String::new();
1859 for c in cipher1.clone()
1860 { write!(txt, "{:02X} ", c); }
1861 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
1862 print!("D (0 rounds) =\t");
1863 for c in cipher2.clone()
1864 { print!("{:02X} ", c); }
1865 println!();
1866 let mut txt = String::new();
1867 for c in cipher2.clone()
1868 { write!(txt, "{:02X} ", c); }
1869 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
1870 println!();
1871
1872 // Normal case for the message of 0 bytes
1873 let key = 0x_1234567890ABCDEF_u64;
1874 println!("K =\t{:#016X}", key);
1875 let mut a_des = DES::new_with_key_u64(key);
1876
1877 let message = "";
1878 println!("M =\t{}", message);
1879 let message = unsafe { message.to_string().as_mut_vec().clone() };
1880 let mut cipher = Vec::<u8>::new();
1881 a_des.encrypt_vec_into_vec(&message, &mut cipher);
1882 print!("C =\t");
1883 for c in cipher.clone()
1884 { print!("{:02X} ", c); }
1885 println!();
1886 let mut txt = String::new();
1887 for c in cipher.clone()
1888 { write!(txt, "{:02X} ", c); }
1889 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
1890 println!();
1891
1892 // Normal case for the message shorter than 8 bytes
1893 let key = 0x_1234567890ABCDEF_u64;
1894 println!("K =\t{:#016X}", key);
1895 let mut a_des = DES::new_with_key_u64(key);
1896
1897 let message = "7 bytes";
1898 println!("M =\t{}", message);
1899 let message = unsafe { message.to_string().as_mut_vec().clone() };
1900 let mut cipher = Vec::<u8>::new();
1901 a_des.encrypt_vec_into_vec(&message, &mut cipher);
1902 print!("C =\t");
1903 for c in cipher.clone()
1904 { print!("{:02X} ", c); }
1905 println!();
1906 let mut txt = String::new();
1907 for c in cipher.clone()
1908 { write!(txt, "{:02X} ", c); }
1909 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
1910 println!();
1911
1912 // Normal case for the message of 8 bytes
1913 let key = 0x_1234567890ABCDEF_u64;
1914 println!("K =\t{:#016X}", key);
1915 let mut a_des = DES::new_with_key_u64(key);
1916
1917 let message = "I am OK.";
1918 println!("M =\t{}", message);
1919 let message = unsafe { message.to_string().as_mut_vec().clone() };
1920 let mut cipher = Vec::<u8>::new();
1921 a_des.encrypt_vec_into_vec(&message, &mut cipher);
1922 print!("C =\t");
1923 for c in cipher.clone()
1924 { print!("{:02X} ", c); }
1925 println!();
1926 let mut txt = String::new();
1927 for c in cipher.clone()
1928 { write!(txt, "{:02X} ", c); }
1929 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
1930 println!();
1931
1932 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
1933 let key = 0x_1234567890ABCDEF_u64;
1934 println!("K =\t{:#016X}", key);
1935 let mut a_des = DES::new_with_key_u64(key);
1936
1937 let message = "PARK Youngho";
1938 println!("M =\t{}", message);
1939 let message = unsafe { message.to_string().as_mut_vec().clone() };
1940 let mut cipher = Vec::<u8>::new();
1941 a_des.encrypt_vec_into_vec(&message, &mut cipher);
1942 print!("C =\t");
1943 for c in cipher.clone()
1944 { print!("{:02X} ", c); }
1945 println!();
1946 let mut txt = String::new();
1947 for c in cipher.clone()
1948 { write!(txt, "{:02X} ", c); }
1949 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
1950 println!();
1951
1952
1953 // Normal case for the message of 16 bytes
1954 let key = 0x_1234567890ABCDEF_u64;
1955 println!("K =\t{:#016X}", key);
1956 let mut a_des = DES::new_with_key_u64(key);
1957
1958 let message = "고맙습니다.";
1959 println!("M =\t{}", message);
1960 let message = unsafe { message.to_string().as_mut_vec().clone() };
1961 let mut cipher = Vec::<u8>::new();
1962 a_des.encrypt_vec_into_vec(&message, &mut cipher);
1963 print!("C =\t");
1964 for c in cipher.clone()
1965 { print!("{:02X} ", c); }
1966 println!();
1967 let mut txt = String::new();
1968 for c in cipher.clone()
1969 { write!(txt, "{:02X} ", c); }
1970 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
1971 println!("-------------------------------");
1972}
Sourcefn encrypt_vec_into_array<U, V, const N: usize>(
&mut self,
message: &Vec<U>,
cipher: &mut [V; N],
) -> u64
fn encrypt_vec_into_array<U, V, const N: usize>( &mut self, message: &Vec<U>, cipher: &mut [V; N], ) -> u64
Encrypts the data stored in a Vec<U>
object with the padding according
to ISO 7816-4 in ECB (Electronic CodeBook) mode, and stores the encrypted
data in array [V; N]
.
§Arguments
message
is aVec<U>
object, and is the plaintext to be encrypted.cipher
is an array[V; N]
object, and is the ciphertext to be stored.
§Output
- This method returns the size of ciphertext including padding bits in bytes.
- When
T
isu64
, the output should be at least8
, and will be only any multiple of8
. - When
T
isu128
, the output should be at least16
, and will be only any multiple of16
. - If this method returns
zero
, it means this method failed in encryption.
§Features
- If
message
is an emptyVec<U>
objectVec::<U>::new()
, only padding bytes will be encrypted, and stored in the array[V; N]
objectcipher
. - If
V::size_in_bytes() * N
is less thanU::size_in_bytes() * message.len()
’s next multiple of 8, this method does not perform encryption and returnszero
. - If
V::size_in_bytes() * N
is equal toU::size_in_bytes() * message.len()
’s next multiple of 8, this method performs encryption, fills the arraycipher
with the encrypted ciphertext, and returns the size of the ciphertext including padding bits in bytes. - If
V::size_in_bytes() * N
is greater thanU::size_in_bytes() * message.len()
’s next multiple of 8, this method performs encryption, fills the arraycipher
with the encrypted ciphertext, and then fills the rest of the elements of the arraycipher
with zeros, and returns the size of the ciphertext including padding bits in bytes. - The padding bits are composed of the bytes that indicate the length of the padding bits in bytes according to ISO 7816-4 defined in RFC 5652.
- For more information about the padding bits according to PKCS#7, Read here.
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, ECB_ISO };
let key = 0x_1234567890ABCDEF_u64;
println!("K =\t{:#016X}", key);
let mut a_des = DES::new_with_key_u64(key);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let message = unsafe { message.to_string().as_mut_vec().clone() };
let mut cipher = [0_u8; 56];
a_des.encrypt_vec_into_array(&message, &mut cipher);
print!("C (16 rounds) =\t");
for c in cipher.clone()
{ print!("{:02X} ", c); }
println!();
let mut txt = String::new();
for c in cipher.clone()
{ write!(txt, "{:02X} ", c); }
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 F4 BE 6B A5 C5 7D F6 5D ");
§For more examples,
click here
Examples found in repository?
1974fn des_encrypt_vec_with_padding_iso_ecb_into_array()
1975{
1976 println!("des_encrypt_vec_with_padding_iso_ecb_into_array");
1977 use std::io::Write;
1978 use std::fmt::Write as _;
1979 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
1980
1981 // Normal case
1982 let key = 0x_1234567890ABCDEF_u64;
1983 println!("K =\t{:#016X}", key);
1984 let mut a_des = DES::new_with_key_u64(key);
1985
1986 let message = "In the beginning God created the heavens and the earth.";
1987 println!("M =\t{}", message);
1988 let message = unsafe { message.to_string().as_mut_vec().clone() };
1989 let mut cipher = [0_u8; 56];
1990 a_des.encrypt_vec_into_array(&message, &mut cipher);
1991 print!("C (16 rounds) =\t");
1992 for c in cipher.clone()
1993 { print!("{:02X} ", c); }
1994 println!();
1995 let mut txt = String::new();
1996 for c in cipher.clone()
1997 { write!(txt, "{:02X} ", c); }
1998 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 F4 BE 6B A5 C5 7D F6 5D ");
1999 println!();
2000
2001 // Expanded case for 128 rounds
2002 let key = 0x_1234567890ABCDEF_u64;
2003 println!("K =\t{:#016X}", key);
2004 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
2005
2006 let message = "In the beginning God created the heavens and the earth.";
2007 println!("M =\t{}", message);
2008 let message = unsafe { message.to_string().as_mut_vec().clone() };
2009 let mut cipher = [0_u8; 56];
2010 a_des.encrypt_vec_into_array(&message, &mut cipher);
2011 print!("C (128 rounds) =\t");
2012 for c in cipher.clone()
2013 { print!("{:02X} ", c); }
2014 println!();
2015 let mut txt = String::new();
2016 for c in cipher.clone()
2017 { write!(txt, "{:02X} ", c); }
2018 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
2019 println!();
2020
2021 // Expanded case for 0 rounds which means that key is meaningless
2022 let key1 = 0x_1234567890ABCDEF_u64;
2023 let key2 = 0_u64;
2024 println!("K1 =\t{:#016X}", key1);
2025 println!("K2 =\t{:#016X}", key2);
2026 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
2027 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
2028
2029 let message = "In the beginning God created the heavens and the earth.";
2030 println!("M =\t{}", message);
2031 let message = unsafe { message.to_string().as_mut_vec().clone() };
2032 let mut cipher1 = [0_u8; 56];
2033 let mut cipher2 = [0_u8; 56];
2034 c_des.encrypt_vec_into_array(&message, &mut cipher1);
2035 d_des.encrypt_vec_into_array(&message, &mut cipher2);
2036 print!("C (0 rounds) =\t");
2037 for c in cipher1.clone()
2038 { print!("{:02X} ", c); }
2039 println!();
2040 let mut txt = String::new();
2041 for c in cipher1.clone()
2042 { write!(txt, "{:02X} ", c); }
2043 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
2044 print!("D (0 rounds) =\t");
2045 for c in cipher2.clone()
2046 { print!("{:02X} ", c); }
2047 println!();
2048 let mut txt = String::new();
2049 for c in cipher2.clone()
2050 { write!(txt, "{:02X} ", c); }
2051 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
2052 println!();
2053
2054 // Normal case for the message of 0 bytes
2055 let key = 0x_1234567890ABCDEF_u64;
2056 println!("K =\t{:#016X}", key);
2057 let mut a_des = DES::new_with_key_u64(key);
2058
2059 let message = "";
2060 println!("M =\t{}", message);
2061 let message = unsafe { message.to_string().as_mut_vec().clone() };
2062 let mut cipher = [0_u8; 8];
2063 a_des.encrypt_vec_into_array(&message, &mut cipher);
2064 print!("C =\t");
2065 for c in cipher.clone()
2066 { print!("{:02X} ", c); }
2067 println!();
2068 let mut txt = String::new();
2069 for c in cipher.clone()
2070 { write!(txt, "{:02X} ", c); }
2071 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
2072 println!();
2073
2074 // Normal case for the message shorter than 8 bytes
2075 let key = 0x_1234567890ABCDEF_u64;
2076 println!("K =\t{:#016X}", key);
2077 let mut a_des = DES::new_with_key_u64(key);
2078
2079 let message = "7 bytes";
2080 println!("M =\t{}", message);
2081 let message = unsafe { message.to_string().as_mut_vec().clone() };
2082 let mut cipher = [0_u8; 8];
2083 a_des.encrypt_vec_into_array(&message, &mut cipher);
2084 print!("C =\t");
2085 for c in cipher.clone()
2086 { print!("{:02X} ", c); }
2087 println!();
2088 let mut txt = String::new();
2089 for c in cipher.clone()
2090 { write!(txt, "{:02X} ", c); }
2091 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
2092 println!();
2093
2094 // Normal case for the message of 8 bytes
2095 let key = 0x_1234567890ABCDEF_u64;
2096 println!("K =\t{:#016X}", key);
2097 let mut a_des = DES::new_with_key_u64(key);
2098
2099 let message = "I am OK.";
2100 println!("M =\t{}", message);
2101 let message = unsafe { message.to_string().as_mut_vec().clone() };
2102 let mut cipher = [0_u8; 16];
2103 a_des.encrypt_vec_into_array(&message, &mut cipher);
2104 print!("C =\t");
2105 for c in cipher.clone()
2106 { print!("{:02X} ", c); }
2107 println!();
2108 let mut txt = String::new();
2109 for c in cipher.clone()
2110 { write!(txt, "{:02X} ", c); }
2111 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
2112 println!();
2113
2114 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
2115 let key = 0x_1234567890ABCDEF_u64;
2116 println!("K =\t{:#016X}", key);
2117 let mut a_des = DES::new_with_key_u64(key);
2118
2119 let message = "PARK Youngho";
2120 println!("M =\t{}", message);
2121 let message = unsafe { message.to_string().as_mut_vec().clone() };
2122 let mut cipher = [0_u8; 16];
2123 a_des.encrypt_vec_into_array(&message, &mut cipher);
2124 print!("C =\t");
2125 for c in cipher.clone()
2126 { print!("{:02X} ", c); }
2127 println!();
2128 let mut txt = String::new();
2129 for c in cipher.clone()
2130 { write!(txt, "{:02X} ", c); }
2131 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
2132 println!();
2133
2134
2135 // Normal case for the message of 16 bytes
2136 let key = 0x_1234567890ABCDEF_u64;
2137 println!("K =\t{:#016X}", key);
2138 let mut a_des = DES::new_with_key_u64(key);
2139
2140 let message = "고맙습니다.";
2141 println!("M =\t{}", message);
2142 let message = unsafe { message.to_string().as_mut_vec().clone() };
2143 let mut cipher = [0_u8; 24];
2144 a_des.encrypt_vec_into_array(&message, &mut cipher);
2145 print!("C =\t");
2146 for c in cipher.clone()
2147 { print!("{:02X} ", c); }
2148 println!();
2149 let mut txt = String::new();
2150 for c in cipher.clone()
2151 { write!(txt, "{:02X} ", c); }
2152 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
2153 println!("-------------------------------");
2154}
Sourcefn encrypt_array<U, const N: usize>(
&mut self,
message: &[U; N],
cipher: *mut u8,
) -> u64
fn encrypt_array<U, const N: usize>( &mut self, message: &[U; N], cipher: *mut u8, ) -> u64
Encrypts the data stored in an array [U; N]
object with the padding
defined according to ISO 7816-4 in ECB (Electronic CodeBook) mode.
§Arguments
message
is the data stored in an array[U; N]
object, and is the plaintext to be encrypted.cipher
is a pointer to u8 which is*mut u8
, and is the ciphertext to be stored.- The size of the memory area which starts at
cipher
and the ciphertext will be stored at is assumed to be enough. - The size of the area for ciphertext should be prepared to be:
(
length_in_bytes
+ 1).next_multiple_of(8) at least whenT
isu64
, and (length_in_bytes
+ 1).next_multiple_of(16) at least whenT
isu128
. So, it is responsible for you to prepare thecipher
area big enough!
§Output
- This method returns the size of ciphertext including padding bits in bytes.
- When
T
isu64
, the output should be at least8
, and will be only any multiple of8
. - When
T
isu128
, the output should be at least16
, and will be only any multiple of16
. - If this method returns
zero
, it means this method failed in encryption.
§Features
- You are not encouraged to use this method in pure Rust programming. Instead, use other safer methods such as encrypt_array_into_*().
- This method is useful to use in hybrid programming with C/C++.
- If
message.len()
is0
, it means the message is empty data. So, only padding bytes will be encrypted, and stored in the memory area that starts fromcipher
. - The padding bits are composed of the bytes that indicate the length of the padding bits in bytes according to ISO 7816-4 defined in RFC 5652.
- For more information about the padding bits according to ISO 7816-4, Read here.
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, ECB_ISO };
let key = 0x_1234567890ABCDEF_u64;
println!("K =\t{:#016X}", key);
let mut a_des = DES::new_with_key_u64(key);
let mes = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", mes);
let mut message = [0_u8; 55];
message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
let mut cipher = [0_u8; 56];
a_des.encrypt_array(&message, cipher.as_mut_ptr());
print!("C (16 rounds) =\t");
for c in cipher.clone()
{ print!("{:02X} ", c); }
println!();
let mut txt = String::new();
for c in cipher.clone()
{ write!(txt, "{:02X} ", c); }
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 F4 BE 6B A5 C5 7D F6 5D ");
§For more examples,
click here
Examples found in repository?
2156fn des_encrypt_array_with_padding_iso_ecb()
2157{
2158 println!("des_encrypt_array_with_padding_iso_ecb");
2159 use std::io::Write;
2160 use std::fmt::Write as _;
2161 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
2162
2163 // Normal case
2164 let key = 0x_1234567890ABCDEF_u64;
2165 println!("K =\t{:#016X}", key);
2166 let mut a_des = DES::new_with_key_u64(key);
2167
2168 let mes = "In the beginning God created the heavens and the earth.";
2169 println!("M =\t{}", mes);
2170 let mut message = [0_u8; 55];
2171 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2172 let mut cipher = [0_u8; 56];
2173 a_des.encrypt_array(&message, cipher.as_mut_ptr());
2174 print!("C (16 rounds) =\t");
2175 for c in cipher.clone()
2176 { print!("{:02X} ", c); }
2177 println!();
2178 let mut txt = String::new();
2179 for c in cipher.clone()
2180 { write!(txt, "{:02X} ", c); }
2181 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 F4 BE 6B A5 C5 7D F6 5D ");
2182 println!();
2183
2184 // Expanded case for 128 rounds
2185 let key = 0x_1234567890ABCDEF_u64;
2186 println!("K =\t{:#016X}", key);
2187 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
2188
2189 let mes = "In the beginning God created the heavens and the earth.";
2190 println!("M =\t{}", mes);
2191 let mut message = [0_u8; 55];
2192 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2193 let mut cipher = [0_u8; 56];
2194 a_des.encrypt_array(&message, cipher.as_mut_ptr());
2195 print!("C (128 rounds) =\t");
2196 for c in cipher.clone()
2197 { print!("{:02X} ", c); }
2198 println!();
2199 let mut txt = String::new();
2200 for c in cipher.clone()
2201 { write!(txt, "{:02X} ", c); }
2202 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
2203 println!();
2204
2205 // Expanded case for 0 rounds which means that key is meaningless
2206 let key1 = 0x_1234567890ABCDEF_u64;
2207 let key2 = 0_u64;
2208 println!("K1 =\t{:#016X}", key1);
2209 println!("K2 =\t{:#016X}", key2);
2210 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
2211 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
2212
2213 let mes = "In the beginning God created the heavens and the earth.";
2214 println!("M =\t{}", mes);
2215 let mut message = [0_u8; 55];
2216 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2217 let mut cipher1 = [0_u8; 56];
2218 let mut cipher2 = [0_u8; 56];
2219 c_des.encrypt_array(&message, cipher1.as_mut_ptr());
2220 d_des.encrypt_array(&message, cipher2.as_mut_ptr());
2221 print!("C (0 rounds) =\t");
2222 for c in cipher1.clone()
2223 { print!("{:02X} ", c); }
2224 println!();
2225 let mut txt = String::new();
2226 for c in cipher1.clone()
2227 { write!(txt, "{:02X} ", c); }
2228 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
2229 print!("D (0 rounds) =\t");
2230 for c in cipher2.clone()
2231 { print!("{:02X} ", c); }
2232 println!();
2233 let mut txt = String::new();
2234 for c in cipher2.clone()
2235 { write!(txt, "{:02X} ", c); }
2236 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
2237 println!();
2238
2239 // Normal case for the message of 0 bytes
2240 let key = 0x_1234567890ABCDEF_u64;
2241 println!("K =\t{:#016X}", key);
2242 let mut a_des = DES::new_with_key_u64(key);
2243
2244 let mes = "";
2245 println!("M =\t{}", mes);
2246 let mut message = [0_u8; 0];
2247 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2248 let mut cipher = [0_u8; 8];
2249 a_des.encrypt_array(&message, cipher.as_mut_ptr());
2250 print!("C =\t");
2251 for c in cipher.clone()
2252 { print!("{:02X} ", c); }
2253 println!();
2254 let mut txt = String::new();
2255 for c in cipher.clone()
2256 { write!(txt, "{:02X} ", c); }
2257 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
2258 println!();
2259
2260 // Normal case for the message shorter than 8 bytes
2261 let key = 0x_1234567890ABCDEF_u64;
2262 println!("K =\t{:#016X}", key);
2263 let mut a_des = DES::new_with_key_u64(key);
2264
2265 let mes = "7 bytes";
2266 println!("M =\t{}", mes);
2267 let mut message = [0_u8; 7];
2268 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2269 let mut cipher = [0_u8; 8];
2270 a_des.encrypt_array(&message, cipher.as_mut_ptr());
2271 print!("C =\t");
2272 for c in cipher.clone()
2273 { print!("{:02X} ", c); }
2274 println!();
2275 let mut txt = String::new();
2276 for c in cipher.clone()
2277 { write!(txt, "{:02X} ", c); }
2278 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
2279 println!();
2280
2281 // Normal case for the message of 8 bytes
2282 let key = 0x_1234567890ABCDEF_u64;
2283 println!("K =\t{:#016X}", key);
2284 let mut a_des = DES::new_with_key_u64(key);
2285
2286 let mes = "I am OK.";
2287 println!("M =\t{}", mes);
2288 let mut message = [0_u8; 8];
2289 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2290 let mut cipher = [0_u8; 16];
2291 a_des.encrypt_array(&message, cipher.as_mut_ptr());
2292 print!("C =\t");
2293 for c in cipher.clone()
2294 { print!("{:02X} ", c); }
2295 println!();
2296 let mut txt = String::new();
2297 for c in cipher.clone()
2298 { write!(txt, "{:02X} ", c); }
2299 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
2300 println!();
2301
2302 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
2303 let key = 0x_1234567890ABCDEF_u64;
2304 println!("K =\t{:#016X}", key);
2305 let mut a_des = DES::new_with_key_u64(key);
2306
2307 let mes = "PARK Youngho";
2308 println!("M =\t{}", mes);
2309 let mut message = [0_u8; 12];
2310 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2311 let mut cipher = [0_u8; 16];
2312 a_des.encrypt_array(&message, cipher.as_mut_ptr());
2313 print!("C =\t");
2314 for c in cipher.clone()
2315 { print!("{:02X} ", c); }
2316 println!();
2317 let mut txt = String::new();
2318 for c in cipher.clone()
2319 { write!(txt, "{:02X} ", c); }
2320 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
2321 println!();
2322
2323
2324 // Normal case for the message of 16 bytes
2325 let key = 0x_1234567890ABCDEF_u64;
2326 println!("K =\t{:#016X}", key);
2327 let mut a_des = DES::new_with_key_u64(key);
2328
2329 let mes = "고맙습니다.";
2330 println!("M =\t{}", mes);
2331 let mut message = [0_u8; 16];
2332 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2333 let mut cipher = [0_u8; 24];
2334 a_des.encrypt_array(&message, cipher.as_mut_ptr());
2335 print!("C =\t");
2336 for c in cipher.clone()
2337 { print!("{:02X} ", c); }
2338 println!();
2339 let mut txt = String::new();
2340 for c in cipher.clone()
2341 { write!(txt, "{:02X} ", c); }
2342 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
2343 println!("-------------------------------");
2344}
Sourcefn encrypt_array_into_vec<U, V, const N: usize>(
&mut self,
message: &[U; N],
cipher: &mut Vec<V>,
) -> u64
fn encrypt_array_into_vec<U, V, const N: usize>( &mut self, message: &[U; N], cipher: &mut Vec<V>, ) -> u64
Encrypts the data stored in an array [U; N]
object with the padding
according to ISO 7816-4 in ECB (Electronic CodeBook) mode, and stores the
encrypted data in Vec<V>
.
§Arguments
message
is an array[U; N]
object, and is the plaintext to be encrypted.cipher
is aVec<V>
object, and is the ciphertext to be stored.
§Output
- This method returns the size of ciphertext including padding bits in bytes.
- When
T
isu64
, the output should be at least8
, and will be only any multiple of8
. - When
T
isu128
, the output should be at least16
, and will be only any multiple of16
. - If this method returns
zero
, it means this method failed in encryption.
§Features
- If
message
is an empty array[U; N]
object [U; 0], only padding bytes will be encrypted, and stored in theVec<U>
objectcipher
. - The padding bits are composed of the bytes that indicate the length of the padding bits in bytes according to ISO 7816-4 defined in RFC 5652.
- For more information about the padding bits according to ISO 7816-4, Read here.
- You don’t have to worry about whether or not the size of the memory area where the ciphertext will be stored is enough.
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, ECB_ISO };
let key = 0x_1234567890ABCDEF_u64;
println!("K =\t{:#016X}", key);
let mut a_des = DES::new_with_key_u64(key);
let mes = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", mes);
let mut message = [0_u8; 55];
message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
let mut cipher = Vec::<u8>::new();
a_des.encrypt_array_into_vec(&message, &mut cipher);
print!("C (16 rounds) =\t");
for c in cipher.clone()
{ print!("{:02X} ", c); }
println!();
let mut txt = String::new();
for c in cipher.clone()
{ write!(txt, "{:02X} ", c); }
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 F4 BE 6B A5 C5 7D F6 5D ");
§For more examples,
click here
Examples found in repository?
2346fn des_encrypt_array_with_padding_iso_ecb_into_vec()
2347{
2348 println!("des_encrypt_array_with_padding_iso_ecb_into_vec");
2349 use std::io::Write;
2350 use std::fmt::Write as _;
2351 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
2352
2353 // Normal case
2354 let key = 0x_1234567890ABCDEF_u64;
2355 println!("K =\t{:#016X}", key);
2356 let mut a_des = DES::new_with_key_u64(key);
2357
2358 let mes = "In the beginning God created the heavens and the earth.";
2359 println!("M =\t{}", mes);
2360 let mut message = [0_u8; 55];
2361 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2362 let mut cipher = Vec::<u8>::new();
2363 a_des.encrypt_array_into_vec(&message, &mut cipher);
2364 print!("C (16 rounds) =\t");
2365 for c in cipher.clone()
2366 { print!("{:02X} ", c); }
2367 println!();
2368 let mut txt = String::new();
2369 for c in cipher.clone()
2370 { write!(txt, "{:02X} ", c); }
2371 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 F4 BE 6B A5 C5 7D F6 5D ");
2372 println!();
2373
2374 // Expanded case for 128 rounds
2375 let key = 0x_1234567890ABCDEF_u64;
2376 println!("K =\t{:#016X}", key);
2377 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
2378
2379 let mes = "In the beginning God created the heavens and the earth.";
2380 println!("M =\t{}", mes);
2381 let mut message = [0_u8; 55];
2382 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2383 let mut cipher = Vec::<u8>::new();
2384 a_des.encrypt_array_into_vec(&message, &mut cipher);
2385 print!("C (128 rounds) =\t");
2386 for c in cipher.clone()
2387 { print!("{:02X} ", c); }
2388 println!();
2389 let mut txt = String::new();
2390 for c in cipher.clone()
2391 { write!(txt, "{:02X} ", c); }
2392 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
2393 println!();
2394
2395 // Expanded case for 0 rounds which means that key is meaningless
2396 let key1 = 0x_1234567890ABCDEF_u64;
2397 let key2 = 0_u64;
2398 println!("K1 =\t{:#016X}", key1);
2399 println!("K2 =\t{:#016X}", key2);
2400 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
2401 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
2402
2403 let mes = "In the beginning God created the heavens and the earth.";
2404 println!("M =\t{}", mes);
2405 let mut message = [0_u8; 55];
2406 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2407
2408 let mut cipher1 = Vec::<u8>::new();
2409 let mut cipher2 = Vec::<u8>::new();
2410 c_des.encrypt_array_into_vec(&message, &mut cipher1);
2411 d_des.encrypt_array_into_vec(&message, &mut cipher2);
2412 print!("C (0 rounds) =\t");
2413 for c in cipher1.clone()
2414 { print!("{:02X} ", c); }
2415 println!();
2416 let mut txt = String::new();
2417 for c in cipher1.clone()
2418 { write!(txt, "{:02X} ", c); }
2419 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
2420 print!("D (0 rounds) =\t");
2421 for c in cipher2.clone()
2422 { print!("{:02X} ", c); }
2423 println!();
2424 let mut txt = String::new();
2425 for c in cipher2.clone()
2426 { write!(txt, "{:02X} ", c); }
2427 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
2428 println!();
2429
2430 // Normal case for the message of 0 bytes
2431 let key = 0x_1234567890ABCDEF_u64;
2432 println!("K =\t{:#016X}", key);
2433 let mut a_des = DES::new_with_key_u64(key);
2434
2435 let mes = "";
2436 println!("M =\t{}", mes);
2437 let mut message = [0_u8; 0];
2438 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2439 let mut cipher = Vec::<u8>::new();
2440 a_des.encrypt_array_into_vec(&message, &mut cipher);
2441 print!("C =\t");
2442 for c in cipher.clone()
2443 { print!("{:02X} ", c); }
2444 println!();
2445 let mut txt = String::new();
2446 for c in cipher.clone()
2447 { write!(txt, "{:02X} ", c); }
2448 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
2449 println!();
2450
2451 // Normal case for the message shorter than 8 bytes
2452 let key = 0x_1234567890ABCDEF_u64;
2453 println!("K =\t{:#016X}", key);
2454 let mut a_des = DES::new_with_key_u64(key);
2455
2456 let mes = "7 bytes";
2457 println!("M =\t{}", mes);
2458 let mut message = [0_u8; 7];
2459 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2460 let mut cipher = Vec::<u8>::new();
2461 a_des.encrypt_array_into_vec(&message, &mut cipher);
2462 print!("C =\t");
2463 for c in cipher.clone()
2464 { print!("{:02X} ", c); }
2465 println!();
2466 let mut txt = String::new();
2467 for c in cipher.clone()
2468 { write!(txt, "{:02X} ", c); }
2469 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
2470 println!();
2471
2472 // Normal case for the message of 8 bytes
2473 let key = 0x_1234567890ABCDEF_u64;
2474 println!("K =\t{:#016X}", key);
2475 let mut a_des = DES::new_with_key_u64(key);
2476
2477 let mes = "I am OK.";
2478 println!("M =\t{}", mes);
2479 let mut message = [0_u8; 8];
2480 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2481 let mut cipher = Vec::<u8>::new();
2482 a_des.encrypt_array_into_vec(&message, &mut cipher);
2483 print!("C =\t");
2484 for c in cipher.clone()
2485 { print!("{:02X} ", c); }
2486 println!();
2487 let mut txt = String::new();
2488 for c in cipher.clone()
2489 { write!(txt, "{:02X} ", c); }
2490 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
2491 println!();
2492
2493 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
2494 let key = 0x_1234567890ABCDEF_u64;
2495 println!("K =\t{:#016X}", key);
2496 let mut a_des = DES::new_with_key_u64(key);
2497
2498 let mes = "PARK Youngho";
2499 println!("M =\t{}", mes);
2500 let mut message = [0_u8; 12];
2501 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2502 let mut cipher = Vec::<u8>::new();
2503 a_des.encrypt_array_into_vec(&message, &mut cipher);
2504 print!("C =\t");
2505 for c in cipher.clone()
2506 { print!("{:02X} ", c); }
2507 println!();
2508 let mut txt = String::new();
2509 for c in cipher.clone()
2510 { write!(txt, "{:02X} ", c); }
2511 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
2512 println!();
2513
2514
2515 // Normal case for the message of 16 bytes
2516 let key = 0x_1234567890ABCDEF_u64;
2517 println!("K =\t{:#016X}", key);
2518 let mut a_des = DES::new_with_key_u64(key);
2519
2520 let mes = "고맙습니다.";
2521 println!("M =\t{}", mes);
2522 let mut message = [0_u8; 16];
2523 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2524 let mut cipher = Vec::<u8>::new();
2525 a_des.encrypt_array_into_vec(&message, &mut cipher);
2526 print!("C =\t");
2527 for c in cipher.clone()
2528 { print!("{:02X} ", c); }
2529 println!();
2530 let mut txt = String::new();
2531 for c in cipher.clone()
2532 { write!(txt, "{:02X} ", c); }
2533 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
2534 println!("-------------------------------");
2535}
Sourcefn encrypt_array_into_array<U, V, const N: usize, const M: usize>(
&mut self,
message: &[U; N],
cipher: &mut [V; M],
) -> u64
fn encrypt_array_into_array<U, V, const N: usize, const M: usize>( &mut self, message: &[U; N], cipher: &mut [V; M], ) -> u64
Encrypts the data stored in an array [U; N]
object with the padding
according to ISO 7816-4 in ECB (Electronic CodeBook) mode, and stores the
encrypted data in array [V; M]
.
§Arguments
message
is an array[U; N]
object, and is the plaintext to be encrypted.cipher
is an array[V; M]
object, and is the ciphertext to be stored.
§Output
- This method returns the size of ciphertext including padding bits in bytes.
- When
T
isu64
, the output should be at least8
, and will be only any multiple of8
. - When
T
isu128
, the output should be at least16
, and will be only any multiple of16
. - If this method returns
zero
, it means this method failed in encryption.
§Features
- If
message
is an empty array[U; N]
object [U; 0], only padding bytes will be encrypted, and stored in the array[V; M]
objectcipher
. - If
V::size_in_bytes() * M
is less thanU::size_in_bytes() * N
’s next multiple of 8, this method does not perform encryption and returnszero
. - If
V::size_in_bytes() * M
is equal toU::size_in_bytes() * N
’s next multiple of 8, this method performs encryption, fills the arraycipher
with the encrypted ciphertext, and returns the size of the ciphertext including padding bits in bytes. - If
V::size_in_bytes() * M
is greater thanU::size_in_bytes() * N
’s next multiple of 8, this method performs encryption, fills the arraycipher
with the encrypted ciphertext, and then fills the rest of the elements of the arraycipher
with zeros, and returns the size of the ciphertext including padding bits in bytes. - The padding bits are composed of the bytes that indicate the length of the padding bits in bytes according to ISO 7816-4 defined in RFC 5652.
- For more information about the padding bits according to PKCS#7, Read here.
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, ECB_ISO };
let key = 0x_1234567890ABCDEF_u64;
println!("K =\t{:#016X}", key);
let mut a_des = DES::new_with_key_u64(key);
let mes = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", mes);
let mut message = [0_u8; 55];
message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
let mut cipher = [0_u8; 56];
a_des.encrypt_array_into_array(&message, &mut cipher);
for c in cipher.clone()
{ print!("{:02X} ", c); }
println!();
let mut txt = String::new();
for c in cipher.clone()
{ write!(txt, "{:02X} ", c); }
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 F4 BE 6B A5 C5 7D F6 5D ");
§For more examples,
click here
Examples found in repository?
2537fn des_encrypt_array_with_padding_iso_ecb_into_array()
2538{
2539 println!("des_encrypt_array_with_padding_iso_ecb_into_array");
2540 use std::io::Write;
2541 use std::fmt::Write as _;
2542 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
2543
2544 // Normal case
2545 let key = 0x_1234567890ABCDEF_u64;
2546 println!("K =\t{:#016X}", key);
2547 let mut a_des = DES::new_with_key_u64(key);
2548
2549 let mes = "In the beginning God created the heavens and the earth.";
2550 println!("M =\t{}", mes);
2551 let mut message = [0_u8; 55];
2552 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2553 let mut cipher = [0_u8; 56];
2554 a_des.encrypt_array_into_array(&message, &mut cipher);
2555 for c in cipher.clone()
2556 { print!("{:02X} ", c); }
2557 println!();
2558 let mut txt = String::new();
2559 for c in cipher.clone()
2560 { write!(txt, "{:02X} ", c); }
2561 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 F4 BE 6B A5 C5 7D F6 5D ");
2562 println!();
2563
2564 // Expanded case for 128 rounds
2565 let key = 0x_1234567890ABCDEF_u64;
2566 println!("K =\t{:#016X}", key);
2567 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
2568
2569 let mes = "In the beginning God created the heavens and the earth.";
2570 println!("M =\t{}", mes);
2571 let mut message = [0_u8; 55];
2572 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2573 let mut cipher = [0_u8; 56];
2574 a_des.encrypt_array_into_array(&message, &mut cipher);
2575 print!("C (128 rounds) =\t");
2576 for c in cipher.clone()
2577 { print!("{:02X} ", c); }
2578 println!();
2579 let mut txt = String::new();
2580 for c in cipher.clone()
2581 { write!(txt, "{:02X} ", c); }
2582 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
2583 println!();
2584
2585 // Expanded case for 0 rounds which means that key is meaningless
2586 let key1 = 0x_1234567890ABCDEF_u64;
2587 let key2 = 0_u64;
2588 println!("K1 =\t{:#016X}", key1);
2589 println!("K2 =\t{:#016X}", key2);
2590 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
2591 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
2592
2593 let mes = "In the beginning God created the heavens and the earth.";
2594 println!("M =\t{}", mes);
2595 let mut message = [0_u8; 55];
2596 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2597 let mut cipher1 = [0_u8; 56];
2598 let mut cipher2 = [0_u8; 56];
2599 c_des.encrypt_array_into_array(&message, &mut cipher1);
2600 d_des.encrypt_array_into_array(&message, &mut cipher2);
2601 print!("C (0 rounds) =\t");
2602 for c in cipher1.clone()
2603 { print!("{:02X} ", c); }
2604 println!();
2605 let mut txt = String::new();
2606 for c in cipher1.clone()
2607 { write!(txt, "{:02X} ", c); }
2608 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
2609 print!("D (0 rounds) =\t");
2610 for c in cipher2.clone()
2611 { print!("{:02X} ", c); }
2612 println!();
2613 let mut txt = String::new();
2614 for c in cipher2.clone()
2615 { write!(txt, "{:02X} ", c); }
2616 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
2617 println!();
2618
2619 // Normal case for the message of 0 bytes
2620 let key = 0x_1234567890ABCDEF_u64;
2621 println!("K =\t{:#016X}", key);
2622 let mut a_des = DES::new_with_key_u64(key);
2623
2624 let mes = "";
2625 println!("M =\t{}", mes);
2626 let mut message = [0_u8; 0];
2627 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2628 let mut cipher = [0_u8; 8];
2629 a_des.encrypt_array_into_array(&message, &mut cipher);
2630 print!("C =\t");
2631 for c in cipher.clone()
2632 { print!("{:02X} ", c); }
2633 println!();
2634 let mut txt = String::new();
2635 for c in cipher.clone()
2636 { write!(txt, "{:02X} ", c); }
2637 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
2638 println!();
2639
2640 // Normal case for the message shorter than 8 bytes
2641 let key = 0x_1234567890ABCDEF_u64;
2642 println!("K =\t{:#016X}", key);
2643 let mut a_des = DES::new_with_key_u64(key);
2644
2645 let mes = "7 bytes";
2646 println!("M =\t{}", mes);
2647 let mut message = [0_u8; 7];
2648 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2649 let mut cipher = [0_u8; 8];
2650 a_des.encrypt_array_into_array(&message, &mut cipher);
2651 print!("C =\t");
2652 for c in cipher.clone()
2653 { print!("{:02X} ", c); }
2654 println!();
2655 let mut txt = String::new();
2656 for c in cipher.clone()
2657 { write!(txt, "{:02X} ", c); }
2658 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
2659 println!();
2660
2661 // Normal case for the message of 8 bytes
2662 let key = 0x_1234567890ABCDEF_u64;
2663 println!("K =\t{:#016X}", key);
2664 let mut a_des = DES::new_with_key_u64(key);
2665
2666 let mes = "I am OK.";
2667 println!("M =\t{}", mes);
2668 let mut message = [0_u8; 8];
2669 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2670 let mut cipher = [0_u8; 16];
2671 a_des.encrypt_array_into_array(&message, &mut cipher);
2672 print!("C =\t");
2673 for c in cipher.clone()
2674 { print!("{:02X} ", c); }
2675 println!();
2676 let mut txt = String::new();
2677 for c in cipher.clone()
2678 { write!(txt, "{:02X} ", c); }
2679 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
2680 println!();
2681
2682 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
2683 let key = 0x_1234567890ABCDEF_u64;
2684 println!("K =\t{:#016X}", key);
2685 let mut a_des = DES::new_with_key_u64(key);
2686
2687 let mes = "PARK Youngho";
2688 println!("M =\t{}", mes);
2689 let mut message = [0_u8; 12];
2690 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2691 let mut cipher = [0_u8; 16];
2692 a_des.encrypt_array_into_array(&message, &mut cipher);
2693 print!("C =\t");
2694 for c in cipher.clone()
2695 { print!("{:02X} ", c); }
2696 println!();
2697 let mut txt = String::new();
2698 for c in cipher.clone()
2699 { write!(txt, "{:02X} ", c); }
2700 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
2701 println!();
2702
2703
2704 // Normal case for the message of 16 bytes
2705 let key = 0x_1234567890ABCDEF_u64;
2706 println!("K =\t{:#016X}", key);
2707 let mut a_des = DES::new_with_key_u64(key);
2708
2709 let mes = "고맙습니다.";
2710 println!("M =\t{}", mes);
2711 let mut message = [0_u8; 16];
2712 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2713 let mut cipher = [0_u8; 24];
2714 a_des.encrypt_array_into_array(&message, &mut cipher);
2715 print!("C =\t");
2716 for c in cipher.clone()
2717 { print!("{:02X} ", c); }
2718 println!();
2719 let mut txt = String::new();
2720 for c in cipher.clone()
2721 { write!(txt, "{:02X} ", c); }
2722 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
2723 println!("-------------------------------");
2724}
Sourcefn decrypt_into_vec<U>(
&mut self,
cipher: *const u8,
length_in_bytes: u64,
message: &mut Vec<U>,
) -> u64
fn decrypt_into_vec<U>( &mut self, cipher: *const u8, length_in_bytes: u64, message: &mut Vec<U>, ) -> u64
Decrypts the data with the padding defined according to ISO 7816-4
in ECB (Electronic CodeBook) mode, and stores the decrypted data
in Vec<U>
.
§Arguments
cipher
is a pointer to u8 which is*const u8
, and is the ciphertext to be decrypted.length_in_bytes
is ofu64
-type, and is the length of the ciphertextcipher
in bytes.message
is aVec<U>
object, and is the plaintext to be stored.
§Output
- This method returns the size of plaintext in bytes.
- If this method returns
zero
, andlength_in_bytes
is greater than8
forT
=u64
or16
forT
=u128
, it means that this method failed in decryption. - If this method returns
zero
, andlength_in_bytes
is8
forT
=u64
or16
forT
=u128
, it means either that this method failed in decryption or that the original plaintext is empty data. Then, you will have to check whether or not it failed by the methodis_successful()
oris_failed()
.
§Features
- This method is useful to use in hybrid programming with C/C++.
- When
T
isu64
,length_in_bytes
can be only any multiple of8
. - When
T
isu128
,length_in_bytes
can be only any multiple of16
. - The padding bits are composed of the bytes that indicate the length of the padding bits in bytes according to ISO 7816-4 defined in RFC 5652.
- For more information about the padding bits according to ISO 7816-4, Read here.
- You don’t have to worry about whether or not the size of the memory area where the plaintext will be stored is enough.
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, ECB_ISO };
let key = 0x_1234567890ABCDEF_u64;
println!("K =\t{:#016X}", key);
let mut a_des = DES::new_with_key_u64(key);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
print!("C (16 rounds) =\t");
for c in cipher.clone()
{ print!("{:02X} ", c); }
println!();
let mut txt = String::new();
for c in cipher.clone()
{ write!(txt, "{:02X} ", c); }
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 F4 BE 6B A5 C5 7D F6 5D ");
let mut recovered = Vec::<u8>::new();
a_des.decrypt_into_vec(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
print!("Ba (16 rounds) =\t");
for b in recovered.clone()
{ print!("{:02X} ", b); }
println!();
let mut txt = String::new();
for c in recovered.clone()
{ write!(txt, "{:02X} ", c); }
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 ");
let mut converted = String::new();
unsafe { converted.as_mut_vec() }.append(&mut recovered);
println!("Bb (16 rounds) =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");
assert_eq!(converted, message);
§For more examples,
click here
Examples found in repository?
3064fn des_decrypt_with_padding_iso_ecb_into_vec()
3065{
3066 println!("des_decrypt_with_padding_iso_ecb_into_vec");
3067 use std::io::Write;
3068 use std::fmt::Write as _;
3069 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
3070
3071 // Normal case
3072 let key = 0x_1234567890ABCDEF_u64;
3073 println!("K =\t{:#016X}", key);
3074 let mut a_des = DES::new_with_key_u64(key);
3075
3076 let message = "In the beginning God created the heavens and the earth.";
3077 println!("M =\t{}", message);
3078 let mut cipher = Vec::<u8>::new();
3079 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3080 print!("C (16 rounds) =\t");
3081 for c in cipher.clone()
3082 { print!("{:02X} ", c); }
3083 println!();
3084 let mut txt = String::new();
3085 for c in cipher.clone()
3086 { write!(txt, "{:02X} ", c); }
3087 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 F4 BE 6B A5 C5 7D F6 5D ");
3088
3089 let mut recovered = Vec::<u8>::new();
3090 a_des.decrypt_into_vec(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3091 print!("Ba (16 rounds) =\t");
3092 for b in recovered.clone()
3093 { print!("{:02X} ", b); }
3094 println!();
3095 let mut txt = String::new();
3096 for c in recovered.clone()
3097 { write!(txt, "{:02X} ", c); }
3098 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 ");
3099
3100 let mut converted = String::new();
3101 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3102
3103 println!("Bb (16 rounds) =\t{}", converted);
3104 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3105 assert_eq!(converted, message);
3106 println!();
3107
3108 // Expanded case for 128 rounds
3109 let key = 0x_1234567890ABCDEF_u64;
3110 println!("K =\t{:#016X}", key);
3111 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
3112
3113 let message = "In the beginning God created the heavens and the earth.";
3114 println!("M =\t{}", message);
3115 let mut cipher = Vec::<u8>::new();
3116 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3117 print!("C (128 rounds) =\t");
3118 for c in cipher.clone()
3119 { print!("{:02X} ", c); }
3120 println!();
3121 let mut txt = String::new();
3122 for c in cipher.clone()
3123 { write!(txt, "{:02X} ", c); }
3124 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
3125
3126 let mut recovered = Vec::<u8>::new();
3127 a_des.decrypt_into_vec(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3128 print!("Ba (128 rounds) =\t");
3129 for b in recovered.clone()
3130 { print!("{:02X} ", b); }
3131 println!();
3132 let mut txt = String::new();
3133 for c in recovered.clone()
3134 { write!(txt, "{:02X} ", c); }
3135 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 ");
3136
3137 let mut converted = String::new();
3138 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3139
3140 println!("Bb (128 rounds) =\t{}", converted);
3141 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3142 assert_eq!(converted, message);
3143 println!();
3144
3145 // Expanded case for 0 rounds which means that key is meaningless
3146 let key1 = 0x_1234567890ABCDEF_u64;
3147 let key2 = 0_u64;
3148 println!("K =\t{:#016X}", key);
3149 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
3150 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
3151
3152 let message = "In the beginning God created the heavens and the earth.";
3153 println!("M =\t{}", message);
3154 let mut cipher1 = Vec::<u8>::new();
3155 let mut cipher2 = Vec::<u8>::new();
3156 c_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher1);
3157 d_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher2);
3158 print!("C (0 rounds) =\t");
3159 for c in cipher1.clone()
3160 { print!("{:02X} ", c); }
3161 println!();
3162 let mut txt = String::new();
3163 for c in cipher1.clone()
3164 { write!(txt, "{:02X} ", c); }
3165 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
3166 print!("D (0 rounds) =\t");
3167 for c in cipher2.clone()
3168 { print!("{:02X} ", c); }
3169 println!();
3170 let mut txt = String::new();
3171 for c in cipher2.clone()
3172 { write!(txt, "{:02X} ", c); }
3173 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
3174
3175 let mut recovered1 = Vec::<u8>::new();
3176 let mut recovered2 = Vec::<u8>::new();
3177 c_des.decrypt_into_vec(cipher1.as_ptr(), cipher1.len() as u64, &mut recovered1);
3178 d_des.decrypt_into_vec(cipher2.as_ptr(), cipher2.len() as u64, &mut recovered2);
3179 print!("B1a (0 rounds) =\t");
3180 for b in recovered1.clone()
3181 { print!("{:02X} ", b); }
3182 println!();
3183 let mut txt = String::new();
3184 for c in recovered1.clone()
3185 { write!(txt, "{:02X} ", c); }
3186 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 ");
3187 print!("B2a (0 rounds) =\t");
3188 for b in recovered2.clone()
3189 { print!("{:02X} ", b); }
3190 println!();
3191 let mut txt = String::new();
3192 for c in recovered2.clone()
3193 { write!(txt, "{:02X} ", c); }
3194 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 ");
3195
3196 let mut converted1 = String::new();
3197 let mut converted2 = String::new();
3198 unsafe { converted1.as_mut_vec() }.append(&mut recovered1);
3199 unsafe { converted2.as_mut_vec() }.append(&mut recovered2);
3200
3201 println!("B1b (0 rounds) =\t{}", converted1);
3202 assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
3203 assert_eq!(converted1, message);
3204 println!("B2b (0 rounds) =\t{}", converted2);
3205 assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
3206 assert_eq!(converted2, message);
3207 assert_eq!(converted1, converted1);
3208 println!();
3209
3210 // Normal case for the message of 0 bytes
3211 let key = 0x_1234567890ABCDEF_u64;
3212 println!("K =\t{:#016X}", key);
3213 let mut a_des = DES::new_with_key_u64(key);
3214
3215 let message = "";
3216 println!("M =\t{}", message);
3217 let mut cipher = Vec::<u8>::new();
3218 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3219 print!("C =\t");
3220 for c in cipher.clone()
3221 { print!("{:02X} ", c); }
3222 println!();
3223 let mut txt = String::new();
3224 for c in cipher.clone()
3225 { write!(txt, "{:02X} ", c); }
3226 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
3227
3228 let mut recovered = Vec::<u8>::new();
3229 a_des.decrypt_into_vec(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3230 print!("Ba =\t");
3231 for b in recovered.clone()
3232 { print!("{:02X} ", b); }
3233 println!();
3234 let mut txt = String::new();
3235 for c in recovered.clone()
3236 { write!(txt, "{:02X} ", c); }
3237 assert_eq!(txt, "");
3238
3239 let mut converted = String::new();
3240 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3241
3242 println!("Bb =\t{}", converted);
3243 assert_eq!(converted, "");
3244 assert_eq!(converted, message);
3245 println!();
3246
3247 // Normal case for the message shorter than 8 bytes
3248 let key = 0x_1234567890ABCDEF_u64;
3249 println!("K =\t{:#016X}", key);
3250 let mut a_des = DES::new_with_key_u64(key);
3251
3252 let message = "7 bytes";
3253 println!("M =\t{}", message);
3254 let mut cipher = Vec::<u8>::new();
3255 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3256 print!("C =\t");
3257 for c in cipher.clone()
3258 { print!("{:02X} ", c); }
3259 println!();
3260 let mut txt = String::new();
3261 for c in cipher.clone()
3262 { write!(txt, "{:02X} ", c); }
3263 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
3264
3265 let mut recovered = Vec::<u8>::new();
3266 a_des.decrypt_into_vec(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3267 print!("Ba =\t");
3268 for b in recovered.clone()
3269 { print!("{:02X} ", b); }
3270 println!();
3271 let mut txt = String::new();
3272 for c in recovered.clone()
3273 { write!(txt, "{:02X} ", c); }
3274 assert_eq!(txt, "37 20 62 79 74 65 73 ");
3275
3276 let mut converted = String::new();
3277 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3278
3279 println!("Bb =\t{}", converted);
3280 assert_eq!(converted, "7 bytes");
3281 assert_eq!(converted, message);
3282 println!();
3283
3284 // Normal case for the message of 8 bytes
3285 let key = 0x_1234567890ABCDEF_u64;
3286 println!("K =\t{:#016X}", key);
3287 let mut a_des = DES::new_with_key_u64(key);
3288
3289 let message = "I am OK.";
3290 println!("M =\t{}", message);
3291 let mut cipher = Vec::<u8>::new();
3292 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3293 print!("C =\t");
3294 for c in cipher.clone()
3295 { print!("{:02X} ", c); }
3296 println!();
3297 let mut txt = String::new();
3298 for c in cipher.clone()
3299 { write!(txt, "{:02X} ", c); }
3300 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
3301
3302 let mut recovered = Vec::<u8>::new();
3303 a_des.decrypt_into_vec(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3304 print!("Ba =\t");
3305 for b in recovered.clone()
3306 { print!("{:02X} ", b); }
3307 println!();
3308 let mut txt = String::new();
3309 for c in recovered.clone()
3310 { write!(txt, "{:02X} ", c); }
3311 assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E ");
3312
3313 let mut converted = String::new();
3314 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3315
3316 println!("Bb =\t{}", converted);
3317 assert_eq!(converted, "I am OK.");
3318 assert_eq!(converted, message);
3319 println!();
3320
3321 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
3322 let key = 0x_1234567890ABCDEF_u64;
3323 println!("K =\t{:#016X}", key);
3324 let mut a_des = DES::new_with_key_u64(key);
3325
3326 let message = "PARK Youngho";
3327 println!("M =\t{}", message);
3328 let mut cipher = Vec::<u8>::new();
3329 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3330 print!("C =\t");
3331 for c in cipher.clone()
3332 { print!("{:02X} ", c); }
3333 println!();
3334 let mut txt = String::new();
3335 for c in cipher.clone()
3336 { write!(txt, "{:02X} ", c); }
3337 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
3338
3339 let mut recovered = Vec::<u8>::new();
3340 a_des.decrypt_into_vec(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3341 print!("Ba =\t");
3342 for b in recovered.clone()
3343 { print!("{:02X} ", b); }
3344 println!();
3345 let mut txt = String::new();
3346 for c in recovered.clone()
3347 { write!(txt, "{:02X} ", c); }
3348 assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F ");
3349
3350 let mut converted = String::new();
3351 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3352
3353 println!("Bb =\t{}", converted);
3354 assert_eq!(converted, "PARK Youngho");
3355 assert_eq!(converted, message);
3356 println!();
3357
3358 // Normal case for the message of 16 bytes
3359 let key = 0x_1234567890ABCDEF_u64;
3360 println!("K =\t{:#016X}", key);
3361 let mut a_des = DES::new_with_key_u64(key);
3362
3363 let message = "고맙습니다.";
3364 println!("M =\t{}", message);
3365 let mut cipher = Vec::<u8>::new();
3366 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3367 print!("C =\t");
3368 for c in cipher.clone()
3369 { print!("{:02X} ", c); }
3370 println!();
3371 let mut txt = String::new();
3372 for c in cipher.clone()
3373 { write!(txt, "{:02X} ", c); }
3374 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
3375
3376 let mut recovered = Vec::<u8>::new();
3377 a_des.decrypt_into_vec(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3378 print!("Ba =\t");
3379 for b in recovered.clone()
3380 { print!("{:02X} ", b); }
3381 println!();
3382 let mut txt = String::new();
3383 for c in recovered.clone()
3384 { write!(txt, "{:02X} ", c); }
3385 assert_eq!(txt, "EA B3 A0 EB A7 99 EC 8A B5 EB 8B 88 EB 8B A4 2E ");
3386
3387 let mut converted = String::new();
3388 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3389
3390 println!("Bb =\t{}", converted);
3391 assert_eq!(converted, "고맙습니다.");
3392 assert_eq!(converted, message);
3393 println!("-------------------------------");
3394}
Sourcefn decrypt_into_string(
&mut self,
cipher: *const u8,
length_in_bytes: u64,
message: &mut String,
) -> u64
fn decrypt_into_string( &mut self, cipher: *const u8, length_in_bytes: u64, message: &mut String, ) -> u64
Decrypts the data with the padding defined according to ISO 7816-4 in ECB (Electronic CodeBook) mode, and stores the decrypted data in String object.
§Arguments
cipher
is a pointer to u8 which is*const u8
, and is the ciphertext to be decrypted.length_in_bytes
is ofu64
-type, and is the length of the ciphertextcipher
in bytes.message
is a String object, and is the plaintext to be stored.
§Output
- This method returns the size of plaintext in bytes.
- If this method returns
zero
, andlength_in_bytes
is greater than8
forT
=u64
or16
forT
=u128
, it means that this method failed in decryption. - If this method returns
zero
, andlength_in_bytes
is8
forT
=u64
or16
forT
=u128
, it means either that this method failed in decryption or that the original plaintext is empty String. Then, you will have to check whether or not it failed by the methodis_successful()
oris_failed()
.
§Features
- This method is useful to use in hybrid programming with C/C++.
- When
T
isu64
,length_in_bytes
can be only any multiple of8
. - When
T
isu128
,length_in_bytes
can be only any multiple of16
. - The padding bits are composed of the bytes that indicate the length of the padding bits in bytes according to ISO 7816-4 defined in RFC 5652.
- For more information about the padding bits according to ISO 7816-4, Read here.
- You don’t have to worry about whether or not the size of the memory area where the plaintext will be stored is enough.
- This method assumes that the original plaintext is a string in the format of UTF-8.
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, ECB_ISO };
let key = 0x_1234567890ABCDEF_u64;
println!("K =\t{:#016X}", key);
let mut a_des = DES::new_with_key_u64(key);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
print!("C (16 rounds) =\t");
for c in cipher.clone()
{ print!("{:02X} ", c); }
println!();
let mut txt = String::new();
for c in cipher.clone()
{ write!(txt, "{:02X} ", c); }
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 F4 BE 6B A5 C5 7D F6 5D ");
let mut recovered = String::new();
a_des.decrypt_into_string(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
println!("B (16 rounds) =\t{}", recovered);
assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
assert_eq!(recovered, message);
§For more examples,
click here
Examples found in repository?
3734fn des_decrypt_with_padding_iso_ecb_into_string()
3735{
3736 println!("des_decrypt_with_padding_iso_ecb_into_string");
3737 use std::io::Write;
3738 use std::fmt::Write as _;
3739 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
3740
3741 // Normal case
3742 let key = 0x_1234567890ABCDEF_u64;
3743 println!("K =\t{:#016X}", key);
3744 let mut a_des = DES::new_with_key_u64(key);
3745
3746 let message = "In the beginning God created the heavens and the earth.";
3747 println!("M =\t{}", message);
3748 let mut cipher = Vec::<u8>::new();
3749 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3750 print!("C (16 rounds) =\t");
3751 for c in cipher.clone()
3752 { print!("{:02X} ", c); }
3753 println!();
3754 let mut txt = String::new();
3755 for c in cipher.clone()
3756 { write!(txt, "{:02X} ", c); }
3757 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 F4 BE 6B A5 C5 7D F6 5D ");
3758
3759 let mut recovered = String::new();
3760 a_des.decrypt_into_string(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3761 println!("B (16 rounds) =\t{}", recovered);
3762 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
3763 assert_eq!(recovered, message);
3764 println!();
3765
3766 // Expanded case for 128 rounds
3767 let key = 0x_1234567890ABCDEF_u64;
3768 println!("K =\t{:#016X}", key);
3769 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
3770
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 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3775 print!("C (128 rounds) =\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, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
3783
3784 let mut recovered = String::new();
3785 a_des.decrypt_into_string(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3786 println!("B (128 rounds) =\t{}", recovered);
3787 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
3788 assert_eq!(recovered, message);
3789 println!();
3790
3791 // Expanded case for 0 rounds which means that key is meaningless
3792 let key1 = 0x_1234567890ABCDEF_u64;
3793 let key2 = 0_u64;
3794 println!("K =\t{:#016X}", key);
3795 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
3796 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
3797
3798 let message = "In the beginning God created the heavens and the earth.";
3799 println!("M =\t{}", message);
3800 let mut cipher1 = Vec::<u8>::new();
3801 let mut cipher2 = Vec::<u8>::new();
3802 c_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher1);
3803 d_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher2);
3804 print!("C (0 rounds) =\t");
3805 for c in cipher1.clone()
3806 { print!("{:02X} ", c); }
3807 println!();
3808 let mut txt = String::new();
3809 for c in cipher1.clone()
3810 { write!(txt, "{:02X} ", c); }
3811 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
3812 print!("D (0 rounds) =\t");
3813 for c in cipher2.clone()
3814 { print!("{:02X} ", c); }
3815 println!();
3816 let mut txt = String::new();
3817 for c in cipher2.clone()
3818 { write!(txt, "{:02X} ", c); }
3819 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
3820
3821 let mut recovered1 = String::new();
3822 let mut recovered2 = String::new();
3823 c_des.decrypt_into_string(cipher1.as_ptr(), cipher1.len() as u64, &mut recovered1);
3824 d_des.decrypt_into_string(cipher2.as_ptr(), cipher2.len() as u64, &mut recovered2);
3825 println!("B1 (0 rounds) =\t{}", recovered1);
3826 println!("B2 (0 rounds) =\t{}", recovered2);
3827 assert_eq!(recovered1, "In the beginning God created the heavens and the earth.");
3828 assert_eq!(recovered2, "In the beginning God created the heavens and the earth.");
3829 assert_eq!(recovered1, message);
3830 assert_eq!(recovered2, message);
3831 assert_eq!(recovered1, recovered2);
3832 println!();
3833
3834 // Normal case for the message of 0 bytes
3835 let key = 0x_1234567890ABCDEF_u64;
3836 println!("K =\t{:#016X}", key);
3837 let mut a_des = DES::new_with_key_u64(key);
3838
3839 let message = "";
3840 println!("M =\t{}", message);
3841 let mut cipher = Vec::<u8>::new();
3842 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3843 print!("C =\t");
3844 for c in cipher.clone()
3845 { print!("{:02X} ", c); }
3846 println!();
3847 let mut txt = String::new();
3848 for c in cipher.clone()
3849 { write!(txt, "{:02X} ", c); }
3850 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
3851
3852 let mut recovered = String::new();
3853 a_des.decrypt_into_string(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3854 println!("B =\t{}", recovered);
3855 assert_eq!(recovered, "");
3856 assert_eq!(recovered, message);
3857 println!();
3858
3859 // Normal case for the message shorter than 8 bytes
3860 let key = 0x_1234567890ABCDEF_u64;
3861 println!("K =\t{:#016X}", key);
3862 let mut a_des = DES::new_with_key_u64(key);
3863
3864 let message = "7 bytes";
3865 println!("M =\t{}", message);
3866 let mut cipher = Vec::<u8>::new();
3867 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3868 print!("C =\t");
3869 for c in cipher.clone()
3870 { print!("{:02X} ", c); }
3871 println!();
3872 let mut txt = String::new();
3873 for c in cipher.clone()
3874 { write!(txt, "{:02X} ", c); }
3875 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
3876
3877 let mut recovered = String::new();
3878 a_des.decrypt_into_string(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3879 println!("B =\t{}", recovered);
3880 assert_eq!(recovered, "7 bytes");
3881 assert_eq!(recovered, message);
3882 println!();
3883
3884 // Normal case for the message of 8 bytes
3885 let key = 0x_1234567890ABCDEF_u64;
3886 println!("K =\t{:#016X}", key);
3887 let mut a_des = DES::new_with_key_u64(key);
3888
3889 let message = "I am OK.";
3890 println!("M =\t{}", message);
3891 let mut cipher = Vec::<u8>::new();
3892 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3893 print!("C =\t");
3894 for c in cipher.clone()
3895 { print!("{:02X} ", c); }
3896 println!();
3897 let mut txt = String::new();
3898 for c in cipher.clone()
3899 { write!(txt, "{:02X} ", c); }
3900 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
3901
3902 let mut recovered = String::new();
3903 a_des.decrypt_into_string(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3904 println!("B =\t{}", recovered);
3905 assert_eq!(recovered, "I am OK.");
3906 assert_eq!(recovered, message);
3907 println!();
3908
3909 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
3910 let key = 0x_1234567890ABCDEF_u64;
3911 println!("K =\t{:#016X}", key);
3912 let mut a_des = DES::new_with_key_u64(key);
3913
3914 let message = "PARK Youngho";
3915 println!("M =\t{}", message);
3916 let mut cipher = Vec::<u8>::new();
3917 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3918 print!("C =\t");
3919 for c in cipher.clone()
3920 { print!("{:02X} ", c); }
3921 println!();
3922 let mut txt = String::new();
3923 for c in cipher.clone()
3924 { write!(txt, "{:02X} ", c); }
3925 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
3926
3927 let mut recovered = String::new();
3928 a_des.decrypt_into_string(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3929 println!("B =\t{}", recovered);
3930 assert_eq!(recovered, "PARK Youngho");
3931 assert_eq!(recovered, message);
3932 println!();
3933
3934 // Normal case for the message of 16 bytes
3935 let key = 0x_1234567890ABCDEF_u64;
3936 println!("K =\t{:#016X}", key);
3937 let mut a_des = DES::new_with_key_u64(key);
3938
3939 let message = "고맙습니다.";
3940 println!("M =\t{}", message);
3941 let mut cipher = Vec::<u8>::new();
3942 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3943 print!("C =\t");
3944 for c in cipher.clone()
3945 { print!("{:02X} ", c); }
3946 println!();
3947 let mut txt = String::new();
3948 for c in cipher.clone()
3949 { write!(txt, "{:02X} ", c); }
3950 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
3951
3952 let mut recovered = String::new();
3953 a_des.decrypt_into_string(cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3954 println!("B =\t{}", recovered);
3955 assert_eq!(recovered, "고맙습니다.");
3956 assert_eq!(recovered, message);
3957 println!("-------------------------------");
3958}
Sourcefn decrypt_vec<U>(&mut self, cipher: &Vec<U>, message: *mut u8) -> u64
fn decrypt_vec<U>(&mut self, cipher: &Vec<U>, message: *mut u8) -> u64
Decrypts the data stored in a Vec<U>
object with the padding defined
according to ISO 7816-4 in ECB (Electronic CodeBook) mode.
§Arguments
cipher
is aVec<U>
object, and is the ciphertext to be decrypted.message
is a pointer to u8 which is*mut u8
, and is the plaintext to be stored.- The size of the memory area which starts at
message
and the plaintext will be stored at is assumed to be enough. - The size of the area for plaintext should be prepared to be:
length_in_bytes
- 1. So, it is responsible for you to prepare themessage
area big enough!
§Output
- This method returns the size of plaintext in bytes.
- If this method returns
zero
, andcipher.len() * U::size_in_bytes()
is greater than8
forT
=u64
or16
forT
=u128
, it means that this method failed in decryption. - If this method returns
zero
, andcipher.len() * U::size_in_bytes()
is8
forT
=u64
or16
forT
=u128
, it means either that this method failed in decryption or that the original plaintext is empty data. Then, you will have to check whether or not it failed by the methodis_successful()
oris_failed()
.
§Features
- You are not encouraged to use this method in pure Rust programming. Instead, use other safer methods such as decrypt_vec_into_*().
- When
T
isu64
,cipher.len() * U::size_in_bytes()
can be only any multiple of8
. - When
T
isu128
,cipher.len() * U::size_in_bytes()
can be only any multiple of16
. - This method is useful to use in hybrid programming with C/C++.
- The padding bits are composed of the bytes that indicate the length of the padding bits in bytes according to ISO 7816-4 defined in RFC 5652.
- For more information about the padding bits according to ISO 7816-4, Read here.
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, ECB_ISO };
let key = 0x_1234567890ABCDEF_u64;
println!("K =\t{:#016X}", key);
let mut a_des = DES::new_with_key_u64(key);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
print!("C (16 rounds) =\t");
for c in cipher.clone()
{ print!("{:02X} ", c); }
println!();
let mut txt = String::new();
for c in cipher.clone()
{ write!(txt, "{:02X} ", c); }
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 F4 BE 6B A5 C5 7D F6 5D ");
let mut recovered = vec![0; 55];
a_des.decrypt_vec(&cipher, recovered.as_mut_ptr());
print!("Ba (16 rounds) =\t");
for b in recovered.clone()
{ print!("{:02X} ", b); }
println!();
let mut txt = String::new();
for c in recovered.clone()
{ write!(txt, "{:02X} ", c); }
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 ");
let mut converted = String::new();
unsafe { converted.as_mut_vec() }.append(&mut recovered);
println!("Bb (16 rounds) =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");
assert_eq!(converted, message);
§For more examples,
click here
Examples found in repository?
3960fn des_decrypt_vec_with_padding_iso_ecb()
3961{
3962 println!("des_decrypt_vec_with_padding_iso_ecb");
3963 use std::io::Write;
3964 use std::fmt::Write as _;
3965 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
3966
3967 // Normal case
3968 let key = 0x_1234567890ABCDEF_u64;
3969 println!("K =\t{:#016X}", key);
3970 let mut a_des = DES::new_with_key_u64(key);
3971
3972 let message = "In the beginning God created the heavens and the earth.";
3973 println!("M =\t{}", message);
3974 let mut cipher = Vec::<u8>::new();
3975 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
3976 print!("C (16 rounds) =\t");
3977 for c in cipher.clone()
3978 { print!("{:02X} ", c); }
3979 println!();
3980 let mut txt = String::new();
3981 for c in cipher.clone()
3982 { write!(txt, "{:02X} ", c); }
3983 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 F4 BE 6B A5 C5 7D F6 5D ");
3984
3985 let mut recovered = vec![0; 55];
3986 a_des.decrypt_vec(&cipher, recovered.as_mut_ptr());
3987 print!("Ba (16 rounds) =\t");
3988 for b in recovered.clone()
3989 { print!("{:02X} ", b); }
3990 println!();
3991 let mut txt = String::new();
3992 for c in recovered.clone()
3993 { write!(txt, "{:02X} ", c); }
3994 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 ");
3995
3996 let mut converted = String::new();
3997 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3998
3999 println!("Bb (16 rounds) =\t{}", converted);
4000 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4001 assert_eq!(converted, message);
4002 println!();
4003
4004 // Expanded case for 128 rounds
4005 let key = 0x_1234567890ABCDEF_u64;
4006 println!("K =\t{:#016X}", key);
4007 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
4008
4009 let message = "In the beginning God created the heavens and the earth.";
4010 println!("M =\t{}", message);
4011 let mut cipher = Vec::<u8>::new();
4012 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4013 print!("C (128 rounds) =\t");
4014 for c in cipher.clone()
4015 { print!("{:02X} ", c); }
4016 println!();
4017 let mut txt = String::new();
4018 for c in cipher.clone()
4019 { write!(txt, "{:02X} ", c); }
4020 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
4021
4022 let mut recovered = vec![0; 55];
4023 a_des.decrypt_vec(&cipher, recovered.as_mut_ptr());
4024 print!("Ba (128 rounds) =\t");
4025 for b in recovered.clone()
4026 { print!("{:02X} ", b); }
4027 println!();
4028 let mut txt = String::new();
4029 for c in recovered.clone()
4030 { write!(txt, "{:02X} ", c); }
4031 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 ");
4032
4033 let mut converted = String::new();
4034 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4035
4036 println!("Bb (128 rounds) =\t{}", converted);
4037 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4038 assert_eq!(converted, message);
4039 println!();
4040
4041 // Expanded case for 0 rounds which means that key is meaningless
4042 let key1 = 0x_1234567890ABCDEF_u64;
4043 let key2 = 0_u64;
4044 println!("K =\t{:#016X}", key);
4045 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
4046 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
4047
4048 let message = "In the beginning God created the heavens and the earth.";
4049 println!("M =\t{}", message);
4050 let mut cipher1 = Vec::<u8>::new();
4051 let mut cipher2 = Vec::<u8>::new();
4052 c_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher1);
4053 d_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher2);
4054 print!("C (0 rounds) =\t");
4055 for c in cipher1.clone()
4056 { print!("{:02X} ", c); }
4057 println!();
4058 let mut txt = String::new();
4059 for c in cipher1.clone()
4060 { write!(txt, "{:02X} ", c); }
4061 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
4062 print!("D (0 rounds) =\t");
4063 for c in cipher2.clone()
4064 { print!("{:02X} ", c); }
4065 println!();
4066 let mut txt = String::new();
4067 for c in cipher2.clone()
4068 { write!(txt, "{:02X} ", c); }
4069 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
4070
4071 let mut recovered1 = vec![0; 55];
4072 let mut recovered2 = vec![0; 55];
4073 c_des.decrypt_vec(&cipher1, recovered1.as_mut_ptr());
4074 d_des.decrypt_vec(&cipher2, recovered2.as_mut_ptr());
4075 print!("B1a (0 rounds) =\t");
4076 for b in recovered1.clone()
4077 { print!("{:02X} ", b); }
4078 println!();
4079 let mut txt = String::new();
4080 for c in recovered1.clone()
4081 { write!(txt, "{:02X} ", c); }
4082 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 ");
4083 print!("B2a (0 rounds) =\t");
4084 for b in recovered2.clone()
4085 { print!("{:02X} ", b); }
4086 println!();
4087 let mut txt = String::new();
4088 for c in recovered2.clone()
4089 { write!(txt, "{:02X} ", c); }
4090 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 ");
4091
4092 let mut converted1 = String::new();
4093 let mut converted2 = String::new();
4094 unsafe { converted1.as_mut_vec() }.append(&mut recovered1);
4095 unsafe { converted2.as_mut_vec() }.append(&mut recovered2);
4096
4097 println!("B1b (0 rounds) =\t{}", converted1);
4098 assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
4099 assert_eq!(converted1, message);
4100 println!("B2b (0 rounds) =\t{}", converted2);
4101 assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
4102 assert_eq!(converted2, message);
4103 assert_eq!(converted1, converted1);
4104 println!();
4105
4106 // Normal case for the message of 0 bytes
4107 let key = 0x_1234567890ABCDEF_u64;
4108 println!("K =\t{:#016X}", key);
4109 let mut a_des = DES::new_with_key_u64(key);
4110
4111 let message = "";
4112 println!("M =\t{}", message);
4113 let mut cipher = Vec::<u8>::new();
4114 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4115 print!("C =\t");
4116 for c in cipher.clone()
4117 { print!("{:02X} ", c); }
4118 println!();
4119 let mut txt = String::new();
4120 for c in cipher.clone()
4121 { write!(txt, "{:02X} ", c); }
4122 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
4123
4124 let mut recovered = vec![0; 8];
4125 let len = a_des.decrypt_vec(&cipher, recovered.as_mut_ptr());
4126 print!("Ba =\t");
4127 for b in recovered.clone()
4128 { print!("{:02X} ", b); }
4129 println!();
4130 let mut txt = String::new();
4131 for c in recovered.clone()
4132 { write!(txt, "{:02X} ", c); }
4133 assert_eq!(txt, "00 00 00 00 00 00 00 00 ");
4134
4135 let mut converted = String::new();
4136 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4137 converted.truncate(len as usize);
4138
4139 println!("Bb =\t{}", converted);
4140 assert_eq!(converted, "");
4141 assert_eq!(converted, message);
4142 println!();
4143
4144 // Normal case for the message shorter than 8 bytes
4145 let key = 0x_1234567890ABCDEF_u64;
4146 println!("K =\t{:#016X}", key);
4147 let mut a_des = DES::new_with_key_u64(key);
4148
4149 let message = "7 bytes";
4150 println!("M =\t{}", message);
4151 let mut cipher = Vec::<u8>::new();
4152 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4153 print!("C =\t");
4154 for c in cipher.clone()
4155 { print!("{:02X} ", c); }
4156 println!();
4157 let mut txt = String::new();
4158 for c in cipher.clone()
4159 { write!(txt, "{:02X} ", c); }
4160 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
4161
4162 let mut recovered = vec![0; 8];
4163 let len = a_des.decrypt_vec(&cipher, recovered.as_mut_ptr());
4164 print!("Ba =\t");
4165 for b in recovered.clone()
4166 { print!("{:02X} ", b); }
4167 println!();
4168 let mut txt = String::new();
4169 for c in recovered.clone()
4170 { write!(txt, "{:02X} ", c); }
4171 assert_eq!(txt, "37 20 62 79 74 65 73 00 ");
4172
4173 let mut converted = String::new();
4174 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4175 converted.truncate(len as usize);
4176
4177 println!("Bb =\t{}", converted);
4178 assert_eq!(converted, "7 bytes");
4179 assert_eq!(converted, message);
4180 println!();
4181
4182 // Normal case for the message of 8 bytes
4183 let key = 0x_1234567890ABCDEF_u64;
4184 println!("K =\t{:#016X}", key);
4185 let mut a_des = DES::new_with_key_u64(key);
4186
4187 let message = "I am OK.";
4188 println!("M =\t{}", message);
4189 let mut cipher = Vec::<u8>::new();
4190 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4191 print!("C =\t");
4192 for c in cipher.clone()
4193 { print!("{:02X} ", c); }
4194 println!();
4195 let mut txt = String::new();
4196 for c in cipher.clone()
4197 { write!(txt, "{:02X} ", c); }
4198 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
4199
4200 let mut recovered = vec![0; 16];
4201 let len = a_des.decrypt_vec(&cipher, recovered.as_mut_ptr());
4202 print!("Ba =\t");
4203 for b in recovered.clone()
4204 { print!("{:02X} ", b); }
4205 println!();
4206 let mut txt = String::new();
4207 for c in recovered.clone()
4208 { write!(txt, "{:02X} ", c); }
4209 assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E 00 00 00 00 00 00 00 00 ");
4210
4211 let mut converted = String::new();
4212 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4213 converted.truncate(len as usize);
4214
4215 println!("Bb =\t{}", converted);
4216 assert_eq!(converted, "I am OK.");
4217 assert_eq!(converted, message);
4218 println!();
4219
4220 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
4221 let key = 0x_1234567890ABCDEF_u64;
4222 println!("K =\t{:#016X}", key);
4223 let mut a_des = DES::new_with_key_u64(key);
4224
4225 let message = "PARK Youngho";
4226 println!("M =\t{}", message);
4227 let mut cipher = Vec::<u8>::new();
4228 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4229 print!("C =\t");
4230 for c in cipher.clone()
4231 { print!("{:02X} ", c); }
4232 println!();
4233 let mut txt = String::new();
4234 for c in cipher.clone()
4235 { write!(txt, "{:02X} ", c); }
4236 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
4237
4238 let mut recovered = vec![0; 16];
4239 let len = a_des.decrypt_vec(&cipher, recovered.as_mut_ptr());
4240 print!("Ba =\t");
4241 for b in recovered.clone()
4242 { print!("{:02X} ", b); }
4243 println!();
4244 let mut txt = String::new();
4245 for c in recovered.clone()
4246 { write!(txt, "{:02X} ", c); }
4247 assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F 00 00 00 00 ");
4248
4249 let mut converted = String::new();
4250 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4251 converted.truncate(len as usize);
4252
4253 println!("Bb =\t{}", converted);
4254 assert_eq!(converted, "PARK Youngho");
4255 assert_eq!(converted, message);
4256 println!();
4257
4258 // Normal case for the message of 16 bytes
4259 let key = 0x_1234567890ABCDEF_u64;
4260 println!("K =\t{:#016X}", key);
4261 let mut a_des = DES::new_with_key_u64(key);
4262
4263 let message = "고맙습니다.";
4264 println!("M =\t{}", message);
4265 let mut cipher = Vec::<u8>::new();
4266 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4267 print!("C =\t");
4268 for c in cipher.clone()
4269 { print!("{:02X} ", c); }
4270 println!();
4271 let mut txt = String::new();
4272 for c in cipher.clone()
4273 { write!(txt, "{:02X} ", c); }
4274 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
4275
4276 let mut recovered = vec![0; 24];
4277 let len = a_des.decrypt_vec(&cipher, recovered.as_mut_ptr());
4278 print!("Ba =\t");
4279 for b in recovered.clone()
4280 { print!("{:02X} ", b); }
4281 println!();
4282 let mut txt = String::new();
4283 for c in recovered.clone()
4284 { write!(txt, "{:02X} ", c); }
4285 assert_eq!(txt, "EA B3 A0 EB A7 99 EC 8A B5 EB 8B 88 EB 8B A4 2E 00 00 00 00 00 00 00 00 ");
4286
4287 let mut converted = String::new();
4288 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4289 converted.truncate(len as usize);
4290
4291 println!("Bb =\t{}", converted);
4292 assert_eq!(converted, "고맙습니다.");
4293 assert_eq!(converted, message);
4294 println!("-------------------------------");
4295}
Sourcefn decrypt_vec_into_vec<U, V>(
&mut self,
cipher: &Vec<U>,
message: &mut Vec<V>,
) -> u64
fn decrypt_vec_into_vec<U, V>( &mut self, cipher: &Vec<U>, message: &mut Vec<V>, ) -> u64
Decrypts the data stored in a Vec<U>
object with the padding according
to ISO 7816-4 in ECB (Electronic CodeBook) mode, and stores the decrypted
data in Vec<V>
.
§Arguments
cipher
is aVec<U>
object, and is the ciphertext to be decrypted.message
is aVec<V>
object, and is the plaintext to be stored.
§Output
- This method returns the size of plaintext in bytes.
- If this method returns
zero
, andcipher.len() * U::size_in_bytes()
is greater than8
forT
=u64
or16
forT
=u128
, it means that this method failed in decryption. - If this method returns
zero
, andcipher.len() * U::size_in_bytes()
is8
forT
=u64
or16
forT
=u128
, it means either that this method failed in decryption or that the original plaintext is empty data. Then, you will have to check whether or not it failed by the methodis_successful()
oris_failed()
.
§Features
- When
T
isu64
,cipher.len() * U::size_in_bytes()
can be only any multiple of8
. - When
T
isu128
,cipher.len() * U::size_in_bytes()
can be only any multiple of16
. - The padding bits are composed of the bytes that indicate the length of the padding bits in bytes according to ISO 7816-4 defined in RFC 5652.
- For more information about the padding bits according to ISO 7816-4, Read here.
- You don’t have to worry about whether or not the size of the memory area where the plaintext will be stored is enough.
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, ECB_ISO };
let key = 0x_1234567890ABCDEF_u64;
println!("K =\t{:#016X}", key);
let mut a_des = DES::new_with_key_u64(key);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
print!("C (16 rounds) =\t");
for c in cipher.clone()
{ print!("{:02X} ", c); }
println!();
let mut txt = String::new();
for c in cipher.clone()
{ write!(txt, "{:02X} ", c); }
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 F4 BE 6B A5 C5 7D F6 5D ");
let mut recovered = Vec::<u8>::new();
a_des.decrypt_vec_into_vec(&cipher, &mut recovered);
print!("Ba (16 rounds) =\t");
for b in recovered.clone()
{ print!("{:02X} ", b); }
println!();
let mut txt = String::new();
for c in recovered.clone()
{ write!(txt, "{:02X} ", c); }
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 ");
let mut converted = String::new();
unsafe { converted.as_mut_vec() }.append(&mut recovered);
println!("Bb (16 rounds) =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");
assert_eq!(converted, message);
§For more examples,
click here
Examples found in repository?
4297fn des_decrypt_vec_with_padding_iso_ecb_into_vec()
4298{
4299 println!("des_decrypt_vec_with_padding_iso_ecb_into_vec");
4300 use std::io::Write;
4301 use std::fmt::Write as _;
4302 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
4303
4304 // Normal case
4305 let key = 0x_1234567890ABCDEF_u64;
4306 println!("K =\t{:#016X}", key);
4307 let mut a_des = DES::new_with_key_u64(key);
4308
4309 let message = "In the beginning God created the heavens and the earth.";
4310 println!("M =\t{}", message);
4311 let mut cipher = Vec::<u8>::new();
4312 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4313 print!("C (16 rounds) =\t");
4314 for c in cipher.clone()
4315 { print!("{:02X} ", c); }
4316 println!();
4317 let mut txt = String::new();
4318 for c in cipher.clone()
4319 { write!(txt, "{:02X} ", c); }
4320 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 F4 BE 6B A5 C5 7D F6 5D ");
4321
4322 let mut recovered = Vec::<u8>::new();
4323 a_des.decrypt_vec_into_vec(&cipher, &mut recovered);
4324 print!("Ba (16 rounds) =\t");
4325 for b in recovered.clone()
4326 { print!("{:02X} ", b); }
4327 println!();
4328 let mut txt = String::new();
4329 for c in recovered.clone()
4330 { write!(txt, "{:02X} ", c); }
4331 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 ");
4332
4333 let mut converted = String::new();
4334 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4335
4336 println!("Bb (16 rounds) =\t{}", converted);
4337 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4338 assert_eq!(converted, message);
4339 println!();
4340
4341 // Expanded case for 128 rounds
4342 let key = 0x_1234567890ABCDEF_u64;
4343 println!("K =\t{:#016X}", key);
4344 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::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 = Vec::<u8>::new();
4349 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4350 print!("C (128 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, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
4358
4359 let mut recovered = Vec::<u8>::new();
4360 a_des.decrypt_vec_into_vec(&cipher, &mut recovered);
4361 print!("Ba (128 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 (128 rounds) =\t{}", converted);
4374 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4375 assert_eq!(converted, message);
4376 println!();
4377
4378 // Expanded case for 0 rounds which means that key is meaningless
4379 let key1 = 0x_1234567890ABCDEF_u64;
4380 let key2 = 0_u64;
4381 println!("K =\t{:#016X}", key);
4382 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
4383 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
4384
4385 let message = "In the beginning God created the heavens and the earth.";
4386 println!("M =\t{}", message);
4387 let mut cipher1 = Vec::<u8>::new();
4388 let mut cipher2 = Vec::<u8>::new();
4389 c_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher1);
4390 d_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher2);
4391 print!("C (0 rounds) =\t");
4392 for c in cipher1.clone()
4393 { print!("{:02X} ", c); }
4394 println!();
4395 let mut txt = String::new();
4396 for c in cipher1.clone()
4397 { write!(txt, "{:02X} ", c); }
4398 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
4399 print!("D (0 rounds) =\t");
4400 for c in cipher2.clone()
4401 { print!("{:02X} ", c); }
4402 println!();
4403 let mut txt = String::new();
4404 for c in cipher2.clone()
4405 { write!(txt, "{:02X} ", c); }
4406 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
4407
4408 let mut recovered1 = Vec::<u8>::new();
4409 let mut recovered2 = Vec::<u8>::new();
4410 c_des.decrypt_vec_into_vec(&cipher1, &mut recovered1);
4411 d_des.decrypt_vec_into_vec(&cipher2, &mut recovered2);
4412 print!("B1a (0 rounds) =\t");
4413 for b in recovered1.clone()
4414 { print!("{:02X} ", b); }
4415 println!();
4416 let mut txt = String::new();
4417 for c in recovered1.clone()
4418 { write!(txt, "{:02X} ", c); }
4419 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 ");
4420 print!("B2a (0 rounds) =\t");
4421 for b in recovered2.clone()
4422 { print!("{:02X} ", b); }
4423 println!();
4424 let mut txt = String::new();
4425 for c in recovered2.clone()
4426 { write!(txt, "{:02X} ", c); }
4427 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 ");
4428
4429 let mut converted1 = String::new();
4430 let mut converted2 = String::new();
4431 unsafe { converted1.as_mut_vec() }.append(&mut recovered1);
4432 unsafe { converted2.as_mut_vec() }.append(&mut recovered2);
4433
4434 println!("B1b (0 rounds) =\t{}", converted1);
4435 assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
4436 assert_eq!(converted1, message);
4437 println!("B2b (0 rounds) =\t{}", converted2);
4438 assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
4439 assert_eq!(converted2, message);
4440 assert_eq!(converted1, converted1);
4441 println!();
4442
4443 // Normal case for the message of 0 bytes
4444 let key = 0x_1234567890ABCDEF_u64;
4445 println!("K =\t{:#016X}", key);
4446 let mut a_des = DES::new_with_key_u64(key);
4447
4448 let message = "";
4449 println!("M =\t{}", message);
4450 let mut cipher = Vec::<u8>::new();
4451 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4452 print!("C =\t");
4453 for c in cipher.clone()
4454 { print!("{:02X} ", c); }
4455 println!();
4456 let mut txt = String::new();
4457 for c in cipher.clone()
4458 { write!(txt, "{:02X} ", c); }
4459 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
4460
4461 let mut recovered = Vec::<u8>::new();
4462 a_des.decrypt_vec_into_vec(&cipher, &mut recovered);
4463 print!("Ba =\t");
4464 for b in recovered.clone()
4465 { print!("{:02X} ", b); }
4466 println!();
4467 let mut txt = String::new();
4468 for c in recovered.clone()
4469 { write!(txt, "{:02X} ", c); }
4470 assert_eq!(txt, "");
4471
4472 let mut converted = String::new();
4473 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4474
4475 println!("Bb =\t{}", converted);
4476 assert_eq!(converted, "");
4477 assert_eq!(converted, message);
4478 println!();
4479
4480 // Normal case for the message shorter than 8 bytes
4481 let key = 0x_1234567890ABCDEF_u64;
4482 println!("K =\t{:#016X}", key);
4483 let mut a_des = DES::new_with_key_u64(key);
4484
4485 let message = "7 bytes";
4486 println!("M =\t{}", message);
4487 let mut cipher = Vec::<u8>::new();
4488 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4489 print!("C =\t");
4490 for c in cipher.clone()
4491 { print!("{:02X} ", c); }
4492 println!();
4493 let mut txt = String::new();
4494 for c in cipher.clone()
4495 { write!(txt, "{:02X} ", c); }
4496 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
4497
4498 let mut recovered = Vec::<u8>::new();
4499 a_des.decrypt_vec_into_vec(&cipher, &mut recovered);
4500 print!("Ba =\t");
4501 for b in recovered.clone()
4502 { print!("{:02X} ", b); }
4503 println!();
4504 let mut txt = String::new();
4505 for c in recovered.clone()
4506 { write!(txt, "{:02X} ", c); }
4507 assert_eq!(txt, "37 20 62 79 74 65 73 ");
4508
4509 let mut converted = String::new();
4510 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4511
4512 println!("Bb =\t{}", converted);
4513 assert_eq!(converted, "7 bytes");
4514 assert_eq!(converted, message);
4515 println!();
4516
4517 // Normal case for the message of 8 bytes
4518 let key = 0x_1234567890ABCDEF_u64;
4519 println!("K =\t{:#016X}", key);
4520 let mut a_des = DES::new_with_key_u64(key);
4521
4522 let message = "I am OK.";
4523 println!("M =\t{}", message);
4524 let mut cipher = Vec::<u8>::new();
4525 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4526 print!("C =\t");
4527 for c in cipher.clone()
4528 { print!("{:02X} ", c); }
4529 println!();
4530 let mut txt = String::new();
4531 for c in cipher.clone()
4532 { write!(txt, "{:02X} ", c); }
4533 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
4534
4535 let mut recovered = Vec::<u8>::new();
4536 a_des.decrypt_vec_into_vec(&cipher, &mut recovered);
4537 print!("Ba =\t");
4538 for b in recovered.clone()
4539 { print!("{:02X} ", b); }
4540 println!();
4541 let mut txt = String::new();
4542 for c in recovered.clone()
4543 { write!(txt, "{:02X} ", c); }
4544 assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E ");
4545
4546 let mut converted = String::new();
4547 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4548
4549 println!("Bb =\t{}", converted);
4550 assert_eq!(converted, "I am OK.");
4551 assert_eq!(converted, message);
4552 println!();
4553
4554 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
4555 let key = 0x_1234567890ABCDEF_u64;
4556 println!("K =\t{:#016X}", key);
4557 let mut a_des = DES::new_with_key_u64(key);
4558
4559 let message = "PARK Youngho";
4560 println!("M =\t{}", message);
4561 let mut cipher = Vec::<u8>::new();
4562 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4563 print!("C =\t");
4564 for c in cipher.clone()
4565 { print!("{:02X} ", c); }
4566 println!();
4567 let mut txt = String::new();
4568 for c in cipher.clone()
4569 { write!(txt, "{:02X} ", c); }
4570 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
4571
4572 let mut recovered = Vec::<u8>::new();
4573 a_des.decrypt_vec_into_vec(&cipher, &mut recovered);
4574 print!("Ba =\t");
4575 for b in recovered.clone()
4576 { print!("{:02X} ", b); }
4577 println!();
4578 let mut txt = String::new();
4579 for c in recovered.clone()
4580 { write!(txt, "{:02X} ", c); }
4581 assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F ");
4582
4583 let mut converted = String::new();
4584 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4585
4586 println!("Bb =\t{}", converted);
4587 assert_eq!(converted, "PARK Youngho");
4588 assert_eq!(converted, message);
4589 println!();
4590
4591 // Normal case for the message of 16 bytes
4592 let key = 0x_1234567890ABCDEF_u64;
4593 println!("K =\t{:#016X}", key);
4594 let mut a_des = DES::new_with_key_u64(key);
4595
4596 let message = "고맙습니다.";
4597 println!("M =\t{}", message);
4598 let mut cipher = Vec::<u8>::new();
4599 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4600 print!("C =\t");
4601 for c in cipher.clone()
4602 { print!("{:02X} ", c); }
4603 println!();
4604 let mut txt = String::new();
4605 for c in cipher.clone()
4606 { write!(txt, "{:02X} ", c); }
4607 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
4608
4609 let mut recovered = Vec::<u8>::new();
4610 a_des.decrypt_vec_into_vec(&cipher, &mut recovered);
4611 print!("Ba =\t");
4612 for b in recovered.clone()
4613 { print!("{:02X} ", b); }
4614 println!();
4615 let mut txt = String::new();
4616 for c in recovered.clone()
4617 { write!(txt, "{:02X} ", c); }
4618 assert_eq!(txt, "EA B3 A0 EB A7 99 EC 8A B5 EB 8B 88 EB 8B A4 2E ");
4619
4620 let mut converted = String::new();
4621 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4622
4623 println!("Bb =\t{}", converted);
4624 assert_eq!(converted, "고맙습니다.");
4625 assert_eq!(converted, message);
4626 println!("-------------------------------");
4627}
Sourcefn decrypt_vec_into_array<U, V, const N: usize>(
&mut self,
cipher: &Vec<U>,
message: &mut [V; N],
) -> u64
fn decrypt_vec_into_array<U, V, const N: usize>( &mut self, cipher: &Vec<U>, message: &mut [V; N], ) -> u64
Decrypts the data stored in a Vec<U>
object with the padding according
to ISO 7816-4 in ECB (Electronic CodeBook) mode, and stores the decrypted
data in array [V; N]
.
§Arguments
cipher
is aVec<U>
object, and is the ciphertext to be decrypted.message
is an array[V; N]
object, and is the plaintext to be stored.
§Output
- This method returns the size of plaintext in bytes.
- If this method returns
zero
, andcipher.len() * U::size_in_bytes()
is greater than8
forT
=u64
or16
forT
=u128
, it means that this method failed in decryption. - If this method returns
zero
, andcipher.len() * U::size_in_bytes()
is8
forT
=u64
or16
forT
=u128
, it means either that this method failed in decryption or that the original plaintext is empty data. Then, you will have to check whether or not it failed by the methodis_successful()
oris_failed()
.
§Features
- When
T
isu64
,cipher.len() * U::size_in_bytes()
can be only any multiple of8
. - When
T
isu128
,cipher.len() * U::size_in_bytes()
can be only any multiple of16
. - If
V::size_in_bytes() * N
is greater than or equal toU::size_in_bytes() * cipher.len() - 1
, this method performs decryption, fills the arraymessage
with the derypted plaintext, and then fills the rest of the elements of the arraymessage
with zeros if any, and returns the size of the plaintext. - If
V::size_in_bytes() * N
is less thanU::size_in_bytes() * cipher.len() - 1
, this method does not perform decryption and returnszero
. - The padding bits are composed of the bytes that indicate the length of the padding bits in bytes according to ISO 7816-4 defined in RFC 5652.
- For more information about the padding bits according to ISO 7816-4, Read here.
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, ECB_ISO };
let key = 0x_1234567890ABCDEF_u64;
println!("K =\t{:#016X}", key);
let mut a_des = DES::new_with_key_u64(key);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
print!("C (16 rounds) =\t");
for c in cipher.clone()
{ print!("{:02X} ", c); }
println!();
let mut txt = String::new();
for c in cipher.clone()
{ write!(txt, "{:02X} ", c); }
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 F4 BE 6B A5 C5 7D F6 5D ");
let mut recovered = [0u8; 56];
let len = a_des.decrypt_vec_into_array(&cipher, &mut recovered);
print!("Ba (16 rounds) =\t");
for b in recovered.clone()
{ print!("{:02X} ", b); }
println!();
let mut txt = String::new();
for c in recovered.clone()
{ write!(txt, "{:02X} ", c); }
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 ");
let mut converted = String::new();
unsafe { converted.as_mut_vec() }.write(&recovered);
unsafe { converted.as_mut_vec() }.truncate(len as usize);
println!("Bb (16 rounds) =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");
assert_eq!(converted, message);
§For more examples,
click here
Examples found in repository?
4629fn des_decrypt_vec_with_padding_iso_ecb_into_array()
4630{
4631 println!("des_decrypt_vec_with_padding_iso_ecb_into_array");
4632 use std::io::Write;
4633 use std::fmt::Write as _;
4634 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
4635
4636 // Normal case
4637 let key = 0x_1234567890ABCDEF_u64;
4638 println!("K =\t{:#016X}", key);
4639 let mut a_des = DES::new_with_key_u64(key);
4640
4641 let message = "In the beginning God created the heavens and the earth.";
4642 println!("M =\t{}", message);
4643 let mut cipher = Vec::<u8>::new();
4644 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4645 print!("C (16 rounds) =\t");
4646 for c in cipher.clone()
4647 { print!("{:02X} ", c); }
4648 println!();
4649 let mut txt = String::new();
4650 for c in cipher.clone()
4651 { write!(txt, "{:02X} ", c); }
4652 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 F4 BE 6B A5 C5 7D F6 5D ");
4653
4654 let mut recovered = [0u8; 56];
4655 let len = a_des.decrypt_vec_into_array(&cipher, &mut recovered);
4656 print!("Ba (16 rounds) =\t");
4657 for b in recovered.clone()
4658 { print!("{:02X} ", b); }
4659 println!();
4660 let mut txt = String::new();
4661 for c in recovered.clone()
4662 { write!(txt, "{:02X} ", c); }
4663 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 ");
4664
4665 let mut converted = String::new();
4666 unsafe { converted.as_mut_vec() }.write(&recovered);
4667 unsafe { converted.as_mut_vec() }.truncate(len as usize);
4668 println!("Bb (16 rounds) =\t{}", converted);
4669 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4670 assert_eq!(converted, message);
4671 println!();
4672
4673 // Expanded case for 128 rounds
4674 let key = 0x_1234567890ABCDEF_u64;
4675 println!("K =\t{:#016X}", key);
4676 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
4677
4678 let message = "In the beginning God created the heavens and the earth.";
4679 println!("M =\t{}", message);
4680 let mut cipher = Vec::<u8>::new();
4681 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4682 print!("C (128 rounds) =\t");
4683 for c in cipher.clone()
4684 { print!("{:02X} ", c); }
4685 println!();
4686 let mut txt = String::new();
4687 for c in cipher.clone()
4688 { write!(txt, "{:02X} ", c); }
4689 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
4690
4691 let mut recovered = [0u8; 56];
4692 let len = a_des.decrypt_vec_into_array(&cipher, &mut recovered);
4693 print!("Ba (16 rounds) =\t");
4694 for b in recovered.clone()
4695 { print!("{:02X} ", b); }
4696 println!();
4697 let mut txt = String::new();
4698 for c in recovered.clone()
4699 { write!(txt, "{:02X} ", c); }
4700 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 ");
4701
4702 let mut converted = String::new();
4703 unsafe { converted.as_mut_vec() }.write(&recovered);
4704 unsafe { converted.as_mut_vec() }.truncate(len as usize);
4705 println!("Bb (16 rounds) =\t{}", converted);
4706 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4707 assert_eq!(converted, message);
4708 println!();
4709
4710 // Expanded case for 0 rounds which means that key is meaningless
4711 let key1 = 0x_1234567890ABCDEF_u64;
4712 let key2 = 0_u64;
4713 println!("K =\t{:#016X}", key);
4714 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
4715 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
4716
4717 let message = "In the beginning God created the heavens and the earth.";
4718 println!("M =\t{}", message);
4719 let mut cipher1 = Vec::<u8>::new();
4720 let mut cipher2 = Vec::<u8>::new();
4721 c_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher1);
4722 d_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher2);
4723 print!("C (0 rounds) =\t");
4724 for c in cipher1.clone()
4725 { print!("{:02X} ", c); }
4726 println!();
4727 let mut txt = String::new();
4728 for c in cipher1.clone()
4729 { write!(txt, "{:02X} ", c); }
4730 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
4731 print!("D (0 rounds) =\t");
4732 for c in cipher2.clone()
4733 { print!("{:02X} ", c); }
4734 println!();
4735 let mut txt = String::new();
4736 for c in cipher2.clone()
4737 { write!(txt, "{:02X} ", c); }
4738 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
4739
4740 let mut recovered1 = [0u8; 56];
4741 let mut recovered2 = [0u8; 56];
4742 let len1 = c_des.decrypt_vec_into_array(&cipher1, &mut recovered1);
4743 let len2 = d_des.decrypt_vec_into_array(&cipher2, &mut recovered2);
4744 print!("B1a (0 rounds) =\t");
4745 for b in recovered1.clone()
4746 { print!("{:02X} ", b); }
4747 println!();
4748 let mut txt = String::new();
4749 for c in recovered1.clone()
4750 { write!(txt, "{:02X} ", c); }
4751 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 ");
4752 print!("B2a (0 rounds) =\t");
4753 for b in recovered2.clone()
4754 { print!("{:02X} ", b); }
4755 println!();
4756 let mut txt = String::new();
4757 for c in recovered.clone()
4758 { write!(txt, "{:02X} ", c); }
4759 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 ");
4760
4761 let mut converted1 = String::new();
4762 let mut converted2 = String::new();
4763 unsafe { converted1.as_mut_vec() }.write(&recovered1);
4764 unsafe { converted2.as_mut_vec() }.write(&recovered2);
4765 unsafe { converted1.as_mut_vec() }.truncate(len1 as usize);
4766 unsafe { converted2.as_mut_vec() }.truncate(len2 as usize);
4767 println!("B1b (0 rounds) =\t{}", converted1);
4768 println!("B2b (0 rounds) =\t{}", converted2);
4769 assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
4770 assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
4771 assert_eq!(converted1, message);
4772 assert_eq!(converted2, message);
4773 assert_eq!(converted1, converted2);
4774 println!();
4775
4776 // Normal case for the message of 0 bytes
4777 let key = 0x_1234567890ABCDEF_u64;
4778 println!("K =\t{:#016X}", key);
4779 let mut a_des = DES::new_with_key_u64(key);
4780
4781 let message = "";
4782 println!("M =\t{}", message);
4783 let mut cipher = Vec::<u8>::new();
4784 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4785 print!("C =\t");
4786 for c in cipher.clone()
4787 { print!("{:02X} ", c); }
4788 println!();
4789 let mut txt = String::new();
4790 for c in cipher.clone()
4791 { write!(txt, "{:02X} ", c); }
4792 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
4793
4794 let mut recovered = [0u8; 8];
4795 let len = a_des.decrypt_vec_into_array(&cipher, &mut recovered);
4796
4797 print!("Ba =\t");
4798 for b in recovered.clone()
4799 { print!("{:02X} ", b); }
4800 println!();
4801 let mut txt = String::new();
4802 for c in recovered.clone()
4803 { write!(txt, "{:02X} ", c); }
4804 assert_eq!(txt, "00 00 00 00 00 00 00 00 ");
4805
4806 let mut converted = String::new();
4807 unsafe { converted.as_mut_vec() }.write(&recovered);
4808 unsafe { converted.as_mut_vec() }.truncate(len as usize);
4809 println!("Bb =\t{}", converted);
4810 assert_eq!(converted, "");
4811 assert_eq!(converted, message);
4812 println!();
4813
4814 // Normal case for the message shorter than 8 bytes
4815 let key = 0x_1234567890ABCDEF_u64;
4816 println!("K =\t{:#016X}", key);
4817 let mut a_des = DES::new_with_key_u64(key);
4818
4819 let message = "7 bytes";
4820 println!("M =\t{}", message);
4821 let mut cipher = Vec::<u8>::new();
4822 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4823 print!("C =\t");
4824 for c in cipher.clone()
4825 { print!("{:02X} ", c); }
4826 println!();
4827 let mut txt = String::new();
4828 for c in cipher.clone()
4829 { write!(txt, "{:02X} ", c); }
4830 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
4831
4832 let mut recovered = [0u8; 8];
4833 let len = a_des.decrypt_vec_into_array(&cipher, &mut recovered);
4834
4835 print!("Ba =\t");
4836 for b in recovered.clone()
4837 { print!("{:02X} ", b); }
4838 println!();
4839 let mut txt = String::new();
4840 for c in recovered.clone()
4841 { write!(txt, "{:02X} ", c); }
4842 assert_eq!(txt, "37 20 62 79 74 65 73 00 ");
4843
4844 let mut converted = String::new();
4845 unsafe { converted.as_mut_vec() }.write(&recovered);
4846 unsafe { converted.as_mut_vec() }.truncate(len as usize);
4847 println!("Bb =\t{}", converted);
4848 assert_eq!(converted, "7 bytes");
4849 assert_eq!(converted, message);
4850 println!();
4851
4852 // Normal case for the message of 8 bytes
4853 let key = 0x_1234567890ABCDEF_u64;
4854 println!("K =\t{:#016X}", key);
4855 let mut a_des = DES::new_with_key_u64(key);
4856
4857 let message = "I am OK.";
4858 println!("M =\t{}", message);
4859 let mut cipher = Vec::<u8>::new();
4860 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4861 print!("C =\t");
4862 for c in cipher.clone()
4863 { print!("{:02X} ", c); }
4864 println!();
4865 let mut txt = String::new();
4866 for c in cipher.clone()
4867 { write!(txt, "{:02X} ", c); }
4868 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
4869
4870 let mut recovered = [0u8; 16];
4871 let len = a_des.decrypt_vec_into_array(&cipher, &mut recovered);
4872
4873 print!("Ba =\t");
4874 for b in recovered.clone()
4875 { print!("{:02X} ", b); }
4876 println!();
4877 let mut txt = String::new();
4878 for c in recovered.clone()
4879 { write!(txt, "{:02X} ", c); }
4880 assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E 00 00 00 00 00 00 00 00 ");
4881
4882 let mut converted = String::new();
4883 unsafe { converted.as_mut_vec() }.write(&recovered);
4884 unsafe { converted.as_mut_vec() }.truncate(len as usize);
4885 println!("Bb =\t{}", converted);
4886 assert_eq!(converted, "I am OK.");
4887 assert_eq!(converted, message);
4888 println!();
4889
4890 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
4891 let key = 0x_1234567890ABCDEF_u64;
4892 println!("K =\t{:#016X}", key);
4893 let mut a_des = DES::new_with_key_u64(key);
4894
4895 let message = "PARK Youngho";
4896 println!("M =\t{}", message);
4897 let mut cipher = Vec::<u8>::new();
4898 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4899 print!("C =\t");
4900 for c in cipher.clone()
4901 { print!("{:02X} ", c); }
4902 println!();
4903 let mut txt = String::new();
4904 for c in cipher.clone()
4905 { write!(txt, "{:02X} ", c); }
4906 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
4907
4908 let mut recovered = [0u8; 16];
4909 let len = a_des.decrypt_vec_into_array(&cipher, &mut recovered);
4910
4911 print!("Ba =\t");
4912 for b in recovered.clone()
4913 { print!("{:02X} ", b); }
4914 println!();
4915 let mut txt = String::new();
4916 for c in recovered.clone()
4917 { write!(txt, "{:02X} ", c); }
4918 assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F 00 00 00 00 ");
4919
4920 let mut converted = String::new();
4921 unsafe { converted.as_mut_vec() }.write(&recovered);
4922 unsafe { converted.as_mut_vec() }.truncate(len as usize);
4923 println!("Bb =\t{}", converted);
4924 assert_eq!(converted, "PARK Youngho");
4925 assert_eq!(converted, message);
4926 println!();
4927
4928 // Normal case for the message of 16 bytes
4929 let key = 0x_1234567890ABCDEF_u64;
4930 println!("K =\t{:#016X}", key);
4931 let mut a_des = DES::new_with_key_u64(key);
4932
4933 let message = "고맙습니다.";
4934 println!("M =\t{}", message);
4935 let mut cipher = Vec::<u8>::new();
4936 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4937 print!("C =\t");
4938 for c in cipher.clone()
4939 { print!("{:02X} ", c); }
4940 println!();
4941 let mut txt = String::new();
4942 for c in cipher.clone()
4943 { write!(txt, "{:02X} ", c); }
4944 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
4945
4946 let mut recovered = [0u8; 24];
4947 let len = a_des.decrypt_vec_into_array(&cipher, &mut recovered);
4948
4949 print!("Ba =\t");
4950 for b in recovered.clone()
4951 { print!("{:02X} ", b); }
4952 println!();
4953 let mut txt = String::new();
4954 for c in recovered.clone()
4955 { write!(txt, "{:02X} ", c); }
4956 assert_eq!(txt, "EA B3 A0 EB A7 99 EC 8A B5 EB 8B 88 EB 8B A4 2E 00 00 00 00 00 00 00 00 ");
4957
4958 let mut converted = String::new();
4959 unsafe { converted.as_mut_vec() }.write(&recovered);
4960 unsafe { converted.as_mut_vec() }.truncate(len as usize);
4961 println!("Bb =\t{}", converted);
4962 assert_eq!(converted, "고맙습니다.");
4963 assert_eq!(converted, message);
4964 println!("-------------------------------");
4965}
Sourcefn decrypt_vec_into_string<U>(
&mut self,
cipher: &Vec<U>,
message: &mut String,
) -> u64
fn decrypt_vec_into_string<U>( &mut self, cipher: &Vec<U>, message: &mut String, ) -> u64
Decrypts the data stored in a Vec<U>
object with the padding according
to ISO 7816-4 in ECB (Electronic CodeBook) mode, and stores the decrypted
data in String object.
§Arguments
cipher
is aVec<U>
object, and is the ciphertext to be decrypted.message
is an String object, and is the plaintext to be stored.
§Output
- This method returns the size of plaintext in bytes.
- If this method returns
zero
, andcipher.len() * U::size_in_bytes()
is greater than8
forT
=u64
or16
forT
=u128
, it means that this method failed in decryption. - If this method returns
zero
, andcipher.len() * U::size_in_bytes()
is8
forT
=u64
or16
forT
=u128
, it means either that this method failed in decryption or that the original plaintext is empty String. Then, you will have to check whether or not it failed by the methodis_successful()
oris_failed()
.
§Features
- When
T
isu64
,cipher.len() * U::size_in_bytes()
can be only any multiple of8
. - When
T
isu128
,cipher.len() * U::size_in_bytes()
can be only any multiple of16
. - The padding bits are composed of the bytes that indicate the length of the padding bits in bytes according to ISO 7816-4 defined in RFC 5652.
- For more information about the padding bits according to ISO 7816-4, Read here.
- You don’t have to worry about whether or not the size of the memory area where the plaintext will be stored is enough.
- This method assumes that the original plaintext is a string in the format of UTF-8.
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, ECB_ISO };
let key = 0x_1234567890ABCDEF_u64;
println!("K =\t{:#016X}", key);
let mut a_des = DES::new_with_key_u64(key);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
print!("C (16 rounds) =\t");
for c in cipher.clone()
{ print!("{:02X} ", c); }
println!();
let mut txt = String::new();
for c in cipher.clone()
{ write!(txt, "{:02X} ", c); }
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 F4 BE 6B A5 C5 7D F6 5D ");
let mut recovered = String::new();
a_des.decrypt_vec_into_string(&cipher, &mut recovered);
println!("B (16 rounds) =\t{}", recovered);
assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
assert_eq!(recovered, message);
§For more examples,
click here
Examples found in repository?
4967fn des_decrypt_vec_with_padding_iso_ecb_into_string()
4968{
4969 println!("des_decrypt_vec_with_padding_iso_ecb_into_string");
4970 use std::io::Write;
4971 use std::fmt::Write as _;
4972 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
4973
4974 // Normal case
4975 let key = 0x_1234567890ABCDEF_u64;
4976 println!("K =\t{:#016X}", key);
4977 let mut a_des = DES::new_with_key_u64(key);
4978
4979 let message = "In the beginning God created the heavens and the earth.";
4980 println!("M =\t{}", message);
4981 let mut cipher = Vec::<u8>::new();
4982 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
4983 print!("C (16 rounds) =\t");
4984 for c in cipher.clone()
4985 { print!("{:02X} ", c); }
4986 println!();
4987 let mut txt = String::new();
4988 for c in cipher.clone()
4989 { write!(txt, "{:02X} ", c); }
4990 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 F4 BE 6B A5 C5 7D F6 5D ");
4991
4992 let mut recovered = String::new();
4993 a_des.decrypt_vec_into_string(&cipher, &mut recovered);
4994 println!("B (16 rounds) =\t{}", recovered);
4995 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
4996 assert_eq!(recovered, message);
4997 println!();
4998
4999 // Expanded case for 128 rounds
5000 let key = 0x_1234567890ABCDEF_u64;
5001 println!("K =\t{:#016X}", key);
5002 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
5003
5004 let message = "In the beginning God created the heavens and the earth.";
5005 println!("M =\t{}", message);
5006 let mut cipher = Vec::<u8>::new();
5007 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
5008 print!("C (128 rounds) =\t");
5009 for c in cipher.clone()
5010 { print!("{:02X} ", c); }
5011 println!();
5012 let mut txt = String::new();
5013 for c in cipher.clone()
5014 { write!(txt, "{:02X} ", c); }
5015 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
5016
5017 let mut recovered = String::new();
5018 a_des.decrypt_vec_into_string(&cipher, &mut recovered);
5019 println!("B (128 rounds) =\t{}", recovered);
5020 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
5021 assert_eq!(recovered, message);
5022 println!();
5023
5024 // Expanded case for 0 rounds which means that key is meaningless
5025 let key1 = 0x_1234567890ABCDEF_u64;
5026 let key2 = 0_u64;
5027 println!("K =\t{:#016X}", key);
5028 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
5029 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
5030
5031 let message = "In the beginning God created the heavens and the earth.";
5032 println!("M =\t{}", message);
5033 let mut cipher1 = Vec::<u8>::new();
5034 let mut cipher2 = Vec::<u8>::new();
5035 c_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher1);
5036 d_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher2);
5037 print!("C (0 rounds) =\t");
5038 for c in cipher1.clone()
5039 { print!("{:02X} ", c); }
5040 println!();
5041 let mut txt = String::new();
5042 for c in cipher1.clone()
5043 { write!(txt, "{:02X} ", c); }
5044 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
5045 print!("D (0 rounds) =\t");
5046 for c in cipher2.clone()
5047 { print!("{:02X} ", c); }
5048 println!();
5049 let mut txt = String::new();
5050 for c in cipher2.clone()
5051 { write!(txt, "{:02X} ", c); }
5052 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
5053
5054 let mut recovered1 = String::new();
5055 let mut recovered2 = String::new();
5056 c_des.decrypt_vec_into_string(&cipher1, &mut recovered1);
5057 d_des.decrypt_vec_into_string(&cipher2, &mut recovered2);
5058 println!("B1 (0 rounds) =\t{}", recovered1);
5059 println!("B2 (0 rounds) =\t{}", recovered2);
5060 assert_eq!(recovered1, "In the beginning God created the heavens and the earth.");
5061 assert_eq!(recovered2, "In the beginning God created the heavens and the earth.");
5062 assert_eq!(recovered1, message);
5063 assert_eq!(recovered2, message);
5064 assert_eq!(recovered1, recovered2);
5065 println!();
5066
5067 // Normal case for the message of 0 bytes
5068 let key = 0x_1234567890ABCDEF_u64;
5069 println!("K =\t{:#016X}", key);
5070 let mut a_des = DES::new_with_key_u64(key);
5071
5072 let message = "";
5073 println!("M =\t{}", message);
5074 let mut cipher = Vec::<u8>::new();
5075 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
5076 print!("C =\t");
5077 for c in cipher.clone()
5078 { print!("{:02X} ", c); }
5079 println!();
5080 let mut txt = String::new();
5081 for c in cipher.clone()
5082 { write!(txt, "{:02X} ", c); }
5083 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
5084
5085 let mut recovered = String::new();
5086 a_des.decrypt_vec_into_string(&cipher, &mut recovered);
5087 println!("B =\t{}", recovered);
5088 assert_eq!(recovered, "");
5089 assert_eq!(recovered, message);
5090 println!();
5091
5092 // Normal case for the message shorter than 8 bytes
5093 let key = 0x_1234567890ABCDEF_u64;
5094 println!("K =\t{:#016X}", key);
5095 let mut a_des = DES::new_with_key_u64(key);
5096
5097 let message = "7 bytes";
5098 println!("M =\t{}", message);
5099 let mut cipher = Vec::<u8>::new();
5100 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
5101 print!("C =\t");
5102 for c in cipher.clone()
5103 { print!("{:02X} ", c); }
5104 println!();
5105 let mut txt = String::new();
5106 for c in cipher.clone()
5107 { write!(txt, "{:02X} ", c); }
5108 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
5109
5110 let mut recovered = String::new();
5111 a_des.decrypt_vec_into_string(&cipher, &mut recovered);
5112 println!("B =\t{}", recovered);
5113 assert_eq!(recovered, "7 bytes");
5114 assert_eq!(recovered, message);
5115 println!();
5116
5117 // Normal case for the message of 8 bytes
5118 let key = 0x_1234567890ABCDEF_u64;
5119 println!("K =\t{:#016X}", key);
5120 let mut a_des = DES::new_with_key_u64(key);
5121
5122 let message = "I am OK.";
5123 println!("M =\t{}", message);
5124 let mut cipher = Vec::<u8>::new();
5125 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
5126 print!("C =\t");
5127 for c in cipher.clone()
5128 { print!("{:02X} ", c); }
5129 println!();
5130 let mut txt = String::new();
5131 for c in cipher.clone()
5132 { write!(txt, "{:02X} ", c); }
5133 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
5134
5135 let mut recovered = String::new();
5136 a_des.decrypt_vec_into_string(&cipher, &mut recovered);
5137 println!("B =\t{}", recovered);
5138 assert_eq!(recovered, "I am OK.");
5139 assert_eq!(recovered, message);
5140 println!();
5141
5142 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
5143 let key = 0x_1234567890ABCDEF_u64;
5144 println!("K =\t{:#016X}", key);
5145 let mut a_des = DES::new_with_key_u64(key);
5146
5147 let message = "PARK Youngho";
5148 println!("M =\t{}", message);
5149 let mut cipher = Vec::<u8>::new();
5150 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
5151 print!("C =\t");
5152 for c in cipher.clone()
5153 { print!("{:02X} ", c); }
5154 println!();
5155 let mut txt = String::new();
5156 for c in cipher.clone()
5157 { write!(txt, "{:02X} ", c); }
5158 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
5159
5160 let mut recovered = String::new();
5161 a_des.decrypt_vec_into_string(&cipher, &mut recovered);
5162 println!("B =\t{}", recovered);
5163 assert_eq!(recovered, "PARK Youngho");
5164 assert_eq!(recovered, message);
5165 println!();
5166
5167 // Normal case for the message of 16 bytes
5168 let key = 0x_1234567890ABCDEF_u64;
5169 println!("K =\t{:#016X}", key);
5170 let mut a_des = DES::new_with_key_u64(key);
5171
5172 let message = "고맙습니다.";
5173 println!("M =\t{}", message);
5174 let mut cipher = Vec::<u8>::new();
5175 a_des.encrypt_into_vec(message.as_ptr(), message.len() as u64, &mut cipher);
5176 print!("C =\t");
5177 for c in cipher.clone()
5178 { print!("{:02X} ", c); }
5179 println!();
5180 let mut txt = String::new();
5181 for c in cipher.clone()
5182 { write!(txt, "{:02X} ", c); }
5183 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
5184
5185 let mut recovered = String::new();
5186 a_des.decrypt_vec_into_string(&cipher, &mut recovered);
5187 println!("B =\t{}", recovered);
5188 assert_eq!(recovered, "고맙습니다.");
5189 assert_eq!(recovered, message);
5190 println!("-------------------------------");
5191}
Sourcefn decrypt_array<U, const N: usize>(
&mut self,
cipher: &[U; N],
message: *mut u8,
) -> u64
fn decrypt_array<U, const N: usize>( &mut self, cipher: &[U; N], message: *mut u8, ) -> u64
Decrypts the data stored in an array [U; N]
object with the padding
defined according to ISO 7816-4 in ECB (Electronic CodeBook) mode.
§Arguments
cipher
is the data stored in an array[U; N]
object, and is the ciphertext to be decrypted.message
is a pointer to u8 which is*mut u8
, and is the plaintext to be stored.- The size of the memory area which starts at
message
and the plaintext will be stored at is assumed to be enough. - The size of the area for plaintext should be prepared to be:
N * U::size_in_bytes()
- 1. So, it is responsible for you to prepare themessage
area big enough!
§Output
- This method returns the size of plaintext in bytes.
- If this method returns
zero
, andcipher.len() * U::size_in_bytes()
is greater than8
forT
=u64
or16
forT
=u128
, it means that this method failed in decryption. - If this method returns
zero
, andcipher.len() * U::size_in_bytes()
is8
forT
=u64
or16
forT
=u128
, it means either that this method failed in decryption or that the original plaintext is empty array [U; 0]. Then, you will have to check whether or not it failed by the methodis_successful()
oris_failed()
.
§Features
- You are not encouraged to use this method in pure Rust programming. Instead, use other safer methods such as decrypt_array_into_*().
- When
T
isu64
,N * U::size_in_bytes()
can be only any multiple of8
. - When
T
isu128
,N * U::size_in_bytes()
can be only any multiple of16
. - This method is useful to use in hybrid programming with C/C++.
- The padding bits are composed of the bytes that indicate the length of the padding bits in bytes according to ISO 7816-4 defined in RFC 5652.
- For more information about the padding bits according to ISO 7816-4, Read here.
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, ECB_ISO };
let key = 0x_1234567890ABCDEF_u64;
println!("K =\t{:#016X}", key);
let mut a_des = DES::new_with_key_u64(key);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = [0_u8; 56];
a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
print!("C (16 rounds) =\t");
for c in cipher.clone()
{ print!("{:02X} ", c); }
println!();
let mut txt = String::new();
for c in cipher.clone()
{ write!(txt, "{:02X} ", c); }
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 F4 BE 6B A5 C5 7D F6 5D ");
let mut recovered = vec![0; 55];
let len = a_des.decrypt_array(&cipher, recovered.as_mut_ptr());
recovered.truncate(len as usize);
print!("Ba (16 rounds) =\t");
for b in recovered.clone()
{ print!("{:02X} ", b); }
println!();
let mut txt = String::new();
for c in recovered.clone()
{ write!(txt, "{:02X} ", c); }
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 ");
let mut converted = String::new();
unsafe { converted.as_mut_vec() }.append(&mut recovered);
println!("Bb (16 rounds) =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");
assert_eq!(converted, message);
§For more examples,
click here
Examples found in repository?
5193fn des_decrypt_array_with_padding_iso_ecb()
5194{
5195 println!("des_decrypt_array_with_padding_iso_ecb");
5196 use std::io::Write;
5197 use std::fmt::Write as _;
5198 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
5199
5200 // Normal case
5201 let key = 0x_1234567890ABCDEF_u64;
5202 println!("K =\t{:#016X}", key);
5203 let mut a_des = DES::new_with_key_u64(key);
5204
5205 let message = "In the beginning God created the heavens and the earth.";
5206 println!("M =\t{}", message);
5207 let mut cipher = [0_u8; 56];
5208 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
5209 print!("C (16 rounds) =\t");
5210 for c in cipher.clone()
5211 { print!("{:02X} ", c); }
5212 println!();
5213 let mut txt = String::new();
5214 for c in cipher.clone()
5215 { write!(txt, "{:02X} ", c); }
5216 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 F4 BE 6B A5 C5 7D F6 5D ");
5217
5218 let mut recovered = vec![0; 55];
5219 let len = a_des.decrypt_array(&cipher, recovered.as_mut_ptr());
5220 recovered.truncate(len as usize);
5221 print!("Ba (16 rounds) =\t");
5222 for b in recovered.clone()
5223 { print!("{:02X} ", b); }
5224 println!();
5225 let mut txt = String::new();
5226 for c in recovered.clone()
5227 { write!(txt, "{:02X} ", c); }
5228 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 ");
5229
5230 let mut converted = String::new();
5231 unsafe { converted.as_mut_vec() }.append(&mut recovered);
5232
5233 println!("Bb (16 rounds) =\t{}", converted);
5234 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
5235 assert_eq!(converted, message);
5236 println!();
5237
5238 // Expanded case for 128 rounds
5239 let key = 0x_1234567890ABCDEF_u64;
5240 println!("K =\t{:#016X}", key);
5241 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
5242
5243 let message = "In the beginning God created the heavens and the earth.";
5244 println!("M =\t{}", message);
5245 let mut cipher = [0_u8; 56];
5246 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
5247 print!("C (128 rounds) =\t");
5248 for c in cipher.clone()
5249 { print!("{:02X} ", c); }
5250 println!();
5251 let mut txt = String::new();
5252 for c in cipher.clone()
5253 { write!(txt, "{:02X} ", c); }
5254 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
5255
5256 let mut recovered = vec![0; 55];
5257 let len = a_des.decrypt_array(&cipher, recovered.as_mut_ptr());
5258 recovered.truncate(len as usize);
5259 print!("Ba (128 rounds) =\t");
5260 for b in recovered.clone()
5261 { print!("{:02X} ", b); }
5262 println!();
5263 let mut txt = String::new();
5264 for c in recovered.clone()
5265 { write!(txt, "{:02X} ", c); }
5266 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 ");
5267
5268 let mut converted = String::new();
5269 unsafe { converted.as_mut_vec() }.append(&mut recovered);
5270
5271 println!("Bb (128 rounds) =\t{}", converted);
5272 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
5273 assert_eq!(converted, message);
5274 println!();
5275
5276 // Expanded case for 0 rounds which means that key is meaningless
5277 let key1 = 0x_1234567890ABCDEF_u64;
5278 let key2 = 0_u64;
5279 println!("K =\t{:#016X}", key);
5280 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
5281 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
5282
5283 let message = "In the beginning God created the heavens and the earth.";
5284 println!("M =\t{}", message);
5285 let mut cipher1 = [0_u8; 56];
5286 let mut cipher2 = [0_u8; 56];
5287 c_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher1);
5288 d_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher2);
5289 print!("C (0 rounds) =\t");
5290 for c in cipher1.clone()
5291 { print!("{:02X} ", c); }
5292 println!();
5293 let mut txt = String::new();
5294 for c in cipher1.clone()
5295 { write!(txt, "{:02X} ", c); }
5296 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
5297 print!("D (0 rounds) =\t");
5298 for c in cipher2.clone()
5299 { print!("{:02X} ", c); }
5300 println!();
5301 let mut txt = String::new();
5302 for c in cipher2.clone()
5303 { write!(txt, "{:02X} ", c); }
5304 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
5305
5306 let mut recovered1 = vec![0; 55];
5307 let mut recovered2 = vec![0; 55];
5308 let len1 = c_des.decrypt_array(&cipher1, recovered1.as_mut_ptr());
5309 let len2 = d_des.decrypt_array(&cipher2, recovered2.as_mut_ptr());
5310 recovered1.truncate(len1 as usize);
5311 recovered2.truncate(len2 as usize);
5312
5313 print!("B1a (0 rounds) =\t");
5314 for b in recovered1.clone()
5315 { print!("{:02X} ", b); }
5316 println!();
5317 let mut txt = String::new();
5318 for c in recovered1.clone()
5319 { write!(txt, "{:02X} ", c); }
5320 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 ");
5321 print!("B2a (0 rounds) =\t");
5322 for b in recovered2.clone()
5323 { print!("{:02X} ", b); }
5324 println!();
5325 let mut txt = String::new();
5326 for c in recovered2.clone()
5327 { write!(txt, "{:02X} ", c); }
5328 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 ");
5329
5330 let mut converted1 = String::new();
5331 let mut converted2 = String::new();
5332 unsafe { converted1.as_mut_vec() }.append(&mut recovered1);
5333 unsafe { converted2.as_mut_vec() }.append(&mut recovered2);
5334
5335 println!("B1b (0 rounds) =\t{}", converted1);
5336 assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
5337 assert_eq!(converted1, message);
5338 println!("B2b (0 rounds) =\t{}", converted2);
5339 assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
5340 assert_eq!(converted2, message);
5341 assert_eq!(converted1, converted1);
5342 println!();
5343
5344 // Normal case for the message of 0 bytes
5345 let key = 0x_1234567890ABCDEF_u64;
5346 println!("K =\t{:#016X}", key);
5347 let mut a_des = DES::new_with_key_u64(key);
5348
5349 let message = "";
5350 println!("M =\t{}", message);
5351 let mut cipher = [0_u8; 8];
5352 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
5353 print!("C =\t");
5354 for c in cipher.clone()
5355 { print!("{:02X} ", c); }
5356 println!();
5357 let mut txt = String::new();
5358 for c in cipher.clone()
5359 { write!(txt, "{:02X} ", c); }
5360 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
5361
5362 let mut recovered = vec![0; 8];
5363 let len = a_des.decrypt_array(&cipher, recovered.as_mut_ptr());
5364 recovered.truncate(len as usize);
5365
5366 print!("Ba =\t");
5367 for b in recovered.clone()
5368 { print!("{:02X} ", b); }
5369 println!();
5370 let mut txt = String::new();
5371 for c in recovered.clone()
5372 { write!(txt, "{:02X} ", c); }
5373 assert_eq!(txt, "");
5374
5375 let mut converted = String::new();
5376 unsafe { converted.as_mut_vec() }.append(&mut recovered);
5377
5378 println!("Bb =\t{}", converted);
5379 assert_eq!(converted, "");
5380 assert_eq!(converted, message);
5381 println!();
5382
5383 // Normal case for the message shorter than 8 bytes
5384 let key = 0x_1234567890ABCDEF_u64;
5385 println!("K =\t{:#016X}", key);
5386 let mut a_des = DES::new_with_key_u64(key);
5387
5388 let message = "7 bytes";
5389 println!("M =\t{}", message);
5390 let mut cipher = [0_u8; 8];
5391 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
5392 print!("C =\t");
5393 for c in cipher.clone()
5394 { print!("{:02X} ", c); }
5395 println!();
5396 let mut txt = String::new();
5397 for c in cipher.clone()
5398 { write!(txt, "{:02X} ", c); }
5399 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
5400
5401 let mut recovered = vec![0; 8];
5402 let len = a_des.decrypt_array(&cipher, recovered.as_mut_ptr());
5403 recovered.truncate(len as usize);
5404
5405 print!("Ba =\t");
5406 for b in recovered.clone()
5407 { print!("{:02X} ", b); }
5408 println!();
5409 let mut txt = String::new();
5410 for c in recovered.clone()
5411 { write!(txt, "{:02X} ", c); }
5412 assert_eq!(txt, "37 20 62 79 74 65 73 ");
5413
5414 let mut converted = String::new();
5415 unsafe { converted.as_mut_vec() }.append(&mut recovered);
5416
5417 println!("Bb =\t{}", converted);
5418 assert_eq!(converted, "7 bytes");
5419 assert_eq!(converted, message);
5420 println!();
5421
5422 // Normal case for the message of 8 bytes
5423 let key = 0x_1234567890ABCDEF_u64;
5424 println!("K =\t{:#016X}", key);
5425 let mut a_des = DES::new_with_key_u64(key);
5426
5427 let message = "I am OK.";
5428 println!("M =\t{}", message);
5429 let mut cipher = [0_u8; 16];
5430 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
5431 print!("C =\t");
5432 for c in cipher.clone()
5433 { print!("{:02X} ", c); }
5434 println!();
5435 let mut txt = String::new();
5436 for c in cipher.clone()
5437 { write!(txt, "{:02X} ", c); }
5438 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
5439
5440 let mut recovered = vec![0; 16];
5441 let len = a_des.decrypt_array(&cipher, recovered.as_mut_ptr());
5442 recovered.truncate(len as usize);
5443
5444 print!("Ba =\t");
5445 for b in recovered.clone()
5446 { print!("{:02X} ", b); }
5447 println!();
5448 let mut txt = String::new();
5449 for c in recovered.clone()
5450 { write!(txt, "{:02X} ", c); }
5451 assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E ");
5452
5453 let mut converted = String::new();
5454 unsafe { converted.as_mut_vec() }.append(&mut recovered);
5455
5456 println!("Bb =\t{}", converted);
5457 assert_eq!(converted, "I am OK.");
5458 assert_eq!(converted, message);
5459 println!();
5460
5461 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
5462 let key = 0x_1234567890ABCDEF_u64;
5463 println!("K =\t{:#016X}", key);
5464 let mut a_des = DES::new_with_key_u64(key);
5465
5466 let message = "PARK Youngho";
5467 println!("M =\t{}", message);
5468 let mut cipher = [0_u8; 16];
5469 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
5470 print!("C =\t");
5471 for c in cipher.clone()
5472 { print!("{:02X} ", c); }
5473 println!();
5474 let mut txt = String::new();
5475 for c in cipher.clone()
5476 { write!(txt, "{:02X} ", c); }
5477 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
5478
5479 let mut recovered = vec![0; 16];
5480 let len = a_des.decrypt_array(&cipher, recovered.as_mut_ptr());
5481 recovered.truncate(len as usize);
5482 print!("Ba =\t");
5483 for b in recovered.clone()
5484 { print!("{:02X} ", b); }
5485 println!();
5486 let mut txt = String::new();
5487 for c in recovered.clone()
5488 { write!(txt, "{:02X} ", c); }
5489 assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F ");
5490
5491 let mut converted = String::new();
5492 unsafe { converted.as_mut_vec() }.append(&mut recovered);
5493
5494 println!("Bb =\t{}", converted);
5495 assert_eq!(converted, "PARK Youngho");
5496 assert_eq!(converted, message);
5497 println!();
5498
5499 // Normal case for the message of 16 bytes
5500 let key = 0x_1234567890ABCDEF_u64;
5501 println!("K =\t{:#016X}", key);
5502 let mut a_des = DES::new_with_key_u64(key);
5503
5504 let message = "고맙습니다.";
5505 println!("M =\t{}", message);
5506 let mut cipher = [0_u8; 24];
5507 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
5508 print!("C =\t");
5509 for c in cipher.clone()
5510 { print!("{:02X} ", c); }
5511 println!();
5512 let mut txt = String::new();
5513 for c in cipher.clone()
5514 { write!(txt, "{:02X} ", c); }
5515 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
5516
5517 let mut recovered = vec![0; 24];
5518 let len = a_des.decrypt_array(&cipher, recovered.as_mut_ptr());
5519 recovered.truncate(len as usize);
5520
5521 print!("Ba =\t");
5522 for b in recovered.clone()
5523 { print!("{:02X} ", b); }
5524 println!();
5525 let mut txt = String::new();
5526 for c in recovered.clone()
5527 { write!(txt, "{:02X} ", c); }
5528 assert_eq!(txt, "EA B3 A0 EB A7 99 EC 8A B5 EB 8B 88 EB 8B A4 2E ");
5529
5530 let mut converted = String::new();
5531 unsafe { converted.as_mut_vec() }.append(&mut recovered);
5532
5533 println!("Bb =\t{}", converted);
5534 assert_eq!(converted, "고맙습니다.");
5535 assert_eq!(converted, message);
5536 println!("-------------------------------");
5537}
Sourcefn decrypt_array_into_vec<U, V, const N: usize>(
&mut self,
cipher: &[U; N],
message: &mut Vec<V>,
) -> u64
fn decrypt_array_into_vec<U, V, const N: usize>( &mut self, cipher: &[U; N], message: &mut Vec<V>, ) -> u64
Decrypts the data stored in an array [U; N]
object with the padding
according to ISO 7816-4 in ECB (Electronic CodeBook) mode, and stores the
decrypted data in Vec<V>
.
§Arguments
cipher
is an array[U; N]
object, and is the ciphertext to be decrypted.message
is aVec<V>
object, and is the plaintext to be stored.
§Output
- This method returns the size of plaintext in bytes.
- If this method returns
zero
, andcipher.len() * U::size_in_bytes()
is greater than8
forT
=u64
or16
forT
=u128
, it means that this method failed in decryption. - If this method returns
zero
, andcipher.len() * U::size_in_bytes()
is8
forT
=u64
or16
forT
=u128
, it means either that this method failed in decryption or that the original plaintext is empty array [U; 0]. Then, you will have to check whether or not it failed by the methodis_successful()
oris_failed()
.
§Features
- When
T
isu64
,N * U::size_in_bytes()
can be only any multiple of8
. - When
T
isu128
,N * U::size_in_bytes()
can be only any multiple of16
. - The padding bits are composed of the bytes that indicate the length of the padding bits in bytes according to ISO 7816-4 defined in RFC 5652.
- For more information about the padding bits according to ISO 7816-4, Read here.
- You don’t have to worry about whether or not the size of the memory area where the ciphertext will be stored is enough.
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, ECB_ISO };
let key = 0x_1234567890ABCDEF_u64;
println!("K =\t{:#016X}", key);
let mut a_des = DES::new_with_key_u64(key);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = [0_u8; 56];
a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
print!("C (16 rounds) =\t");
for c in cipher.clone()
{ print!("{:02X} ", c); }
println!();
let mut txt = String::new();
for c in cipher.clone()
{ write!(txt, "{:02X} ", c); }
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 F4 BE 6B A5 C5 7D F6 5D ");
let mut recovered = Vec::<u8>::new();
a_des.decrypt_array_into_vec(&cipher, &mut recovered);
print!("Ba (16 rounds) =\t");
for b in recovered.clone()
{ print!("{:02X} ", b); }
println!();
let mut txt = String::new();
for c in recovered.clone()
{ write!(txt, "{:02X} ", c); }
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 ");
let mut converted = String::new();
unsafe { converted.as_mut_vec() }.append(&mut recovered);
println!("Bb (16 rounds) =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");
assert_eq!(converted, message);
§For more examples,
click here
Examples found in repository?
5539fn des_decrypt_array_with_padding_iso_ecb_into_vec()
5540{
5541 println!("des_decrypt_array_with_padding_iso_ecb_into_vec");
5542 use std::io::Write;
5543 use std::fmt::Write as _;
5544 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
5545
5546 // Normal case
5547 let key = 0x_1234567890ABCDEF_u64;
5548 println!("K =\t{:#016X}", key);
5549 let mut a_des = DES::new_with_key_u64(key);
5550
5551 let message = "In the beginning God created the heavens and the earth.";
5552 println!("M =\t{}", message);
5553 let mut cipher = [0_u8; 56];
5554 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
5555 print!("C (16 rounds) =\t");
5556 for c in cipher.clone()
5557 { print!("{:02X} ", c); }
5558 println!();
5559 let mut txt = String::new();
5560 for c in cipher.clone()
5561 { write!(txt, "{:02X} ", c); }
5562 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 F4 BE 6B A5 C5 7D F6 5D ");
5563
5564 let mut recovered = Vec::<u8>::new();
5565 a_des.decrypt_array_into_vec(&cipher, &mut recovered);
5566 print!("Ba (16 rounds) =\t");
5567 for b in recovered.clone()
5568 { print!("{:02X} ", b); }
5569 println!();
5570 let mut txt = String::new();
5571 for c in recovered.clone()
5572 { write!(txt, "{:02X} ", c); }
5573 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 ");
5574
5575 let mut converted = String::new();
5576 unsafe { converted.as_mut_vec() }.append(&mut recovered);
5577
5578 println!("Bb (16 rounds) =\t{}", converted);
5579 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
5580 assert_eq!(converted, message);
5581 println!();
5582
5583 // Expanded case for 128 rounds
5584 let key = 0x_1234567890ABCDEF_u64;
5585 println!("K =\t{:#016X}", key);
5586 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
5587
5588 let message = "In the beginning God created the heavens and the earth.";
5589 println!("M =\t{}", message);
5590 let mut cipher = [0_u8; 56];
5591 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
5592 print!("C (128 rounds) =\t");
5593 for c in cipher.clone()
5594 { print!("{:02X} ", c); }
5595 println!();
5596 let mut txt = String::new();
5597 for c in cipher.clone()
5598 { write!(txt, "{:02X} ", c); }
5599 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
5600
5601 let mut recovered = Vec::<u8>::new();
5602 a_des.decrypt_array_into_vec(&cipher, &mut recovered);
5603 print!("Ba (128 rounds) =\t");
5604 for b in recovered.clone()
5605 { print!("{:02X} ", b); }
5606 println!();
5607 let mut txt = String::new();
5608 for c in recovered.clone()
5609 { write!(txt, "{:02X} ", c); }
5610 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 ");
5611
5612 let mut converted = String::new();
5613 unsafe { converted.as_mut_vec() }.append(&mut recovered);
5614
5615 println!("Bb (128 rounds) =\t{}", converted);
5616 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
5617 assert_eq!(converted, message);
5618 println!();
5619
5620 // Expanded case for 0 rounds which means that key is meaningless
5621 let key1 = 0x_1234567890ABCDEF_u64;
5622 let key2 = 0_u64;
5623 println!("K =\t{:#016X}", key);
5624 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
5625 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
5626
5627 let message = "In the beginning God created the heavens and the earth.";
5628 println!("M =\t{}", message);
5629 let mut cipher1 = [0_u8; 56];
5630 let mut cipher2 = [0_u8; 56];
5631 c_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher1);
5632 d_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher2);
5633 print!("C (0 rounds) =\t");
5634 for c in cipher1.clone()
5635 { print!("{:02X} ", c); }
5636 println!();
5637 let mut txt = String::new();
5638 for c in cipher1.clone()
5639 { write!(txt, "{:02X} ", c); }
5640 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
5641 print!("D (0 rounds) =\t");
5642 for c in cipher2.clone()
5643 { print!("{:02X} ", c); }
5644 println!();
5645 let mut txt = String::new();
5646 for c in cipher2.clone()
5647 { write!(txt, "{:02X} ", c); }
5648 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
5649
5650 let mut recovered1 = Vec::<u8>::new();
5651 let mut recovered2 = Vec::<u8>::new();
5652 c_des.decrypt_array_into_vec(&cipher1, &mut recovered1);
5653 d_des.decrypt_array_into_vec(&cipher2, &mut recovered2);
5654 print!("B1a (0 rounds) =\t");
5655 for b in recovered1.clone()
5656 { print!("{:02X} ", b); }
5657 println!();
5658 let mut txt = String::new();
5659 for c in recovered1.clone()
5660 { write!(txt, "{:02X} ", c); }
5661 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 ");
5662 print!("B2a (0 rounds) =\t");
5663 for b in recovered2.clone()
5664 { print!("{:02X} ", b); }
5665 println!();
5666 let mut txt = String::new();
5667 for c in recovered2.clone()
5668 { write!(txt, "{:02X} ", c); }
5669 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 ");
5670
5671 let mut converted1 = String::new();
5672 let mut converted2 = String::new();
5673 unsafe { converted1.as_mut_vec() }.append(&mut recovered1);
5674 unsafe { converted2.as_mut_vec() }.append(&mut recovered2);
5675
5676 println!("B1b (0 rounds) =\t{}", converted1);
5677 assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
5678 assert_eq!(converted1, message);
5679 println!("B2b (0 rounds) =\t{}", converted2);
5680 assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
5681 assert_eq!(converted2, message);
5682 assert_eq!(converted1, converted1);
5683 println!();
5684
5685 // Normal case for the message of 0 bytes
5686 let key = 0x_1234567890ABCDEF_u64;
5687 println!("K =\t{:#016X}", key);
5688 let mut a_des = DES::new_with_key_u64(key);
5689
5690 let message = "";
5691 println!("M =\t{}", message);
5692 let mut cipher = [0_u8; 8];
5693 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
5694 print!("C =\t");
5695 for c in cipher.clone()
5696 { print!("{:02X} ", c); }
5697 println!();
5698 let mut txt = String::new();
5699 for c in cipher.clone()
5700 { write!(txt, "{:02X} ", c); }
5701 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
5702
5703 let mut recovered = Vec::<u8>::new();
5704 a_des.decrypt_array_into_vec(&cipher, &mut recovered);
5705 print!("Ba =\t");
5706 for b in recovered.clone()
5707 { print!("{:02X} ", b); }
5708 println!();
5709 let mut txt = String::new();
5710 for c in recovered.clone()
5711 { write!(txt, "{:02X} ", c); }
5712 assert_eq!(txt, "");
5713
5714 let mut converted = String::new();
5715 unsafe { converted.as_mut_vec() }.append(&mut recovered);
5716
5717 println!("Bb =\t{}", converted);
5718 assert_eq!(converted, "");
5719 assert_eq!(converted, message);
5720 println!();
5721
5722 // Normal case for the message shorter than 8 bytes
5723 let key = 0x_1234567890ABCDEF_u64;
5724 println!("K =\t{:#016X}", key);
5725 let mut a_des = DES::new_with_key_u64(key);
5726
5727 let message = "7 bytes";
5728 println!("M =\t{}", message);
5729 let mut cipher = [0_u8; 8];
5730 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
5731 print!("C =\t");
5732 for c in cipher.clone()
5733 { print!("{:02X} ", c); }
5734 println!();
5735 let mut txt = String::new();
5736 for c in cipher.clone()
5737 { write!(txt, "{:02X} ", c); }
5738 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
5739
5740 let mut recovered = Vec::<u8>::new();
5741 a_des.decrypt_array_into_vec(&cipher, &mut recovered);
5742 print!("Ba =\t");
5743 for b in recovered.clone()
5744 { print!("{:02X} ", b); }
5745 println!();
5746 let mut txt = String::new();
5747 for c in recovered.clone()
5748 { write!(txt, "{:02X} ", c); }
5749 assert_eq!(txt, "37 20 62 79 74 65 73 ");
5750
5751 let mut converted = String::new();
5752 unsafe { converted.as_mut_vec() }.append(&mut recovered);
5753
5754 println!("Bb =\t{}", converted);
5755 assert_eq!(converted, "7 bytes");
5756 assert_eq!(converted, message);
5757 println!();
5758
5759 // Normal case for the message of 8 bytes
5760 let key = 0x_1234567890ABCDEF_u64;
5761 println!("K =\t{:#016X}", key);
5762 let mut a_des = DES::new_with_key_u64(key);
5763
5764 let message = "I am OK.";
5765 println!("M =\t{}", message);
5766 let mut cipher = [0_u8; 16];
5767 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
5768 print!("C =\t");
5769 for c in cipher.clone()
5770 { print!("{:02X} ", c); }
5771 println!();
5772 let mut txt = String::new();
5773 for c in cipher.clone()
5774 { write!(txt, "{:02X} ", c); }
5775 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
5776
5777 let mut recovered = Vec::<u8>::new();
5778 a_des.decrypt_array_into_vec(&cipher, &mut recovered);
5779 print!("Ba =\t");
5780 for b in recovered.clone()
5781 { print!("{:02X} ", b); }
5782 println!();
5783 let mut txt = String::new();
5784 for c in recovered.clone()
5785 { write!(txt, "{:02X} ", c); }
5786 assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E ");
5787
5788 let mut converted = String::new();
5789 unsafe { converted.as_mut_vec() }.append(&mut recovered);
5790
5791 println!("Bb =\t{}", converted);
5792 assert_eq!(converted, "I am OK.");
5793 assert_eq!(converted, message);
5794 println!();
5795
5796 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
5797 let key = 0x_1234567890ABCDEF_u64;
5798 println!("K =\t{:#016X}", key);
5799 let mut a_des = DES::new_with_key_u64(key);
5800
5801 let message = "PARK Youngho";
5802 println!("M =\t{}", message);
5803 let mut cipher = [0_u8; 16];
5804 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
5805 print!("C =\t");
5806 for c in cipher.clone()
5807 { print!("{:02X} ", c); }
5808 println!();
5809 let mut txt = String::new();
5810 for c in cipher.clone()
5811 { write!(txt, "{:02X} ", c); }
5812 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
5813
5814 let mut recovered = Vec::<u8>::new();
5815 a_des.decrypt_array_into_vec(&cipher, &mut recovered);
5816 print!("Ba =\t");
5817 for b in recovered.clone()
5818 { print!("{:02X} ", b); }
5819 println!();
5820 let mut txt = String::new();
5821 for c in recovered.clone()
5822 { write!(txt, "{:02X} ", c); }
5823 assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F ");
5824
5825 let mut converted = String::new();
5826 unsafe { converted.as_mut_vec() }.append(&mut recovered);
5827
5828 println!("Bb =\t{}", converted);
5829 assert_eq!(converted, "PARK Youngho");
5830 assert_eq!(converted, message);
5831 println!();
5832
5833 // Normal case for the message of 16 bytes
5834 let key = 0x_1234567890ABCDEF_u64;
5835 println!("K =\t{:#016X}", key);
5836 let mut a_des = DES::new_with_key_u64(key);
5837
5838 let message = "고맙습니다.";
5839 println!("M =\t{}", message);
5840 let mut cipher = [0_u8; 24];
5841 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
5842 print!("C =\t");
5843 for c in cipher.clone()
5844 { print!("{:02X} ", c); }
5845 println!();
5846 let mut txt = String::new();
5847 for c in cipher.clone()
5848 { write!(txt, "{:02X} ", c); }
5849 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
5850
5851 let mut recovered = Vec::<u8>::new();
5852 a_des.decrypt_array_into_vec(&cipher, &mut recovered);
5853 print!("Ba =\t");
5854 for b in recovered.clone()
5855 { print!("{:02X} ", b); }
5856 println!();
5857 let mut txt = String::new();
5858 for c in recovered.clone()
5859 { write!(txt, "{:02X} ", c); }
5860 assert_eq!(txt, "EA B3 A0 EB A7 99 EC 8A B5 EB 8B 88 EB 8B A4 2E ");
5861
5862 let mut converted = String::new();
5863 unsafe { converted.as_mut_vec() }.append(&mut recovered);
5864
5865 println!("Bb =\t{}", converted);
5866 assert_eq!(converted, "고맙습니다.");
5867 assert_eq!(converted, message);
5868 println!("-------------------------------");
5869}
Sourcefn decrypt_array_into_array<U, V, const N: usize, const M: usize>(
&mut self,
cipher: &[U; N],
message: &mut [V; M],
) -> u64
fn decrypt_array_into_array<U, V, const N: usize, const M: usize>( &mut self, cipher: &[U; N], message: &mut [V; M], ) -> u64
Decrypts the data stored in an array [U; N]
object with the padding
according to ISO 7816-4 in ECB (Electronic CodeBook) mode, and stores the
decrypted data in array [V; M]
.
§Arguments
cipher
is an array[U; N]
object, and is the ciphertext to be decrypted.message
is an array[V; M]
object, and is the plaintext to be stored.
§Output
- This method returns the size of plaintext in bytes.
- If this method returns
zero
, andcipher.len() * U::size_in_bytes()
is greater than8
forT
=u64
or16
forT
=u128
, it means that this method failed in decryption. - If this method returns
zero
, andcipher.len() * U::size_in_bytes()
is8
forT
=u64
or16
forT
=u128
, it means either that this method failed in decryption or that the original plaintext is empty array [U; 0]. Then, you will have to check whether or not it failed by the methodis_successful()
oris_failed()
.
§Features
- When
T
isu64
,N * U::size_in_bytes()
can be only any multiple of8
. - When
T
isu128
,N * U::size_in_bytes()
can be only any multiple of16
. - If
V::size_in_bytes() * M
is less thanU::size_in_bytes() * N - 1
, this method does not perform decryption and returnszero
. - If
V::size_in_bytes() * M
is greater than or qual toU::size_in_bytes() * N - 1
, this method performs decryption, fills the arraymessage
with the derypted plaintext, and then fills the rest of the elements of the arraymessage
with zeros if any, and returns the size of the plaintext. - The padding bits are composed of the bytes that indicate the length of the padding bits in bytes according to ISO 7816-4 defined in RFC 5652.
- For more information about the padding bits according to ISO 7816-4, Read here.
- You don’t have to worry about whether or not the size of the memory area where the ciphertext will be stored is enough.
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, ECB_ISO };
let key = 0x_1234567890ABCDEF_u64;
println!("K =\t{:#016X}", key);
let mut a_des = DES::new_with_key_u64(key);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = [0_u8; 56];
a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
print!("C (16 rounds) =\t");
for c in cipher.clone()
{ print!("{:02X} ", c); }
println!();
let mut txt = String::new();
for c in cipher.clone()
{ write!(txt, "{:02X} ", c); }
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 F4 BE 6B A5 C5 7D F6 5D ");
let mut recovered = [0u8; 56];
let len = a_des.decrypt_array_into_array(&cipher, &mut recovered);
print!("Ba (16 rounds) =\t");
for b in recovered.clone()
{ print!("{:02X} ", b); }
println!();
let mut txt = String::new();
for c in recovered.clone()
{ write!(txt, "{:02X} ", c); }
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 ");
let mut converted = String::new();
unsafe { converted.as_mut_vec() }.write(&recovered);
unsafe { converted.as_mut_vec() }.truncate(len as usize);
println!("Bb (16 rounds) =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");
assert_eq!(converted, message);
§For more examples,
click here
Examples found in repository?
5871fn des_decrypt_array_with_padding_iso_ecb_into_array()
5872{
5873 println!("des_decrypt_array_with_padding_iso_ecb_into_array");
5874 use std::io::Write;
5875 use std::fmt::Write as _;
5876 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
5877
5878 // Normal case
5879 let key = 0x_1234567890ABCDEF_u64;
5880 println!("K =\t{:#016X}", key);
5881 let mut a_des = DES::new_with_key_u64(key);
5882
5883 let message = "In the beginning God created the heavens and the earth.";
5884 println!("M =\t{}", message);
5885 let mut cipher = [0_u8; 56];
5886 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
5887 print!("C (16 rounds) =\t");
5888 for c in cipher.clone()
5889 { print!("{:02X} ", c); }
5890 println!();
5891 let mut txt = String::new();
5892 for c in cipher.clone()
5893 { write!(txt, "{:02X} ", c); }
5894 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 F4 BE 6B A5 C5 7D F6 5D ");
5895
5896 let mut recovered = [0u8; 56];
5897 let len = a_des.decrypt_array_into_array(&cipher, &mut recovered);
5898 print!("Ba (16 rounds) =\t");
5899 for b in recovered.clone()
5900 { print!("{:02X} ", b); }
5901 println!();
5902 let mut txt = String::new();
5903 for c in recovered.clone()
5904 { write!(txt, "{:02X} ", c); }
5905 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 ");
5906
5907 let mut converted = String::new();
5908 unsafe { converted.as_mut_vec() }.write(&recovered);
5909 unsafe { converted.as_mut_vec() }.truncate(len as usize);
5910 println!("Bb (16 rounds) =\t{}", converted);
5911 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
5912 assert_eq!(converted, message);
5913 println!();
5914
5915 // Expanded case for 128 rounds
5916 let key = 0x_1234567890ABCDEF_u64;
5917 println!("K =\t{:#016X}", key);
5918 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
5919
5920 let message = "In the beginning God created the heavens and the earth.";
5921 println!("M =\t{}", message);
5922 let mut cipher = [0_u8; 56];
5923 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
5924 print!("C (128 rounds) =\t");
5925 for c in cipher.clone()
5926 { print!("{:02X} ", c); }
5927 println!();
5928 let mut txt = String::new();
5929 for c in cipher.clone()
5930 { write!(txt, "{:02X} ", c); }
5931 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
5932
5933 let mut recovered = [0u8; 56];
5934 let len = a_des.decrypt_array_into_array(&cipher, &mut recovered);
5935 print!("Ba (16 rounds) =\t");
5936 for b in recovered.clone()
5937 { print!("{:02X} ", b); }
5938 println!();
5939 let mut txt = String::new();
5940 for c in recovered.clone()
5941 { write!(txt, "{:02X} ", c); }
5942 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 ");
5943
5944 let mut converted = String::new();
5945 unsafe { converted.as_mut_vec() }.write(&recovered);
5946 unsafe { converted.as_mut_vec() }.truncate(len as usize);
5947 println!("Bb (16 rounds) =\t{}", converted);
5948 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
5949 assert_eq!(converted, message);
5950 println!();
5951
5952 // Expanded case for 0 rounds which means that key is meaningless
5953 let key1 = 0x_1234567890ABCDEF_u64;
5954 let key2 = 0_u64;
5955 println!("K =\t{:#016X}", key);
5956 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
5957 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
5958
5959 let message = "In the beginning God created the heavens and the earth.";
5960 println!("M =\t{}", message);
5961 let mut cipher1 = [0_u8; 56];
5962 let mut cipher2 = [0_u8; 56];
5963 c_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher1);
5964 d_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher2);
5965 print!("C (0 rounds) =\t");
5966 for c in cipher1.clone()
5967 { print!("{:02X} ", c); }
5968 println!();
5969 let mut txt = String::new();
5970 for c in cipher1.clone()
5971 { write!(txt, "{:02X} ", c); }
5972 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
5973 print!("D (0 rounds) =\t");
5974 for c in cipher2.clone()
5975 { print!("{:02X} ", c); }
5976 println!();
5977 let mut txt = String::new();
5978 for c in cipher2.clone()
5979 { write!(txt, "{:02X} ", c); }
5980 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
5981
5982 let mut recovered1 = [0u8; 56];
5983 let mut recovered2 = [0u8; 56];
5984 let len1 = c_des.decrypt_array_into_array(&cipher1, &mut recovered1);
5985 let len2 = d_des.decrypt_array_into_array(&cipher2, &mut recovered2);
5986 print!("B1a (0 rounds) =\t");
5987 for b in recovered1.clone()
5988 { print!("{:02X} ", b); }
5989 println!();
5990 let mut txt = String::new();
5991 for c in recovered1.clone()
5992 { write!(txt, "{:02X} ", c); }
5993 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 ");
5994 print!("B2a (0 rounds) =\t");
5995 for b in recovered2.clone()
5996 { print!("{:02X} ", b); }
5997 println!();
5998 let mut txt = String::new();
5999 for c in recovered.clone()
6000 { write!(txt, "{:02X} ", c); }
6001 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 ");
6002
6003 let mut converted1 = String::new();
6004 let mut converted2 = String::new();
6005 unsafe { converted1.as_mut_vec() }.write(&recovered1);
6006 unsafe { converted2.as_mut_vec() }.write(&recovered2);
6007 unsafe { converted1.as_mut_vec() }.truncate(len1 as usize);
6008 unsafe { converted2.as_mut_vec() }.truncate(len2 as usize);
6009 println!("B1b (0 rounds) =\t{}", converted1);
6010 println!("B2b (0 rounds) =\t{}", converted2);
6011 assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
6012 assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
6013 assert_eq!(converted1, message);
6014 assert_eq!(converted2, message);
6015 assert_eq!(converted1, converted2);
6016 println!();
6017
6018 // Normal case for the message of 0 bytes
6019 let key = 0x_1234567890ABCDEF_u64;
6020 println!("K =\t{:#016X}", key);
6021 let mut a_des = DES::new_with_key_u64(key);
6022
6023 let message = "";
6024 println!("M =\t{}", message);
6025 let mut cipher = [0_u8; 8];
6026 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
6027 print!("C =\t");
6028 for c in cipher.clone()
6029 { print!("{:02X} ", c); }
6030 println!();
6031 let mut txt = String::new();
6032 for c in cipher.clone()
6033 { write!(txt, "{:02X} ", c); }
6034 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
6035
6036 let mut recovered = [0u8; 8];
6037 let len = a_des.decrypt_array_into_array(&cipher, &mut recovered);
6038
6039 print!("Ba =\t");
6040 for b in recovered.clone()
6041 { print!("{:02X} ", b); }
6042 println!();
6043 let mut txt = String::new();
6044 for c in recovered.clone()
6045 { write!(txt, "{:02X} ", c); }
6046 assert_eq!(txt, "00 00 00 00 00 00 00 00 ");
6047
6048 let mut converted = String::new();
6049 unsafe { converted.as_mut_vec() }.write(&recovered);
6050 unsafe { converted.as_mut_vec() }.truncate(len as usize);
6051 println!("Bb =\t{}", converted);
6052 assert_eq!(converted, "");
6053 assert_eq!(converted, message);
6054 println!();
6055
6056 // Normal case for the message shorter than 8 bytes
6057 let key = 0x_1234567890ABCDEF_u64;
6058 println!("K =\t{:#016X}", key);
6059 let mut a_des = DES::new_with_key_u64(key);
6060
6061 let message = "7 bytes";
6062 println!("M =\t{}", message);
6063 let mut cipher = [0_u8; 8];
6064 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
6065 print!("C =\t");
6066 for c in cipher.clone()
6067 { print!("{:02X} ", c); }
6068 println!();
6069 let mut txt = String::new();
6070 for c in cipher.clone()
6071 { write!(txt, "{:02X} ", c); }
6072 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
6073
6074 let mut recovered = [0u8; 8];
6075 let len = a_des.decrypt_array_into_array(&cipher, &mut recovered);
6076
6077 print!("Ba =\t");
6078 for b in recovered.clone()
6079 { print!("{:02X} ", b); }
6080 println!();
6081 let mut txt = String::new();
6082 for c in recovered.clone()
6083 { write!(txt, "{:02X} ", c); }
6084 assert_eq!(txt, "37 20 62 79 74 65 73 00 ");
6085
6086 let mut converted = String::new();
6087 unsafe { converted.as_mut_vec() }.write(&recovered);
6088 unsafe { converted.as_mut_vec() }.truncate(len as usize);
6089 println!("Bb =\t{}", converted);
6090 assert_eq!(converted, "7 bytes");
6091 assert_eq!(converted, message);
6092 println!();
6093
6094 // Normal case for the message of 8 bytes
6095 let key = 0x_1234567890ABCDEF_u64;
6096 println!("K =\t{:#016X}", key);
6097 let mut a_des = DES::new_with_key_u64(key);
6098
6099 let message = "I am OK.";
6100 println!("M =\t{}", message);
6101 let mut cipher = [0_u8; 16];
6102 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
6103 print!("C =\t");
6104 for c in cipher.clone()
6105 { print!("{:02X} ", c); }
6106 println!();
6107 let mut txt = String::new();
6108 for c in cipher.clone()
6109 { write!(txt, "{:02X} ", c); }
6110 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
6111
6112 let mut recovered = [0u8; 16];
6113 let len = a_des.decrypt_array_into_array(&cipher, &mut recovered);
6114
6115 print!("Ba =\t");
6116 for b in recovered.clone()
6117 { print!("{:02X} ", b); }
6118 println!();
6119 let mut txt = String::new();
6120 for c in recovered.clone()
6121 { write!(txt, "{:02X} ", c); }
6122 assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E 00 00 00 00 00 00 00 00 ");
6123
6124 let mut converted = String::new();
6125 unsafe { converted.as_mut_vec() }.write(&recovered);
6126 unsafe { converted.as_mut_vec() }.truncate(len as usize);
6127 println!("Bb =\t{}", converted);
6128 assert_eq!(converted, "I am OK.");
6129 assert_eq!(converted, message);
6130 println!();
6131
6132 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
6133 let key = 0x_1234567890ABCDEF_u64;
6134 println!("K =\t{:#016X}", key);
6135 let mut a_des = DES::new_with_key_u64(key);
6136
6137 let message = "PARK Youngho";
6138 println!("M =\t{}", message);
6139 let mut cipher = [0_u8; 16];
6140 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
6141 print!("C =\t");
6142 for c in cipher.clone()
6143 { print!("{:02X} ", c); }
6144 println!();
6145 let mut txt = String::new();
6146 for c in cipher.clone()
6147 { write!(txt, "{:02X} ", c); }
6148 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
6149
6150 let mut recovered = [0u8; 16];
6151 let len = a_des.decrypt_array_into_array(&cipher, &mut recovered);
6152
6153 print!("Ba =\t");
6154 for b in recovered.clone()
6155 { print!("{:02X} ", b); }
6156 println!();
6157 let mut txt = String::new();
6158 for c in recovered.clone()
6159 { write!(txt, "{:02X} ", c); }
6160 assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F 00 00 00 00 ");
6161
6162 let mut converted = String::new();
6163 unsafe { converted.as_mut_vec() }.write(&recovered);
6164 unsafe { converted.as_mut_vec() }.truncate(len as usize);
6165 println!("Bb =\t{}", converted);
6166 assert_eq!(converted, "PARK Youngho");
6167 assert_eq!(converted, message);
6168 println!();
6169
6170 // Normal case for the message of 16 bytes
6171 let key = 0x_1234567890ABCDEF_u64;
6172 println!("K =\t{:#016X}", key);
6173 let mut a_des = DES::new_with_key_u64(key);
6174
6175 let message = "고맙습니다.";
6176 println!("M =\t{}", message);
6177 let mut cipher = [0_u8; 24];
6178 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
6179 print!("C =\t");
6180 for c in cipher.clone()
6181 { print!("{:02X} ", c); }
6182 println!();
6183 let mut txt = String::new();
6184 for c in cipher.clone()
6185 { write!(txt, "{:02X} ", c); }
6186 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
6187
6188 let mut recovered = [0u8; 24];
6189 let len = a_des.decrypt_array_into_array(&cipher, &mut recovered);
6190
6191 print!("Ba =\t");
6192 for b in recovered.clone()
6193 { print!("{:02X} ", b); }
6194 println!();
6195 let mut txt = String::new();
6196 for c in recovered.clone()
6197 { write!(txt, "{:02X} ", c); }
6198 assert_eq!(txt, "EA B3 A0 EB A7 99 EC 8A B5 EB 8B 88 EB 8B A4 2E 00 00 00 00 00 00 00 00 ");
6199
6200 let mut converted = String::new();
6201 unsafe { converted.as_mut_vec() }.write(&recovered);
6202 unsafe { converted.as_mut_vec() }.truncate(len as usize);
6203 println!("Bb =\t{}", converted);
6204 assert_eq!(converted, "고맙습니다.");
6205 assert_eq!(converted, message);
6206 println!("-------------------------------");
6207}
Sourcefn decrypt_array_into_string<U, const N: usize>(
&mut self,
cipher: &[U; N],
message: &mut String,
) -> u64
fn decrypt_array_into_string<U, const N: usize>( &mut self, cipher: &[U; N], message: &mut String, ) -> u64
Decrypts the data stored in an array [U; N]
object with the padding according
to ISO 7816-4 in ECB (Electronic CodeBook) mode, and stores the decrypted
data in a String object.
§Arguments
cipher
is an array[U; N]
object, and is the ciphertext to be decrypted.message
is a String object, and is the plaintext to be encrypted.
§Output
- This method returns the size of plaintext in bytes.
- If this method returns
zero
, andN * U::size_in_bytes()
is greater than8
forT
=u64
or16
forT
=u128
, it means that this method failed in decryption. - If this method returns
zero
, andN * U::size_in_bytes()
is8
forT
=u64
or16
forT
=u128
, it means either that this method failed in decryption or that the original plaintext is empty array [U; 0]. Then, you will have to check whether or not it failed by the methodis_successful()
oris_failed()
.
§Features
- When
T
isu64
,N * U::size_in_bytes()
can be only any multiple of8
. - When
T
isu128
,N * U::size_in_bytes()
can be only any multiple of16
. - The padding bits are composed of the bytes that indicate the length of the padding bits in bytes according to ISO 7816-4 defined in RFC 5652.
- For more information about the padding bits according to ISO 7816-4, Read here.
- You don’t have to worry about whether or not the size of the memory area where the ciphertext will be stored is enough.
- This method assumes that the original plaintext is a string in the format of UTF-8.
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, ECB_ISO };
let key = 0x_1234567890ABCDEF_u64;
println!("K =\t{:#016X}", key);
let mut a_des = DES::new_with_key_u64(key);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = [0_u8; 56];
a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
print!("C (16 rounds) =\t");
for c in cipher.clone()
{ print!("{:02X} ", c); }
println!();
let mut txt = String::new();
for c in cipher.clone()
{ write!(txt, "{:02X} ", c); }
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 F4 BE 6B A5 C5 7D F6 5D ");
let mut recovered = String::new();
a_des.decrypt_array_into_string(&cipher, &mut recovered);
println!("B (16 rounds) =\t{}", recovered);
assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
assert_eq!(recovered, message);
§For more examples,
click here
Examples found in repository?
6209fn des_decrypt_array_with_padding_iso_ecb_into_string()
6210{
6211 println!("des_decrypt_array_with_padding_iso_ecb_into_string");
6212 use std::io::Write;
6213 use std::fmt::Write as _;
6214 use cryptocol::symmetric::{ DES, DES_Expanded, ECB_ISO };
6215
6216 // Normal case
6217 let key = 0x_1234567890ABCDEF_u64;
6218 println!("K =\t{:#016X}", key);
6219 let mut a_des = DES::new_with_key_u64(key);
6220
6221 let message = "In the beginning God created the heavens and the earth.";
6222 println!("M =\t{}", message);
6223 let mut cipher = [0_u8; 56];
6224 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
6225 print!("C (16 rounds) =\t");
6226 for c in cipher.clone()
6227 { print!("{:02X} ", c); }
6228 println!();
6229 let mut txt = String::new();
6230 for c in cipher.clone()
6231 { write!(txt, "{:02X} ", c); }
6232 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 F4 BE 6B A5 C5 7D F6 5D ");
6233
6234 let mut recovered = String::new();
6235 a_des.decrypt_array_into_string(&cipher, &mut recovered);
6236 println!("B (16 rounds) =\t{}", recovered);
6237 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
6238 assert_eq!(recovered, message);
6239 println!();
6240
6241 // Expanded case for 128 rounds
6242 let key = 0x_1234567890ABCDEF_u64;
6243 println!("K =\t{:#016X}", key);
6244 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
6245
6246 let message = "In the beginning God created the heavens and the earth.";
6247 println!("M =\t{}", message);
6248 let mut cipher = [0_u8; 56];
6249 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
6250 print!("C (128 rounds) =\t");
6251 for c in cipher.clone()
6252 { print!("{:02X} ", c); }
6253 println!();
6254 let mut txt = String::new();
6255 for c in cipher.clone()
6256 { write!(txt, "{:02X} ", c); }
6257 assert_eq!(txt, "DD C6 D8 D1 B0 66 D9 AC F7 F3 B4 FD D6 6C ED 78 20 FB A6 8D 35 38 EA 65 B0 65 23 05 FF D4 53 B1 D1 E0 C5 52 36 1E AC E2 19 EF 94 B8 98 04 A9 69 22 62 41 CF 85 0E E5 3F ");
6258
6259 let mut recovered = String::new();
6260 a_des.decrypt_array_into_string(&cipher, &mut recovered);
6261 println!("B (128 rounds) =\t{}", recovered);
6262 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
6263 assert_eq!(recovered, message);
6264 println!();
6265
6266 // Expanded case for 0 rounds which means that key is meaningless
6267 let key1 = 0x_1234567890ABCDEF_u64;
6268 let key2 = 0_u64;
6269 println!("K =\t{:#016X}", key);
6270 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
6271 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
6272
6273 let message = "In the beginning God created the heavens and the earth.";
6274 println!("M =\t{}", message);
6275 let mut cipher1 = [0_u8; 56];
6276 let mut cipher2 = [0_u8; 56];
6277 c_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher1);
6278 d_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher2);
6279 print!("C (0 rounds) =\t");
6280 for c in cipher1.clone()
6281 { print!("{:02X} ", c); }
6282 println!();
6283 let mut txt = String::new();
6284 for c in cipher1.clone()
6285 { write!(txt, "{:02X} ", c); }
6286 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
6287 print!("D (0 rounds) =\t");
6288 for c in cipher2.clone()
6289 { print!("{:02X} ", c); }
6290 println!();
6291 let mut txt = String::new();
6292 for c in cipher2.clone()
6293 { write!(txt, "{:02X} ", c); }
6294 assert_eq!(txt, "86 9D 10 B8 94 9A 10 91 9A 9B 96 9D 9D 96 9D 9B 10 8B 9F 98 10 93 B1 9A 92 B8 9A 98 10 B8 94 9A 10 94 9A 92 B9 9A 9D B3 10 92 9D 98 10 B8 94 9A 10 9A 92 B1 B8 94 1D 40 ");
6295
6296 let mut recovered1 = String::new();
6297 let mut recovered2 = String::new();
6298 c_des.decrypt_array_into_string(&cipher1, &mut recovered1);
6299 d_des.decrypt_array_into_string(&cipher2, &mut recovered2);
6300 println!("B1 (0 rounds) =\t{}", recovered1);
6301 println!("B2 (0 rounds) =\t{}", recovered2);
6302 assert_eq!(recovered1, "In the beginning God created the heavens and the earth.");
6303 assert_eq!(recovered2, "In the beginning God created the heavens and the earth.");
6304 assert_eq!(recovered1, message);
6305 assert_eq!(recovered2, message);
6306 assert_eq!(recovered1, recovered2);
6307 println!();
6308
6309 // Normal case for the message of 0 bytes
6310 let key = 0x_1234567890ABCDEF_u64;
6311 println!("K =\t{:#016X}", key);
6312 let mut a_des = DES::new_with_key_u64(key);
6313
6314 let message = "";
6315 println!("M =\t{}", message);
6316 let mut cipher = [0_u8; 8];
6317 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
6318 print!("C =\t");
6319 for c in cipher.clone()
6320 { print!("{:02X} ", c); }
6321 println!();
6322 let mut txt = String::new();
6323 for c in cipher.clone()
6324 { write!(txt, "{:02X} ", c); }
6325 assert_eq!(txt, "E2 22 32 FE 87 E8 0A 93 ");
6326
6327 let mut recovered = String::new();
6328 a_des.decrypt_array_into_string(&cipher, &mut recovered);
6329 println!("B =\t{}", recovered);
6330 assert_eq!(recovered, "");
6331 assert_eq!(recovered, message);
6332 println!();
6333
6334 // Normal case for the message shorter than 8 bytes
6335 let key = 0x_1234567890ABCDEF_u64;
6336 println!("K =\t{:#016X}", key);
6337 let mut a_des = DES::new_with_key_u64(key);
6338
6339 let message = "7 bytes";
6340 println!("M =\t{}", message);
6341 let mut cipher = [0_u8; 8];
6342 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
6343 print!("C =\t");
6344 for c in cipher.clone()
6345 { print!("{:02X} ", c); }
6346 println!();
6347 let mut txt = String::new();
6348 for c in cipher.clone()
6349 { write!(txt, "{:02X} ", c); }
6350 assert_eq!(txt, "ED 30 F1 06 B7 E3 E7 07 ");
6351
6352 let mut recovered = String::new();
6353 a_des.decrypt_array_into_string(&cipher, &mut recovered);
6354 println!("B =\t{}", recovered);
6355 assert_eq!(recovered, "7 bytes");
6356 assert_eq!(recovered, message);
6357 println!();
6358
6359 // Normal case for the message of 8 bytes
6360 let key = 0x_1234567890ABCDEF_u64;
6361 println!("K =\t{:#016X}", key);
6362 let mut a_des = DES::new_with_key_u64(key);
6363
6364 let message = "I am OK.";
6365 println!("M =\t{}", message);
6366 let mut cipher = [0_u8; 16];
6367 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
6368 print!("C =\t");
6369 for c in cipher.clone()
6370 { print!("{:02X} ", c); }
6371 println!();
6372 let mut txt = String::new();
6373 for c in cipher.clone()
6374 { write!(txt, "{:02X} ", c); }
6375 assert_eq!(txt, "27 F5 93 EE 76 DC 64 87 E2 22 32 FE 87 E8 0A 93 ");
6376
6377 let mut recovered = String::new();
6378 a_des.decrypt_array_into_string(&cipher, &mut recovered);
6379 println!("B =\t{}", recovered);
6380 assert_eq!(recovered, "I am OK.");
6381 assert_eq!(recovered, message);
6382 println!();
6383
6384 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
6385 let key = 0x_1234567890ABCDEF_u64;
6386 println!("K =\t{:#016X}", key);
6387 let mut a_des = DES::new_with_key_u64(key);
6388
6389 let message = "PARK Youngho";
6390 println!("M =\t{}", message);
6391 let mut cipher = [0_u8; 16];
6392 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
6393 print!("C =\t");
6394 for c in cipher.clone()
6395 { print!("{:02X} ", c); }
6396 println!();
6397 let mut txt = String::new();
6398 for c in cipher.clone()
6399 { write!(txt, "{:02X} ", c); }
6400 assert_eq!(txt, "8E 52 20 47 78 78 51 B7 BB 55 6A 78 71 84 72 58 ");
6401
6402 let mut recovered = String::new();
6403 a_des.decrypt_array_into_string(&cipher, &mut recovered);
6404 println!("B =\t{}", recovered);
6405 assert_eq!(recovered, "PARK Youngho");
6406 assert_eq!(recovered, message);
6407 println!();
6408
6409 // Normal case for the message of 16 bytes
6410 let key = 0x_1234567890ABCDEF_u64;
6411 println!("K =\t{:#016X}", key);
6412 let mut a_des = DES::new_with_key_u64(key);
6413
6414 let message = "고맙습니다.";
6415 println!("M =\t{}", message);
6416 let mut cipher = [0_u8; 24];
6417 a_des.encrypt_into_array(message.as_ptr(), message.len() as u64, &mut cipher);
6418 print!("C =\t");
6419 for c in cipher.clone()
6420 { print!("{:02X} ", c); }
6421 println!();
6422 let mut txt = String::new();
6423 for c in cipher.clone()
6424 { write!(txt, "{:02X} ", c); }
6425 assert_eq!(txt, "20 83 6B 12 1D 3A 5D BA 4D D6 5F 5A 8E 2E AC E7 E2 22 32 FE 87 E8 0A 93 ");
6426
6427 let mut recovered = String::new();
6428 a_des.decrypt_array_into_string(&cipher, &mut recovered);
6429 println!("B =\t{}", recovered);
6430 assert_eq!(recovered, "고맙습니다.");
6431 assert_eq!(recovered, message);
6432 println!("-------------------------------");
6433}
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.