pub trait OFB<T>: Sized {
Show 27 methods
// Required methods
fn encrypt(
&mut self,
iv: T,
message: *const u8,
length_in_bytes: u64,
cipher: *mut u8,
) -> u64;
fn encrypt_into_vec<U>(
&mut self,
iv: T,
message: *const u8,
length_in_bytes: u64,
cipher: &mut Vec<U>,
) -> u64
where U: SmallUInt + Copy + Clone;
fn encrypt_into_array<U, const N: usize>(
&mut self,
iv: T,
message: *const u8,
length_in_bytes: u64,
cipher: &mut [U; N],
) -> u64
where U: SmallUInt + Copy + Clone;
fn decrypt_into_array<U, const N: usize>(
&mut self,
iv: T,
cipher: *const u8,
length_in_bytes: u64,
message: &mut [U; N],
) -> u64
where U: SmallUInt + Copy + Clone;
// Provided methods
fn encrypt_str(&mut self, iv: T, message: &str, cipher: *mut u8) -> u64 { ... }
fn encrypt_str_into_vec<U>(
&mut self,
iv: T,
message: &str,
cipher: &mut Vec<U>,
) -> u64
where U: SmallUInt + Copy + Clone { ... }
fn encrypt_str_into_array<U, const N: usize>(
&mut self,
iv: T,
message: &str,
cipher: &mut [U; N],
) -> u64
where U: SmallUInt + Copy + Clone { ... }
fn encrypt_string(
&mut self,
iv: T,
message: &String,
cipher: *mut u8,
) -> u64 { ... }
fn encrypt_string_into_vec<U>(
&mut self,
iv: T,
message: &String,
cipher: &mut Vec<U>,
) -> u64
where U: SmallUInt + Copy + Clone { ... }
fn encrypt_string_into_array<U, const N: usize>(
&mut self,
iv: T,
message: &String,
cipher: &mut [U; N],
) -> u64
where U: SmallUInt + Copy + Clone { ... }
fn encrypt_vec<U>(
&mut self,
iv: T,
message: &Vec<U>,
cipher: *mut u8,
) -> u64
where U: SmallUInt + Copy + Clone { ... }
fn encrypt_vec_into_vec<U, V>(
&mut self,
iv: T,
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,
iv: T,
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,
iv: T,
message: &[U; N],
cipher: *mut u8,
) -> u64
where U: SmallUInt + Copy + Clone { ... }
fn encrypt_array_into_vec<U, V, const N: usize>(
&mut self,
iv: T,
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,
iv: T,
message: &[U; N],
cipher: &mut [V; M],
) -> u64
where U: SmallUInt + Copy + Clone,
V: SmallUInt + Copy + Clone { ... }
fn decrypt(
&mut self,
iv: T,
cipher: *const u8,
length_in_bytes: u64,
message: *mut u8,
) -> u64 { ... }
fn decrypt_into_vec<U>(
&mut self,
iv: T,
cipher: *const u8,
length_in_bytes: u64,
message: &mut Vec<U>,
) -> u64
where U: SmallUInt + Copy + Clone { ... }
fn decrypt_into_string(
&mut self,
iv: T,
cipher: *const u8,
length_in_bytes: u64,
message: &mut String,
) -> u64 { ... }
fn decrypt_vec<U>(
&mut self,
iv: T,
cipher: &Vec<U>,
message: *mut u8,
) -> u64
where U: SmallUInt + Copy + Clone { ... }
fn decrypt_vec_into_vec<U, V>(
&mut self,
iv: T,
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,
iv: T,
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,
iv: T,
cipher: &Vec<U>,
message: &mut String,
) -> u64
where U: SmallUInt + Copy + Clone { ... }
fn decrypt_array<U, const N: usize>(
&mut self,
iv: T,
cipher: &[U; N],
message: *mut u8,
) -> u64
where U: SmallUInt + Copy + Clone { ... }
fn decrypt_array_into_vec<U, V, const N: usize>(
&mut self,
iv: T,
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,
iv: T,
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,
iv: T,
cipher: &[U; N],
message: &mut String,
) -> u64
where U: SmallUInt + Copy + Clone { ... }
}Expand description
OFB (Output FeedBack) is one of the operation modes for encryption/decryption.
Required Methods§
Sourcefn encrypt(
&mut self,
iv: T,
message: *const u8,
length_in_bytes: u64,
cipher: *mut u8,
) -> u64
fn encrypt( &mut self, iv: T, message: *const u8, length_in_bytes: u64, cipher: *mut u8, ) -> u64
Encrypts the data without any padding in OFB (Output FeedBack) mode.
§Arguments
ivis an initialization vector for OFB mode.messageis an immutable pointer tou8which is*const u8, and is the place where the plaintext to be encrypted is stored.length_in_bytesis ofu64-type, and is the length of the plaintextmessagein bytes.cipheris a mutable pointer tou8which is*mut u8, and is the place where the encrypted data will be stored.
§Output
- This method returns the size of ciphertext.
- If this method failed in encryption or
length_in_bytesis0, this method returnszero.
§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_bytesis0, it means the message is null string. So, nothing will be encrypted, and stored in the memory area that starts fromcipher. - The size of the memory area which starts at
cipheris assumed to be enough to store the ciphertext. - The size of the area for ciphertext should be prepared to be the same
as
length_in_bytesat least. So, it is responsible for you to prepare thecipherarea big enough!
§For Rijndael or AES, and its variants
§Example 1 for AES-128
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ AES_128, CBC_ISO };
let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
println!("K =\t{:#016X}", key);
let mut a_aes = AES_128::new_with_key_u128(key);
let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = [0_u8; 55];
a_aes.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
print!("C =\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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 07 AF 4E C8 EF 5D 0A 74 97 3F 90 9E 05 9E 9E 32 FB 55 54 45 7C ED A2 2B F8 07 66 C0 7B CB 98 F3 BF 93 15 BA 26 1C 47 ");§For more examples,
click here
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, OFB };
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 iv = 0x_FEDCBA0987654321_u64;
println!("IV = {}", iv);
let mut cipher = [0_u8; 55];
a_des.encrypt(iv, 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, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");§For more examples,
click here
§For BigCryptor128
§Example 1 for TAES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
+ AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
+ AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
println!("IV = {:#034X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = [0_u8; 55];
taes.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
print!("C =\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, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");§For more examples,
click here
§For BigCryptor64
§Example 1 for TDES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
+ DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
+ DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
let iv = 0x_FEDCBA0987654321_u64;
println!("IV = {:#018X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = [0_u8; 55];
tdes.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
print!("C =\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, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");§For more examples,
click here
Sourcefn encrypt_into_vec<U>(
&mut self,
iv: T,
message: *const u8,
length_in_bytes: u64,
cipher: &mut Vec<U>,
) -> u64
fn encrypt_into_vec<U>( &mut self, iv: T, message: *const u8, length_in_bytes: u64, cipher: &mut Vec<U>, ) -> u64
Encrypts the data without any padding in OFB (Output FeedBack) mode,
and stores the encrypted data in Vec<U>.
§Arguments
ivis an initialization vector for OFB mode.messageis an immutable pointer tou8which is*const u8, and is the place where the plaintext to be encrypted is stored.length_in_bytesis ofu64-type, and is the length of the plaintextmessagein bytes.cipheris a mutable reference toVec<U>object, and is the place where the encrypted data will be stored.
§Output
- This method returns the size of ciphertext.
- If this method failed in encryption or
length_in_bytesis0, this method returnszero.
§Features
- You are not encouraged to use this method in pure Rust programming. Instead, use other safer methods such as encrypt_*_into_vec().
- This method is useful to use in hybrid programming with C/C++.
- If
length_in_bytesis0, it means the message is a null string. So, nothing will be encrypted, and stored in theVec<U>object which is referred to ascipher. - 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 Rijndael or AES, and its variants
§Example 1 for AES-128
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ AES_128, CBC_ISO };
let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
println!("K =\t{:#016X}", key);
let mut a_aes = AES_128::new_with_key_u128(key);
let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
a_aes.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher);
print!("C =\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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 07 AF 4E C8 EF 5D 0A 74 97 3F 90 9E 05 9E 9E 32 FB 55 54 45 7C ED A2 2B F8 07 66 C0 7B CB 98 F3 BF 93 15 BA 26 1C 47 ");§For more examples,
click here
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, OFB };
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 iv = 0x_FEDCBA0987654321_u64;
println!("IV = {}", iv);
let mut cipher = Vec::<u8>::new();
a_des.encrypt_into_vec(iv, 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, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");§For more examples,
click here
§For BigCryptor128
§Example 1 for TAES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
+ AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
+ AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
println!("IV = {:#034X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
taes.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher);
print!("C =\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, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");§For more examples,
click here
§For BigCryptor64
§Example 1 for TDES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
+ DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
+ DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
let iv = 0x_FEDCBA0987654321_u64;
println!("IV = {:#018X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
tdes.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher);
print!("C =\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, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");§For more examples,
click here
Sourcefn encrypt_into_array<U, const N: usize>(
&mut self,
iv: T,
message: *const u8,
length_in_bytes: u64,
cipher: &mut [U; N],
) -> u64
fn encrypt_into_array<U, const N: usize>( &mut self, iv: T, message: *const u8, length_in_bytes: u64, cipher: &mut [U; N], ) -> u64
Encrypts the data without any padding in OFB (Output FeedBack) mode,
and stores the encrypted data in array [U; N].
§Arguments
ivis an initialization vector for OFB mode.messageis an immutable pointer tou8which is*const u8, and is the place where the plaintext to be encrypted is stored.length_in_bytesis ofu64-type, and is the length of the plaintextmessagein bytes.cipheris a mutable reference to an array[U; N]object, and is the place where the encrypted data will be stored.
§Output
- This method returns the size of ciphertext.
- If this method failed in encryption or
length_in_bytesis0, this method returnszero.
§Features
- You are not encouraged to use this method in pure Rust programming. Instead, use other safer methods such as encrypt_*_into_array().
- This method is useful to use in hybrid programming with C/C++.
- If
length_in_bytesis0, it means the message is null data. So, nothing will be encrypted, and stored in the array[U; N]objectcipher. - If
U::size_in_bytes()*Nis less thanlength_in_bytes, this method does not perform encryption but returnszero. - If
U::size_in_bytes()*Nis equal tolength_in_bytes, this method performs encryption, fills the arraycipherwith the encrypted data, and returns the size of the ciphertext in bytes. - If
U::size_in_bytes()*Nis greater thanlength_in_bytes, this method performs encryption, fills the arraycipherwith the encrypted data, and then fills the rest of the elements of the arraycipherwith zeros, and returns the size of the ciphertext in bytes. - The size of the area for ciphertext should be prepared to be the same
as
length_in_bytesat least. So, it is responsible for you to prepare thecipherarea big enough!
§For Rijndael or AES, and its variants
§Example 1 for AES-128
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ AES_128, CBC_ISO };
let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
println!("K =\t{:#016X}", key);
let mut a_aes = AES_128::new_with_key_u128(key);
let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = [0_u8; 55];
a_aes.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
print!("C =\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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 07 AF 4E C8 EF 5D 0A 74 97 3F 90 9E 05 9E 9E 32 FB 55 54 45 7C ED A2 2B F8 07 66 C0 7B CB 98 F3 BF 93 15 BA 26 1C 47 ");§For more examples,
click here
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, OFB };
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 iv = 0x_FEDCBA0987654321_u64;
println!("IV = {}", iv);
let mut cipher = [0_u8; 55];
a_des.encrypt_into_array(iv, 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, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");§For more examples,
click here
§For BigCryptor128
§Example 1 for TAES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
+ AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
+ AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
println!("IV = {:#034X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = [0_u8; 55];
taes.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
print!("C =\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, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");§For more examples,
click here
§For BigCryptor64
§Example 1 for TDES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
+ DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
+ DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
let iv = 0x_FEDCBA0987654321_u64;
println!("IV = {:#018X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = [0_u8; 55];
tdes.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
print!("C =\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, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");§For more examples,
click here
Sourcefn decrypt_into_array<U, const N: usize>(
&mut self,
iv: T,
cipher: *const u8,
length_in_bytes: u64,
message: &mut [U; N],
) -> u64
fn decrypt_into_array<U, const N: usize>( &mut self, iv: T, cipher: *const u8, length_in_bytes: u64, message: &mut [U; N], ) -> u64
Decrypts the data without any padding in OFB (Output FeedBack) mode,
and stores the decrypted data in array [U; N].
§Arguments
ivis an initialization vector for OFB mode.cipheris an immutable pointer tou8which is*const u8, and is the place where the ciphertext to be decrypted is stored.length_in_bytesis ofu64-type, and is the length of the ciphertextcipherin bytes.messageis a mutable reference to an array[U; N]object, and is the place where the decrypted data will be stored.
§Output
- This method returns the size of plaintext in bytes.
- If this method failed in decryption, it always returns
zero. - Even if this method succeeded in decryption, it returns
zerowhen the original plaintext is zero-length empty data. Then, you will have to check whether or not it failed by using the methodis_successful()oris_failed(). - If
length_in_bytesis greater than0(which means that the original plaintext is surely not empty data) and it returnszero, you can interpret it that this method surely failed in decryption.
§Features
- You are not encouraged to use this method in pure Rust programming. Instead, use other safer methods such as decrypt_*_into_array().
- This method is useful to use in hybrid programming with C/C++.
- If
U::size_in_bytes()*Nis less thanlength_in_bytes, this method does not perform decryption but returnszero. - If
U::size_in_bytes()*Nis equal to or greater thanlength_in_bytes, this method performs decryption, fills the arraymessagewith the decrypted data, and then fills the rest of the elements of the arraymessagewith zeros, and returns the size of the plaintext. - It is responsible for you to prepare the
messagearea big enough! - The size of the area for plaintext does not have to be prepared more
than
length_in_bytes.
§For Rijndael or AES, and its variants
§Example 1 for AES-128
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ AES_128, CBC_ISO };
let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
println!("K =\t{:#016X}", key);
let mut a_aes = AES_128::new_with_key_u128(key);
let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = [0_u8; 55];
a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
print!("C =\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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 07 AF 4E C8 EF 5D 0A 74 97 3F 90 9E 05 9E 9E 32 FB 55 54 45 7C ED A2 2B F8 07 66 C0 7B CB 98 F3 BF 93 15 BA 26 1C 47 ");
let mut recovered = [0; 64];
let len = a_aes.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
print!("Ba =\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 00 00 00 00 00 00 00 00 ");
let mut converted = String::new();
unsafe { converted.as_mut_vec() }.write(&recovered);
unsafe { converted.as_mut_vec() }.truncate(len as usize);
println!("Bb =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");
assert_eq!(converted, message);§For more examples,
click here
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, OFB };
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 iv = 0x_FEDCBA0987654321_u64;
println!("IV = {}", iv);
let mut cipher = Vec::<u8>::new();
a_des.encrypt_str_into_vec(iv, &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, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
let mut recovered = [0u8; 56];
let len = a_des.decrypt_into_array(iv, 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
§For BigCryptor128
§Example 1 for TAES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
+ AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
+ AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
println!("IV = {:#034X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
taes.encrypt_str_into_vec(iv, &message, &mut cipher);
print!("C =\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, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
let mut recovered = [0u8; 56];
let len = taes.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
print!("Ba =\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 =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");
assert_eq!(converted, message);§For more examples,
click here
§For BigCryptor64
§Example 1 for TDES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
+ DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
+ DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
let iv = 0x_FEDCBA0987654321_u64;
println!("IV = {:#018X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
print!("C =\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, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
let mut recovered = [0u8; 56];
let len = tdes.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
print!("Ba =\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 =\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_str(&mut self, iv: T, message: &str, cipher: *mut u8) -> u64
fn encrypt_str(&mut self, iv: T, message: &str, cipher: *mut u8) -> u64
Encrypts the data in a str object without any padding in OFB (Output
FeedBack) mode.
§Arguments
ivis an initialization vector for OFB mode.messageis an immutable reference tostrobject which is&str, and is the place where the plaintext to be encrypted is stored.cipheris a mutable pointer tou8which is*mut u8, and is the place where the encrypted data will be stored.
§Output
- This method returns the size of ciphertext.
- If this method failed in encryption or
message.len()is0, this method returnszero.
§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
messageis a null string “”, nothing will be encrypted, and stored in the memory area that starts fromcipher. - The size of the memory area which starts at
cipheris assumed to be enough to store the ciphertext. - The size of the area for ciphertext should be prepared to be
message.len()at least. So, it is responsible for you to prepare thecipherarea big enough!
§For Rijndael or AES, and its variants
§Example 1 for AES-128
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ AES_128, CBC_ISO };
let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
println!("K =\t{:#016X}", key);
let mut a_aes = AES_128::new_with_key_u128(key);
let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = [0_u8; 55];
a_aes.encrypt_str(iv, &message, cipher.as_mut_ptr());
print!("C =\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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 07 AF 4E C8 EF 5D 0A 74 97 3F 90 9E 05 9E 9E 32 FB 55 54 45 7C ED A2 2B F8 07 66 C0 7B CB 98 F3 BF 93 15 BA 26 1C 47 ");§For more examples,
click here
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, OFB };
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 iv = 0x_FEDCBA0987654321_u64;
println!("IV = {}", iv);
let mut cipher = [0_u8; 55];
a_des.encrypt_str(iv, &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, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");§For more examples,
click here
§For BigCryptor128
§Example 1 for TAES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
+ AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
+ AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
println!("IV = {:#034X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = [0_u8; 55];
taes.encrypt_str(iv, &message, cipher.as_mut_ptr());
print!("C =\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, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");§For more examples,
click here
§For BigCryptor64
§Example 1 for TDES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
+ DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
+ DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
let iv = 0x_FEDCBA0987654321_u64;
println!("IV = {:#018X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = [0_u8; 55];
tdes.encrypt_str(iv, &message, cipher.as_mut_ptr());
print!("C =\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, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");§For more examples,
click here
Examples found in repository?
138fn bigcryptor64_encrypt_str_ofb()
139{
140 println!("bigcryptor64_encrypt_str_ofb()");
141 use std::io::Write;
142 use std::fmt::Write as _;
143 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
144
145 // TDES case
146 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
147 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
148 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
149 let iv = 0x_FEDCBA0987654321_u64;
150 println!("IV = {:#018X}", iv);
151 let message = "In the beginning God created the heavens and the earth.";
152 println!("M =\t{}", message);
153 let mut cipher = [0_u8; 55];
154 tdes.encrypt_str(iv, &message, cipher.as_mut_ptr());
155 print!("C =\t");
156 for c in cipher.clone()
157 { print!("{:02X} ", c); }
158 println!();
159 let mut txt = String::new();
160 for c in cipher.clone()
161 { write!(txt, "{:02X} ", c); }
162 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
163 println!("-------------------------------");
164}More examples
138fn bigcryptor128_encrypt_str_ofb()
139{
140 println!("bigcryptor128_encrypt_str_ofb()");
141 use std::io::Write;
142 use std::fmt::Write as _;
143 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
144
145 // TAES_128 case
146 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
147 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
148 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
149 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
150 println!("IV = {:#034X}", iv);
151 let message = "In the beginning God created the heavens and the earth.";
152 println!("M =\t{}", message);
153 let mut cipher = [0_u8; 55];
154 taes.encrypt_str(iv, &message, cipher.as_mut_ptr());
155 print!("C =\t");
156 for c in cipher.clone()
157 { print!("{:02X} ", c); }
158 println!();
159 let mut txt = String::new();
160 for c in cipher.clone()
161 { write!(txt, "{:02X} ", c); }
162 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
163 println!("-------------------------------");
164}451fn aes_encrypt_str_ofb()
452{
453 println!("aes_encrypt_str_ofb()");
454 use std::io::Write;
455 use std::fmt::Write as _;
456 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
457
458 // Normal case for AES-128
459 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
460 println!("K =\t{:#016X}", key);
461 let mut a_aes = AES_128::new_with_key_u128(key);
462 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
463 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
464
465 let message = "In the beginning God created the heavens and the earth.";
466 println!("M =\t{}", message);
467 let mut cipher = [0_u8; 55];
468 a_aes.encrypt_str(iv, &message, cipher.as_mut_ptr());
469 print!("C =\t");
470 for c in cipher.clone()
471 { print!("{:02X} ", c); }
472 println!();
473 let mut txt = String::new();
474 for c in cipher.clone()
475 { write!(txt, "{:02X} ", c); }
476 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
477 println!();
478
479 // Normal case for AES-192
480 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
481 print!("K =\t");
482 for i in 0..24
483 { print!("{:02X}", key[i]); }
484 println!();
485 let mut a_aes = AES_192::new_with_key(&key);
486 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
487 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
488
489 let message = "In the beginning God created the heavens and the earth.";
490 println!("M =\t{}", message);
491 let mut cipher = [0_u8; 55];
492 a_aes.encrypt_str(iv, &message, cipher.as_mut_ptr());
493 print!("C =\t");
494 for c in cipher.clone()
495 { print!("{:02X} ", c); }
496 println!();
497 let mut txt = String::new();
498 for c in cipher.clone()
499 { write!(txt, "{:02X} ", c); }
500 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
501 println!();
502
503 // Normal case for AES-256
504 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
505 print!("K =\t");
506 for i in 0..32
507 { print!("{:02X}", key[i]); }
508 println!();
509 let mut a_aes = AES_256::new_with_key(&key);
510 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
511 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
512
513 let message = "In the beginning God created the heavens and the earth.";
514 println!("M =\t{}", message);
515 let mut cipher = [0_u8; 55];
516 a_aes.encrypt_str(iv, &message, cipher.as_mut_ptr());
517 print!("C =\t");
518 for c in cipher.clone()
519 { print!("{:02X} ", c); }
520 println!();
521 let mut txt = String::new();
522 for c in cipher.clone()
523 { write!(txt, "{:02X} ", c); }
524 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
525 println!();
526
527 // Normal case for Rijndael-256-256
528 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
529 print!("K =\t");
530 for i in 0..32
531 { print!("{:02X}", key[i]); }
532 println!();
533 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
534 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
535 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
536
537 let message = "In the beginning God created the heavens and the earth.";
538 println!("M =\t{}", message);
539 let mut cipher = [0_u8; 55];
540 a_rijndael.encrypt_str(iv, &message, cipher.as_mut_ptr());
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, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
549 println!();
550
551 // Normal case for Rijndael-512-512 for post-quantum
552 use cryptocol::number::SharedArrays;
553 use cryptocol::hash::SHA3_512;
554 let mut sha3 = SHA3_512::new();
555 sha3.absorb_str("Post-quantum");
556 let key: [u8; 64] = sha3.get_hash_value_in_array();
557 print!("K =\t");
558 for i in 0..64
559 { print!("{:02X}", key[i]); }
560 println!();
561 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
562 sha3.absorb_str("Initialize");
563 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
564 iv.src = sha3.get_hash_value_in_array();
565 let iv = unsafe { iv.des };
566 print!("IV =\t");
567 for i in 0..16
568 { print!("{:08X}", iv[i].to_be()); }
569 println!();
570 let message = "In the beginning God created the heavens and the earth.";
571 println!("M =\t{}", message);
572 let mut cipher = [0_u8; 55];
573 a_rijndael.encrypt_str(iv, &message, cipher.as_mut_ptr());
574 print!("C =\t");
575 for c in cipher.clone()
576 { print!("{:02X} ", c); }
577 println!();
578 let mut txt = String::new();
579 for c in cipher.clone()
580 { write!(txt, "{:02X} ", c); }
581 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
582 println!("-------------------------------");
583}
584
585fn aes_encrypt_str_ofb_into_vec()
586{
587 println!("aes_encrypt_str_ofb_into_vec()");
588 use std::io::Write;
589 use std::fmt::Write as _;
590 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
591
592 // Normal case for AES-128
593 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
594 println!("K =\t{:#016X}", key);
595 let mut a_aes = AES_128::new_with_key_u128(key);
596 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
597 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
598
599 let message = "In the beginning God created the heavens and the earth.";
600 println!("M =\t{}", message);
601 let mut cipher = Vec::<u8>::new();
602 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
603 print!("C =\t");
604 for c in cipher.clone()
605 { print!("{:02X} ", c); }
606 println!();
607 let mut txt = String::new();
608 for c in cipher.clone()
609 { write!(txt, "{:02X} ", c); }
610 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
611 println!();
612
613 // Normal case for AES-192
614 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
615 print!("K =\t");
616 for i in 0..24
617 { print!("{:02X}", key[i]); }
618 println!();
619 let mut a_aes = AES_192::new_with_key(&key);
620 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
621 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
622
623 let message = "In the beginning God created the heavens and the earth.";
624 println!("M =\t{}", message);
625 let mut cipher = Vec::<u8>::new();
626 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
627 print!("C =\t");
628 for c in cipher.clone()
629 { print!("{:02X} ", c); }
630 println!();
631 let mut txt = String::new();
632 for c in cipher.clone()
633 { write!(txt, "{:02X} ", c); }
634 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
635 println!();
636
637 // Normal case for AES-256
638 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
639 print!("K =\t");
640 for i in 0..32
641 { print!("{:02X}", key[i]); }
642 println!();
643 let mut a_aes = AES_256::new_with_key(&key);
644 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
645 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
646
647 let message = "In the beginning God created the heavens and the earth.";
648 println!("M =\t{}", message);
649 let mut cipher = Vec::<u8>::new();
650 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
651 print!("C =\t");
652 for c in cipher.clone()
653 { print!("{:02X} ", c); }
654 println!();
655 let mut txt = String::new();
656 for c in cipher.clone()
657 { write!(txt, "{:02X} ", c); }
658 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
659 println!();
660
661 // Normal case for Rijndael-256-256
662 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
663 print!("K =\t");
664 for i in 0..32
665 { print!("{:02X}", key[i]); }
666 println!();
667 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
668 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
669 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
670
671 let message = "In the beginning God created the heavens and the earth.";
672 println!("M =\t{}", message);
673 let mut cipher = Vec::<u8>::new();
674 a_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
675 print!("C =\t");
676 for c in cipher.clone()
677 { print!("{:02X} ", c); }
678 println!();
679 let mut txt = String::new();
680 for c in cipher.clone()
681 { write!(txt, "{:02X} ", c); }
682 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
683 println!();
684
685 // Normal case for Rijndael-512-512 for post-quantum
686 use cryptocol::number::SharedArrays;
687 use cryptocol::hash::SHA3_512;
688 let mut sha3 = SHA3_512::new();
689 sha3.absorb_str("Post-quantum");
690 let key: [u8; 64] = sha3.get_hash_value_in_array();
691 print!("K =\t");
692 for i in 0..64
693 { print!("{:02X}", key[i]); }
694 println!();
695 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
696 sha3.absorb_str("Initialize");
697 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
698 iv.src = sha3.get_hash_value_in_array();
699 let iv = unsafe { iv.des };
700 print!("IV =\t");
701 for i in 0..16
702 { print!("{:08X}", iv[i].to_be()); }
703 println!();
704 let message = "In the beginning God created the heavens and the earth.";
705 println!("M =\t{}", message);
706 let mut cipher = Vec::<u8>::new();
707 a_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
708 print!("C =\t");
709 for c in cipher.clone()
710 { print!("{:02X} ", c); }
711 println!();
712 let mut txt = String::new();
713 for c in cipher.clone()
714 { write!(txt, "{:02X} ", c); }
715 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
716 println!("-------------------------------");
717}
718
719fn aes_encrypt_str_ofb_into_array()
720{
721 println!("aes_encrypt_str_ofb_into_array()");
722 use std::io::Write;
723 use std::fmt::Write as _;
724 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
725
726 // Normal case for AES-128
727 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
728 println!("K =\t{:#016X}", key);
729 let mut a_aes = AES_128::new_with_key_u128(key);
730 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
731 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
732
733 let message = "In the beginning God created the heavens and the earth.";
734 println!("M =\t{}", message);
735 let mut cipher = [0_u8; 55];
736 a_aes.encrypt_str_into_array(iv, &message, &mut cipher);
737 print!("C =\t");
738 for c in cipher.clone()
739 { print!("{:02X} ", c); }
740 println!();
741 let mut txt = String::new();
742 for c in cipher.clone()
743 { write!(txt, "{:02X} ", c); }
744 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
745 println!();
746
747 // Normal case for AES-192
748 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
749 print!("K =\t");
750 for i in 0..24
751 { print!("{:02X}", key[i]); }
752 println!();
753 let mut a_aes = AES_192::new_with_key(&key);
754 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
755 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
756
757 let message = "In the beginning God created the heavens and the earth.";
758 println!("M =\t{}", message);
759 let mut cipher = [0_u8; 55];
760 a_aes.encrypt_str_into_array(iv, &message, &mut cipher);
761 print!("C =\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, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
769 println!();
770
771 // Normal case for AES-256
772 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
773 print!("K =\t");
774 for i in 0..32
775 { print!("{:02X}", key[i]); }
776 println!();
777 let mut a_aes = AES_256::new_with_key(&key);
778 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
779 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
780
781 let message = "In the beginning God created the heavens and the earth.";
782 println!("M =\t{}", message);
783 let mut cipher = [0_u8; 55];
784 a_aes.encrypt_str_into_array(iv, &message, &mut cipher);
785 print!("C =\t");
786 for c in cipher.clone()
787 { print!("{:02X} ", c); }
788 println!();
789 let mut txt = String::new();
790 for c in cipher.clone()
791 { write!(txt, "{:02X} ", c); }
792 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
793 println!();
794
795 // Normal case for Rijndael-256-256
796 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
797 print!("K =\t");
798 for i in 0..32
799 { print!("{:02X}", key[i]); }
800 println!();
801 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
802 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
803 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
804
805 let message = "In the beginning God created the heavens and the earth.";
806 println!("M =\t{}", message);
807 let mut cipher = [0_u8; 55];
808 a_rijndael.encrypt_str_into_array(iv, &message, &mut cipher);
809 print!("C =\t");
810 for c in cipher.clone()
811 { print!("{:02X} ", c); }
812 println!();
813 let mut txt = String::new();
814 for c in cipher.clone()
815 { write!(txt, "{:02X} ", c); }
816 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
817 println!();
818
819 // Normal case for Rijndael-512-512 for post-quantum
820 use cryptocol::number::SharedArrays;
821 use cryptocol::hash::SHA3_512;
822 let mut sha3 = SHA3_512::new();
823 sha3.absorb_str("Post-quantum");
824 let key: [u8; 64] = sha3.get_hash_value_in_array();
825 print!("K =\t");
826 for i in 0..64
827 { print!("{:02X}", key[i]); }
828 println!();
829 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
830 sha3.absorb_str("Initialize");
831 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
832 iv.src = sha3.get_hash_value_in_array();
833 let iv = unsafe { iv.des };
834 print!("IV =\t");
835 for i in 0..16
836 { print!("{:08X}", iv[i].to_be()); }
837 println!();
838 let message = "In the beginning God created the heavens and the earth.";
839 println!("M =\t{}", message);
840 let mut cipher = [0_u8; 55];
841 a_rijndael.encrypt_str_into_array(iv, &message, &mut cipher);
842 print!("C =\t");
843 for c in cipher.clone()
844 { print!("{:02X} ", c); }
845 println!();
846 let mut txt = String::new();
847 for c in cipher.clone()
848 { write!(txt, "{:02X} ", c); }
849 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
850 println!("-------------------------------");
851}
852
853fn aes_encrypt_string_ofb()
854{
855 println!("aes_encrypt_string_ofb()");
856 use std::io::Write;
857 use std::fmt::Write as _;
858 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
859
860 // Normal case for AES-128
861 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
862 println!("K =\t{:#016X}", key);
863 let mut a_aes = AES_128::new_with_key_u128(key);
864 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
865 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
866
867 let message = "In the beginning God created the heavens and the earth.".to_string();
868 println!("M =\t{}", message);
869 let mut cipher = [0_u8; 55];
870 a_aes.encrypt_string(iv, &message, cipher.as_mut_ptr());
871 print!("C =\t");
872 for c in cipher.clone()
873 { print!("{:02X} ", c); }
874 println!();
875 let mut txt = String::new();
876 for c in cipher.clone()
877 { write!(txt, "{:02X} ", c); }
878 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
879 println!();
880
881 // Normal case for AES-192
882 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
883 print!("K =\t");
884 for i in 0..24
885 { print!("{:02X}", key[i]); }
886 println!();
887 let mut a_aes = AES_192::new_with_key(&key);
888 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
889 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
890
891 let message = "In the beginning God created the heavens and the earth.".to_string();
892 println!("M =\t{}", message);
893 let mut cipher = [0_u8; 55];
894 a_aes.encrypt_string(iv, &message, cipher.as_mut_ptr());
895 print!("C =\t");
896 for c in cipher.clone()
897 { print!("{:02X} ", c); }
898 println!();
899 let mut txt = String::new();
900 for c in cipher.clone()
901 { write!(txt, "{:02X} ", c); }
902 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
903 println!();
904
905 // Normal case for AES-256
906 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
907 print!("K =\t");
908 for i in 0..32
909 { print!("{:02X}", key[i]); }
910 println!();
911 let mut a_aes = AES_256::new_with_key(&key);
912 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
913 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
914
915 let message = "In the beginning God created the heavens and the earth.".to_string();
916 println!("M =\t{}", message);
917 let mut cipher = [0_u8; 55];
918 a_aes.encrypt_string(iv, &message, cipher.as_mut_ptr());
919 print!("C =\t");
920 for c in cipher.clone()
921 { print!("{:02X} ", c); }
922 println!();
923 let mut txt = String::new();
924 for c in cipher.clone()
925 { write!(txt, "{:02X} ", c); }
926 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
927 println!();
928
929 // Normal case for Rijndael-256-256
930 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
931 print!("K =\t");
932 for i in 0..32
933 { print!("{:02X}", key[i]); }
934 println!();
935 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
936 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
937 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
938
939 let message = "In the beginning God created the heavens and the earth.".to_string();
940 println!("M =\t{}", message);
941 let mut cipher = [0_u8; 55];
942 a_rijndael.encrypt_string(iv, &message, cipher.as_mut_ptr());
943 print!("C =\t");
944 for c in cipher.clone()
945 { print!("{:02X} ", c); }
946 println!();
947 let mut txt = String::new();
948 for c in cipher.clone()
949 { write!(txt, "{:02X} ", c); }
950 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
951 println!();
952
953 // Normal case for Rijndael-512-512 for post-quantum
954 use cryptocol::number::SharedArrays;
955 use cryptocol::hash::SHA3_512;
956 let mut sha3 = SHA3_512::new();
957 sha3.absorb_str("Post-quantum");
958 let key: [u8; 64] = sha3.get_hash_value_in_array();
959 print!("K =\t");
960 for i in 0..64
961 { print!("{:02X}", key[i]); }
962 println!();
963 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
964 sha3.absorb_str("Initialize");
965 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
966 iv.src = sha3.get_hash_value_in_array();
967 let iv = unsafe { iv.des };
968 print!("IV =\t");
969 for i in 0..16
970 { print!("{:08X}", iv[i].to_be()); }
971 println!();
972
973 let message = "In the beginning God created the heavens and the earth.".to_string();
974 println!("M =\t{}", message);
975 let mut cipher = [0_u8; 55];
976 a_rijndael.encrypt_string(iv, &message, cipher.as_mut_ptr());
977 print!("C =\t");
978 for c in cipher.clone()
979 { print!("{:02X} ", c); }
980 println!();
981 let mut txt = String::new();
982 for c in cipher.clone()
983 { write!(txt, "{:02X} ", c); }
984 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
985 println!("-------------------------------");
986}
987
988fn aes_encrypt_string_ofb_into_vec()
989{
990 println!("aes_encrypt_string_ofb_into_vec()");
991 use std::io::Write;
992 use std::fmt::Write as _;
993 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
994
995 // Normal case for AES-128
996 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
997 println!("K =\t{:#016X}", key);
998 let mut a_aes = AES_128::new_with_key_u128(key);
999 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1000 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1001
1002 let message = "In the beginning God created the heavens and the earth.".to_string();
1003 println!("M =\t{}", message);
1004 let mut cipher = Vec::<u8>::new();
1005 a_aes.encrypt_string_into_vec(iv, &message, &mut cipher);
1006 print!("C =\t");
1007 for c in cipher.clone()
1008 { print!("{:02X} ", c); }
1009 println!();
1010 let mut txt = String::new();
1011 for c in cipher.clone()
1012 { write!(txt, "{:02X} ", c); }
1013 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1014 println!();
1015
1016 // Normal case for AES-192
1017 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1018 print!("K =\t");
1019 for i in 0..24
1020 { print!("{:02X}", key[i]); }
1021 println!();
1022 let mut a_aes = AES_192::new_with_key(&key);
1023 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1024 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1025
1026 let message = "In the beginning God created the heavens and the earth.".to_string();
1027 println!("M =\t{}", message);
1028 let mut cipher = Vec::<u8>::new();
1029 a_aes.encrypt_string_into_vec(iv, &message, &mut cipher);
1030 print!("C =\t");
1031 for c in cipher.clone()
1032 { print!("{:02X} ", c); }
1033 println!();
1034 let mut txt = String::new();
1035 for c in cipher.clone()
1036 { write!(txt, "{:02X} ", c); }
1037 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
1038 println!();
1039
1040 // Normal case for AES-256
1041 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1042 print!("K =\t");
1043 for i in 0..32
1044 { print!("{:02X}", key[i]); }
1045 println!();
1046 let mut a_aes = AES_256::new_with_key(&key);
1047 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1048 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1049
1050 let message = "In the beginning God created the heavens and the earth.".to_string();
1051 println!("M =\t{}", message);
1052 let mut cipher = Vec::<u8>::new();
1053 a_aes.encrypt_string_into_vec(iv, &message, &mut cipher);
1054 print!("C =\t");
1055 for c in cipher.clone()
1056 { print!("{:02X} ", c); }
1057 println!();
1058 let mut txt = String::new();
1059 for c in cipher.clone()
1060 { write!(txt, "{:02X} ", c); }
1061 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
1062 println!();
1063
1064 // Normal case for Rijndael-256-256
1065 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1066 print!("K =\t");
1067 for i in 0..32
1068 { print!("{:02X}", key[i]); }
1069 println!();
1070 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
1071 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1072 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
1073
1074 let message = "In the beginning God created the heavens and the earth.".to_string();
1075 println!("M =\t{}", message);
1076 let mut cipher = Vec::<u8>::new();
1077 a_rijndael.encrypt_string_into_vec(iv, &message, &mut cipher);
1078 print!("C =\t");
1079 for c in cipher.clone()
1080 { print!("{:02X} ", c); }
1081 println!();
1082 let mut txt = String::new();
1083 for c in cipher.clone()
1084 { write!(txt, "{:02X} ", c); }
1085 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
1086 println!();
1087
1088 // Normal case for Rijndael-512-512 for post-quantum
1089 use cryptocol::number::SharedArrays;
1090 use cryptocol::hash::SHA3_512;
1091 let mut sha3 = SHA3_512::new();
1092 sha3.absorb_str("Post-quantum");
1093 let key: [u8; 64] = sha3.get_hash_value_in_array();
1094 print!("K =\t");
1095 for i in 0..64
1096 { print!("{:02X}", key[i]); }
1097 println!();
1098 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
1099 sha3.absorb_str("Initialize");
1100 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
1101 iv.src = sha3.get_hash_value_in_array();
1102 let iv = unsafe { iv.des };
1103 print!("IV =\t");
1104 for i in 0..16
1105 { print!("{:08X}", iv[i].to_be()); }
1106 println!();
1107 let message = "In the beginning God created the heavens and the earth.".to_string();
1108 println!("M =\t{}", message);
1109 let mut cipher = Vec::<u8>::new();
1110 a_rijndael.encrypt_string_into_vec(iv, &message, &mut cipher);
1111 print!("C =\t");
1112 for c in cipher.clone()
1113 { print!("{:02X} ", c); }
1114 println!();
1115 let mut txt = String::new();
1116 for c in cipher.clone()
1117 { write!(txt, "{:02X} ", c); }
1118 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
1119 println!("-------------------------------");
1120}
1121
1122fn aes_encrypt_string_ofb_into_array()
1123{
1124 println!("aes_encrypt_string_ofb_into_array()");
1125 use std::io::Write;
1126 use std::fmt::Write as _;
1127 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
1128
1129 // Normal case for AES-128
1130 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1131 println!("K =\t{:#016X}", key);
1132 let mut a_aes = AES_128::new_with_key_u128(key);
1133 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1134 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1135
1136 let message = "In the beginning God created the heavens and the earth.".to_string();
1137 println!("M =\t{}", message);
1138 let mut cipher = [0_u8; 55];
1139 a_aes.encrypt_string_into_array(iv, &message, &mut cipher);
1140 print!("C =\t");
1141 for c in cipher.clone()
1142 { print!("{:02X} ", c); }
1143 println!();
1144 let mut txt = String::new();
1145 for c in cipher.clone()
1146 { write!(txt, "{:02X} ", c); }
1147 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1148 println!();
1149
1150 // Normal case for AES-192
1151 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1152 print!("K =\t");
1153 for i in 0..24
1154 { print!("{:02X}", key[i]); }
1155 println!();
1156 let mut a_aes = AES_192::new_with_key(&key);
1157 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1158 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1159
1160 let message = "In the beginning God created the heavens and the earth.".to_string();
1161 println!("M =\t{}", message);
1162 let mut cipher = [0_u8; 55];
1163 a_aes.encrypt_string_into_array(iv, &message, &mut cipher);
1164 print!("C =\t");
1165 for c in cipher.clone()
1166 { print!("{:02X} ", c); }
1167 println!();
1168 let mut txt = String::new();
1169 for c in cipher.clone()
1170 { write!(txt, "{:02X} ", c); }
1171 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
1172 println!();
1173
1174 // Normal case for AES-256
1175 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1176 print!("K =\t");
1177 for i in 0..32
1178 { print!("{:02X}", key[i]); }
1179 println!();
1180 let mut a_aes = AES_256::new_with_key(&key);
1181 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1182 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1183
1184 let message = "In the beginning God created the heavens and the earth.".to_string();
1185 println!("M =\t{}", message);
1186 let mut cipher = [0_u8; 55];
1187 a_aes.encrypt_string_into_array(iv, &message, &mut cipher);
1188 print!("C =\t");
1189 for c in cipher.clone()
1190 { print!("{:02X} ", c); }
1191 println!();
1192 let mut txt = String::new();
1193 for c in cipher.clone()
1194 { write!(txt, "{:02X} ", c); }
1195 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
1196 println!();
1197
1198 // Normal case for Rijndael-256-256
1199 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1200 print!("K =\t");
1201 for i in 0..32
1202 { print!("{:02X}", key[i]); }
1203 println!();
1204 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
1205 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1206 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
1207
1208 let message = "In the beginning God created the heavens and the earth.".to_string();
1209 println!("M =\t{}", message);
1210 let mut cipher = [0_u8; 55];
1211 a_rijndael.encrypt_string_into_array(iv, &message, &mut cipher);
1212 print!("C =\t");
1213 for c in cipher.clone()
1214 { print!("{:02X} ", c); }
1215 println!();
1216 let mut txt = String::new();
1217 for c in cipher.clone()
1218 { write!(txt, "{:02X} ", c); }
1219 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
1220 println!();
1221
1222 // Normal case for Rijndael-512-512 for post-quantum
1223 use cryptocol::number::SharedArrays;
1224 use cryptocol::hash::SHA3_512;
1225 let mut sha3 = SHA3_512::new();
1226 sha3.absorb_str("Post-quantum");
1227 let key: [u8; 64] = sha3.get_hash_value_in_array();
1228 print!("K =\t");
1229 for i in 0..64
1230 { print!("{:02X}", key[i]); }
1231 println!();
1232 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
1233 sha3.absorb_str("Initialize");
1234 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
1235 iv.src = sha3.get_hash_value_in_array();
1236 let iv = unsafe { iv.des };
1237 print!("IV =\t");
1238 for i in 0..16
1239 { print!("{:08X}", iv[i].to_be()); }
1240 println!();
1241 let message = "In the beginning God created the heavens and the earth.".to_string();
1242 println!("M =\t{}", message);
1243 let mut cipher = [0_u8; 55];
1244 a_rijndael.encrypt_string_into_array(iv, &message, &mut cipher);
1245 print!("C =\t");
1246 for c in cipher.clone()
1247 { print!("{:02X} ", c); }
1248 println!();
1249 let mut txt = String::new();
1250 for c in cipher.clone()
1251 { write!(txt, "{:02X} ", c); }
1252 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
1253 println!("-------------------------------");
1254}
1255
1256fn aes_encrypt_vec_ofb()
1257{
1258 println!("aes_encrypt_vec_ofb()");
1259 use std::io::Write;
1260 use std::fmt::Write as _;
1261 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
1262
1263 // Normal case for AES-128
1264 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1265 println!("K =\t{:#016X}", key);
1266 let mut a_aes = AES_128::new_with_key_u128(key);
1267 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1268 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1269
1270 let message = "In the beginning God created the heavens and the earth.";
1271 println!("M =\t{}", message);
1272 let message = unsafe { message.to_string().as_mut_vec().clone() };
1273 let mut cipher = [0_u8; 55];
1274 a_aes.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1275 print!("C =\t");
1276 for c in cipher.clone()
1277 { print!("{:02X} ", c); }
1278 println!();
1279 let mut txt = String::new();
1280 for c in cipher.clone()
1281 { write!(txt, "{:02X} ", c); }
1282 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1283 println!();
1284
1285 // Normal case for AES-192
1286 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1287 print!("K =\t");
1288 for i in 0..24
1289 { print!("{:02X}", key[i]); }
1290 println!();
1291 let mut a_aes = AES_192::new_with_key(&key);
1292 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1293 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1294
1295 let message = "In the beginning God created the heavens and the earth.";
1296 println!("M =\t{}", message);
1297 let message = unsafe { message.to_string().as_mut_vec().clone() };
1298 let mut cipher = [0_u8; 55];
1299 a_aes.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1300 print!("C =\t");
1301 for c in cipher.clone()
1302 { print!("{:02X} ", c); }
1303 println!();
1304 let mut txt = String::new();
1305 for c in cipher.clone()
1306 { write!(txt, "{:02X} ", c); }
1307 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
1308 println!();
1309
1310 // Normal case for AES-256
1311 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1312 print!("K =\t");
1313 for i in 0..32
1314 { print!("{:02X}", key[i]); }
1315 println!();
1316 let mut a_aes = AES_256::new_with_key(&key);
1317 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1318 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1319
1320 let message = "In the beginning God created the heavens and the earth.";
1321 println!("M =\t{}", message);
1322 let message = unsafe { message.to_string().as_mut_vec().clone() };
1323 let mut cipher = [0_u8; 55];
1324 a_aes.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1325 print!("C =\t");
1326 for c in cipher.clone()
1327 { print!("{:02X} ", c); }
1328 println!();
1329 let mut txt = String::new();
1330 for c in cipher.clone()
1331 { write!(txt, "{:02X} ", c); }
1332 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
1333 println!();
1334
1335 // Normal case for Rijndael-256-256
1336 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1337 print!("K =\t");
1338 for i in 0..32
1339 { print!("{:02X}", key[i]); }
1340 println!();
1341 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
1342 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1343 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
1344
1345 let message = "In the beginning God created the heavens and the earth.";
1346 println!("M =\t{}", message);
1347 let message = unsafe { message.to_string().as_mut_vec().clone() };
1348 let mut cipher = [0_u8; 55];
1349 a_rijndael.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1350 print!("C =\t");
1351 for c in cipher.clone()
1352 { print!("{:02X} ", c); }
1353 println!();
1354 let mut txt = String::new();
1355 for c in cipher.clone()
1356 { write!(txt, "{:02X} ", c); }
1357 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
1358 println!();
1359
1360 // Normal case for Rijndael-512-512 for post-quantum
1361 use cryptocol::number::SharedArrays;
1362 use cryptocol::hash::SHA3_512;
1363 let mut sha3 = SHA3_512::new();
1364 sha3.absorb_str("Post-quantum");
1365 let key: [u8; 64] = sha3.get_hash_value_in_array();
1366 print!("K =\t");
1367 for i in 0..64
1368 { print!("{:02X}", key[i]); }
1369 println!();
1370 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
1371 sha3.absorb_str("Initialize");
1372 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
1373 iv.src = sha3.get_hash_value_in_array();
1374 let iv = unsafe { iv.des };
1375 print!("IV =\t");
1376 for i in 0..16
1377 { print!("{:08X}", iv[i].to_be()); }
1378 println!();
1379 let message = "In the beginning God created the heavens and the earth.";
1380 println!("M =\t{}", message);
1381 let message = unsafe { message.to_string().as_mut_vec().clone() };
1382 let mut cipher = [0_u8; 55];
1383 a_rijndael.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1384 print!("C =\t");
1385 for c in cipher.clone()
1386 { print!("{:02X} ", c); }
1387 println!();
1388 let mut txt = String::new();
1389 for c in cipher.clone()
1390 { write!(txt, "{:02X} ", c); }
1391 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
1392 println!("-------------------------------");
1393}
1394
1395fn aes_encrypt_vec_ofb_into_vec()
1396{
1397 println!("aes_encrypt_vec_ofb_into_vec()");
1398 use std::io::Write;
1399 use std::fmt::Write as _;
1400 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
1401
1402 // Normal case for AES-128
1403 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1404 println!("K =\t{:#016X}", key);
1405 let mut a_aes = AES_128::new_with_key_u128(key);
1406 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1407 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1408
1409 let message = "In the beginning God created the heavens and the earth.";
1410 println!("M =\t{}", message);
1411 let message = unsafe { message.to_string().as_mut_vec().clone() };
1412 let mut cipher = Vec::<u8>::new();
1413 a_aes.encrypt_vec_into_vec(iv, &message, &mut cipher);
1414 print!("C =\t");
1415 for c in cipher.clone()
1416 { print!("{:02X} ", c); }
1417 println!();
1418 let mut txt = String::new();
1419 for c in cipher.clone()
1420 { write!(txt, "{:02X} ", c); }
1421 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1422 println!();
1423
1424 // Normal case for AES-192
1425 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1426 print!("K =\t");
1427 for i in 0..24
1428 { print!("{:02X}", key[i]); }
1429 println!();
1430 let mut a_aes = AES_192::new_with_key(&key);
1431 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1432 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1433
1434 let message = "In the beginning God created the heavens and the earth.";
1435 println!("M =\t{}", message);
1436 let message = unsafe { message.to_string().as_mut_vec().clone() };
1437 let mut cipher = Vec::<u8>::new();
1438 a_aes.encrypt_vec_into_vec(iv, &message, &mut cipher);
1439 print!("C =\t");
1440 for c in cipher.clone()
1441 { print!("{:02X} ", c); }
1442 println!();
1443 let mut txt = String::new();
1444 for c in cipher.clone()
1445 { write!(txt, "{:02X} ", c); }
1446 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
1447 println!();
1448
1449 // Normal case for AES-256
1450 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1451 print!("K =\t");
1452 for i in 0..32
1453 { print!("{:02X}", key[i]); }
1454 println!();
1455 let mut a_aes = AES_256::new_with_key(&key);
1456 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1457 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1458
1459 let message = "In the beginning God created the heavens and the earth.";
1460 println!("M =\t{}", message);
1461 let message = unsafe { message.to_string().as_mut_vec().clone() };
1462 let mut cipher = Vec::<u8>::new();
1463 a_aes.encrypt_vec_into_vec(iv, &message, &mut cipher);
1464 print!("C =\t");
1465 for c in cipher.clone()
1466 { print!("{:02X} ", c); }
1467 println!();
1468 let mut txt = String::new();
1469 for c in cipher.clone()
1470 { write!(txt, "{:02X} ", c); }
1471 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
1472 println!();
1473
1474 // Normal case for Rijndael-256-256
1475 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1476 print!("K =\t");
1477 for i in 0..32
1478 { print!("{:02X}", key[i]); }
1479 println!();
1480 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
1481 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1482 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
1483
1484 let message = "In the beginning God created the heavens and the earth.";
1485 println!("M =\t{}", message);
1486 let message = unsafe { message.to_string().as_mut_vec().clone() };
1487 let mut cipher = Vec::<u8>::new();
1488 a_rijndael.encrypt_vec_into_vec(iv, &message, &mut cipher);
1489 print!("C =\t");
1490 for c in cipher.clone()
1491 { print!("{:02X} ", c); }
1492 println!();
1493 let mut txt = String::new();
1494 for c in cipher.clone()
1495 { write!(txt, "{:02X} ", c); }
1496 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
1497 println!();
1498
1499 // Normal case for Rijndael-512-512 for post-quantum
1500 use cryptocol::number::SharedArrays;
1501 use cryptocol::hash::SHA3_512;
1502 let mut sha3 = SHA3_512::new();
1503 sha3.absorb_str("Post-quantum");
1504 let key: [u8; 64] = sha3.get_hash_value_in_array();
1505 print!("K =\t");
1506 for i in 0..64
1507 { print!("{:02X}", key[i]); }
1508 println!();
1509 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
1510 sha3.absorb_str("Initialize");
1511 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
1512 iv.src = sha3.get_hash_value_in_array();
1513 let iv = unsafe { iv.des };
1514 print!("IV =\t");
1515 for i in 0..16
1516 { print!("{:08X}", iv[i].to_be()); }
1517 println!();
1518 let message = "In the beginning God created the heavens and the earth.";
1519 println!("M =\t{}", message);
1520 let message = unsafe { message.to_string().as_mut_vec().clone() };
1521 let mut cipher = Vec::<u8>::new();
1522 a_rijndael.encrypt_vec_into_vec(iv, &message, &mut cipher);
1523 print!("C =\t");
1524 for c in cipher.clone()
1525 { print!("{:02X} ", c); }
1526 println!();
1527 let mut txt = String::new();
1528 for c in cipher.clone()
1529 { write!(txt, "{:02X} ", c); }
1530 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
1531 println!("-------------------------------");
1532}
1533
1534fn aes_encrypt_vec_ofb_into_array()
1535{
1536 println!("aes_encrypt_vec_ofb_into_array()");
1537 use std::io::Write;
1538 use std::fmt::Write as _;
1539 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
1540
1541 // Normal case for AES-128
1542 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1543 println!("K =\t{:#016X}", key);
1544 let mut a_aes = AES_128::new_with_key_u128(key);
1545 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1546 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1547
1548 let message = "In the beginning God created the heavens and the earth.";
1549 println!("M =\t{}", message);
1550 let message = unsafe { message.to_string().as_mut_vec().clone() };
1551 let mut cipher = [0_u8; 55];
1552 a_aes.encrypt_vec_into_array(iv, &message, &mut cipher);
1553 print!("C =\t");
1554 for c in cipher.clone()
1555 { print!("{:02X} ", c); }
1556 println!();
1557 let mut txt = String::new();
1558 for c in cipher.clone()
1559 { write!(txt, "{:02X} ", c); }
1560 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1561 println!();
1562
1563 // Normal case for AES-192
1564 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1565 print!("K =\t");
1566 for i in 0..24
1567 { print!("{:02X}", key[i]); }
1568 println!();
1569 let mut a_aes = AES_192::new_with_key(&key);
1570 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1571 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1572
1573 let message = "In the beginning God created the heavens and the earth.";
1574 println!("M =\t{}", message);
1575 let message = unsafe { message.to_string().as_mut_vec().clone() };
1576 let mut cipher = [0_u8; 55];
1577 a_aes.encrypt_vec_into_array(iv, &message, &mut cipher);
1578 print!("C =\t");
1579 for c in cipher.clone()
1580 { print!("{:02X} ", c); }
1581 println!();
1582 let mut txt = String::new();
1583 for c in cipher.clone()
1584 { write!(txt, "{:02X} ", c); }
1585 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
1586 println!();
1587
1588 // Normal case for AES-256
1589 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1590 print!("K =\t");
1591 for i in 0..32
1592 { print!("{:02X}", key[i]); }
1593 println!();
1594 let mut a_aes = AES_256::new_with_key(&key);
1595 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1596 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1597
1598 let message = "In the beginning God created the heavens and the earth.";
1599 println!("M =\t{}", message);
1600 let message = unsafe { message.to_string().as_mut_vec().clone() };
1601 let mut cipher = [0_u8; 55];
1602 a_aes.encrypt_vec_into_array(iv, &message, &mut cipher);
1603 print!("C =\t");
1604 for c in cipher.clone()
1605 { print!("{:02X} ", c); }
1606 println!();
1607 let mut txt = String::new();
1608 for c in cipher.clone()
1609 { write!(txt, "{:02X} ", c); }
1610 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
1611 println!();
1612
1613 // Normal case for Rijndael-256-256
1614 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1615 print!("K =\t");
1616 for i in 0..32
1617 { print!("{:02X}", key[i]); }
1618 println!();
1619 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
1620 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1621 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
1622
1623 let message = "In the beginning God created the heavens and the earth.";
1624 println!("M =\t{}", message);
1625 let message = unsafe { message.to_string().as_mut_vec().clone() };
1626 let mut cipher = [0_u8; 55];
1627 a_rijndael.encrypt_vec_into_array(iv, &message, &mut cipher);
1628 print!("C =\t");
1629 for c in cipher.clone()
1630 { print!("{:02X} ", c); }
1631 println!();
1632 let mut txt = String::new();
1633 for c in cipher.clone()
1634 { write!(txt, "{:02X} ", c); }
1635 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
1636 println!();
1637
1638 // Normal case for Rijndael-512-512 for post-quantum
1639 use cryptocol::number::SharedArrays;
1640 use cryptocol::hash::SHA3_512;
1641 let mut sha3 = SHA3_512::new();
1642 sha3.absorb_str("Post-quantum");
1643 let key: [u8; 64] = sha3.get_hash_value_in_array();
1644 print!("K =\t");
1645 for i in 0..64
1646 { print!("{:02X}", key[i]); }
1647 println!();
1648 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
1649 sha3.absorb_str("Initialize");
1650 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
1651 iv.src = sha3.get_hash_value_in_array();
1652 let iv = unsafe { iv.des };
1653 print!("IV =\t");
1654 for i in 0..16
1655 { print!("{:08X}", iv[i].to_be()); }
1656 println!();
1657 let message = "In the beginning God created the heavens and the earth.";
1658 println!("M =\t{}", message);
1659 let message = unsafe { message.to_string().as_mut_vec().clone() };
1660 let mut cipher = [0_u8; 55];
1661 a_rijndael.encrypt_vec_into_array(iv, &message, &mut cipher);
1662 print!("C =\t");
1663 for c in cipher.clone()
1664 { print!("{:02X} ", c); }
1665 println!();
1666 let mut txt = String::new();
1667 for c in cipher.clone()
1668 { write!(txt, "{:02X} ", c); }
1669 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
1670 println!("-------------------------------");
1671}
1672
1673fn aes_encrypt_array_ofb()
1674{
1675 println!("aes_encrypt_array_ofb()");
1676 use std::io::Write;
1677 use std::fmt::Write as _;
1678 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
1679
1680 // Normal case for AES-128
1681 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1682 println!("K =\t{:#016X}", key);
1683 let mut a_aes = AES_128::new_with_key_u128(key);
1684 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1685 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1686
1687 let mes = "In the beginning God created the heavens and the earth.";
1688 println!("M =\t{}", mes);
1689 let mut message = [0_u8; 55];
1690 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1691 let mut cipher = [0_u8; 55];
1692 a_aes.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
1693 print!("C =\t");
1694 for c in cipher.clone()
1695 { print!("{:02X} ", c); }
1696 println!();
1697 let mut txt = String::new();
1698 for c in cipher.clone()
1699 { write!(txt, "{:02X} ", c); }
1700 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1701 println!();
1702
1703 // Normal case for AES-192
1704 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1705 print!("K =\t");
1706 for i in 0..24
1707 { print!("{:02X}", key[i]); }
1708 println!();
1709 let mut a_aes = AES_192::new_with_key(&key);
1710 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1711 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1712
1713 let mes = "In the beginning God created the heavens and the earth.";
1714 println!("M =\t{}", mes);
1715 let mut message = [0_u8; 55];
1716 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1717 let mut cipher = [0_u8; 55];
1718 a_aes.encrypt(iv, message.as_ptr(), message.len() as u64, 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, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
1727 println!();
1728
1729 // Normal case for AES-256
1730 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1731 print!("K =\t");
1732 for i in 0..32
1733 { print!("{:02X}", key[i]); }
1734 println!();
1735 let mut a_aes = AES_256::new_with_key(&key);
1736 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1737 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1738
1739 let mes = "In the beginning God created the heavens and the earth.";
1740 println!("M =\t{}", mes);
1741 let mut message = [0_u8; 55];
1742 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1743 let mut cipher = [0_u8; 55];
1744 a_aes.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
1745 print!("C =\t");
1746 for c in cipher.clone()
1747 { print!("{:02X} ", c); }
1748 println!();
1749 let mut txt = String::new();
1750 for c in cipher.clone()
1751 { write!(txt, "{:02X} ", c); }
1752 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
1753 println!();
1754
1755 // Normal case for Rijndael-256-256
1756 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1757 print!("K =\t");
1758 for i in 0..32
1759 { print!("{:02X}", key[i]); }
1760 println!();
1761 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
1762 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1763 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
1764
1765 let mes = "In the beginning God created the heavens and the earth.";
1766 println!("M =\t{}", mes);
1767 let mut message = [0_u8; 55];
1768 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1769 let mut cipher = [0_u8; 55];
1770 a_rijndael.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
1771 print!("C =\t");
1772 for c in cipher.clone()
1773 { print!("{:02X} ", c); }
1774 println!();
1775 let mut txt = String::new();
1776 for c in cipher.clone()
1777 { write!(txt, "{:02X} ", c); }
1778 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
1779 println!();
1780
1781 // Normal case for Rijndael-512-512 for post-quantum
1782 use cryptocol::number::SharedArrays;
1783 use cryptocol::hash::SHA3_512;
1784 let mut sha3 = SHA3_512::new();
1785 sha3.absorb_str("Post-quantum");
1786 let key: [u8; 64] = sha3.get_hash_value_in_array();
1787 print!("K =\t");
1788 for i in 0..64
1789 { print!("{:02X}", key[i]); }
1790 println!();
1791 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
1792 sha3.absorb_str("Initialize");
1793 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
1794 iv.src = sha3.get_hash_value_in_array();
1795 let iv = unsafe { iv.des };
1796 print!("IV =\t");
1797 for i in 0..16
1798 { print!("{:08X}", iv[i].to_be()); }
1799 println!();
1800 let mes = "In the beginning God created the heavens and the earth.";
1801 println!("M =\t{}", mes);
1802 let mut message = [0_u8; 55];
1803 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1804 let mut cipher = [0_u8; 55];
1805 a_rijndael.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
1806 print!("C =\t");
1807 for c in cipher.clone()
1808 { print!("{:02X} ", c); }
1809 println!();
1810 let mut txt = String::new();
1811 for c in cipher.clone()
1812 { write!(txt, "{:02X} ", c); }
1813 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
1814 println!("-------------------------------");
1815}
1816
1817fn aes_encrypt_array_ofb_into_vec()
1818{
1819 println!("aes_encrypt_array_ofb_into_vec()");
1820 use std::io::Write;
1821 use std::fmt::Write as _;
1822 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
1823
1824 // Normal case for AES-128
1825 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1826 println!("K =\t{:#016X}", key);
1827 let mut a_aes = AES_128::new_with_key_u128(key);
1828 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1829 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1830
1831 let mes = "In the beginning God created the heavens and the earth.";
1832 println!("M =\t{}", mes);
1833 let mut message = [0_u8; 55];
1834 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1835 let mut cipher = Vec::<u8>::new();
1836 a_aes.encrypt_array_into_vec(iv, &message, &mut cipher);
1837 print!("C =\t");
1838 for c in cipher.clone()
1839 { print!("{:02X} ", c); }
1840 println!();
1841 let mut txt = String::new();
1842 for c in cipher.clone()
1843 { write!(txt, "{:02X} ", c); }
1844 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1845 println!();
1846
1847 // Normal case for AES-192
1848 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1849 print!("K =\t");
1850 for i in 0..24
1851 { print!("{:02X}", key[i]); }
1852 println!();
1853 let mut a_aes = AES_192::new_with_key(&key);
1854 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1855 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1856
1857 let mes = "In the beginning God created the heavens and the earth.";
1858 println!("M =\t{}", mes);
1859 let mut message = [0_u8; 55];
1860 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1861 let mut cipher = Vec::<u8>::new();
1862 a_aes.encrypt_array_into_vec(iv, &message, &mut cipher);
1863 print!("C =\t");
1864 for c in cipher.clone()
1865 { print!("{:02X} ", c); }
1866 println!();
1867 let mut txt = String::new();
1868 for c in cipher.clone()
1869 { write!(txt, "{:02X} ", c); }
1870 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
1871 println!();
1872
1873 // Normal case for AES-256
1874 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1875 print!("K =\t");
1876 for i in 0..32
1877 { print!("{:02X}", key[i]); }
1878 println!();
1879 let mut a_aes = AES_256::new_with_key(&key);
1880 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1881 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1882
1883 let mes = "In the beginning God created the heavens and the earth.";
1884 println!("M =\t{}", mes);
1885 let mut message = [0_u8; 55];
1886 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1887 let mut cipher = Vec::<u8>::new();
1888 a_aes.encrypt_array_into_vec(iv, &message, &mut cipher);
1889 print!("C =\t");
1890 for c in cipher.clone()
1891 { print!("{:02X} ", c); }
1892 println!();
1893 let mut txt = String::new();
1894 for c in cipher.clone()
1895 { write!(txt, "{:02X} ", c); }
1896 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
1897 println!();
1898
1899 // Normal case for Rijndael-256-256
1900 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1901 print!("K =\t");
1902 for i in 0..32
1903 { print!("{:02X}", key[i]); }
1904 println!();
1905 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
1906 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1907 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
1908
1909 let mes = "In the beginning God created the heavens and the earth.";
1910 println!("M =\t{}", mes);
1911 let mut message = [0_u8; 55];
1912 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1913 let mut cipher = Vec::<u8>::new();
1914 a_rijndael.encrypt_array_into_vec(iv, &message, &mut cipher);
1915 print!("C =\t");
1916 for c in cipher.clone()
1917 { print!("{:02X} ", c); }
1918 println!();
1919 let mut txt = String::new();
1920 for c in cipher.clone()
1921 { write!(txt, "{:02X} ", c); }
1922 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
1923 println!();
1924
1925 // Normal case for Rijndael-512-512 for post-quantum
1926 use cryptocol::number::SharedArrays;
1927 use cryptocol::hash::SHA3_512;
1928 let mut sha3 = SHA3_512::new();
1929 sha3.absorb_str("Post-quantum");
1930 let key: [u8; 64] = sha3.get_hash_value_in_array();
1931 print!("K =\t");
1932 for i in 0..64
1933 { print!("{:02X}", key[i]); }
1934 println!();
1935 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
1936 sha3.absorb_str("Initialize");
1937 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
1938 iv.src = sha3.get_hash_value_in_array();
1939 let iv = unsafe { iv.des };
1940 print!("IV =\t");
1941 for i in 0..16
1942 { print!("{:08X}", iv[i].to_be()); }
1943 println!();
1944 let mes = "In the beginning God created the heavens and the earth.";
1945 println!("M =\t{}", mes);
1946 let mut message = [0_u8; 55];
1947 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1948 let mut cipher = Vec::<u8>::new();
1949 a_rijndael.encrypt_array_into_vec(iv, &message, &mut cipher);
1950 print!("C =\t");
1951 for c in cipher.clone()
1952 { print!("{:02X} ", c); }
1953 println!();
1954 let mut txt = String::new();
1955 for c in cipher.clone()
1956 { write!(txt, "{:02X} ", c); }
1957 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
1958 println!("-------------------------------");
1959}
1960
1961fn aes_encrypt_array_ofb_into_array()
1962{
1963 println!("aes_encrypt_array_ofb_into_array()");
1964 use std::io::Write;
1965 use std::fmt::Write as _;
1966 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
1967
1968 // Normal case for AES-128
1969 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1970 println!("K =\t{:#016X}", key);
1971 let mut a_aes = AES_128::new_with_key_u128(key);
1972 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1973 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1974
1975 let mes = "In the beginning God created the heavens and the earth.";
1976 println!("M =\t{}", mes);
1977 let mut message = [0_u8; 55];
1978 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1979 let mut cipher = [0_u8; 55];
1980 a_aes.encrypt_array_into_array(iv, &message, &mut cipher);
1981 print!("C =\t");
1982 for c in cipher.clone()
1983 { print!("{:02X} ", c); }
1984 println!();
1985 let mut txt = String::new();
1986 for c in cipher.clone()
1987 { write!(txt, "{:02X} ", c); }
1988 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1989 println!();
1990
1991 // Normal case for AES-192
1992 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1993 print!("K =\t");
1994 for i in 0..24
1995 { print!("{:02X}", key[i]); }
1996 println!();
1997 let mut a_aes = AES_192::new_with_key(&key);
1998 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1999 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2000
2001 let mes = "In the beginning God created the heavens and the earth.";
2002 println!("M =\t{}", mes);
2003 let mut message = [0_u8; 55];
2004 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2005 let mut cipher = [0_u8; 55];
2006 a_aes.encrypt_array_into_array(iv, &message, &mut cipher);
2007 print!("C =\t");
2008 for c in cipher.clone()
2009 { print!("{:02X} ", c); }
2010 println!();
2011 let mut txt = String::new();
2012 for c in cipher.clone()
2013 { write!(txt, "{:02X} ", c); }
2014 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
2015 println!();
2016
2017 // Normal case for AES-256
2018 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2019 print!("K =\t");
2020 for i in 0..32
2021 { print!("{:02X}", key[i]); }
2022 println!();
2023 let mut a_aes = AES_256::new_with_key(&key);
2024 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2025 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2026
2027 let mes = "In the beginning God created the heavens and the earth.";
2028 println!("M =\t{}", mes);
2029 let mut message = [0_u8; 55];
2030 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2031 let mut cipher = [0_u8; 55];
2032 a_aes.encrypt_array_into_array(iv, &message, &mut cipher);
2033 print!("C =\t");
2034 for c in cipher.clone()
2035 { print!("{:02X} ", c); }
2036 println!();
2037 let mut txt = String::new();
2038 for c in cipher.clone()
2039 { write!(txt, "{:02X} ", c); }
2040 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
2041 println!();
2042
2043 // Normal case for Rijndael-256-256
2044 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2045 print!("K =\t");
2046 for i in 0..32
2047 { print!("{:02X}", key[i]); }
2048 println!();
2049 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
2050 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2051 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
2052
2053 let mes = "In the beginning God created the heavens and the earth.";
2054 println!("M =\t{}", mes);
2055 let mut message = [0_u8; 55];
2056 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2057 let mut cipher = [0_u8; 55];
2058 a_rijndael.encrypt_array_into_array(iv, &message, &mut cipher);
2059 print!("C =\t");
2060 for c in cipher.clone()
2061 { print!("{:02X} ", c); }
2062 println!();
2063 let mut txt = String::new();
2064 for c in cipher.clone()
2065 { write!(txt, "{:02X} ", c); }
2066 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
2067 println!();
2068
2069 // Normal case for Rijndael-512-512 for post-quantum
2070 use cryptocol::number::SharedArrays;
2071 use cryptocol::hash::SHA3_512;
2072 let mut sha3 = SHA3_512::new();
2073 sha3.absorb_str("Post-quantum");
2074 let key: [u8; 64] = sha3.get_hash_value_in_array();
2075 print!("K =\t");
2076 for i in 0..64
2077 { print!("{:02X}", key[i]); }
2078 println!();
2079 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
2080 sha3.absorb_str("Initialize");
2081 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
2082 iv.src = sha3.get_hash_value_in_array();
2083 let iv = unsafe { iv.des };
2084 print!("IV =\t");
2085 for i in 0..16
2086 { print!("{:08X}", iv[i].to_be()); }
2087 println!();
2088 let mes = "In the beginning God created the heavens and the earth.";
2089 println!("M =\t{}", mes);
2090 let mut message = [0_u8; 55];
2091 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2092 let mut cipher = [0_u8; 55];
2093 a_rijndael.encrypt_array_into_array(iv, &message, &mut cipher);
2094 print!("C =\t");
2095 for c in cipher.clone()
2096 { print!("{:02X} ", c); }
2097 println!();
2098 let mut txt = String::new();
2099 for c in cipher.clone()
2100 { write!(txt, "{:02X} ", c); }
2101 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
2102 println!("-------------------------------");
2103}
2104
2105
2106fn aes_decrypt_ofb()
2107{
2108 println!("aes_decrypt_ofb");
2109 use std::io::Write;
2110 use std::fmt::Write as _;
2111 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
2112
2113 // Normal case for AES-128
2114 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
2115 println!("K =\t{:#016X}", key);
2116 let mut a_aes = AES_128::new_with_key_u128(key);
2117 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2118 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0], iv[1], iv[2], iv[3]);
2119
2120 let message = "In the beginning God created the heavens and the earth.";
2121 println!("M =\t{}", message);
2122 let mut cipher = [0_u8; 55];
2123 a_aes.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
2132
2133 let mut recovered = vec![0; 55];
2134 a_aes.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2135 print!("Ba =\t");
2136 for b in recovered.clone()
2137 { print!("{:02X} ", b); }
2138 println!();
2139 let mut txt = String::new();
2140 for c in recovered.clone()
2141 { write!(txt, "{:02X} ", c); }
2142 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 ");
2143
2144 let mut converted = String::new();
2145 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2146
2147 println!("Bb =\t{}", converted);
2148 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2149 assert_eq!(converted, message);
2150 println!();
2151
2152 // Normal case for AES-192
2153 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2154 print!("K =\t");
2155 for i in 0..24
2156 { print!("{:02X}", key[i]); }
2157 println!();
2158 let mut a_aes = AES_192::new_with_key(&key);
2159 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2160 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2161
2162 let message = "In the beginning God created the heavens and the earth.";
2163 println!("M =\t{}", message);
2164 let mut cipher = [0_u8; 55];
2165 a_aes.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
2166 print!("C =\t");
2167 for c in cipher.clone()
2168 { print!("{:02X} ", c); }
2169 println!();
2170 let mut txt = String::new();
2171 for c in cipher.clone()
2172 { write!(txt, "{:02X} ", c); }
2173 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
2174
2175 let mut recovered = vec![0; 55];
2176 a_aes.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2177 print!("Ba =\t");
2178 for b in recovered.clone()
2179 { print!("{:02X} ", b); }
2180 println!();
2181 let mut txt = String::new();
2182 for c in recovered.clone()
2183 { write!(txt, "{:02X} ", c); }
2184 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 ");
2185
2186 let mut converted = String::new();
2187 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2188
2189 println!("Bb =\t{}", converted);
2190 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2191 assert_eq!(converted, message);
2192 println!();
2193
2194 // Normal case for AES-256
2195 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2196 print!("K =\t");
2197 for i in 0..32
2198 { print!("{:02X}", key[i]); }
2199 println!();
2200 let mut a_aes = AES_256::new_with_key(&key);
2201 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2202 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2203
2204 let message = "In the beginning God created the heavens and the earth.";
2205 println!("M =\t{}", message);
2206 let mut cipher = [0_u8; 55];
2207 a_aes.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
2208 print!("C =\t");
2209 for c in cipher.clone()
2210 { print!("{:02X} ", c); }
2211 println!();
2212 let mut txt = String::new();
2213 for c in cipher.clone()
2214 { write!(txt, "{:02X} ", c); }
2215 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
2216
2217 let mut recovered = vec![0; 55];
2218 a_aes.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2219 print!("Ba =\t");
2220 for b in recovered.clone()
2221 { print!("{:02X} ", b); }
2222 println!();
2223 let mut txt = String::new();
2224 for c in recovered.clone()
2225 { write!(txt, "{:02X} ", c); }
2226 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 ");
2227
2228 let mut converted = String::new();
2229 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2230
2231 println!("Bb =\t{}", converted);
2232 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2233 assert_eq!(converted, message);
2234 println!();
2235
2236 // Normal case for Rijndael-256-256
2237 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2238 print!("K =\t");
2239 for i in 0..32
2240 { print!("{:02X}", key[i]); }
2241 println!();
2242 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
2243 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2244 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
2245
2246 let message = "In the beginning God created the heavens and the earth.";
2247 println!("M =\t{}", message);
2248 let mut cipher = [0_u8; 55];
2249 a_rijndael.encrypt(iv, message.as_ptr(), message.len() as u64, 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, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
2258
2259 let mut recovered = vec![0; 55];
2260 a_rijndael.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2261 print!("Ba =\t");
2262 for b in recovered.clone()
2263 { print!("{:02X} ", b); }
2264 println!();
2265 let mut txt = String::new();
2266 for c in recovered.clone()
2267 { write!(txt, "{:02X} ", c); }
2268 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 ");
2269
2270 let mut converted = String::new();
2271 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2272
2273 println!("Bb =\t{}", converted);
2274 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2275 assert_eq!(converted, message);
2276 println!();
2277
2278 // Normal case for Rijndael-512-512 for post-quantum
2279 use cryptocol::number::SharedArrays;
2280 use cryptocol::hash::SHA3_512;
2281 let mut sha3 = SHA3_512::new();
2282 sha3.absorb_str("Post-quantum");
2283 let key: [u8; 64] = sha3.get_hash_value_in_array();
2284 print!("K =\t");
2285 for i in 0..64
2286 { print!("{:02X}", key[i]); }
2287 println!();
2288 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
2289 sha3.absorb_str("Initialize");
2290 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
2291 iv.src = sha3.get_hash_value_in_array();
2292 let iv = unsafe { iv.des };
2293 print!("IV =\t");
2294 for i in 0..16
2295 { print!("{:08X}", iv[i].to_be()); }
2296 println!();
2297 let message = "In the beginning God created the heavens and the earth.";
2298 println!("M =\t{}", message);
2299 let mut cipher = [0_u8; 55];
2300 a_rijndael.encrypt(iv.clone(), message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
2301 print!("C =\t");
2302 for c in cipher.clone()
2303 { print!("{:02X} ", c); }
2304 println!();
2305 let mut txt = String::new();
2306 for c in cipher.clone()
2307 { write!(txt, "{:02X} ", c); }
2308 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
2309
2310 let mut recovered = vec![0; 55];
2311 a_rijndael.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2312 print!("Ba =\t");
2313 for b in recovered.clone()
2314 { print!("{:02X} ", b); }
2315 println!();
2316 let mut txt = String::new();
2317 for c in recovered.clone()
2318 { write!(txt, "{:02X} ", c); }
2319 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 ");
2320
2321 let mut converted = String::new();
2322 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2323
2324 println!("Bb =\t{}", converted);
2325 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2326 assert_eq!(converted, message);
2327 println!("-------------------------------");
2328}
2329
2330
2331fn aes_decrypt_ofb_into_vec()
2332{
2333 println!("aes_decrypt_ofb_into_vec()");
2334 use std::io::Write;
2335 use std::fmt::Write as _;
2336 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
2337
2338 // Normal case for AES-128
2339 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
2340 println!("K =\t{:#016X}", key);
2341 let mut a_aes = AES_128::new_with_key_u128(key);
2342 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2343 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2344
2345 let message = "In the beginning God created the heavens and the earth.";
2346 println!("M =\t{}", message);
2347 let mut cipher = [0_u8; 55];
2348 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2349 print!("C =\t");
2350 for c in cipher.clone()
2351 { print!("{:02X} ", c); }
2352 println!();
2353 let mut txt = String::new();
2354 for c in cipher.clone()
2355 { write!(txt, "{:02X} ", c); }
2356 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
2357 println!();
2358
2359 let mut recovered = Vec::<u8>::new();
2360 a_aes.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2361 print!("Ba =\t");
2362 for b in recovered.clone()
2363 { print!("{:02X} ", b); }
2364 println!();
2365 let mut txt = String::new();
2366 for c in recovered.clone()
2367 { write!(txt, "{:02X} ", c); }
2368 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 ");
2369
2370 let mut converted = String::new();
2371 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2372
2373 println!("Bb =\t{}", converted);
2374 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2375 assert_eq!(converted, message);
2376 println!();
2377
2378 // Normal case for AES-192
2379 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2380 print!("K =\t");
2381 for i in 0..24
2382 { print!("{:02X}", key[i]); }
2383 println!();
2384 let mut a_aes = AES_192::new_with_key(&key);
2385 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2386 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2387
2388 let message = "In the beginning God created the heavens and the earth.";
2389 println!("M =\t{}", message);
2390 let mut cipher = [0_u8; 55];
2391 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2392 print!("C =\t");
2393 for c in cipher.clone()
2394 { print!("{:02X} ", c); }
2395 println!();
2396 let mut txt = String::new();
2397 for c in cipher.clone()
2398 { write!(txt, "{:02X} ", c); }
2399 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
2400 println!();
2401
2402 let mut recovered = Vec::<u8>::new();
2403 a_aes.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2404 print!("Ba =\t");
2405 for b in recovered.clone()
2406 { print!("{:02X} ", b); }
2407 println!();
2408 let mut txt = String::new();
2409 for c in recovered.clone()
2410 { write!(txt, "{:02X} ", c); }
2411 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 ");
2412
2413 let mut converted = String::new();
2414 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2415
2416 println!("Bb =\t{}", converted);
2417 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2418 assert_eq!(converted, message);
2419 println!();
2420
2421 // Normal case for AES-256
2422 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2423 print!("K =\t");
2424 for i in 0..32
2425 { print!("{:02X}", key[i]); }
2426 println!();
2427 let mut a_aes = AES_256::new_with_key(&key);
2428 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2429 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2430
2431 let message = "In the beginning God created the heavens and the earth.";
2432 println!("M =\t{}", message);
2433 let mut cipher = [0_u8; 55];
2434 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2435 print!("C =\t");
2436 for c in cipher.clone()
2437 { print!("{:02X} ", c); }
2438 println!();
2439 let mut txt = String::new();
2440 for c in cipher.clone()
2441 { write!(txt, "{:02X} ", c); }
2442 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
2443 println!();
2444
2445 let mut recovered = Vec::<u8>::new();
2446 a_aes.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2447 print!("Ba =\t");
2448 for b in recovered.clone()
2449 { print!("{:02X} ", b); }
2450 println!();
2451 let mut txt = String::new();
2452 for c in recovered.clone()
2453 { write!(txt, "{:02X} ", c); }
2454 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 ");
2455
2456 let mut converted = String::new();
2457 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2458
2459 println!("Bb =\t{}", converted);
2460 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2461 assert_eq!(converted, message);
2462 println!();
2463
2464 // Normal case for Rijndael-256-256
2465 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2466 print!("K =\t");
2467 for i in 0..32
2468 { print!("{:02X}", key[i]); }
2469 println!();
2470 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
2471 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2472 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
2473
2474 let message = "In the beginning God created the heavens and the earth.";
2475 println!("M =\t{}", message);
2476 let mut cipher = [0_u8; 55];
2477 a_rijndael.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2478 print!("C =\t");
2479 for c in cipher.clone()
2480 { print!("{:02X} ", c); }
2481 println!();
2482 let mut txt = String::new();
2483 for c in cipher.clone()
2484 { write!(txt, "{:02X} ", c); }
2485 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
2486 println!();
2487
2488 let mut recovered = Vec::<u8>::new();
2489 a_rijndael.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2490 print!("Ba =\t");
2491 for b in recovered.clone()
2492 { print!("{:02X} ", b); }
2493 println!();
2494 let mut txt = String::new();
2495 for c in recovered.clone()
2496 { write!(txt, "{:02X} ", c); }
2497 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 ");
2498
2499 let mut converted = String::new();
2500 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2501
2502 println!("Bb =\t{}", converted);
2503 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2504 assert_eq!(converted, message);
2505 println!();
2506
2507 // Normal case for Rijndael-512-512 for post-quantum
2508 use cryptocol::number::SharedArrays;
2509 use cryptocol::hash::SHA3_512;
2510 let mut sha3 = SHA3_512::new();
2511 sha3.absorb_str("Post-quantum");
2512 let key: [u8; 64] = sha3.get_hash_value_in_array();
2513 print!("K =\t");
2514 for i in 0..64
2515 { print!("{:02X}", key[i]); }
2516 println!();
2517 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
2518 sha3.absorb_str("Initialize");
2519 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
2520 iv.src = sha3.get_hash_value_in_array();
2521 let iv = unsafe { iv.des };
2522 print!("IV =\t");
2523 for i in 0..16
2524 { print!("{:08X}", iv[i].to_be()); }
2525 println!();
2526 let message = "In the beginning God created the heavens and the earth.";
2527 println!("M =\t{}", message);
2528 let mut cipher = [0_u8; 55];
2529 a_rijndael.encrypt(iv.clone(), message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
2530 print!("C =\t");
2531 for c in cipher.clone()
2532 { print!("{:02X} ", c); }
2533 println!();
2534 let mut txt = String::new();
2535 for c in cipher.clone()
2536 { write!(txt, "{:02X} ", c); }
2537 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
2538
2539 let mut recovered = Vec::<u8>::new();
2540 a_rijndael.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2541 print!("Ba =\t");
2542 for b in recovered.clone()
2543 { print!("{:02X} ", b); }
2544 println!();
2545 let mut txt = String::new();
2546 for c in recovered.clone()
2547 { write!(txt, "{:02X} ", c); }
2548 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 ");
2549
2550 let mut converted = String::new();
2551 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2552
2553 println!("Bb =\t{}", converted);
2554 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2555 assert_eq!(converted, message);
2556 println!("-------------------------------");
2557}
2558
2559fn aes_decrypt_ofb_into_array()
2560{
2561 println!("aes_decrypt_ofb_into_array()");
2562 use std::io::Write;
2563 use std::fmt::Write as _;
2564 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
2565
2566 // Normal case for AES-128
2567 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
2568 println!("K =\t{:#016X}", key);
2569 let mut a_aes = AES_128::new_with_key_u128(key);
2570 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2571 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2572
2573 let message = "In the beginning God created the heavens and the earth.";
2574 println!("M =\t{}", message);
2575 let mut cipher = [0_u8; 55];
2576 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2577 print!("C =\t");
2578 for c in cipher.clone()
2579 { print!("{:02X} ", c); }
2580 println!();
2581 let mut txt = String::new();
2582 for c in cipher.clone()
2583 { write!(txt, "{:02X} ", c); }
2584 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
2585
2586 let mut recovered = [0; 64];
2587 let len = a_aes.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2588 print!("Ba =\t");
2589 for b in recovered.clone()
2590 { print!("{:02X} ", b); }
2591 println!();
2592 let mut txt = String::new();
2593 for c in recovered.clone()
2594 { write!(txt, "{:02X} ", c); }
2595 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
2596
2597 let mut converted = String::new();
2598 unsafe { converted.as_mut_vec() }.write(&recovered);
2599 unsafe { converted.as_mut_vec() }.truncate(len as usize);
2600
2601 println!("Bb =\t{}", converted);
2602 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2603 assert_eq!(converted, message);
2604 println!();
2605
2606 // Normal case for AES-192
2607 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2608 print!("K =\t");
2609 for i in 0..24
2610 { print!("{:02X}", key[i]); }
2611 println!();
2612 let mut a_aes = AES_192::new_with_key(&key);
2613 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2614 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2615
2616 let message = "In the beginning God created the heavens and the earth.";
2617 println!("M =\t{}", message);
2618 let mut cipher = [0_u8; 55];
2619 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2620 print!("C =\t");
2621 for c in cipher.clone()
2622 { print!("{:02X} ", c); }
2623 println!();
2624 let mut txt = String::new();
2625 for c in cipher.clone()
2626 { write!(txt, "{:02X} ", c); }
2627 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
2628
2629 let mut recovered = [0; 64];
2630 a_aes.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2631 print!("Ba =\t");
2632 for b in recovered.clone()
2633 { print!("{:02X} ", b); }
2634 println!();
2635 let mut txt = String::new();
2636
2637 for c in recovered.clone()
2638 { write!(txt, "{:02X} ", c); }
2639 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
2640
2641 let mut converted = String::new();
2642 unsafe { converted.as_mut_vec() }.write(&recovered);
2643 unsafe { converted.as_mut_vec() }.truncate(len as usize);
2644
2645 println!("Bb =\t{}", converted);
2646 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2647 assert_eq!(converted, message);
2648 println!();
2649
2650 // Normal case for AES-256
2651 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2652 print!("K =\t");
2653 for i in 0..32
2654 { print!("{:02X}", key[i]); }
2655 println!();
2656 let mut a_aes = AES_256::new_with_key(&key);
2657 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2658 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2659
2660 let message = "In the beginning God created the heavens and the earth.";
2661 println!("M =\t{}", message);
2662 let mut cipher = [0_u8; 55];
2663 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2664 print!("C =\t");
2665 for c in cipher.clone()
2666 { print!("{:02X} ", c); }
2667 println!();
2668 let mut txt = String::new();
2669 for c in cipher.clone()
2670 { write!(txt, "{:02X} ", c); }
2671 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
2672
2673 let mut recovered = [0; 64];
2674 a_aes.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2675 print!("Ba =\t");
2676 for b in recovered.clone()
2677 { print!("{:02X} ", b); }
2678 println!();
2679 let mut txt = String::new();
2680 for c in recovered.clone()
2681 { write!(txt, "{:02X} ", c); }
2682 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
2683
2684 let mut converted = String::new();
2685 unsafe { converted.as_mut_vec() }.write(&recovered);
2686 unsafe { converted.as_mut_vec() }.truncate(len as usize);
2687
2688 println!("Bb =\t{}", converted);
2689 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2690 assert_eq!(converted, message);
2691 println!();
2692
2693 // Normal case for Rijndael-256-256
2694 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2695 print!("K =\t");
2696 for i in 0..32
2697 { print!("{:02X}", key[i]); }
2698 println!();
2699 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
2700 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2701 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
2702
2703 let message = "In the beginning God created the heavens and the earth.";
2704 println!("M =\t{}", message);
2705 let mut cipher = [0_u8; 55];
2706 a_rijndael.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2707 print!("C =\t");
2708 for c in cipher.clone()
2709 { print!("{:02X} ", c); }
2710 println!();
2711 let mut txt = String::new();
2712 for c in cipher.clone()
2713 { write!(txt, "{:02X} ", c); }
2714 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
2715
2716 let mut recovered = [0; 64];
2717 a_rijndael.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2718 for b in recovered.clone()
2719 { print!("{:02X} ", b); }
2720 println!();
2721 let mut txt = String::new();
2722 for c in recovered.clone()
2723 { write!(txt, "{:02X} ", c); }
2724 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
2725
2726 let mut converted = String::new();
2727 unsafe { converted.as_mut_vec() }.write(&recovered);
2728 unsafe { converted.as_mut_vec() }.truncate(len as usize);
2729
2730 println!("Bb =\t{}", converted);
2731 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2732 assert_eq!(converted, message);
2733 println!();
2734
2735 // Normal case for Rijndael-512-512 for post-quantum
2736 use cryptocol::number::SharedArrays;
2737 use cryptocol::hash::SHA3_512;
2738 let mut sha3 = SHA3_512::new();
2739 sha3.absorb_str("Post-quantum");
2740 let key: [u8; 64] = sha3.get_hash_value_in_array();
2741 print!("K =\t");
2742 for i in 0..64
2743 { print!("{:02X}", key[i]); }
2744 println!();
2745 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
2746 sha3.absorb_str("Initialize");
2747 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
2748 iv.src = sha3.get_hash_value_in_array();
2749 let iv = unsafe { iv.des };
2750 print!("IV =\t");
2751 for i in 0..16
2752 { print!("{:08X}", iv[i].to_be()); }
2753 println!();
2754
2755 let message = "In the beginning God created the heavens and the earth.";
2756 println!("M =\t{}", message);
2757 let mut cipher = [0_u8; 55];
2758 a_rijndael.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2759 print!("C =\t");
2760 for c in cipher.clone()
2761 { print!("{:02X} ", c); }
2762 println!();
2763 let mut txt = String::new();
2764 for c in cipher.clone()
2765 { write!(txt, "{:02X} ", c); }
2766 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
2767
2768 let mut recovered = [0; 64];
2769 a_rijndael.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2770 print!("Ba =\t");
2771 for b in recovered.clone()
2772 { print!("{:02X} ", b); }
2773 println!();
2774 let mut txt = String::new();
2775 for c in recovered.clone()
2776 { write!(txt, "{:02X} ", c); }
2777 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
2778
2779 let mut converted = String::new();
2780 unsafe { converted.as_mut_vec() }.write(&recovered);
2781 unsafe { converted.as_mut_vec() }.truncate(len as usize);
2782
2783 println!("Bb =\t{}", converted);
2784 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2785 assert_eq!(converted, message);
2786 println!("-------------------------------");
2787}
2788
2789fn aes_decrypt_ofb_into_string()
2790{
2791 println!("aes_decrypt_ofb_into_string()");
2792 use std::io::Write;
2793 use std::fmt::Write as _;
2794 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
2795
2796 // Normal case for AES-128
2797 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
2798 println!("K =\t{:#016X}", key);
2799 let mut a_aes = AES_128::new_with_key_u128(key);
2800 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2801 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2802
2803 let message = "In the beginning God created the heavens and the earth.";
2804 println!("M =\t{}", message);
2805 let mut cipher = [0_u8; 55];
2806 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2807 print!("C =\t");
2808 for c in cipher.clone()
2809 { print!("{:02X} ", c); }
2810 println!();
2811 let mut txt = String::new();
2812 for c in cipher.clone()
2813 { write!(txt, "{:02X} ", c); }
2814 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
2815
2816 let mut converted= String::new();
2817 a_aes.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut converted);
2818 println!("B =\t{}", converted);
2819 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2820 assert_eq!(converted, message);
2821 println!();
2822
2823 // Normal case for AES-192
2824 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2825 print!("K =\t");
2826 for i in 0..24
2827 { print!("{:02X}", key[i]); }
2828 println!();
2829 let mut a_aes = AES_192::new_with_key(&key);
2830 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2831 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2832
2833 let message = "In the beginning God created the heavens and the earth.";
2834 println!("M =\t{}", message);
2835 let mut cipher = [0_u8; 55];
2836 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2837 print!("C =\t");
2838 for c in cipher.clone()
2839 { print!("{:02X} ", c); }
2840 println!();
2841 let mut txt = String::new();
2842 for c in cipher.clone()
2843 { write!(txt, "{:02X} ", c); }
2844 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
2845
2846 let mut converted= String::new();
2847 a_aes.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut converted);
2848 println!("B =\t{}", converted);
2849 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2850 assert_eq!(converted, message);
2851 println!();
2852
2853 // Normal case for AES-256
2854 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2855 print!("K =\t");
2856 for i in 0..32
2857 { print!("{:02X}", key[i]); }
2858 println!();
2859 let mut a_aes = AES_256::new_with_key(&key);
2860 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2861 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2862
2863 let message = "In the beginning God created the heavens and the earth.";
2864 println!("M =\t{}", message);
2865 let mut cipher = [0_u8; 55];
2866 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2867 print!("C =\t");
2868 for c in cipher.clone()
2869 { print!("{:02X} ", c); }
2870 println!();
2871 let mut txt = String::new();
2872 for c in cipher.clone()
2873 { write!(txt, "{:02X} ", c); }
2874 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
2875
2876 let mut converted= String::new();
2877 a_aes.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut converted);
2878 println!("B =\t{}", converted);
2879 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2880 assert_eq!(converted, message);
2881 println!();
2882
2883 // Normal case for Rijndael-256-256
2884 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2885 print!("K =\t");
2886 for i in 0..32
2887 { print!("{:02X}", key[i]); }
2888 println!();
2889 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
2890 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2891 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
2892
2893 let message = "In the beginning God created the heavens and the earth.";
2894 println!("M =\t{}", message);
2895 let mut cipher = [0_u8; 55];
2896 a_rijndael.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2897 print!("C =\t");
2898 for c in cipher.clone()
2899 { print!("{:02X} ", c); }
2900 println!();
2901 let mut txt = String::new();
2902 for c in cipher.clone()
2903 { write!(txt, "{:02X} ", c); }
2904 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
2905
2906 let mut converted= String::new();
2907 a_rijndael.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut converted);
2908 println!("B =\t{}", converted);
2909 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2910 assert_eq!(converted, message);
2911 println!();
2912
2913 // Normal case for Rijndael-512-512 for post-quantum
2914 use cryptocol::number::SharedArrays;
2915 use cryptocol::hash::SHA3_512;
2916 let mut sha3 = SHA3_512::new();
2917 sha3.absorb_str("Post-quantum");
2918 let key: [u8; 64] = sha3.get_hash_value_in_array();
2919 print!("K =\t");
2920 for i in 0..64
2921 { print!("{:02X}", key[i]); }
2922 println!();
2923 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
2924 sha3.absorb_str("Initialize");
2925 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
2926 iv.src = sha3.get_hash_value_in_array();
2927 let iv = unsafe { iv.des };
2928 print!("IV =\t");
2929 for i in 0..16
2930 { print!("{:08X}", iv[i].to_be()); }
2931 println!();
2932 let message = "In the beginning God created the heavens and the earth.";
2933 println!("M =\t{}", message);
2934 let mut cipher = [0_u8; 55];
2935 a_rijndael.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2936 print!("C =\t");
2937 for c in cipher.clone()
2938 { print!("{:02X} ", c); }
2939 println!();
2940 let mut txt = String::new();
2941 for c in cipher.clone()
2942 { write!(txt, "{:02X} ", c); }
2943 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
2944
2945 let mut converted= String::new();
2946 a_rijndael.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut converted);
2947 println!("B =\t{}", converted);
2948 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2949 assert_eq!(converted, message);
2950 println!("-------------------------------");
2951}619fn des_encrypt_str_ofb()
620{
621 println!("des_encrypt_str_ofb()");
622 use std::io::Write;
623 use std::fmt::Write as _;
624 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
625
626 // Normal case
627 let key = 0x_1234567890ABCDEF_u64;
628 println!("K =\t{:#016X}", key);
629 let mut a_des = DES::new_with_key_u64(key);
630
631 let message = "In the beginning God created the heavens and the earth.";
632 println!("M =\t{}", message);
633 let iv = 0x_FEDCBA0987654321_u64;
634 println!("IV = {}", iv);
635 let mut cipher = [0_u8; 55];
636 a_des.encrypt_str(iv, &message, cipher.as_mut_ptr());
637 print!("C (16 rounds) =\t");
638 for c in cipher.clone()
639 { print!("{:02X} ", c); }
640 println!();
641 let mut txt = String::new();
642 for c in cipher.clone()
643 { write!(txt, "{:02X} ", c); }
644 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
645 println!();
646
647 // Expanded case for 128 rounds
648 let key = 0x_1234567890ABCDEF_u64;
649 println!("K =\t{:#016X}", key);
650 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
651
652 let message = "In the beginning God created the heavens and the earth.";
653 println!("M =\t{}", message);
654 let iv = 0x_FEDCBA0987654321_u64;
655 println!("IV = {}", iv);
656 let mut cipher = [0_u8; 55];
657 a_des.encrypt_str(iv, &message, cipher.as_mut_ptr());
658 print!("C (128 rounds) =\t");
659 for c in cipher.clone()
660 { print!("{:02X} ", c); }
661 println!();
662 let mut txt = String::new();
663 for c in cipher.clone()
664 { write!(txt, "{:02X} ", c); }
665 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
666 println!();
667
668 // Expanded case for 0 rounds which means that key is meaningless
669 let key1 = 0x_1234567890ABCDEF_u64;
670 let key2 = 0_u64;
671 println!("K =\t{:#016X}", key);
672 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
673 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
674
675 let message = "In the beginning God created the heavens and the earth.";
676 println!("M =\t{}", message);
677 let iv = 0x_FEDCBA0987654321_u64;
678 println!("IV = {}", iv);
679 let mut cipher1 = [0_u8; 55];
680 let mut cipher2 = [0_u8; 55];
681 c_des.encrypt_str(iv, &message, cipher1.as_mut_ptr());
682 d_des.encrypt_str(iv, &message, cipher2.as_mut_ptr());
683 print!("C (0 rounds) =\t");
684 for c in cipher1.clone()
685 { print!("{:02X} ", c); }
686 println!();
687 let mut txt = String::new();
688 for c in cipher1.clone()
689 { write!(txt, "{:02X} ", c); }
690 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
691 print!("D (0 rounds) =\t");
692 for c in cipher2.clone()
693 { print!("{:02X} ", c); }
694 println!();
695 let mut txt = String::new();
696 for c in cipher2.clone()
697 { write!(txt, "{:02X} ", c); }
698 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
699 println!();
700
701 // Normal case for the message of 0 bytes
702 let key = 0x_1234567890ABCDEF_u64;
703 println!("K =\t{:#016X}", key);
704 let mut a_des = DES::new_with_key_u64(key);
705
706 let message = "";
707 println!("M =\t{}", message);
708 let iv = 0x_FEDCBA0987654321_u64;
709 println!("IV = {}", iv);
710 let mut cipher = [0_u8; 0];
711 a_des.encrypt_str(iv, &message, cipher.as_mut_ptr());
712 print!("C =\t");
713 for c in cipher.clone()
714 { print!("{:02X} ", c); }
715 println!();
716 let mut txt = String::new();
717 for c in cipher.clone()
718 { write!(txt, "{:02X} ", c); }
719 assert_eq!(txt, "");
720 println!();
721
722 // Normal case for the message shorter than 8 bytes
723 let key = 0x_1234567890ABCDEF_u64;
724 println!("K =\t{:#016X}", key);
725 let mut a_des = DES::new_with_key_u64(key);
726
727 let message = "7 bytes";
728 println!("M =\t{}", message);
729 let iv = 0x_FEDCBA0987654321_u64;
730 println!("IV = {}", iv);
731 let mut cipher = [0_u8; 7];
732 a_des.encrypt_str(iv, &message, cipher.as_mut_ptr());
733 print!("C =\t");
734 for c in cipher.clone()
735 { print!("{:02X} ", c); }
736 println!();
737 let mut txt = String::new();
738 for c in cipher.clone()
739 { write!(txt, "{:02X} ", c); }
740 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
741 println!();
742
743 // Normal case for the message of 8 bytes
744 let key = 0x_1234567890ABCDEF_u64;
745 println!("K =\t{:#016X}", key);
746 let mut a_des = DES::new_with_key_u64(key);
747
748 let message = "I am OK.";
749 println!("M =\t{}", message);
750 let iv = 0x_FEDCBA0987654321_u64;
751 println!("IV = {}", iv);
752 let mut cipher = [0_u8; 8];
753 a_des.encrypt_str(iv, &message, cipher.as_mut_ptr());
754 print!("C =\t");
755 for c in cipher.clone()
756 { print!("{:02X} ", c); }
757 println!();
758 let mut txt = String::new();
759 for c in cipher.clone()
760 { write!(txt, "{:02X} ", c); }
761 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
762 println!();
763
764 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
765 let key = 0x_1234567890ABCDEF_u64;
766 println!("K =\t{:#016X}", key);
767 let mut a_des = DES::new_with_key_u64(key);
768
769 let message = "PARK Youngho";
770 println!("M =\t{}", message);
771 let iv = 0x_FEDCBA0987654321_u64;
772 println!("IV = {}", iv);
773 let mut cipher = [0_u8; 12];
774 a_des.encrypt_str(iv, &message, cipher.as_mut_ptr());
775 print!("C =\t");
776 for c in cipher.clone()
777 { print!("{:02X} ", c); }
778 println!();
779 let mut txt = String::new();
780 for c in cipher.clone()
781 { write!(txt, "{:02X} ", c); }
782 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
783 println!();
784
785 // Normal case for the message of 16 bytes
786 let key = 0x_1234567890ABCDEF_u64;
787 println!("K =\t{:#016X}", key);
788 let mut a_des = DES::new_with_key_u64(key);
789
790 let message = "고맙습니다.";
791 println!("M =\t{}", message);
792 let iv = 0x_FEDCBA0987654321_u64;
793 println!("IV = {}", iv);
794 let mut cipher = [0_u8; 16];
795 a_des.encrypt_str(iv, &message, cipher.as_mut_ptr());
796 print!("C =\t");
797 for c in cipher.clone()
798 { print!("{:02X} ", c); }
799 println!();
800 let mut txt = String::new();
801 for c in cipher.clone()
802 { write!(txt, "{:02X} ", c); }
803 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
804 println!("-------------------------------");
805}Sourcefn encrypt_str_into_vec<U>(
&mut self,
iv: T,
message: &str,
cipher: &mut Vec<U>,
) -> u64
fn encrypt_str_into_vec<U>( &mut self, iv: T, message: &str, cipher: &mut Vec<U>, ) -> u64
Encrypts the data in str without any padding in OFB (Output FeedBack)
mode, and stores the encrypted data in Vec<U>.
§Arguments
ivis an initialization vector for OFB mode.messageis an immutable reference tostrobject which is&str, and is the place where the plaintext to be encrypted is stored.cipheris a mutable reference toVec<U>object, and is the place where the encrypted data will be stored.
§Output
- This method returns the size of ciphertext.
- If this method failed in encryption or
message.len()is0, this method returnszero.
§Features
- If
messageis a null string “”, nothing will be encrypted, and stored in theVec<U>object which is referred to ascipher. - 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 Rijndael or AES, and its variants
§Example 1 for AES-128
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ AES_128, CBC_ISO };
let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
println!("K =\t{:#016X}", key);
let mut a_aes = AES_128::new_with_key_u128(key);
let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
print!("C =\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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 07 AF 4E C8 EF 5D 0A 74 97 3F 90 9E 05 9E 9E 32 FB 55 54 45 7C ED A2 2B F8 07 66 C0 7B CB 98 F3 BF 93 15 BA 26 1C 47 ");§For more examples,
click here
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, OFB };
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 iv = 0x_FEDCBA0987654321_u64;
println!("IV = {}", iv);
let mut cipher = Vec::<u8>::new();
a_des.encrypt_str_into_vec(iv, &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, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");§For more examples,
click here
§For BigCryptor128
§Example 1 for TAES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
+ AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
+ AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
println!("IV = {:#034X}", iv);
let message = "In the beginning God created the heavens and the earth.";
let mut cipher = Vec::<u8>::new();
taes.encrypt_str_into_vec(iv, &message, &mut cipher);
print!("C =\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, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");§For more examples,
click here
§For BigCryptor64
§Example 1 for TDES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
+ DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
+ DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
let iv = 0x_FEDCBA0987654321_u64;
println!("IV = {:#018X}", iv);
let message = "In the beginning God created the heavens and the earth.";
let mut cipher = Vec::<u8>::new();
tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
print!("C =\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, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");§For more examples,
click here
Examples found in repository?
166fn bigcryptor64_encrypt_str_ofb_into_vec()
167{
168 println!("bigcryptor64_encrypt_str_ofb_into_vec()");
169 use std::io::Write;
170 use std::fmt::Write as _;
171 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
172
173 // TDES case
174 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
175 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
176 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
177 let iv = 0x_FEDCBA0987654321_u64;
178 println!("IV = {:#018X}", iv);
179 let message = "In the beginning God created the heavens and the earth.";
180 let mut cipher = Vec::<u8>::new();
181 tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
182 print!("C =\t");
183 for c in cipher.clone()
184 { print!("{:02X} ", c); }
185 println!();
186 let mut txt = String::new();
187 for c in cipher.clone()
188 { write!(txt, "{:02X} ", c); }
189 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
190 println!("-------------------------------");
191}
192
193fn bigcryptor64_encrypt_str_ofb_into_array()
194{
195 println!("bigcryptor64_encrypt_str_ofb_into_array()");
196 use std::io::Write;
197 use std::fmt::Write as _;
198 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
199
200 // TDES case
201 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
202 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
203 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
204 let iv = 0x_FEDCBA0987654321_u64;
205 println!("IV = {:#018X}", iv);
206 let message = "In the beginning God created the heavens and the earth.";
207 let mut cipher = [0_u8; 55];
208 tdes.encrypt_str_into_array(iv, &message, &mut cipher);
209 print!("C =\t");
210 for c in cipher.clone()
211 { print!("{:02X} ", c); }
212 println!();
213 let mut txt = String::new();
214 for c in cipher.clone()
215 { write!(txt, "{:02X} ", c); }
216 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
217 println!("-------------------------------");
218}
219
220fn bigcryptor64_encrypt_string_ofb()
221{
222 println!("bigcryptor64_encrypt_string_ofb()");
223 use std::io::Write;
224 use std::fmt::Write as _;
225 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
226
227 // TDES case
228 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
229 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
230 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
231 let iv = 0x_FEDCBA0987654321_u64;
232 println!("IV = {:#018X}", iv);
233 let message = "In the beginning God created the heavens and the earth.".to_string();
234 let mut cipher = [0_u8; 55];
235 tdes.encrypt_string(iv, &message, cipher.as_mut_ptr());
236 print!("C =\t");
237 for c in cipher.clone()
238 { print!("{:02X} ", c); }
239 println!();
240 let mut txt = String::new();
241 for c in cipher.clone()
242 { write!(txt, "{:02X} ", c); }
243 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
244 println!("-------------------------------");
245}
246
247fn bigcryptor64_encrypt_string_ofb_into_vec()
248{
249 println!("bigcryptor64_encrypt_string_ofb_into_vec()");
250 use std::io::Write;
251 use std::fmt::Write as _;
252 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
253
254 // TDES case
255 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
256 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
257 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
258 let iv = 0x_FEDCBA0987654321_u64;
259 println!("IV = {:#018X}", iv);
260 let message = "In the beginning God created the heavens and the earth.".to_string();
261 let mut cipher = Vec::<u8>::new();
262 tdes.encrypt_string_into_vec(iv, &message, &mut cipher);
263 print!("C =\t");
264 for c in cipher.clone()
265 { print!("{:02X} ", c); }
266 println!();
267 let mut txt = String::new();
268 for c in cipher.clone()
269 { write!(txt, "{:02X} ", c); }
270 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
271 println!("-------------------------------");
272}
273
274fn bigcryptor64_encrypt_string_ofb_into_array()
275{
276 println!("bigcryptor64_encrypt_string_ofb_into_array()");
277 use std::io::Write;
278 use std::fmt::Write as _;
279 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
280
281 // TDES case
282 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
283 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
284 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
285 let iv = 0x_FEDCBA0987654321_u64;
286 println!("IV = {:#018X}", iv);
287 let message = "In the beginning God created the heavens and the earth.".to_string();
288 let mut cipher = [0_u8; 55];
289 tdes.encrypt_string_into_array(iv, &message, &mut cipher);
290 print!("C =\t");
291 for c in cipher.clone()
292 { print!("{:02X} ", c); }
293 println!();
294 let mut txt = String::new();
295 for c in cipher.clone()
296 { write!(txt, "{:02X} ", c); }
297 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
298 println!("-------------------------------");
299}
300
301fn bigcryptor64_encrypt_vec_ofb()
302{
303 println!("bigcryptor64_encrypt_vec_ofb()");
304 use std::io::Write;
305 use std::fmt::Write as _;
306 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
307
308 // TDES case
309 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
310 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
311 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
312 let iv = 0x_FEDCBA0987654321_u64;
313 println!("IV = {:#018X}", iv);
314 let message = "In the beginning God created the heavens and the earth.";
315 println!("M =\t{}", message);
316 let message = unsafe { message.to_string().as_mut_vec().clone() };
317 let mut cipher = [0_u8; 55];
318 tdes.encrypt_vec(iv, &message, cipher.as_mut_ptr());
319 print!("C =\t");
320 for c in cipher.clone()
321 { print!("{:02X} ", c); }
322 println!();
323 let mut txt = String::new();
324 for c in cipher.clone()
325 { write!(txt, "{:02X} ", c); }
326 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
327 println!("-------------------------------");
328}
329
330fn bigcryptor64_encrypt_vec_ofb_into_vec()
331{
332 println!("bigcryptor64_encrypt_vec_ofb_into_vec()");
333 use std::io::Write;
334 use std::fmt::Write as _;
335 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
336
337 // TDES case
338 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
339 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
340 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
341 let iv = 0x_FEDCBA0987654321_u64;
342 println!("IV = {:#018X}", iv);
343 let message = "In the beginning God created the heavens and the earth.";
344 println!("M =\t{}", message);
345 let message = unsafe { message.to_string().as_mut_vec().clone() };
346 let mut cipher = Vec::<u8>::new();
347 tdes.encrypt_vec_into_vec(iv, &message, &mut cipher);
348 print!("C =\t");
349 for c in cipher.clone()
350 { print!("{:02X} ", c); }
351 println!();
352 let mut txt = String::new();
353 for c in cipher.clone()
354 { write!(txt, "{:02X} ", c); }
355 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
356 println!("-------------------------------");
357}
358
359fn bigcryptor64_encrypt_vec_ofb_into_array()
360{
361 println!("bigcryptor64_encrypt_vec_ofb_into_array()");
362 use std::io::Write;
363 use std::fmt::Write as _;
364 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
365
366 // TDES case
367 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
368 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
369 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
370 let iv = 0x_FEDCBA0987654321_u64;
371 println!("IV = {:#018X}", iv);
372 let message = "In the beginning God created the heavens and the earth.";
373 println!("M =\t{}", message);
374 let message = unsafe { message.to_string().as_mut_vec().clone() };
375 let mut cipher = [0_u8; 55];
376 tdes.encrypt_vec_into_array(iv, &message, &mut cipher);
377 print!("C =\t");
378 for c in cipher.clone()
379 { print!("{:02X} ", c); }
380 println!();
381 let mut txt = String::new();
382 for c in cipher.clone()
383 { write!(txt, "{:02X} ", c); }
384 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
385 println!("-------------------------------");
386}
387
388fn bigcryptor64_encrypt_array_ofb()
389{
390 println!("bigcryptor64_encrypt_array_ofb()");
391 use std::io::Write;
392 use std::fmt::Write as _;
393 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
394
395 // TDES case
396 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
397 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
398 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
399 let iv = 0x_FEDCBA0987654321_u64;
400 println!("IV = {:#018X}", iv);
401 let mes = "In the beginning God created the heavens and the earth.";
402 println!("M =\t{}", mes);
403 let mut message = [0_u8; 55];
404 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
405 let mut cipher = [0_u8; 55];
406 tdes.encrypt_array(iv, &message, cipher.as_mut_ptr());
407 print!("C =\t");
408 for c in cipher.clone()
409 { print!("{:02X} ", c); }
410 println!();
411 let mut txt = String::new();
412 for c in cipher.clone()
413 { write!(txt, "{:02X} ", c); }
414 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
415 println!("-------------------------------");
416}
417
418fn bigcryptor64_encrypt_array_ofb_into_vec()
419{
420 println!("bigcryptor64_encrypt_array_ofb_into_vec()");
421 use std::io::Write;
422 use std::fmt::Write as _;
423 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
424
425 // TDES case
426 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
427 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
428 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
429 let iv = 0x_FEDCBA0987654321_u64;
430 println!("IV = {:#018X}", iv);
431 let mes = "In the beginning God created the heavens and the earth.";
432 println!("M =\t{}", mes);
433 let mut message = [0_u8; 55];
434 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
435 let mut cipher = Vec::<u8>::new();
436 tdes.encrypt_array_into_vec(iv, &message, &mut cipher);
437 print!("C =\t");
438 for c in cipher.clone()
439 { print!("{:02X} ", c); }
440 println!();
441 let mut txt = String::new();
442 for c in cipher.clone()
443 { write!(txt, "{:02X} ", c); }
444 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
445 println!("-------------------------------");
446}
447
448fn bigcryptor64_encrypt_array_ofb_into_array()
449{
450 println!("bigcryptor64_encrypt_array_ofb_into_array()");
451 use std::io::Write;
452 use std::fmt::Write as _;
453 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
454
455 // TDES case
456 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
457 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
458 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
459 let iv = 0x_FEDCBA0987654321_u64;
460 println!("IV = {:#018X}", iv);
461 let mes = "In the beginning God created the heavens and the earth.";
462 println!("M =\t{}", mes);
463 let mut message = [0_u8; 55];
464 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
465 let mut cipher = [0_u8; 55];
466 tdes.encrypt_array_into_array(iv, &message, &mut cipher);
467 for c in cipher.clone()
468 { print!("{:02X} ", c); }
469 println!();
470 let mut txt = String::new();
471 for c in cipher.clone()
472 { write!(txt, "{:02X} ", c); }
473 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
474 println!("-------------------------------");
475}
476
477fn bigcryptor64_decrypt_ofb()
478{
479 println!("bigcryptor64_decrypt_ofb()");
480 use std::io::Write;
481 use std::fmt::Write as _;
482 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
483
484 // TDES case
485 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
486 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
487 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
488 let iv = 0x_FEDCBA0987654321_u64;
489 println!("IV = {:#018X}", iv);
490 let message = "In the beginning God created the heavens and the earth.";
491 println!("M =\t{}", message);
492 let mut cipher = Vec::<u8>::new();
493 tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
494 print!("C =\t");
495 for c in cipher.clone()
496 { print!("{:02X} ", c); }
497 println!();
498 let mut txt = String::new();
499 for c in cipher.clone()
500 { write!(txt, "{:02X} ", c); }
501 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
502
503 let mut recovered = vec![0; 55];
504 tdes.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
505 print!("Ba =\t");
506 for b in recovered.clone()
507 { print!("{:02X} ", b); }
508 println!();
509 let mut txt = String::new();
510 for c in recovered.clone()
511 { write!(txt, "{:02X} ", c); }
512 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 ");
513
514 let mut converted = String::new();
515 unsafe { converted.as_mut_vec() }.append(&mut recovered);
516
517 println!("Bb =\t{}", converted);
518 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
519 assert_eq!(converted, message);
520 println!("-------------------------------");
521}
522
523fn bigcryptor64_decrypt_ofb_into_vec()
524{
525 println!("bigcryptor64_decrypt_ofb_into_vec()");
526 use std::io::Write;
527 use std::fmt::Write as _;
528 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
529
530 // TDES case
531 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
532 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
533 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
534 let iv = 0x_FEDCBA0987654321_u64;
535 println!("IV = {:#018X}", iv);
536 let message = "In the beginning God created the heavens and the earth.";
537 println!("M =\t{}", message);
538 let mut cipher = Vec::<u8>::new();
539 tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
540 print!("C =\t");
541 for c in cipher.clone()
542 { print!("{:02X} ", c); }
543 println!();
544 let mut txt = String::new();
545 for c in cipher.clone()
546 { write!(txt, "{:02X} ", c); }
547 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
548
549 let mut recovered = Vec::<u8>::new();
550 tdes.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
551 print!("Ba =\t");
552 for b in recovered.clone()
553 { print!("{:02X} ", b); }
554 println!();
555 let mut txt = String::new();
556 for c in recovered.clone()
557 { write!(txt, "{:02X} ", c); }
558 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 ");
559
560 let mut converted = String::new();
561 unsafe { converted.as_mut_vec() }.append(&mut recovered);
562
563 println!("Bb =\t{}", converted);
564 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
565 assert_eq!(converted, message);
566 println!("-------------------------------");
567}
568
569fn bigcryptor64_decrypt_ofb_into_array()
570{
571 println!("bigcryptor64_decrypt_ofb_into_array()");
572 use std::io::Write;
573 use std::fmt::Write as _;
574 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
575
576 // TDES case
577 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
578 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
579 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
580 let iv = 0x_FEDCBA0987654321_u64;
581 println!("IV = {:#018X}", iv);
582 let message = "In the beginning God created the heavens and the earth.";
583 println!("M =\t{}", message);
584 let mut cipher = Vec::<u8>::new();
585 tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
586 print!("C =\t");
587 for c in cipher.clone()
588 { print!("{:02X} ", c); }
589 println!();
590 let mut txt = String::new();
591 for c in cipher.clone()
592 { write!(txt, "{:02X} ", c); }
593 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
594
595 let mut recovered = [0u8; 56];
596 let len = tdes.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
597 print!("Ba =\t");
598 for b in recovered.clone()
599 { print!("{:02X} ", b); }
600 println!();
601 let mut txt = String::new();
602 for c in recovered.clone()
603 { write!(txt, "{:02X} ", c); }
604 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 ");
605
606 let mut converted = String::new();
607 unsafe { converted.as_mut_vec() }.write(&recovered);
608 unsafe { converted.as_mut_vec() }.truncate(len as usize);
609 println!("Bb =\t{}", converted);
610 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
611 assert_eq!(converted, message);
612 println!("-------------------------------");
613}
614
615fn bigcryptor64_decrypt_ofb_into_string()
616{
617 println!("bigcryptor64_decrypt_ofb_into_string()");
618 use std::io::Write;
619 use std::fmt::Write as _;
620 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
621
622 // TDES case
623 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
624 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
625 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
626 let iv = 0x_FEDCBA0987654321_u64;
627 println!("IV = {:#018X}", iv);
628 let message = "In the beginning God created the heavens and the earth.";
629 println!("M =\t{}", message);
630 let mut cipher = Vec::<u8>::new();
631 tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
632 print!("C =\t");
633 for c in cipher.clone()
634 { print!("{:02X} ", c); }
635 println!();
636 let mut txt = String::new();
637 for c in cipher.clone()
638 { write!(txt, "{:02X} ", c); }
639 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
640
641 let mut recovered = String::new();
642 tdes.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
643 println!("B =\t{}", recovered);
644 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
645 assert_eq!(recovered, message);
646 println!("-------------------------------");
647}
648
649fn bigcryptor64_decrypt_vec_ofb()
650{
651 println!("bigcryptor64_decrypt_vec_ofb()");
652 use std::io::Write;
653 use std::fmt::Write as _;
654 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
655
656 // TDES case
657 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
658 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
659 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
660 let iv = 0x_FEDCBA0987654321_u64;
661 println!("IV = {:#018X}", iv);
662 let message = "In the beginning God created the heavens and the earth.";
663 println!("M =\t{}", message);
664 let mut cipher = Vec::<u8>::new();
665 tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
666 print!("C =\t");
667 for c in cipher.clone()
668 { print!("{:02X} ", c); }
669 println!();
670 let mut txt = String::new();
671 for c in cipher.clone()
672 { write!(txt, "{:02X} ", c); }
673 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
674
675 let mut recovered = vec![0; 55];
676 tdes.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
677 print!("Ba =\t");
678 for b in recovered.clone()
679 { print!("{:02X} ", b); }
680 println!();
681 let mut txt = String::new();
682 for c in recovered.clone()
683 { write!(txt, "{:02X} ", c); }
684 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 ");
685
686 let mut converted = String::new();
687 unsafe { converted.as_mut_vec() }.append(&mut recovered);
688
689 println!("Bb =\t{}", converted);
690 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
691 assert_eq!(converted, message);
692 println!("-------------------------------");
693}
694
695fn bigcryptor64_decrypt_vec_ofb_into_vec()
696{
697 println!("bigcryptor64_decrypt_vec_ofb_into_vec()");
698 use std::io::Write;
699 use std::fmt::Write as _;
700 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
701
702 // TDES case
703 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
704 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
705 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
706 let iv = 0x_FEDCBA0987654321_u64;
707 println!("IV = {:#018X}", iv);
708 let message = "In the beginning God created the heavens and the earth.";
709 println!("M =\t{}", message);
710 let mut cipher = Vec::<u8>::new();
711 tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
712 print!("C =\t");
713 for c in cipher.clone()
714 { print!("{:02X} ", c); }
715 println!();
716 let mut txt = String::new();
717 for c in cipher.clone()
718 { write!(txt, "{:02X} ", c); }
719 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
720
721 let mut recovered = Vec::<u8>::new();
722 tdes.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
723 print!("Ba =\t");
724 for b in recovered.clone()
725 { print!("{:02X} ", b); }
726 println!();
727 let mut txt = String::new();
728 for c in recovered.clone()
729 { write!(txt, "{:02X} ", c); }
730 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 ");
731
732 let mut converted = String::new();
733 unsafe { converted.as_mut_vec() }.append(&mut recovered);
734
735 println!("Bb =\t{}", converted);
736 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
737 assert_eq!(converted, message);
738 println!("-------------------------------");
739}
740
741fn bigcryptor64_decrypt_vec_ofb_into_array()
742{
743 println!("bigcryptor64_decrypt_vec_ofb_into_array()");
744 use std::io::Write;
745 use std::fmt::Write as _;
746 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
747
748 // TDES case
749 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
750 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
751 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
752 let iv = 0x_FEDCBA0987654321_u64;
753 println!("IV = {:#018X}", iv);
754 let message = "In the beginning God created the heavens and the earth.";
755 println!("M =\t{}", message);
756 let mut cipher = Vec::<u8>::new();
757 tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
758 print!("C =\t");
759 for c in cipher.clone()
760 { print!("{:02X} ", c); }
761 println!();
762 let mut txt = String::new();
763 for c in cipher.clone()
764 { write!(txt, "{:02X} ", c); }
765 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
766
767 let mut recovered = [0u8; 56];
768 let len = tdes.decrypt_vec_into_array(iv, &cipher, &mut recovered);
769 print!("Ba =\t");
770 for b in recovered.clone()
771 { print!("{:02X} ", b); }
772 println!();
773 let mut txt = String::new();
774 for c in recovered.clone()
775 { write!(txt, "{:02X} ", c); }
776 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 ");
777
778 let mut converted = String::new();
779 unsafe { converted.as_mut_vec() }.write(&recovered);
780 unsafe { converted.as_mut_vec() }.truncate(len as usize);
781 println!("Bb =\t{}", converted);
782 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
783 assert_eq!(converted, message);
784 println!("-------------------------------");
785}
786
787fn bigcryptor64_decrypt_vec_ofb_into_string()
788{
789 println!("bigcryptor64_decrypt_vec_ofb_into_string()");
790 use std::io::Write;
791 use std::fmt::Write as _;
792 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
793
794 // TDES case
795 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
796 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
797 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
798 let iv = 0x_FEDCBA0987654321_u64;
799 println!("IV = {:#018X}", iv);
800 let message = "In the beginning God created the heavens and the earth.";
801 println!("M =\t{}", message);
802 let mut cipher = Vec::<u8>::new();
803 tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
804 print!("C =\t");
805 for c in cipher.clone()
806 { print!("{:02X} ", c); }
807 println!();
808 let mut txt = String::new();
809 for c in cipher.clone()
810 { write!(txt, "{:02X} ", c); }
811 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
812
813 let mut recovered = String::new();
814 tdes.decrypt_vec_into_string(iv, &cipher, &mut recovered);
815 println!("B =\t{}", recovered);
816 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
817 assert_eq!(recovered, message);
818 println!("-------------------------------");
819}More examples
166fn bigcryptor128_encrypt_str_ofb_into_vec()
167{
168 println!("bigcryptor128_encrypt_str_ofb_into_vec()");
169 use std::io::Write;
170 use std::fmt::Write as _;
171 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
172
173 // TAES_128 case
174 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
175 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
176 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
177 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
178 println!("IV = {:#034X}", iv);
179 let message = "In the beginning God created the heavens and the earth.";
180 let mut cipher = Vec::<u8>::new();
181 taes.encrypt_str_into_vec(iv, &message, &mut cipher);
182 print!("C =\t");
183 for c in cipher.clone()
184 { print!("{:02X} ", c); }
185 println!();
186 let mut txt = String::new();
187 for c in cipher.clone()
188 { write!(txt, "{:02X} ", c); }
189 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
190 println!("-------------------------------");
191}
192
193fn bigcryptor128_encrypt_str_ofb_into_array()
194{
195 println!("bigcryptor128_encrypt_str_ofb_into_array()");
196 use std::io::Write;
197 use std::fmt::Write as _;
198 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
199
200 // TAES_128 case
201 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
202 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
203 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
204 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
205 println!("IV = {:#034X}", iv);
206 let message = "In the beginning God created the heavens and the earth.";
207 let mut cipher = [0_u8; 55];
208 taes.encrypt_str_into_array(iv, &message, &mut cipher);
209 print!("C =\t");
210 for c in cipher.clone()
211 { print!("{:02X} ", c); }
212 println!();
213 let mut txt = String::new();
214 for c in cipher.clone()
215 { write!(txt, "{:02X} ", c); }
216 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
217 println!("-------------------------------");
218}
219
220fn bigcryptor128_encrypt_string_ofb()
221{
222 println!("bigcryptor128_encrypt_string_ofb()");
223 use std::io::Write;
224 use std::fmt::Write as _;
225 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
226
227 // TAES_128 case
228 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
229 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
230 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
231 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
232 println!("IV = {:#034X}", iv);
233 let message = "In the beginning God created the heavens and the earth.".to_string();
234 let mut cipher = [0_u8; 55];
235 taes.encrypt_string(iv, &message, cipher.as_mut_ptr());
236 print!("C =\t");
237 for c in cipher.clone()
238 { print!("{:02X} ", c); }
239 println!();
240 let mut txt = String::new();
241 for c in cipher.clone()
242 { write!(txt, "{:02X} ", c); }
243 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
244 println!("-------------------------------");
245}
246
247fn bigcryptor128_encrypt_string_ofb_into_vec()
248{
249 println!("bigcryptor128_encrypt_string_ofb_into_vec()");
250 use std::io::Write;
251 use std::fmt::Write as _;
252 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
253
254 // TAES_128 case
255 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
256 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
257 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
258 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
259 println!("IV = {:#034X}", iv);
260 let message = "In the beginning God created the heavens and the earth.".to_string();
261 let mut cipher = Vec::<u8>::new();
262 taes.encrypt_string_into_vec(iv, &message, &mut cipher);
263 print!("C =\t");
264 for c in cipher.clone()
265 { print!("{:02X} ", c); }
266 println!();
267 let mut txt = String::new();
268 for c in cipher.clone()
269 { write!(txt, "{:02X} ", c); }
270 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
271 println!("-------------------------------");
272}
273
274fn bigcryptor128_encrypt_string_ofb_into_array()
275{
276 println!("bigcryptor128_encrypt_string_ofb_into_array()");
277 use std::io::Write;
278 use std::fmt::Write as _;
279 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
280
281 // TAES_128 case
282 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
283 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
284 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
285 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
286 println!("IV = {:#034X}", iv);
287 let message = "In the beginning God created the heavens and the earth.".to_string();
288 let mut cipher = [0_u8; 55];
289 taes.encrypt_string_into_array(iv, &message, &mut cipher);
290 print!("C =\t");
291 for c in cipher.clone()
292 { print!("{:02X} ", c); }
293 println!();
294 let mut txt = String::new();
295 for c in cipher.clone()
296 { write!(txt, "{:02X} ", c); }
297 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
298 println!("-------------------------------");
299}
300
301fn bigcryptor128_encrypt_vec_ofb()
302{
303 println!("bigcryptor128_encrypt_vec_ofb()");
304 use std::io::Write;
305 use std::fmt::Write as _;
306 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
307
308 // TAES_128 case
309 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
310 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
311 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
312 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
313 println!("IV = {:#034X}", iv);
314 let message = "In the beginning God created the heavens and the earth.";
315 println!("M =\t{}", message);
316 let message = unsafe { message.to_string().as_mut_vec().clone() };
317 let mut cipher = [0_u8; 55];
318 taes.encrypt_vec(iv, &message, cipher.as_mut_ptr());
319 print!("C =\t");
320 for c in cipher.clone()
321 { print!("{:02X} ", c); }
322 println!();
323 let mut txt = String::new();
324 for c in cipher.clone()
325 { write!(txt, "{:02X} ", c); }
326 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
327 println!("-------------------------------");
328}
329
330fn bigcryptor128_encrypt_vec_ofb_into_vec()
331{
332 println!("bigcryptor128_encrypt_vec_ofb_into_vec()");
333 use std::io::Write;
334 use std::fmt::Write as _;
335 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
336
337 // TAES_128 case
338 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
339 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
340 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
341 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
342 println!("IV = {:#034X}", iv);
343 let message = "In the beginning God created the heavens and the earth.";
344 println!("M =\t{}", message);
345 let message = unsafe { message.to_string().as_mut_vec().clone() };
346 let mut cipher = Vec::<u8>::new();
347 taes.encrypt_vec_into_vec(iv, &message, &mut cipher);
348 print!("C =\t");
349 for c in cipher.clone()
350 { print!("{:02X} ", c); }
351 println!();
352 let mut txt = String::new();
353 for c in cipher.clone()
354 { write!(txt, "{:02X} ", c); }
355 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
356 println!("-------------------------------");
357}
358
359fn bigcryptor128_encrypt_vec_ofb_into_array()
360{
361 println!("bigcryptor128_encrypt_vec_ofb_into_array()");
362 use std::io::Write;
363 use std::fmt::Write as _;
364 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
365
366 // TAES_128 case
367 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
368 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
369 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
370 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
371 println!("IV = {:#034X}", iv);
372 let message = "In the beginning God created the heavens and the earth.";
373 println!("M =\t{}", message);
374 let message = unsafe { message.to_string().as_mut_vec().clone() };
375 let mut cipher = [0_u8; 55];
376 taes.encrypt_vec_into_array(iv, &message, &mut cipher);
377 print!("C =\t");
378 for c in cipher.clone()
379 { print!("{:02X} ", c); }
380 println!();
381 let mut txt = String::new();
382 for c in cipher.clone()
383 { write!(txt, "{:02X} ", c); }
384 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
385 println!("-------------------------------");
386}
387
388fn bigcryptor128_encrypt_array_ofb()
389{
390 println!("bigcryptor128_encrypt_array_ofb()");
391 use std::io::Write;
392 use std::fmt::Write as _;
393 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
394
395 // TAES_128 case
396 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
397 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
398 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
399 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
400 println!("IV = {:#034X}", iv);
401 let mes = "In the beginning God created the heavens and the earth.";
402 println!("M =\t{}", mes);
403 let mut message = [0_u8; 55];
404 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
405 let mut cipher = [0_u8; 55];
406 taes.encrypt_array(iv, &message, cipher.as_mut_ptr());
407 print!("C =\t");
408 for c in cipher.clone()
409 { print!("{:02X} ", c); }
410 println!();
411 let mut txt = String::new();
412 for c in cipher.clone()
413 { write!(txt, "{:02X} ", c); }
414 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
415 println!("-------------------------------");
416}
417
418fn bigcryptor128_encrypt_array_ofb_into_vec()
419{
420 println!("bigcryptor128_encrypt_array_ofb_into_vec()");
421 use std::io::Write;
422 use std::fmt::Write as _;
423 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
424
425 // TAES_128 case
426 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
427 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
428 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
429 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
430 println!("IV = {:#034X}", iv);
431 let mes = "In the beginning God created the heavens and the earth.";
432 println!("M =\t{}", mes);
433 let mut message = [0_u8; 55];
434 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
435 let mut cipher = Vec::<u8>::new();
436 taes.encrypt_array_into_vec(iv, &message, &mut cipher);
437 print!("C =\t");
438 for c in cipher.clone()
439 { print!("{:02X} ", c); }
440 println!();
441 let mut txt = String::new();
442 for c in cipher.clone()
443 { write!(txt, "{:02X} ", c); }
444 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
445 println!("-------------------------------");
446}
447
448fn bigcryptor128_encrypt_array_ofb_into_array()
449{
450 println!("bigcryptor128_encrypt_array_ofb_into_array()");
451 use std::io::Write;
452 use std::fmt::Write as _;
453 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
454
455 // TAES_128 case
456 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
457 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
458 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
459 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
460 println!("IV = {:#034X}", iv);
461 let mes = "In the beginning God created the heavens and the earth.";
462 println!("M =\t{}", mes);
463 let mut message = [0_u8; 55];
464 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
465 let mut cipher = [0_u8; 55];
466 taes.encrypt_array_into_array(iv, &message, &mut cipher);
467 for c in cipher.clone()
468 { print!("{:02X} ", c); }
469 println!();
470 let mut txt = String::new();
471 for c in cipher.clone()
472 { write!(txt, "{:02X} ", c); }
473 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
474 println!("-------------------------------");
475}
476
477fn bigcryptor128_decrypt_ofb()
478{
479 println!("bigcryptor128_decrypt_ofb()");
480 use std::io::Write;
481 use std::fmt::Write as _;
482 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
483
484 // TAES_128 case
485 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
486 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
487 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
488 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
489 println!("IV = {:#034X}", iv);
490 let message = "In the beginning God created the heavens and the earth.";
491 println!("M =\t{}", message);
492 let mut cipher = Vec::<u8>::new();
493 taes.encrypt_str_into_vec(iv, &message, &mut cipher);
494 print!("C =\t");
495 for c in cipher.clone()
496 { print!("{:02X} ", c); }
497 println!();
498 let mut txt = String::new();
499 for c in cipher.clone()
500 { write!(txt, "{:02X} ", c); }
501 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
502
503 let mut recovered = vec![0; 55];
504 taes.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
505 print!("Ba =\t");
506 for b in recovered.clone()
507 { print!("{:02X} ", b); }
508 println!();
509 let mut txt = String::new();
510 for c in recovered.clone()
511 { write!(txt, "{:02X} ", c); }
512 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 ");
513
514 let mut converted = String::new();
515 unsafe { converted.as_mut_vec() }.append(&mut recovered);
516
517 println!("Bb =\t{}", converted);
518 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
519 assert_eq!(converted, message);
520 println!("-------------------------------");
521}
522
523fn bigcryptor128_decrypt_ofb_into_vec()
524{
525 println!("bigcryptor128_decrypt_ofb_into_vec()");
526 use std::io::Write;
527 use std::fmt::Write as _;
528 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
529
530 // TAES_128 case
531 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
532 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
533 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
534 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
535 println!("IV = {:#034X}", iv);
536 let message = "In the beginning God created the heavens and the earth.";
537 println!("M =\t{}", message);
538 let mut cipher = Vec::<u8>::new();
539 taes.encrypt_str_into_vec(iv, &message, &mut cipher);
540 print!("C =\t");
541 for c in cipher.clone()
542 { print!("{:02X} ", c); }
543 println!();
544 let mut txt = String::new();
545 for c in cipher.clone()
546 { write!(txt, "{:02X} ", c); }
547 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
548
549 let mut recovered = Vec::<u8>::new();
550 taes.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
551 print!("Ba =\t");
552 for b in recovered.clone()
553 { print!("{:02X} ", b); }
554 println!();
555 let mut txt = String::new();
556 for c in recovered.clone()
557 { write!(txt, "{:02X} ", c); }
558 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 ");
559
560 let mut converted = String::new();
561 unsafe { converted.as_mut_vec() }.append(&mut recovered);
562
563 println!("Bb =\t{}", converted);
564 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
565 assert_eq!(converted, message);
566 println!("-------------------------------");
567}
568
569fn bigcryptor128_decrypt_ofb_into_array()
570{
571 println!("bigcryptor128_decrypt_ofb_into_array()");
572 use std::io::Write;
573 use std::fmt::Write as _;
574 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
575
576 // TAES_128 case
577 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
578 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
579 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
580 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
581 println!("IV = {:#034X}", iv);
582 let message = "In the beginning God created the heavens and the earth.";
583 println!("M =\t{}", message);
584 let mut cipher = Vec::<u8>::new();
585 taes.encrypt_str_into_vec(iv, &message, &mut cipher);
586 print!("C =\t");
587 for c in cipher.clone()
588 { print!("{:02X} ", c); }
589 println!();
590 let mut txt = String::new();
591 for c in cipher.clone()
592 { write!(txt, "{:02X} ", c); }
593 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
594
595 let mut recovered = [0u8; 56];
596 let len = taes.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
597 print!("Ba =\t");
598 for b in recovered.clone()
599 { print!("{:02X} ", b); }
600 println!();
601 let mut txt = String::new();
602 for c in recovered.clone()
603 { write!(txt, "{:02X} ", c); }
604 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 ");
605
606 let mut converted = String::new();
607 unsafe { converted.as_mut_vec() }.write(&recovered);
608 unsafe { converted.as_mut_vec() }.truncate(len as usize);
609 println!("Bb =\t{}", converted);
610 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
611 assert_eq!(converted, message);
612 println!("-------------------------------");
613}
614
615fn bigcryptor128_decrypt_ofb_into_string()
616{
617 println!("bigcryptor128_decrypt_ofb_into_string()");
618 use std::io::Write;
619 use std::fmt::Write as _;
620 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
621
622 // TAES_128 case
623 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
624 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
625 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
626 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
627 println!("IV = {:#034X}", iv);
628 let message = "In the beginning God created the heavens and the earth.";
629 println!("M =\t{}", message);
630 let mut cipher = Vec::<u8>::new();
631 taes.encrypt_str_into_vec(iv, &message, &mut cipher);
632 print!("C =\t");
633 for c in cipher.clone()
634 { print!("{:02X} ", c); }
635 println!();
636 let mut txt = String::new();
637 for c in cipher.clone()
638 { write!(txt, "{:02X} ", c); }
639 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
640
641 let mut recovered = String::new();
642 taes.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
643 println!("B =\t{}", recovered);
644 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
645 assert_eq!(recovered, message);
646 println!("-------------------------------");
647}
648
649fn bigcryptor128_decrypt_vec_ofb()
650{
651 println!("bigcryptor128_decrypt_vec_ofb()");
652 use std::io::Write;
653 use std::fmt::Write as _;
654 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
655
656 // TAES_128 case
657 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
658 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
659 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
660 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
661 println!("IV = {:#034X}", iv);
662 let message = "In the beginning God created the heavens and the earth.";
663 println!("M =\t{}", message);
664 let mut cipher = Vec::<u8>::new();
665 taes.encrypt_str_into_vec(iv, &message, &mut cipher);
666 print!("C =\t");
667 for c in cipher.clone()
668 { print!("{:02X} ", c); }
669 println!();
670 let mut txt = String::new();
671 for c in cipher.clone()
672 { write!(txt, "{:02X} ", c); }
673 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
674
675 let mut recovered = vec![0; 55];
676 taes.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
677 print!("Ba =\t");
678 for b in recovered.clone()
679 { print!("{:02X} ", b); }
680 println!();
681 let mut txt = String::new();
682 for c in recovered.clone()
683 { write!(txt, "{:02X} ", c); }
684 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 ");
685
686 let mut converted = String::new();
687 unsafe { converted.as_mut_vec() }.append(&mut recovered);
688
689 println!("Bb =\t{}", converted);
690 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
691 assert_eq!(converted, message);
692 println!("-------------------------------");
693}
694
695fn bigcryptor128_decrypt_vec_ofb_into_vec()
696{
697 println!("bigcryptor128_decrypt_vec_ofb_into_vec()");
698 use std::io::Write;
699 use std::fmt::Write as _;
700 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
701
702 // TAES_128 case
703 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
704 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
705 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
706 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
707 println!("IV = {:#034X}", iv);
708 let message = "In the beginning God created the heavens and the earth.";
709 println!("M =\t{}", message);
710 let mut cipher = Vec::<u8>::new();
711 taes.encrypt_str_into_vec(iv, &message, &mut cipher);
712 print!("C =\t");
713 for c in cipher.clone()
714 { print!("{:02X} ", c); }
715 println!();
716 let mut txt = String::new();
717 for c in cipher.clone()
718 { write!(txt, "{:02X} ", c); }
719 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
720
721 let mut recovered = Vec::<u8>::new();
722 taes.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
723 print!("Ba =\t");
724 for b in recovered.clone()
725 { print!("{:02X} ", b); }
726 println!();
727 let mut txt = String::new();
728 for c in recovered.clone()
729 { write!(txt, "{:02X} ", c); }
730 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 ");
731
732 let mut converted = String::new();
733 unsafe { converted.as_mut_vec() }.append(&mut recovered);
734
735 println!("Bb =\t{}", converted);
736 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
737 assert_eq!(converted, message);
738 println!("-------------------------------");
739}
740
741fn bigcryptor128_decrypt_vec_ofb_into_array()
742{
743 println!("bigcryptor128_decrypt_vec_ofb_into_array()");
744 use std::io::Write;
745 use std::fmt::Write as _;
746 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
747
748 // TAES_128 case
749 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
750 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
751 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
752 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
753 println!("IV = {:#034X}", iv);
754 let message = "In the beginning God created the heavens and the earth.";
755 println!("M =\t{}", message);
756 let mut cipher = Vec::<u8>::new();
757 taes.encrypt_str_into_vec(iv, &message, &mut cipher);
758 print!("C =\t");
759 for c in cipher.clone()
760 { print!("{:02X} ", c); }
761 println!();
762 let mut txt = String::new();
763 for c in cipher.clone()
764 { write!(txt, "{:02X} ", c); }
765 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
766
767 let mut recovered = [0u8; 56];
768 let len = taes.decrypt_vec_into_array(iv, &cipher, &mut recovered);
769 print!("Ba =\t");
770 for b in recovered.clone()
771 { print!("{:02X} ", b); }
772 println!();
773 let mut txt = String::new();
774 for c in recovered.clone()
775 { write!(txt, "{:02X} ", c); }
776 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 ");
777
778 let mut converted = String::new();
779 unsafe { converted.as_mut_vec() }.write(&recovered);
780 unsafe { converted.as_mut_vec() }.truncate(len as usize);
781 println!("Bb =\t{}", converted);
782 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
783 assert_eq!(converted, message);
784 println!("-------------------------------");
785}
786
787fn bigcryptor128_decrypt_vec_ofb_into_string()
788{
789 println!("bigcryptor128_decrypt_vec_ofb_into_string()");
790 use std::io::Write;
791 use std::fmt::Write as _;
792 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
793
794 // TAES_128 case
795 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
796 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
797 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
798 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
799 println!("IV = {:#034X}", iv);
800 let message = "In the beginning God created the heavens and the earth.";
801 println!("M =\t{}", message);
802 let mut cipher = Vec::<u8>::new();
803 taes.encrypt_str_into_vec(iv, &message, &mut cipher);
804 print!("C =\t");
805 for c in cipher.clone()
806 { print!("{:02X} ", c); }
807 println!();
808 let mut txt = String::new();
809 for c in cipher.clone()
810 { write!(txt, "{:02X} ", c); }
811 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
812
813 let mut recovered = String::new();
814 taes.decrypt_vec_into_string(iv, &cipher, &mut recovered);
815 println!("B =\t{}", recovered);
816 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
817 assert_eq!(recovered, message);
818 println!("-------------------------------");
819}183fn aes_encrypt_ofb_into_vec()
184{
185 println!("aes_encrypt_ofb_into_vec()");
186 use std::io::Write;
187 use std::fmt::Write as _;
188 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
189
190 // Normal case for AES-128
191 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
192 println!("K =\t{:#016X}", key);
193 let mut a_aes = AES_128::new_with_key_u128(key);
194 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
195 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
196
197 let message = "In the beginning God created the heavens and the earth.";
198 println!("M =\t{}", message);
199 let mut cipher = Vec::<u8>::new();
200 a_aes.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher);
201 print!("C =\t");
202 for c in cipher.clone()
203 { print!("{:02X} ", c); }
204 println!();
205 let mut txt = String::new();
206 for c in cipher.clone()
207 { write!(txt, "{:02X} ", c); }
208 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
209 println!();
210
211 // Normal case for AES-192
212 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
213 print!("K =\t");
214 for i in 0..24
215 { print!("{:02X}", key[i]); }
216 println!();
217 let mut a_aes = AES_192::new_with_key(&key);
218 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
219 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
220
221 let message = "In the beginning God created the heavens and the earth.";
222 println!("M =\t{}", message);
223 let mut cipher = Vec::<u8>::new();
224 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
225 print!("C =\t");
226 for c in cipher.clone()
227 { print!("{:02X} ", c); }
228 println!();
229 let mut txt = String::new();
230 for c in cipher.clone()
231 { write!(txt, "{:02X} ", c); }
232 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
233 println!();
234
235 // Normal case for AES-256
236 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
237 print!("K =\t");
238 for i in 0..32
239 { print!("{:02X}", key[i]); }
240 println!();
241 let mut a_aes = AES_256::new_with_key(&key);
242 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
243 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
244
245 let message = "In the beginning God created the heavens and the earth.";
246 println!("M =\t{}", message);
247 let mut cipher = Vec::<u8>::new();
248 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
249 print!("C =\t");
250 for c in cipher.clone()
251 { print!("{:02X} ", c); }
252 println!();
253 let mut txt = String::new();
254 for c in cipher.clone()
255 { write!(txt, "{:02X} ", c); }
256 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
257 println!();
258
259 // Normal case for Rijndael-256-256
260 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
261 print!("K =\t");
262 for i in 0..32
263 { print!("{:02X}", key[i]); }
264 println!();
265 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
266 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
267 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
268
269 let message = "In the beginning God created the heavens and the earth.";
270 println!("M =\t{}", message);
271 let mut cipher = Vec::<u8>::new();
272 a_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
273 print!("C =\t");
274 for c in cipher.clone()
275 { print!("{:02X} ", c); }
276 println!();
277 let mut txt = String::new();
278 for c in cipher.clone()
279 { write!(txt, "{:02X} ", c); }
280 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
281 println!();
282
283 // Normal case for Rijndael-512-512 for post-quantum
284 use cryptocol::number::SharedArrays;
285 use cryptocol::hash::SHA3_512;
286 let mut sha3 = SHA3_512::new();
287 sha3.absorb_str("Post-quantum");
288 let key: [u8; 64] = sha3.get_hash_value_in_array();
289 print!("K =\t");
290 for i in 0..64
291 { print!("{:02X}", key[i]); }
292 println!();
293 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
294 sha3.absorb_str("Initialize");
295 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
296 iv.src = sha3.get_hash_value_in_array();
297 let iv = unsafe { iv.des };
298 print!("IV =\t");
299 for i in 0..16
300 { print!("{:08X}", iv[i].to_be()); }
301 println!();
302 let message = "In the beginning God created the heavens and the earth.";
303 println!("M =\t{}", message);
304 let mut cipher = Vec::<u8>::new();
305 a_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
306 print!("C =\t");
307 for c in cipher.clone()
308 { print!("{:02X} ", c); }
309 println!();
310 let mut txt = String::new();
311 for c in cipher.clone()
312 { write!(txt, "{:02X} ", c); }
313 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
314 println!("-------------------------------");
315}
316
317fn aes_encrypt_ofb_into_array()
318{
319 println!("aes_encrypt_ofb_into_array()");
320 use std::io::Write;
321 use std::fmt::Write as _;
322 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
323
324 // Normal case for AES-128
325 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
326 println!("K =\t{:#016X}", key);
327 let mut a_aes = AES_128::new_with_key_u128(key);
328 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
329 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
330
331 let message = "In the beginning God created the heavens and the earth.";
332 println!("M =\t{}", message);
333 let mut cipher = [0_u8; 55];
334 a_aes.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
335 print!("C =\t");
336 for c in cipher.clone()
337 { print!("{:02X} ", c); }
338 println!();
339 let mut txt = String::new();
340 for c in cipher.clone()
341 { write!(txt, "{:02X} ", c); }
342 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
343 println!();
344
345 // Normal case for AES-192
346 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
347 print!("K =\t");
348 for i in 0..24
349 { print!("{:02X}", key[i]); }
350 println!();
351 let mut a_aes = AES_192::new_with_key(&key);
352 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
353 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
354
355 let message = "In the beginning God created the heavens and the earth.";
356 println!("M =\t{}", message);
357 let mut cipher = [0_u8; 55];
358 a_aes.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
359 print!("C =\t");
360 for c in cipher.clone()
361 { print!("{:02X} ", c); }
362 println!();
363 let mut txt = String::new();
364 for c in cipher.clone()
365 { write!(txt, "{:02X} ", c); }
366 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
367 println!();
368
369 // Normal case for AES-256
370 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
371 print!("K =\t");
372 for i in 0..32
373 { print!("{:02X}", key[i]); }
374 println!();
375 let mut a_aes = AES_256::new_with_key(&key);
376 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
377 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
378
379 let message = "In the beginning God created the heavens and the earth.";
380 println!("M =\t{}", message);
381 let mut cipher = [0_u8; 55];
382 a_aes.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
383 print!("C =\t");
384 for c in cipher.clone()
385 { print!("{:02X} ", c); }
386 println!();
387 let mut txt = String::new();
388 for c in cipher.clone()
389 { write!(txt, "{:02X} ", c); }
390 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
391 println!();
392
393 // Normal case for Rijndael-256-256
394 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
395 print!("K =\t");
396 for i in 0..32
397 { print!("{:02X}", key[i]); }
398 println!();
399 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
400 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
401 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
402
403 let message = "In the beginning God created the heavens and the earth.";
404 println!("M =\t{}", message);
405 let mut cipher = [0_u8; 55];
406 a_rijndael.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
407 print!("C =\t");
408 for c in cipher.clone()
409 { print!("{:02X} ", c); }
410 println!();
411 let mut txt = String::new();
412 for c in cipher.clone()
413 { write!(txt, "{:02X} ", c); }
414 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
415 println!();
416
417 // Normal case for Rijndael-512-512 for post-quantum
418 use cryptocol::number::SharedArrays;
419 use cryptocol::hash::SHA3_512;
420 let mut sha3 = SHA3_512::new();
421 sha3.absorb_str("Post-quantum");
422 let key: [u8; 64] = sha3.get_hash_value_in_array();
423 print!("K =\t");
424 for i in 0..64
425 { print!("{:02X}", key[i]); }
426 println!();
427 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
428 sha3.absorb_str("Initialize");
429 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
430 iv.src = sha3.get_hash_value_in_array();
431 let iv = unsafe { iv.des };
432 print!("IV =\t");
433 for i in 0..16
434 { print!("{:08X}", iv[i].to_be()); }
435 println!();
436 let message = "In the beginning God created the heavens and the earth.";
437 println!("M =\t{}", message);
438 let mut cipher = [0_u8; 55];
439 a_rijndael.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
440 print!("C =\t");
441 for c in cipher.clone()
442 { print!("{:02X} ", c); }
443 println!();
444 let mut txt = String::new();
445 for c in cipher.clone()
446 { write!(txt, "{:02X} ", c); }
447 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
448 println!("-------------------------------");
449}
450
451fn aes_encrypt_str_ofb()
452{
453 println!("aes_encrypt_str_ofb()");
454 use std::io::Write;
455 use std::fmt::Write as _;
456 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
457
458 // Normal case for AES-128
459 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
460 println!("K =\t{:#016X}", key);
461 let mut a_aes = AES_128::new_with_key_u128(key);
462 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
463 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
464
465 let message = "In the beginning God created the heavens and the earth.";
466 println!("M =\t{}", message);
467 let mut cipher = [0_u8; 55];
468 a_aes.encrypt_str(iv, &message, cipher.as_mut_ptr());
469 print!("C =\t");
470 for c in cipher.clone()
471 { print!("{:02X} ", c); }
472 println!();
473 let mut txt = String::new();
474 for c in cipher.clone()
475 { write!(txt, "{:02X} ", c); }
476 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
477 println!();
478
479 // Normal case for AES-192
480 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
481 print!("K =\t");
482 for i in 0..24
483 { print!("{:02X}", key[i]); }
484 println!();
485 let mut a_aes = AES_192::new_with_key(&key);
486 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
487 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
488
489 let message = "In the beginning God created the heavens and the earth.";
490 println!("M =\t{}", message);
491 let mut cipher = [0_u8; 55];
492 a_aes.encrypt_str(iv, &message, cipher.as_mut_ptr());
493 print!("C =\t");
494 for c in cipher.clone()
495 { print!("{:02X} ", c); }
496 println!();
497 let mut txt = String::new();
498 for c in cipher.clone()
499 { write!(txt, "{:02X} ", c); }
500 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
501 println!();
502
503 // Normal case for AES-256
504 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
505 print!("K =\t");
506 for i in 0..32
507 { print!("{:02X}", key[i]); }
508 println!();
509 let mut a_aes = AES_256::new_with_key(&key);
510 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
511 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
512
513 let message = "In the beginning God created the heavens and the earth.";
514 println!("M =\t{}", message);
515 let mut cipher = [0_u8; 55];
516 a_aes.encrypt_str(iv, &message, cipher.as_mut_ptr());
517 print!("C =\t");
518 for c in cipher.clone()
519 { print!("{:02X} ", c); }
520 println!();
521 let mut txt = String::new();
522 for c in cipher.clone()
523 { write!(txt, "{:02X} ", c); }
524 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
525 println!();
526
527 // Normal case for Rijndael-256-256
528 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
529 print!("K =\t");
530 for i in 0..32
531 { print!("{:02X}", key[i]); }
532 println!();
533 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
534 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
535 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
536
537 let message = "In the beginning God created the heavens and the earth.";
538 println!("M =\t{}", message);
539 let mut cipher = [0_u8; 55];
540 a_rijndael.encrypt_str(iv, &message, cipher.as_mut_ptr());
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, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
549 println!();
550
551 // Normal case for Rijndael-512-512 for post-quantum
552 use cryptocol::number::SharedArrays;
553 use cryptocol::hash::SHA3_512;
554 let mut sha3 = SHA3_512::new();
555 sha3.absorb_str("Post-quantum");
556 let key: [u8; 64] = sha3.get_hash_value_in_array();
557 print!("K =\t");
558 for i in 0..64
559 { print!("{:02X}", key[i]); }
560 println!();
561 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
562 sha3.absorb_str("Initialize");
563 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
564 iv.src = sha3.get_hash_value_in_array();
565 let iv = unsafe { iv.des };
566 print!("IV =\t");
567 for i in 0..16
568 { print!("{:08X}", iv[i].to_be()); }
569 println!();
570 let message = "In the beginning God created the heavens and the earth.";
571 println!("M =\t{}", message);
572 let mut cipher = [0_u8; 55];
573 a_rijndael.encrypt_str(iv, &message, cipher.as_mut_ptr());
574 print!("C =\t");
575 for c in cipher.clone()
576 { print!("{:02X} ", c); }
577 println!();
578 let mut txt = String::new();
579 for c in cipher.clone()
580 { write!(txt, "{:02X} ", c); }
581 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
582 println!("-------------------------------");
583}
584
585fn aes_encrypt_str_ofb_into_vec()
586{
587 println!("aes_encrypt_str_ofb_into_vec()");
588 use std::io::Write;
589 use std::fmt::Write as _;
590 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
591
592 // Normal case for AES-128
593 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
594 println!("K =\t{:#016X}", key);
595 let mut a_aes = AES_128::new_with_key_u128(key);
596 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
597 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
598
599 let message = "In the beginning God created the heavens and the earth.";
600 println!("M =\t{}", message);
601 let mut cipher = Vec::<u8>::new();
602 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
603 print!("C =\t");
604 for c in cipher.clone()
605 { print!("{:02X} ", c); }
606 println!();
607 let mut txt = String::new();
608 for c in cipher.clone()
609 { write!(txt, "{:02X} ", c); }
610 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
611 println!();
612
613 // Normal case for AES-192
614 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
615 print!("K =\t");
616 for i in 0..24
617 { print!("{:02X}", key[i]); }
618 println!();
619 let mut a_aes = AES_192::new_with_key(&key);
620 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
621 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
622
623 let message = "In the beginning God created the heavens and the earth.";
624 println!("M =\t{}", message);
625 let mut cipher = Vec::<u8>::new();
626 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
627 print!("C =\t");
628 for c in cipher.clone()
629 { print!("{:02X} ", c); }
630 println!();
631 let mut txt = String::new();
632 for c in cipher.clone()
633 { write!(txt, "{:02X} ", c); }
634 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
635 println!();
636
637 // Normal case for AES-256
638 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
639 print!("K =\t");
640 for i in 0..32
641 { print!("{:02X}", key[i]); }
642 println!();
643 let mut a_aes = AES_256::new_with_key(&key);
644 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
645 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
646
647 let message = "In the beginning God created the heavens and the earth.";
648 println!("M =\t{}", message);
649 let mut cipher = Vec::<u8>::new();
650 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
651 print!("C =\t");
652 for c in cipher.clone()
653 { print!("{:02X} ", c); }
654 println!();
655 let mut txt = String::new();
656 for c in cipher.clone()
657 { write!(txt, "{:02X} ", c); }
658 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
659 println!();
660
661 // Normal case for Rijndael-256-256
662 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
663 print!("K =\t");
664 for i in 0..32
665 { print!("{:02X}", key[i]); }
666 println!();
667 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
668 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
669 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
670
671 let message = "In the beginning God created the heavens and the earth.";
672 println!("M =\t{}", message);
673 let mut cipher = Vec::<u8>::new();
674 a_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
675 print!("C =\t");
676 for c in cipher.clone()
677 { print!("{:02X} ", c); }
678 println!();
679 let mut txt = String::new();
680 for c in cipher.clone()
681 { write!(txt, "{:02X} ", c); }
682 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
683 println!();
684
685 // Normal case for Rijndael-512-512 for post-quantum
686 use cryptocol::number::SharedArrays;
687 use cryptocol::hash::SHA3_512;
688 let mut sha3 = SHA3_512::new();
689 sha3.absorb_str("Post-quantum");
690 let key: [u8; 64] = sha3.get_hash_value_in_array();
691 print!("K =\t");
692 for i in 0..64
693 { print!("{:02X}", key[i]); }
694 println!();
695 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
696 sha3.absorb_str("Initialize");
697 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
698 iv.src = sha3.get_hash_value_in_array();
699 let iv = unsafe { iv.des };
700 print!("IV =\t");
701 for i in 0..16
702 { print!("{:08X}", iv[i].to_be()); }
703 println!();
704 let message = "In the beginning God created the heavens and the earth.";
705 println!("M =\t{}", message);
706 let mut cipher = Vec::<u8>::new();
707 a_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
708 print!("C =\t");
709 for c in cipher.clone()
710 { print!("{:02X} ", c); }
711 println!();
712 let mut txt = String::new();
713 for c in cipher.clone()
714 { write!(txt, "{:02X} ", c); }
715 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
716 println!("-------------------------------");
717}
718
719fn aes_encrypt_str_ofb_into_array()
720{
721 println!("aes_encrypt_str_ofb_into_array()");
722 use std::io::Write;
723 use std::fmt::Write as _;
724 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
725
726 // Normal case for AES-128
727 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
728 println!("K =\t{:#016X}", key);
729 let mut a_aes = AES_128::new_with_key_u128(key);
730 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
731 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
732
733 let message = "In the beginning God created the heavens and the earth.";
734 println!("M =\t{}", message);
735 let mut cipher = [0_u8; 55];
736 a_aes.encrypt_str_into_array(iv, &message, &mut cipher);
737 print!("C =\t");
738 for c in cipher.clone()
739 { print!("{:02X} ", c); }
740 println!();
741 let mut txt = String::new();
742 for c in cipher.clone()
743 { write!(txt, "{:02X} ", c); }
744 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
745 println!();
746
747 // Normal case for AES-192
748 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
749 print!("K =\t");
750 for i in 0..24
751 { print!("{:02X}", key[i]); }
752 println!();
753 let mut a_aes = AES_192::new_with_key(&key);
754 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
755 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
756
757 let message = "In the beginning God created the heavens and the earth.";
758 println!("M =\t{}", message);
759 let mut cipher = [0_u8; 55];
760 a_aes.encrypt_str_into_array(iv, &message, &mut cipher);
761 print!("C =\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, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
769 println!();
770
771 // Normal case for AES-256
772 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
773 print!("K =\t");
774 for i in 0..32
775 { print!("{:02X}", key[i]); }
776 println!();
777 let mut a_aes = AES_256::new_with_key(&key);
778 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
779 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
780
781 let message = "In the beginning God created the heavens and the earth.";
782 println!("M =\t{}", message);
783 let mut cipher = [0_u8; 55];
784 a_aes.encrypt_str_into_array(iv, &message, &mut cipher);
785 print!("C =\t");
786 for c in cipher.clone()
787 { print!("{:02X} ", c); }
788 println!();
789 let mut txt = String::new();
790 for c in cipher.clone()
791 { write!(txt, "{:02X} ", c); }
792 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
793 println!();
794
795 // Normal case for Rijndael-256-256
796 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
797 print!("K =\t");
798 for i in 0..32
799 { print!("{:02X}", key[i]); }
800 println!();
801 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
802 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
803 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
804
805 let message = "In the beginning God created the heavens and the earth.";
806 println!("M =\t{}", message);
807 let mut cipher = [0_u8; 55];
808 a_rijndael.encrypt_str_into_array(iv, &message, &mut cipher);
809 print!("C =\t");
810 for c in cipher.clone()
811 { print!("{:02X} ", c); }
812 println!();
813 let mut txt = String::new();
814 for c in cipher.clone()
815 { write!(txt, "{:02X} ", c); }
816 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
817 println!();
818
819 // Normal case for Rijndael-512-512 for post-quantum
820 use cryptocol::number::SharedArrays;
821 use cryptocol::hash::SHA3_512;
822 let mut sha3 = SHA3_512::new();
823 sha3.absorb_str("Post-quantum");
824 let key: [u8; 64] = sha3.get_hash_value_in_array();
825 print!("K =\t");
826 for i in 0..64
827 { print!("{:02X}", key[i]); }
828 println!();
829 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
830 sha3.absorb_str("Initialize");
831 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
832 iv.src = sha3.get_hash_value_in_array();
833 let iv = unsafe { iv.des };
834 print!("IV =\t");
835 for i in 0..16
836 { print!("{:08X}", iv[i].to_be()); }
837 println!();
838 let message = "In the beginning God created the heavens and the earth.";
839 println!("M =\t{}", message);
840 let mut cipher = [0_u8; 55];
841 a_rijndael.encrypt_str_into_array(iv, &message, &mut cipher);
842 print!("C =\t");
843 for c in cipher.clone()
844 { print!("{:02X} ", c); }
845 println!();
846 let mut txt = String::new();
847 for c in cipher.clone()
848 { write!(txt, "{:02X} ", c); }
849 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
850 println!("-------------------------------");
851}
852
853fn aes_encrypt_string_ofb()
854{
855 println!("aes_encrypt_string_ofb()");
856 use std::io::Write;
857 use std::fmt::Write as _;
858 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
859
860 // Normal case for AES-128
861 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
862 println!("K =\t{:#016X}", key);
863 let mut a_aes = AES_128::new_with_key_u128(key);
864 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
865 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
866
867 let message = "In the beginning God created the heavens and the earth.".to_string();
868 println!("M =\t{}", message);
869 let mut cipher = [0_u8; 55];
870 a_aes.encrypt_string(iv, &message, cipher.as_mut_ptr());
871 print!("C =\t");
872 for c in cipher.clone()
873 { print!("{:02X} ", c); }
874 println!();
875 let mut txt = String::new();
876 for c in cipher.clone()
877 { write!(txt, "{:02X} ", c); }
878 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
879 println!();
880
881 // Normal case for AES-192
882 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
883 print!("K =\t");
884 for i in 0..24
885 { print!("{:02X}", key[i]); }
886 println!();
887 let mut a_aes = AES_192::new_with_key(&key);
888 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
889 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
890
891 let message = "In the beginning God created the heavens and the earth.".to_string();
892 println!("M =\t{}", message);
893 let mut cipher = [0_u8; 55];
894 a_aes.encrypt_string(iv, &message, cipher.as_mut_ptr());
895 print!("C =\t");
896 for c in cipher.clone()
897 { print!("{:02X} ", c); }
898 println!();
899 let mut txt = String::new();
900 for c in cipher.clone()
901 { write!(txt, "{:02X} ", c); }
902 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
903 println!();
904
905 // Normal case for AES-256
906 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
907 print!("K =\t");
908 for i in 0..32
909 { print!("{:02X}", key[i]); }
910 println!();
911 let mut a_aes = AES_256::new_with_key(&key);
912 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
913 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
914
915 let message = "In the beginning God created the heavens and the earth.".to_string();
916 println!("M =\t{}", message);
917 let mut cipher = [0_u8; 55];
918 a_aes.encrypt_string(iv, &message, cipher.as_mut_ptr());
919 print!("C =\t");
920 for c in cipher.clone()
921 { print!("{:02X} ", c); }
922 println!();
923 let mut txt = String::new();
924 for c in cipher.clone()
925 { write!(txt, "{:02X} ", c); }
926 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
927 println!();
928
929 // Normal case for Rijndael-256-256
930 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
931 print!("K =\t");
932 for i in 0..32
933 { print!("{:02X}", key[i]); }
934 println!();
935 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
936 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
937 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
938
939 let message = "In the beginning God created the heavens and the earth.".to_string();
940 println!("M =\t{}", message);
941 let mut cipher = [0_u8; 55];
942 a_rijndael.encrypt_string(iv, &message, cipher.as_mut_ptr());
943 print!("C =\t");
944 for c in cipher.clone()
945 { print!("{:02X} ", c); }
946 println!();
947 let mut txt = String::new();
948 for c in cipher.clone()
949 { write!(txt, "{:02X} ", c); }
950 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
951 println!();
952
953 // Normal case for Rijndael-512-512 for post-quantum
954 use cryptocol::number::SharedArrays;
955 use cryptocol::hash::SHA3_512;
956 let mut sha3 = SHA3_512::new();
957 sha3.absorb_str("Post-quantum");
958 let key: [u8; 64] = sha3.get_hash_value_in_array();
959 print!("K =\t");
960 for i in 0..64
961 { print!("{:02X}", key[i]); }
962 println!();
963 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
964 sha3.absorb_str("Initialize");
965 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
966 iv.src = sha3.get_hash_value_in_array();
967 let iv = unsafe { iv.des };
968 print!("IV =\t");
969 for i in 0..16
970 { print!("{:08X}", iv[i].to_be()); }
971 println!();
972
973 let message = "In the beginning God created the heavens and the earth.".to_string();
974 println!("M =\t{}", message);
975 let mut cipher = [0_u8; 55];
976 a_rijndael.encrypt_string(iv, &message, cipher.as_mut_ptr());
977 print!("C =\t");
978 for c in cipher.clone()
979 { print!("{:02X} ", c); }
980 println!();
981 let mut txt = String::new();
982 for c in cipher.clone()
983 { write!(txt, "{:02X} ", c); }
984 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
985 println!("-------------------------------");
986}
987
988fn aes_encrypt_string_ofb_into_vec()
989{
990 println!("aes_encrypt_string_ofb_into_vec()");
991 use std::io::Write;
992 use std::fmt::Write as _;
993 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
994
995 // Normal case for AES-128
996 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
997 println!("K =\t{:#016X}", key);
998 let mut a_aes = AES_128::new_with_key_u128(key);
999 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1000 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1001
1002 let message = "In the beginning God created the heavens and the earth.".to_string();
1003 println!("M =\t{}", message);
1004 let mut cipher = Vec::<u8>::new();
1005 a_aes.encrypt_string_into_vec(iv, &message, &mut cipher);
1006 print!("C =\t");
1007 for c in cipher.clone()
1008 { print!("{:02X} ", c); }
1009 println!();
1010 let mut txt = String::new();
1011 for c in cipher.clone()
1012 { write!(txt, "{:02X} ", c); }
1013 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1014 println!();
1015
1016 // Normal case for AES-192
1017 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1018 print!("K =\t");
1019 for i in 0..24
1020 { print!("{:02X}", key[i]); }
1021 println!();
1022 let mut a_aes = AES_192::new_with_key(&key);
1023 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1024 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1025
1026 let message = "In the beginning God created the heavens and the earth.".to_string();
1027 println!("M =\t{}", message);
1028 let mut cipher = Vec::<u8>::new();
1029 a_aes.encrypt_string_into_vec(iv, &message, &mut cipher);
1030 print!("C =\t");
1031 for c in cipher.clone()
1032 { print!("{:02X} ", c); }
1033 println!();
1034 let mut txt = String::new();
1035 for c in cipher.clone()
1036 { write!(txt, "{:02X} ", c); }
1037 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
1038 println!();
1039
1040 // Normal case for AES-256
1041 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1042 print!("K =\t");
1043 for i in 0..32
1044 { print!("{:02X}", key[i]); }
1045 println!();
1046 let mut a_aes = AES_256::new_with_key(&key);
1047 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1048 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1049
1050 let message = "In the beginning God created the heavens and the earth.".to_string();
1051 println!("M =\t{}", message);
1052 let mut cipher = Vec::<u8>::new();
1053 a_aes.encrypt_string_into_vec(iv, &message, &mut cipher);
1054 print!("C =\t");
1055 for c in cipher.clone()
1056 { print!("{:02X} ", c); }
1057 println!();
1058 let mut txt = String::new();
1059 for c in cipher.clone()
1060 { write!(txt, "{:02X} ", c); }
1061 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
1062 println!();
1063
1064 // Normal case for Rijndael-256-256
1065 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1066 print!("K =\t");
1067 for i in 0..32
1068 { print!("{:02X}", key[i]); }
1069 println!();
1070 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
1071 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1072 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
1073
1074 let message = "In the beginning God created the heavens and the earth.".to_string();
1075 println!("M =\t{}", message);
1076 let mut cipher = Vec::<u8>::new();
1077 a_rijndael.encrypt_string_into_vec(iv, &message, &mut cipher);
1078 print!("C =\t");
1079 for c in cipher.clone()
1080 { print!("{:02X} ", c); }
1081 println!();
1082 let mut txt = String::new();
1083 for c in cipher.clone()
1084 { write!(txt, "{:02X} ", c); }
1085 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
1086 println!();
1087
1088 // Normal case for Rijndael-512-512 for post-quantum
1089 use cryptocol::number::SharedArrays;
1090 use cryptocol::hash::SHA3_512;
1091 let mut sha3 = SHA3_512::new();
1092 sha3.absorb_str("Post-quantum");
1093 let key: [u8; 64] = sha3.get_hash_value_in_array();
1094 print!("K =\t");
1095 for i in 0..64
1096 { print!("{:02X}", key[i]); }
1097 println!();
1098 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
1099 sha3.absorb_str("Initialize");
1100 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
1101 iv.src = sha3.get_hash_value_in_array();
1102 let iv = unsafe { iv.des };
1103 print!("IV =\t");
1104 for i in 0..16
1105 { print!("{:08X}", iv[i].to_be()); }
1106 println!();
1107 let message = "In the beginning God created the heavens and the earth.".to_string();
1108 println!("M =\t{}", message);
1109 let mut cipher = Vec::<u8>::new();
1110 a_rijndael.encrypt_string_into_vec(iv, &message, &mut cipher);
1111 print!("C =\t");
1112 for c in cipher.clone()
1113 { print!("{:02X} ", c); }
1114 println!();
1115 let mut txt = String::new();
1116 for c in cipher.clone()
1117 { write!(txt, "{:02X} ", c); }
1118 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
1119 println!("-------------------------------");
1120}
1121
1122fn aes_encrypt_string_ofb_into_array()
1123{
1124 println!("aes_encrypt_string_ofb_into_array()");
1125 use std::io::Write;
1126 use std::fmt::Write as _;
1127 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
1128
1129 // Normal case for AES-128
1130 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1131 println!("K =\t{:#016X}", key);
1132 let mut a_aes = AES_128::new_with_key_u128(key);
1133 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1134 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1135
1136 let message = "In the beginning God created the heavens and the earth.".to_string();
1137 println!("M =\t{}", message);
1138 let mut cipher = [0_u8; 55];
1139 a_aes.encrypt_string_into_array(iv, &message, &mut cipher);
1140 print!("C =\t");
1141 for c in cipher.clone()
1142 { print!("{:02X} ", c); }
1143 println!();
1144 let mut txt = String::new();
1145 for c in cipher.clone()
1146 { write!(txt, "{:02X} ", c); }
1147 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1148 println!();
1149
1150 // Normal case for AES-192
1151 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1152 print!("K =\t");
1153 for i in 0..24
1154 { print!("{:02X}", key[i]); }
1155 println!();
1156 let mut a_aes = AES_192::new_with_key(&key);
1157 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1158 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1159
1160 let message = "In the beginning God created the heavens and the earth.".to_string();
1161 println!("M =\t{}", message);
1162 let mut cipher = [0_u8; 55];
1163 a_aes.encrypt_string_into_array(iv, &message, &mut cipher);
1164 print!("C =\t");
1165 for c in cipher.clone()
1166 { print!("{:02X} ", c); }
1167 println!();
1168 let mut txt = String::new();
1169 for c in cipher.clone()
1170 { write!(txt, "{:02X} ", c); }
1171 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
1172 println!();
1173
1174 // Normal case for AES-256
1175 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1176 print!("K =\t");
1177 for i in 0..32
1178 { print!("{:02X}", key[i]); }
1179 println!();
1180 let mut a_aes = AES_256::new_with_key(&key);
1181 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1182 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1183
1184 let message = "In the beginning God created the heavens and the earth.".to_string();
1185 println!("M =\t{}", message);
1186 let mut cipher = [0_u8; 55];
1187 a_aes.encrypt_string_into_array(iv, &message, &mut cipher);
1188 print!("C =\t");
1189 for c in cipher.clone()
1190 { print!("{:02X} ", c); }
1191 println!();
1192 let mut txt = String::new();
1193 for c in cipher.clone()
1194 { write!(txt, "{:02X} ", c); }
1195 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
1196 println!();
1197
1198 // Normal case for Rijndael-256-256
1199 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1200 print!("K =\t");
1201 for i in 0..32
1202 { print!("{:02X}", key[i]); }
1203 println!();
1204 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
1205 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1206 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
1207
1208 let message = "In the beginning God created the heavens and the earth.".to_string();
1209 println!("M =\t{}", message);
1210 let mut cipher = [0_u8; 55];
1211 a_rijndael.encrypt_string_into_array(iv, &message, &mut cipher);
1212 print!("C =\t");
1213 for c in cipher.clone()
1214 { print!("{:02X} ", c); }
1215 println!();
1216 let mut txt = String::new();
1217 for c in cipher.clone()
1218 { write!(txt, "{:02X} ", c); }
1219 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
1220 println!();
1221
1222 // Normal case for Rijndael-512-512 for post-quantum
1223 use cryptocol::number::SharedArrays;
1224 use cryptocol::hash::SHA3_512;
1225 let mut sha3 = SHA3_512::new();
1226 sha3.absorb_str("Post-quantum");
1227 let key: [u8; 64] = sha3.get_hash_value_in_array();
1228 print!("K =\t");
1229 for i in 0..64
1230 { print!("{:02X}", key[i]); }
1231 println!();
1232 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
1233 sha3.absorb_str("Initialize");
1234 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
1235 iv.src = sha3.get_hash_value_in_array();
1236 let iv = unsafe { iv.des };
1237 print!("IV =\t");
1238 for i in 0..16
1239 { print!("{:08X}", iv[i].to_be()); }
1240 println!();
1241 let message = "In the beginning God created the heavens and the earth.".to_string();
1242 println!("M =\t{}", message);
1243 let mut cipher = [0_u8; 55];
1244 a_rijndael.encrypt_string_into_array(iv, &message, &mut cipher);
1245 print!("C =\t");
1246 for c in cipher.clone()
1247 { print!("{:02X} ", c); }
1248 println!();
1249 let mut txt = String::new();
1250 for c in cipher.clone()
1251 { write!(txt, "{:02X} ", c); }
1252 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
1253 println!("-------------------------------");
1254}
1255
1256fn aes_encrypt_vec_ofb()
1257{
1258 println!("aes_encrypt_vec_ofb()");
1259 use std::io::Write;
1260 use std::fmt::Write as _;
1261 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
1262
1263 // Normal case for AES-128
1264 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1265 println!("K =\t{:#016X}", key);
1266 let mut a_aes = AES_128::new_with_key_u128(key);
1267 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1268 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1269
1270 let message = "In the beginning God created the heavens and the earth.";
1271 println!("M =\t{}", message);
1272 let message = unsafe { message.to_string().as_mut_vec().clone() };
1273 let mut cipher = [0_u8; 55];
1274 a_aes.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1275 print!("C =\t");
1276 for c in cipher.clone()
1277 { print!("{:02X} ", c); }
1278 println!();
1279 let mut txt = String::new();
1280 for c in cipher.clone()
1281 { write!(txt, "{:02X} ", c); }
1282 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1283 println!();
1284
1285 // Normal case for AES-192
1286 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1287 print!("K =\t");
1288 for i in 0..24
1289 { print!("{:02X}", key[i]); }
1290 println!();
1291 let mut a_aes = AES_192::new_with_key(&key);
1292 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1293 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1294
1295 let message = "In the beginning God created the heavens and the earth.";
1296 println!("M =\t{}", message);
1297 let message = unsafe { message.to_string().as_mut_vec().clone() };
1298 let mut cipher = [0_u8; 55];
1299 a_aes.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1300 print!("C =\t");
1301 for c in cipher.clone()
1302 { print!("{:02X} ", c); }
1303 println!();
1304 let mut txt = String::new();
1305 for c in cipher.clone()
1306 { write!(txt, "{:02X} ", c); }
1307 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
1308 println!();
1309
1310 // Normal case for AES-256
1311 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1312 print!("K =\t");
1313 for i in 0..32
1314 { print!("{:02X}", key[i]); }
1315 println!();
1316 let mut a_aes = AES_256::new_with_key(&key);
1317 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1318 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1319
1320 let message = "In the beginning God created the heavens and the earth.";
1321 println!("M =\t{}", message);
1322 let message = unsafe { message.to_string().as_mut_vec().clone() };
1323 let mut cipher = [0_u8; 55];
1324 a_aes.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1325 print!("C =\t");
1326 for c in cipher.clone()
1327 { print!("{:02X} ", c); }
1328 println!();
1329 let mut txt = String::new();
1330 for c in cipher.clone()
1331 { write!(txt, "{:02X} ", c); }
1332 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
1333 println!();
1334
1335 // Normal case for Rijndael-256-256
1336 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1337 print!("K =\t");
1338 for i in 0..32
1339 { print!("{:02X}", key[i]); }
1340 println!();
1341 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
1342 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1343 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
1344
1345 let message = "In the beginning God created the heavens and the earth.";
1346 println!("M =\t{}", message);
1347 let message = unsafe { message.to_string().as_mut_vec().clone() };
1348 let mut cipher = [0_u8; 55];
1349 a_rijndael.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1350 print!("C =\t");
1351 for c in cipher.clone()
1352 { print!("{:02X} ", c); }
1353 println!();
1354 let mut txt = String::new();
1355 for c in cipher.clone()
1356 { write!(txt, "{:02X} ", c); }
1357 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
1358 println!();
1359
1360 // Normal case for Rijndael-512-512 for post-quantum
1361 use cryptocol::number::SharedArrays;
1362 use cryptocol::hash::SHA3_512;
1363 let mut sha3 = SHA3_512::new();
1364 sha3.absorb_str("Post-quantum");
1365 let key: [u8; 64] = sha3.get_hash_value_in_array();
1366 print!("K =\t");
1367 for i in 0..64
1368 { print!("{:02X}", key[i]); }
1369 println!();
1370 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
1371 sha3.absorb_str("Initialize");
1372 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
1373 iv.src = sha3.get_hash_value_in_array();
1374 let iv = unsafe { iv.des };
1375 print!("IV =\t");
1376 for i in 0..16
1377 { print!("{:08X}", iv[i].to_be()); }
1378 println!();
1379 let message = "In the beginning God created the heavens and the earth.";
1380 println!("M =\t{}", message);
1381 let message = unsafe { message.to_string().as_mut_vec().clone() };
1382 let mut cipher = [0_u8; 55];
1383 a_rijndael.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1384 print!("C =\t");
1385 for c in cipher.clone()
1386 { print!("{:02X} ", c); }
1387 println!();
1388 let mut txt = String::new();
1389 for c in cipher.clone()
1390 { write!(txt, "{:02X} ", c); }
1391 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
1392 println!("-------------------------------");
1393}
1394
1395fn aes_encrypt_vec_ofb_into_vec()
1396{
1397 println!("aes_encrypt_vec_ofb_into_vec()");
1398 use std::io::Write;
1399 use std::fmt::Write as _;
1400 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
1401
1402 // Normal case for AES-128
1403 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1404 println!("K =\t{:#016X}", key);
1405 let mut a_aes = AES_128::new_with_key_u128(key);
1406 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1407 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1408
1409 let message = "In the beginning God created the heavens and the earth.";
1410 println!("M =\t{}", message);
1411 let message = unsafe { message.to_string().as_mut_vec().clone() };
1412 let mut cipher = Vec::<u8>::new();
1413 a_aes.encrypt_vec_into_vec(iv, &message, &mut cipher);
1414 print!("C =\t");
1415 for c in cipher.clone()
1416 { print!("{:02X} ", c); }
1417 println!();
1418 let mut txt = String::new();
1419 for c in cipher.clone()
1420 { write!(txt, "{:02X} ", c); }
1421 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1422 println!();
1423
1424 // Normal case for AES-192
1425 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1426 print!("K =\t");
1427 for i in 0..24
1428 { print!("{:02X}", key[i]); }
1429 println!();
1430 let mut a_aes = AES_192::new_with_key(&key);
1431 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1432 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1433
1434 let message = "In the beginning God created the heavens and the earth.";
1435 println!("M =\t{}", message);
1436 let message = unsafe { message.to_string().as_mut_vec().clone() };
1437 let mut cipher = Vec::<u8>::new();
1438 a_aes.encrypt_vec_into_vec(iv, &message, &mut cipher);
1439 print!("C =\t");
1440 for c in cipher.clone()
1441 { print!("{:02X} ", c); }
1442 println!();
1443 let mut txt = String::new();
1444 for c in cipher.clone()
1445 { write!(txt, "{:02X} ", c); }
1446 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
1447 println!();
1448
1449 // Normal case for AES-256
1450 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1451 print!("K =\t");
1452 for i in 0..32
1453 { print!("{:02X}", key[i]); }
1454 println!();
1455 let mut a_aes = AES_256::new_with_key(&key);
1456 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1457 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1458
1459 let message = "In the beginning God created the heavens and the earth.";
1460 println!("M =\t{}", message);
1461 let message = unsafe { message.to_string().as_mut_vec().clone() };
1462 let mut cipher = Vec::<u8>::new();
1463 a_aes.encrypt_vec_into_vec(iv, &message, &mut cipher);
1464 print!("C =\t");
1465 for c in cipher.clone()
1466 { print!("{:02X} ", c); }
1467 println!();
1468 let mut txt = String::new();
1469 for c in cipher.clone()
1470 { write!(txt, "{:02X} ", c); }
1471 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
1472 println!();
1473
1474 // Normal case for Rijndael-256-256
1475 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1476 print!("K =\t");
1477 for i in 0..32
1478 { print!("{:02X}", key[i]); }
1479 println!();
1480 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
1481 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1482 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
1483
1484 let message = "In the beginning God created the heavens and the earth.";
1485 println!("M =\t{}", message);
1486 let message = unsafe { message.to_string().as_mut_vec().clone() };
1487 let mut cipher = Vec::<u8>::new();
1488 a_rijndael.encrypt_vec_into_vec(iv, &message, &mut cipher);
1489 print!("C =\t");
1490 for c in cipher.clone()
1491 { print!("{:02X} ", c); }
1492 println!();
1493 let mut txt = String::new();
1494 for c in cipher.clone()
1495 { write!(txt, "{:02X} ", c); }
1496 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
1497 println!();
1498
1499 // Normal case for Rijndael-512-512 for post-quantum
1500 use cryptocol::number::SharedArrays;
1501 use cryptocol::hash::SHA3_512;
1502 let mut sha3 = SHA3_512::new();
1503 sha3.absorb_str("Post-quantum");
1504 let key: [u8; 64] = sha3.get_hash_value_in_array();
1505 print!("K =\t");
1506 for i in 0..64
1507 { print!("{:02X}", key[i]); }
1508 println!();
1509 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
1510 sha3.absorb_str("Initialize");
1511 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
1512 iv.src = sha3.get_hash_value_in_array();
1513 let iv = unsafe { iv.des };
1514 print!("IV =\t");
1515 for i in 0..16
1516 { print!("{:08X}", iv[i].to_be()); }
1517 println!();
1518 let message = "In the beginning God created the heavens and the earth.";
1519 println!("M =\t{}", message);
1520 let message = unsafe { message.to_string().as_mut_vec().clone() };
1521 let mut cipher = Vec::<u8>::new();
1522 a_rijndael.encrypt_vec_into_vec(iv, &message, &mut cipher);
1523 print!("C =\t");
1524 for c in cipher.clone()
1525 { print!("{:02X} ", c); }
1526 println!();
1527 let mut txt = String::new();
1528 for c in cipher.clone()
1529 { write!(txt, "{:02X} ", c); }
1530 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
1531 println!("-------------------------------");
1532}
1533
1534fn aes_encrypt_vec_ofb_into_array()
1535{
1536 println!("aes_encrypt_vec_ofb_into_array()");
1537 use std::io::Write;
1538 use std::fmt::Write as _;
1539 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
1540
1541 // Normal case for AES-128
1542 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1543 println!("K =\t{:#016X}", key);
1544 let mut a_aes = AES_128::new_with_key_u128(key);
1545 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1546 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1547
1548 let message = "In the beginning God created the heavens and the earth.";
1549 println!("M =\t{}", message);
1550 let message = unsafe { message.to_string().as_mut_vec().clone() };
1551 let mut cipher = [0_u8; 55];
1552 a_aes.encrypt_vec_into_array(iv, &message, &mut cipher);
1553 print!("C =\t");
1554 for c in cipher.clone()
1555 { print!("{:02X} ", c); }
1556 println!();
1557 let mut txt = String::new();
1558 for c in cipher.clone()
1559 { write!(txt, "{:02X} ", c); }
1560 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1561 println!();
1562
1563 // Normal case for AES-192
1564 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1565 print!("K =\t");
1566 for i in 0..24
1567 { print!("{:02X}", key[i]); }
1568 println!();
1569 let mut a_aes = AES_192::new_with_key(&key);
1570 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1571 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1572
1573 let message = "In the beginning God created the heavens and the earth.";
1574 println!("M =\t{}", message);
1575 let message = unsafe { message.to_string().as_mut_vec().clone() };
1576 let mut cipher = [0_u8; 55];
1577 a_aes.encrypt_vec_into_array(iv, &message, &mut cipher);
1578 print!("C =\t");
1579 for c in cipher.clone()
1580 { print!("{:02X} ", c); }
1581 println!();
1582 let mut txt = String::new();
1583 for c in cipher.clone()
1584 { write!(txt, "{:02X} ", c); }
1585 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
1586 println!();
1587
1588 // Normal case for AES-256
1589 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1590 print!("K =\t");
1591 for i in 0..32
1592 { print!("{:02X}", key[i]); }
1593 println!();
1594 let mut a_aes = AES_256::new_with_key(&key);
1595 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1596 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1597
1598 let message = "In the beginning God created the heavens and the earth.";
1599 println!("M =\t{}", message);
1600 let message = unsafe { message.to_string().as_mut_vec().clone() };
1601 let mut cipher = [0_u8; 55];
1602 a_aes.encrypt_vec_into_array(iv, &message, &mut cipher);
1603 print!("C =\t");
1604 for c in cipher.clone()
1605 { print!("{:02X} ", c); }
1606 println!();
1607 let mut txt = String::new();
1608 for c in cipher.clone()
1609 { write!(txt, "{:02X} ", c); }
1610 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
1611 println!();
1612
1613 // Normal case for Rijndael-256-256
1614 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1615 print!("K =\t");
1616 for i in 0..32
1617 { print!("{:02X}", key[i]); }
1618 println!();
1619 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
1620 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1621 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
1622
1623 let message = "In the beginning God created the heavens and the earth.";
1624 println!("M =\t{}", message);
1625 let message = unsafe { message.to_string().as_mut_vec().clone() };
1626 let mut cipher = [0_u8; 55];
1627 a_rijndael.encrypt_vec_into_array(iv, &message, &mut cipher);
1628 print!("C =\t");
1629 for c in cipher.clone()
1630 { print!("{:02X} ", c); }
1631 println!();
1632 let mut txt = String::new();
1633 for c in cipher.clone()
1634 { write!(txt, "{:02X} ", c); }
1635 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
1636 println!();
1637
1638 // Normal case for Rijndael-512-512 for post-quantum
1639 use cryptocol::number::SharedArrays;
1640 use cryptocol::hash::SHA3_512;
1641 let mut sha3 = SHA3_512::new();
1642 sha3.absorb_str("Post-quantum");
1643 let key: [u8; 64] = sha3.get_hash_value_in_array();
1644 print!("K =\t");
1645 for i in 0..64
1646 { print!("{:02X}", key[i]); }
1647 println!();
1648 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
1649 sha3.absorb_str("Initialize");
1650 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
1651 iv.src = sha3.get_hash_value_in_array();
1652 let iv = unsafe { iv.des };
1653 print!("IV =\t");
1654 for i in 0..16
1655 { print!("{:08X}", iv[i].to_be()); }
1656 println!();
1657 let message = "In the beginning God created the heavens and the earth.";
1658 println!("M =\t{}", message);
1659 let message = unsafe { message.to_string().as_mut_vec().clone() };
1660 let mut cipher = [0_u8; 55];
1661 a_rijndael.encrypt_vec_into_array(iv, &message, &mut cipher);
1662 print!("C =\t");
1663 for c in cipher.clone()
1664 { print!("{:02X} ", c); }
1665 println!();
1666 let mut txt = String::new();
1667 for c in cipher.clone()
1668 { write!(txt, "{:02X} ", c); }
1669 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
1670 println!("-------------------------------");
1671}
1672
1673fn aes_encrypt_array_ofb()
1674{
1675 println!("aes_encrypt_array_ofb()");
1676 use std::io::Write;
1677 use std::fmt::Write as _;
1678 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
1679
1680 // Normal case for AES-128
1681 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1682 println!("K =\t{:#016X}", key);
1683 let mut a_aes = AES_128::new_with_key_u128(key);
1684 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1685 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1686
1687 let mes = "In the beginning God created the heavens and the earth.";
1688 println!("M =\t{}", mes);
1689 let mut message = [0_u8; 55];
1690 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1691 let mut cipher = [0_u8; 55];
1692 a_aes.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
1693 print!("C =\t");
1694 for c in cipher.clone()
1695 { print!("{:02X} ", c); }
1696 println!();
1697 let mut txt = String::new();
1698 for c in cipher.clone()
1699 { write!(txt, "{:02X} ", c); }
1700 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1701 println!();
1702
1703 // Normal case for AES-192
1704 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1705 print!("K =\t");
1706 for i in 0..24
1707 { print!("{:02X}", key[i]); }
1708 println!();
1709 let mut a_aes = AES_192::new_with_key(&key);
1710 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1711 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1712
1713 let mes = "In the beginning God created the heavens and the earth.";
1714 println!("M =\t{}", mes);
1715 let mut message = [0_u8; 55];
1716 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1717 let mut cipher = [0_u8; 55];
1718 a_aes.encrypt(iv, message.as_ptr(), message.len() as u64, 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, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
1727 println!();
1728
1729 // Normal case for AES-256
1730 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1731 print!("K =\t");
1732 for i in 0..32
1733 { print!("{:02X}", key[i]); }
1734 println!();
1735 let mut a_aes = AES_256::new_with_key(&key);
1736 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1737 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1738
1739 let mes = "In the beginning God created the heavens and the earth.";
1740 println!("M =\t{}", mes);
1741 let mut message = [0_u8; 55];
1742 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1743 let mut cipher = [0_u8; 55];
1744 a_aes.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
1745 print!("C =\t");
1746 for c in cipher.clone()
1747 { print!("{:02X} ", c); }
1748 println!();
1749 let mut txt = String::new();
1750 for c in cipher.clone()
1751 { write!(txt, "{:02X} ", c); }
1752 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
1753 println!();
1754
1755 // Normal case for Rijndael-256-256
1756 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1757 print!("K =\t");
1758 for i in 0..32
1759 { print!("{:02X}", key[i]); }
1760 println!();
1761 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
1762 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1763 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
1764
1765 let mes = "In the beginning God created the heavens and the earth.";
1766 println!("M =\t{}", mes);
1767 let mut message = [0_u8; 55];
1768 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1769 let mut cipher = [0_u8; 55];
1770 a_rijndael.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
1771 print!("C =\t");
1772 for c in cipher.clone()
1773 { print!("{:02X} ", c); }
1774 println!();
1775 let mut txt = String::new();
1776 for c in cipher.clone()
1777 { write!(txt, "{:02X} ", c); }
1778 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
1779 println!();
1780
1781 // Normal case for Rijndael-512-512 for post-quantum
1782 use cryptocol::number::SharedArrays;
1783 use cryptocol::hash::SHA3_512;
1784 let mut sha3 = SHA3_512::new();
1785 sha3.absorb_str("Post-quantum");
1786 let key: [u8; 64] = sha3.get_hash_value_in_array();
1787 print!("K =\t");
1788 for i in 0..64
1789 { print!("{:02X}", key[i]); }
1790 println!();
1791 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
1792 sha3.absorb_str("Initialize");
1793 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
1794 iv.src = sha3.get_hash_value_in_array();
1795 let iv = unsafe { iv.des };
1796 print!("IV =\t");
1797 for i in 0..16
1798 { print!("{:08X}", iv[i].to_be()); }
1799 println!();
1800 let mes = "In the beginning God created the heavens and the earth.";
1801 println!("M =\t{}", mes);
1802 let mut message = [0_u8; 55];
1803 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1804 let mut cipher = [0_u8; 55];
1805 a_rijndael.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
1806 print!("C =\t");
1807 for c in cipher.clone()
1808 { print!("{:02X} ", c); }
1809 println!();
1810 let mut txt = String::new();
1811 for c in cipher.clone()
1812 { write!(txt, "{:02X} ", c); }
1813 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
1814 println!("-------------------------------");
1815}
1816
1817fn aes_encrypt_array_ofb_into_vec()
1818{
1819 println!("aes_encrypt_array_ofb_into_vec()");
1820 use std::io::Write;
1821 use std::fmt::Write as _;
1822 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
1823
1824 // Normal case for AES-128
1825 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1826 println!("K =\t{:#016X}", key);
1827 let mut a_aes = AES_128::new_with_key_u128(key);
1828 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1829 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1830
1831 let mes = "In the beginning God created the heavens and the earth.";
1832 println!("M =\t{}", mes);
1833 let mut message = [0_u8; 55];
1834 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1835 let mut cipher = Vec::<u8>::new();
1836 a_aes.encrypt_array_into_vec(iv, &message, &mut cipher);
1837 print!("C =\t");
1838 for c in cipher.clone()
1839 { print!("{:02X} ", c); }
1840 println!();
1841 let mut txt = String::new();
1842 for c in cipher.clone()
1843 { write!(txt, "{:02X} ", c); }
1844 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1845 println!();
1846
1847 // Normal case for AES-192
1848 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1849 print!("K =\t");
1850 for i in 0..24
1851 { print!("{:02X}", key[i]); }
1852 println!();
1853 let mut a_aes = AES_192::new_with_key(&key);
1854 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1855 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1856
1857 let mes = "In the beginning God created the heavens and the earth.";
1858 println!("M =\t{}", mes);
1859 let mut message = [0_u8; 55];
1860 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1861 let mut cipher = Vec::<u8>::new();
1862 a_aes.encrypt_array_into_vec(iv, &message, &mut cipher);
1863 print!("C =\t");
1864 for c in cipher.clone()
1865 { print!("{:02X} ", c); }
1866 println!();
1867 let mut txt = String::new();
1868 for c in cipher.clone()
1869 { write!(txt, "{:02X} ", c); }
1870 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
1871 println!();
1872
1873 // Normal case for AES-256
1874 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1875 print!("K =\t");
1876 for i in 0..32
1877 { print!("{:02X}", key[i]); }
1878 println!();
1879 let mut a_aes = AES_256::new_with_key(&key);
1880 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1881 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1882
1883 let mes = "In the beginning God created the heavens and the earth.";
1884 println!("M =\t{}", mes);
1885 let mut message = [0_u8; 55];
1886 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1887 let mut cipher = Vec::<u8>::new();
1888 a_aes.encrypt_array_into_vec(iv, &message, &mut cipher);
1889 print!("C =\t");
1890 for c in cipher.clone()
1891 { print!("{:02X} ", c); }
1892 println!();
1893 let mut txt = String::new();
1894 for c in cipher.clone()
1895 { write!(txt, "{:02X} ", c); }
1896 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
1897 println!();
1898
1899 // Normal case for Rijndael-256-256
1900 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1901 print!("K =\t");
1902 for i in 0..32
1903 { print!("{:02X}", key[i]); }
1904 println!();
1905 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
1906 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1907 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
1908
1909 let mes = "In the beginning God created the heavens and the earth.";
1910 println!("M =\t{}", mes);
1911 let mut message = [0_u8; 55];
1912 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1913 let mut cipher = Vec::<u8>::new();
1914 a_rijndael.encrypt_array_into_vec(iv, &message, &mut cipher);
1915 print!("C =\t");
1916 for c in cipher.clone()
1917 { print!("{:02X} ", c); }
1918 println!();
1919 let mut txt = String::new();
1920 for c in cipher.clone()
1921 { write!(txt, "{:02X} ", c); }
1922 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
1923 println!();
1924
1925 // Normal case for Rijndael-512-512 for post-quantum
1926 use cryptocol::number::SharedArrays;
1927 use cryptocol::hash::SHA3_512;
1928 let mut sha3 = SHA3_512::new();
1929 sha3.absorb_str("Post-quantum");
1930 let key: [u8; 64] = sha3.get_hash_value_in_array();
1931 print!("K =\t");
1932 for i in 0..64
1933 { print!("{:02X}", key[i]); }
1934 println!();
1935 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
1936 sha3.absorb_str("Initialize");
1937 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
1938 iv.src = sha3.get_hash_value_in_array();
1939 let iv = unsafe { iv.des };
1940 print!("IV =\t");
1941 for i in 0..16
1942 { print!("{:08X}", iv[i].to_be()); }
1943 println!();
1944 let mes = "In the beginning God created the heavens and the earth.";
1945 println!("M =\t{}", mes);
1946 let mut message = [0_u8; 55];
1947 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1948 let mut cipher = Vec::<u8>::new();
1949 a_rijndael.encrypt_array_into_vec(iv, &message, &mut cipher);
1950 print!("C =\t");
1951 for c in cipher.clone()
1952 { print!("{:02X} ", c); }
1953 println!();
1954 let mut txt = String::new();
1955 for c in cipher.clone()
1956 { write!(txt, "{:02X} ", c); }
1957 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
1958 println!("-------------------------------");
1959}
1960
1961fn aes_encrypt_array_ofb_into_array()
1962{
1963 println!("aes_encrypt_array_ofb_into_array()");
1964 use std::io::Write;
1965 use std::fmt::Write as _;
1966 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
1967
1968 // Normal case for AES-128
1969 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1970 println!("K =\t{:#016X}", key);
1971 let mut a_aes = AES_128::new_with_key_u128(key);
1972 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1973 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1974
1975 let mes = "In the beginning God created the heavens and the earth.";
1976 println!("M =\t{}", mes);
1977 let mut message = [0_u8; 55];
1978 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1979 let mut cipher = [0_u8; 55];
1980 a_aes.encrypt_array_into_array(iv, &message, &mut cipher);
1981 print!("C =\t");
1982 for c in cipher.clone()
1983 { print!("{:02X} ", c); }
1984 println!();
1985 let mut txt = String::new();
1986 for c in cipher.clone()
1987 { write!(txt, "{:02X} ", c); }
1988 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1989 println!();
1990
1991 // Normal case for AES-192
1992 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1993 print!("K =\t");
1994 for i in 0..24
1995 { print!("{:02X}", key[i]); }
1996 println!();
1997 let mut a_aes = AES_192::new_with_key(&key);
1998 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1999 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2000
2001 let mes = "In the beginning God created the heavens and the earth.";
2002 println!("M =\t{}", mes);
2003 let mut message = [0_u8; 55];
2004 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2005 let mut cipher = [0_u8; 55];
2006 a_aes.encrypt_array_into_array(iv, &message, &mut cipher);
2007 print!("C =\t");
2008 for c in cipher.clone()
2009 { print!("{:02X} ", c); }
2010 println!();
2011 let mut txt = String::new();
2012 for c in cipher.clone()
2013 { write!(txt, "{:02X} ", c); }
2014 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
2015 println!();
2016
2017 // Normal case for AES-256
2018 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2019 print!("K =\t");
2020 for i in 0..32
2021 { print!("{:02X}", key[i]); }
2022 println!();
2023 let mut a_aes = AES_256::new_with_key(&key);
2024 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2025 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2026
2027 let mes = "In the beginning God created the heavens and the earth.";
2028 println!("M =\t{}", mes);
2029 let mut message = [0_u8; 55];
2030 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2031 let mut cipher = [0_u8; 55];
2032 a_aes.encrypt_array_into_array(iv, &message, &mut cipher);
2033 print!("C =\t");
2034 for c in cipher.clone()
2035 { print!("{:02X} ", c); }
2036 println!();
2037 let mut txt = String::new();
2038 for c in cipher.clone()
2039 { write!(txt, "{:02X} ", c); }
2040 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
2041 println!();
2042
2043 // Normal case for Rijndael-256-256
2044 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2045 print!("K =\t");
2046 for i in 0..32
2047 { print!("{:02X}", key[i]); }
2048 println!();
2049 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
2050 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2051 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
2052
2053 let mes = "In the beginning God created the heavens and the earth.";
2054 println!("M =\t{}", mes);
2055 let mut message = [0_u8; 55];
2056 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2057 let mut cipher = [0_u8; 55];
2058 a_rijndael.encrypt_array_into_array(iv, &message, &mut cipher);
2059 print!("C =\t");
2060 for c in cipher.clone()
2061 { print!("{:02X} ", c); }
2062 println!();
2063 let mut txt = String::new();
2064 for c in cipher.clone()
2065 { write!(txt, "{:02X} ", c); }
2066 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
2067 println!();
2068
2069 // Normal case for Rijndael-512-512 for post-quantum
2070 use cryptocol::number::SharedArrays;
2071 use cryptocol::hash::SHA3_512;
2072 let mut sha3 = SHA3_512::new();
2073 sha3.absorb_str("Post-quantum");
2074 let key: [u8; 64] = sha3.get_hash_value_in_array();
2075 print!("K =\t");
2076 for i in 0..64
2077 { print!("{:02X}", key[i]); }
2078 println!();
2079 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
2080 sha3.absorb_str("Initialize");
2081 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
2082 iv.src = sha3.get_hash_value_in_array();
2083 let iv = unsafe { iv.des };
2084 print!("IV =\t");
2085 for i in 0..16
2086 { print!("{:08X}", iv[i].to_be()); }
2087 println!();
2088 let mes = "In the beginning God created the heavens and the earth.";
2089 println!("M =\t{}", mes);
2090 let mut message = [0_u8; 55];
2091 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2092 let mut cipher = [0_u8; 55];
2093 a_rijndael.encrypt_array_into_array(iv, &message, &mut cipher);
2094 print!("C =\t");
2095 for c in cipher.clone()
2096 { print!("{:02X} ", c); }
2097 println!();
2098 let mut txt = String::new();
2099 for c in cipher.clone()
2100 { write!(txt, "{:02X} ", c); }
2101 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
2102 println!("-------------------------------");
2103}
2104
2105
2106fn aes_decrypt_ofb()
2107{
2108 println!("aes_decrypt_ofb");
2109 use std::io::Write;
2110 use std::fmt::Write as _;
2111 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
2112
2113 // Normal case for AES-128
2114 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
2115 println!("K =\t{:#016X}", key);
2116 let mut a_aes = AES_128::new_with_key_u128(key);
2117 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2118 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0], iv[1], iv[2], iv[3]);
2119
2120 let message = "In the beginning God created the heavens and the earth.";
2121 println!("M =\t{}", message);
2122 let mut cipher = [0_u8; 55];
2123 a_aes.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
2132
2133 let mut recovered = vec![0; 55];
2134 a_aes.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2135 print!("Ba =\t");
2136 for b in recovered.clone()
2137 { print!("{:02X} ", b); }
2138 println!();
2139 let mut txt = String::new();
2140 for c in recovered.clone()
2141 { write!(txt, "{:02X} ", c); }
2142 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 ");
2143
2144 let mut converted = String::new();
2145 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2146
2147 println!("Bb =\t{}", converted);
2148 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2149 assert_eq!(converted, message);
2150 println!();
2151
2152 // Normal case for AES-192
2153 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2154 print!("K =\t");
2155 for i in 0..24
2156 { print!("{:02X}", key[i]); }
2157 println!();
2158 let mut a_aes = AES_192::new_with_key(&key);
2159 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2160 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2161
2162 let message = "In the beginning God created the heavens and the earth.";
2163 println!("M =\t{}", message);
2164 let mut cipher = [0_u8; 55];
2165 a_aes.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
2166 print!("C =\t");
2167 for c in cipher.clone()
2168 { print!("{:02X} ", c); }
2169 println!();
2170 let mut txt = String::new();
2171 for c in cipher.clone()
2172 { write!(txt, "{:02X} ", c); }
2173 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
2174
2175 let mut recovered = vec![0; 55];
2176 a_aes.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2177 print!("Ba =\t");
2178 for b in recovered.clone()
2179 { print!("{:02X} ", b); }
2180 println!();
2181 let mut txt = String::new();
2182 for c in recovered.clone()
2183 { write!(txt, "{:02X} ", c); }
2184 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 ");
2185
2186 let mut converted = String::new();
2187 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2188
2189 println!("Bb =\t{}", converted);
2190 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2191 assert_eq!(converted, message);
2192 println!();
2193
2194 // Normal case for AES-256
2195 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2196 print!("K =\t");
2197 for i in 0..32
2198 { print!("{:02X}", key[i]); }
2199 println!();
2200 let mut a_aes = AES_256::new_with_key(&key);
2201 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2202 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2203
2204 let message = "In the beginning God created the heavens and the earth.";
2205 println!("M =\t{}", message);
2206 let mut cipher = [0_u8; 55];
2207 a_aes.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
2208 print!("C =\t");
2209 for c in cipher.clone()
2210 { print!("{:02X} ", c); }
2211 println!();
2212 let mut txt = String::new();
2213 for c in cipher.clone()
2214 { write!(txt, "{:02X} ", c); }
2215 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
2216
2217 let mut recovered = vec![0; 55];
2218 a_aes.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2219 print!("Ba =\t");
2220 for b in recovered.clone()
2221 { print!("{:02X} ", b); }
2222 println!();
2223 let mut txt = String::new();
2224 for c in recovered.clone()
2225 { write!(txt, "{:02X} ", c); }
2226 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 ");
2227
2228 let mut converted = String::new();
2229 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2230
2231 println!("Bb =\t{}", converted);
2232 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2233 assert_eq!(converted, message);
2234 println!();
2235
2236 // Normal case for Rijndael-256-256
2237 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2238 print!("K =\t");
2239 for i in 0..32
2240 { print!("{:02X}", key[i]); }
2241 println!();
2242 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
2243 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2244 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
2245
2246 let message = "In the beginning God created the heavens and the earth.";
2247 println!("M =\t{}", message);
2248 let mut cipher = [0_u8; 55];
2249 a_rijndael.encrypt(iv, message.as_ptr(), message.len() as u64, 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, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
2258
2259 let mut recovered = vec![0; 55];
2260 a_rijndael.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2261 print!("Ba =\t");
2262 for b in recovered.clone()
2263 { print!("{:02X} ", b); }
2264 println!();
2265 let mut txt = String::new();
2266 for c in recovered.clone()
2267 { write!(txt, "{:02X} ", c); }
2268 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 ");
2269
2270 let mut converted = String::new();
2271 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2272
2273 println!("Bb =\t{}", converted);
2274 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2275 assert_eq!(converted, message);
2276 println!();
2277
2278 // Normal case for Rijndael-512-512 for post-quantum
2279 use cryptocol::number::SharedArrays;
2280 use cryptocol::hash::SHA3_512;
2281 let mut sha3 = SHA3_512::new();
2282 sha3.absorb_str("Post-quantum");
2283 let key: [u8; 64] = sha3.get_hash_value_in_array();
2284 print!("K =\t");
2285 for i in 0..64
2286 { print!("{:02X}", key[i]); }
2287 println!();
2288 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
2289 sha3.absorb_str("Initialize");
2290 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
2291 iv.src = sha3.get_hash_value_in_array();
2292 let iv = unsafe { iv.des };
2293 print!("IV =\t");
2294 for i in 0..16
2295 { print!("{:08X}", iv[i].to_be()); }
2296 println!();
2297 let message = "In the beginning God created the heavens and the earth.";
2298 println!("M =\t{}", message);
2299 let mut cipher = [0_u8; 55];
2300 a_rijndael.encrypt(iv.clone(), message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
2301 print!("C =\t");
2302 for c in cipher.clone()
2303 { print!("{:02X} ", c); }
2304 println!();
2305 let mut txt = String::new();
2306 for c in cipher.clone()
2307 { write!(txt, "{:02X} ", c); }
2308 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
2309
2310 let mut recovered = vec![0; 55];
2311 a_rijndael.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2312 print!("Ba =\t");
2313 for b in recovered.clone()
2314 { print!("{:02X} ", b); }
2315 println!();
2316 let mut txt = String::new();
2317 for c in recovered.clone()
2318 { write!(txt, "{:02X} ", c); }
2319 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 ");
2320
2321 let mut converted = String::new();
2322 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2323
2324 println!("Bb =\t{}", converted);
2325 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2326 assert_eq!(converted, message);
2327 println!("-------------------------------");
2328}
2329
2330
2331fn aes_decrypt_ofb_into_vec()
2332{
2333 println!("aes_decrypt_ofb_into_vec()");
2334 use std::io::Write;
2335 use std::fmt::Write as _;
2336 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
2337
2338 // Normal case for AES-128
2339 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
2340 println!("K =\t{:#016X}", key);
2341 let mut a_aes = AES_128::new_with_key_u128(key);
2342 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2343 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2344
2345 let message = "In the beginning God created the heavens and the earth.";
2346 println!("M =\t{}", message);
2347 let mut cipher = [0_u8; 55];
2348 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2349 print!("C =\t");
2350 for c in cipher.clone()
2351 { print!("{:02X} ", c); }
2352 println!();
2353 let mut txt = String::new();
2354 for c in cipher.clone()
2355 { write!(txt, "{:02X} ", c); }
2356 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
2357 println!();
2358
2359 let mut recovered = Vec::<u8>::new();
2360 a_aes.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2361 print!("Ba =\t");
2362 for b in recovered.clone()
2363 { print!("{:02X} ", b); }
2364 println!();
2365 let mut txt = String::new();
2366 for c in recovered.clone()
2367 { write!(txt, "{:02X} ", c); }
2368 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 ");
2369
2370 let mut converted = String::new();
2371 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2372
2373 println!("Bb =\t{}", converted);
2374 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2375 assert_eq!(converted, message);
2376 println!();
2377
2378 // Normal case for AES-192
2379 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2380 print!("K =\t");
2381 for i in 0..24
2382 { print!("{:02X}", key[i]); }
2383 println!();
2384 let mut a_aes = AES_192::new_with_key(&key);
2385 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2386 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2387
2388 let message = "In the beginning God created the heavens and the earth.";
2389 println!("M =\t{}", message);
2390 let mut cipher = [0_u8; 55];
2391 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2392 print!("C =\t");
2393 for c in cipher.clone()
2394 { print!("{:02X} ", c); }
2395 println!();
2396 let mut txt = String::new();
2397 for c in cipher.clone()
2398 { write!(txt, "{:02X} ", c); }
2399 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
2400 println!();
2401
2402 let mut recovered = Vec::<u8>::new();
2403 a_aes.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2404 print!("Ba =\t");
2405 for b in recovered.clone()
2406 { print!("{:02X} ", b); }
2407 println!();
2408 let mut txt = String::new();
2409 for c in recovered.clone()
2410 { write!(txt, "{:02X} ", c); }
2411 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 ");
2412
2413 let mut converted = String::new();
2414 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2415
2416 println!("Bb =\t{}", converted);
2417 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2418 assert_eq!(converted, message);
2419 println!();
2420
2421 // Normal case for AES-256
2422 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2423 print!("K =\t");
2424 for i in 0..32
2425 { print!("{:02X}", key[i]); }
2426 println!();
2427 let mut a_aes = AES_256::new_with_key(&key);
2428 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2429 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2430
2431 let message = "In the beginning God created the heavens and the earth.";
2432 println!("M =\t{}", message);
2433 let mut cipher = [0_u8; 55];
2434 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2435 print!("C =\t");
2436 for c in cipher.clone()
2437 { print!("{:02X} ", c); }
2438 println!();
2439 let mut txt = String::new();
2440 for c in cipher.clone()
2441 { write!(txt, "{:02X} ", c); }
2442 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
2443 println!();
2444
2445 let mut recovered = Vec::<u8>::new();
2446 a_aes.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2447 print!("Ba =\t");
2448 for b in recovered.clone()
2449 { print!("{:02X} ", b); }
2450 println!();
2451 let mut txt = String::new();
2452 for c in recovered.clone()
2453 { write!(txt, "{:02X} ", c); }
2454 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 ");
2455
2456 let mut converted = String::new();
2457 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2458
2459 println!("Bb =\t{}", converted);
2460 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2461 assert_eq!(converted, message);
2462 println!();
2463
2464 // Normal case for Rijndael-256-256
2465 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2466 print!("K =\t");
2467 for i in 0..32
2468 { print!("{:02X}", key[i]); }
2469 println!();
2470 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
2471 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2472 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
2473
2474 let message = "In the beginning God created the heavens and the earth.";
2475 println!("M =\t{}", message);
2476 let mut cipher = [0_u8; 55];
2477 a_rijndael.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2478 print!("C =\t");
2479 for c in cipher.clone()
2480 { print!("{:02X} ", c); }
2481 println!();
2482 let mut txt = String::new();
2483 for c in cipher.clone()
2484 { write!(txt, "{:02X} ", c); }
2485 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
2486 println!();
2487
2488 let mut recovered = Vec::<u8>::new();
2489 a_rijndael.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2490 print!("Ba =\t");
2491 for b in recovered.clone()
2492 { print!("{:02X} ", b); }
2493 println!();
2494 let mut txt = String::new();
2495 for c in recovered.clone()
2496 { write!(txt, "{:02X} ", c); }
2497 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 ");
2498
2499 let mut converted = String::new();
2500 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2501
2502 println!("Bb =\t{}", converted);
2503 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2504 assert_eq!(converted, message);
2505 println!();
2506
2507 // Normal case for Rijndael-512-512 for post-quantum
2508 use cryptocol::number::SharedArrays;
2509 use cryptocol::hash::SHA3_512;
2510 let mut sha3 = SHA3_512::new();
2511 sha3.absorb_str("Post-quantum");
2512 let key: [u8; 64] = sha3.get_hash_value_in_array();
2513 print!("K =\t");
2514 for i in 0..64
2515 { print!("{:02X}", key[i]); }
2516 println!();
2517 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
2518 sha3.absorb_str("Initialize");
2519 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
2520 iv.src = sha3.get_hash_value_in_array();
2521 let iv = unsafe { iv.des };
2522 print!("IV =\t");
2523 for i in 0..16
2524 { print!("{:08X}", iv[i].to_be()); }
2525 println!();
2526 let message = "In the beginning God created the heavens and the earth.";
2527 println!("M =\t{}", message);
2528 let mut cipher = [0_u8; 55];
2529 a_rijndael.encrypt(iv.clone(), message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
2530 print!("C =\t");
2531 for c in cipher.clone()
2532 { print!("{:02X} ", c); }
2533 println!();
2534 let mut txt = String::new();
2535 for c in cipher.clone()
2536 { write!(txt, "{:02X} ", c); }
2537 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
2538
2539 let mut recovered = Vec::<u8>::new();
2540 a_rijndael.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2541 print!("Ba =\t");
2542 for b in recovered.clone()
2543 { print!("{:02X} ", b); }
2544 println!();
2545 let mut txt = String::new();
2546 for c in recovered.clone()
2547 { write!(txt, "{:02X} ", c); }
2548 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 ");
2549
2550 let mut converted = String::new();
2551 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2552
2553 println!("Bb =\t{}", converted);
2554 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2555 assert_eq!(converted, message);
2556 println!("-------------------------------");
2557}
2558
2559fn aes_decrypt_ofb_into_array()
2560{
2561 println!("aes_decrypt_ofb_into_array()");
2562 use std::io::Write;
2563 use std::fmt::Write as _;
2564 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
2565
2566 // Normal case for AES-128
2567 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
2568 println!("K =\t{:#016X}", key);
2569 let mut a_aes = AES_128::new_with_key_u128(key);
2570 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2571 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2572
2573 let message = "In the beginning God created the heavens and the earth.";
2574 println!("M =\t{}", message);
2575 let mut cipher = [0_u8; 55];
2576 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2577 print!("C =\t");
2578 for c in cipher.clone()
2579 { print!("{:02X} ", c); }
2580 println!();
2581 let mut txt = String::new();
2582 for c in cipher.clone()
2583 { write!(txt, "{:02X} ", c); }
2584 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
2585
2586 let mut recovered = [0; 64];
2587 let len = a_aes.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2588 print!("Ba =\t");
2589 for b in recovered.clone()
2590 { print!("{:02X} ", b); }
2591 println!();
2592 let mut txt = String::new();
2593 for c in recovered.clone()
2594 { write!(txt, "{:02X} ", c); }
2595 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
2596
2597 let mut converted = String::new();
2598 unsafe { converted.as_mut_vec() }.write(&recovered);
2599 unsafe { converted.as_mut_vec() }.truncate(len as usize);
2600
2601 println!("Bb =\t{}", converted);
2602 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2603 assert_eq!(converted, message);
2604 println!();
2605
2606 // Normal case for AES-192
2607 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2608 print!("K =\t");
2609 for i in 0..24
2610 { print!("{:02X}", key[i]); }
2611 println!();
2612 let mut a_aes = AES_192::new_with_key(&key);
2613 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2614 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2615
2616 let message = "In the beginning God created the heavens and the earth.";
2617 println!("M =\t{}", message);
2618 let mut cipher = [0_u8; 55];
2619 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2620 print!("C =\t");
2621 for c in cipher.clone()
2622 { print!("{:02X} ", c); }
2623 println!();
2624 let mut txt = String::new();
2625 for c in cipher.clone()
2626 { write!(txt, "{:02X} ", c); }
2627 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
2628
2629 let mut recovered = [0; 64];
2630 a_aes.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2631 print!("Ba =\t");
2632 for b in recovered.clone()
2633 { print!("{:02X} ", b); }
2634 println!();
2635 let mut txt = String::new();
2636
2637 for c in recovered.clone()
2638 { write!(txt, "{:02X} ", c); }
2639 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
2640
2641 let mut converted = String::new();
2642 unsafe { converted.as_mut_vec() }.write(&recovered);
2643 unsafe { converted.as_mut_vec() }.truncate(len as usize);
2644
2645 println!("Bb =\t{}", converted);
2646 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2647 assert_eq!(converted, message);
2648 println!();
2649
2650 // Normal case for AES-256
2651 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2652 print!("K =\t");
2653 for i in 0..32
2654 { print!("{:02X}", key[i]); }
2655 println!();
2656 let mut a_aes = AES_256::new_with_key(&key);
2657 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2658 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2659
2660 let message = "In the beginning God created the heavens and the earth.";
2661 println!("M =\t{}", message);
2662 let mut cipher = [0_u8; 55];
2663 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2664 print!("C =\t");
2665 for c in cipher.clone()
2666 { print!("{:02X} ", c); }
2667 println!();
2668 let mut txt = String::new();
2669 for c in cipher.clone()
2670 { write!(txt, "{:02X} ", c); }
2671 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
2672
2673 let mut recovered = [0; 64];
2674 a_aes.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2675 print!("Ba =\t");
2676 for b in recovered.clone()
2677 { print!("{:02X} ", b); }
2678 println!();
2679 let mut txt = String::new();
2680 for c in recovered.clone()
2681 { write!(txt, "{:02X} ", c); }
2682 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
2683
2684 let mut converted = String::new();
2685 unsafe { converted.as_mut_vec() }.write(&recovered);
2686 unsafe { converted.as_mut_vec() }.truncate(len as usize);
2687
2688 println!("Bb =\t{}", converted);
2689 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2690 assert_eq!(converted, message);
2691 println!();
2692
2693 // Normal case for Rijndael-256-256
2694 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2695 print!("K =\t");
2696 for i in 0..32
2697 { print!("{:02X}", key[i]); }
2698 println!();
2699 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
2700 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2701 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
2702
2703 let message = "In the beginning God created the heavens and the earth.";
2704 println!("M =\t{}", message);
2705 let mut cipher = [0_u8; 55];
2706 a_rijndael.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2707 print!("C =\t");
2708 for c in cipher.clone()
2709 { print!("{:02X} ", c); }
2710 println!();
2711 let mut txt = String::new();
2712 for c in cipher.clone()
2713 { write!(txt, "{:02X} ", c); }
2714 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
2715
2716 let mut recovered = [0; 64];
2717 a_rijndael.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2718 for b in recovered.clone()
2719 { print!("{:02X} ", b); }
2720 println!();
2721 let mut txt = String::new();
2722 for c in recovered.clone()
2723 { write!(txt, "{:02X} ", c); }
2724 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
2725
2726 let mut converted = String::new();
2727 unsafe { converted.as_mut_vec() }.write(&recovered);
2728 unsafe { converted.as_mut_vec() }.truncate(len as usize);
2729
2730 println!("Bb =\t{}", converted);
2731 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2732 assert_eq!(converted, message);
2733 println!();
2734
2735 // Normal case for Rijndael-512-512 for post-quantum
2736 use cryptocol::number::SharedArrays;
2737 use cryptocol::hash::SHA3_512;
2738 let mut sha3 = SHA3_512::new();
2739 sha3.absorb_str("Post-quantum");
2740 let key: [u8; 64] = sha3.get_hash_value_in_array();
2741 print!("K =\t");
2742 for i in 0..64
2743 { print!("{:02X}", key[i]); }
2744 println!();
2745 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
2746 sha3.absorb_str("Initialize");
2747 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
2748 iv.src = sha3.get_hash_value_in_array();
2749 let iv = unsafe { iv.des };
2750 print!("IV =\t");
2751 for i in 0..16
2752 { print!("{:08X}", iv[i].to_be()); }
2753 println!();
2754
2755 let message = "In the beginning God created the heavens and the earth.";
2756 println!("M =\t{}", message);
2757 let mut cipher = [0_u8; 55];
2758 a_rijndael.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2759 print!("C =\t");
2760 for c in cipher.clone()
2761 { print!("{:02X} ", c); }
2762 println!();
2763 let mut txt = String::new();
2764 for c in cipher.clone()
2765 { write!(txt, "{:02X} ", c); }
2766 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
2767
2768 let mut recovered = [0; 64];
2769 a_rijndael.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2770 print!("Ba =\t");
2771 for b in recovered.clone()
2772 { print!("{:02X} ", b); }
2773 println!();
2774 let mut txt = String::new();
2775 for c in recovered.clone()
2776 { write!(txt, "{:02X} ", c); }
2777 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
2778
2779 let mut converted = String::new();
2780 unsafe { converted.as_mut_vec() }.write(&recovered);
2781 unsafe { converted.as_mut_vec() }.truncate(len as usize);
2782
2783 println!("Bb =\t{}", converted);
2784 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2785 assert_eq!(converted, message);
2786 println!("-------------------------------");
2787}
2788
2789fn aes_decrypt_ofb_into_string()
2790{
2791 println!("aes_decrypt_ofb_into_string()");
2792 use std::io::Write;
2793 use std::fmt::Write as _;
2794 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
2795
2796 // Normal case for AES-128
2797 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
2798 println!("K =\t{:#016X}", key);
2799 let mut a_aes = AES_128::new_with_key_u128(key);
2800 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2801 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2802
2803 let message = "In the beginning God created the heavens and the earth.";
2804 println!("M =\t{}", message);
2805 let mut cipher = [0_u8; 55];
2806 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2807 print!("C =\t");
2808 for c in cipher.clone()
2809 { print!("{:02X} ", c); }
2810 println!();
2811 let mut txt = String::new();
2812 for c in cipher.clone()
2813 { write!(txt, "{:02X} ", c); }
2814 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
2815
2816 let mut converted= String::new();
2817 a_aes.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut converted);
2818 println!("B =\t{}", converted);
2819 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2820 assert_eq!(converted, message);
2821 println!();
2822
2823 // Normal case for AES-192
2824 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2825 print!("K =\t");
2826 for i in 0..24
2827 { print!("{:02X}", key[i]); }
2828 println!();
2829 let mut a_aes = AES_192::new_with_key(&key);
2830 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2831 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2832
2833 let message = "In the beginning God created the heavens and the earth.";
2834 println!("M =\t{}", message);
2835 let mut cipher = [0_u8; 55];
2836 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2837 print!("C =\t");
2838 for c in cipher.clone()
2839 { print!("{:02X} ", c); }
2840 println!();
2841 let mut txt = String::new();
2842 for c in cipher.clone()
2843 { write!(txt, "{:02X} ", c); }
2844 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
2845
2846 let mut converted= String::new();
2847 a_aes.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut converted);
2848 println!("B =\t{}", converted);
2849 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2850 assert_eq!(converted, message);
2851 println!();
2852
2853 // Normal case for AES-256
2854 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2855 print!("K =\t");
2856 for i in 0..32
2857 { print!("{:02X}", key[i]); }
2858 println!();
2859 let mut a_aes = AES_256::new_with_key(&key);
2860 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2861 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2862
2863 let message = "In the beginning God created the heavens and the earth.";
2864 println!("M =\t{}", message);
2865 let mut cipher = [0_u8; 55];
2866 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2867 print!("C =\t");
2868 for c in cipher.clone()
2869 { print!("{:02X} ", c); }
2870 println!();
2871 let mut txt = String::new();
2872 for c in cipher.clone()
2873 { write!(txt, "{:02X} ", c); }
2874 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
2875
2876 let mut converted= String::new();
2877 a_aes.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut converted);
2878 println!("B =\t{}", converted);
2879 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2880 assert_eq!(converted, message);
2881 println!();
2882
2883 // Normal case for Rijndael-256-256
2884 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2885 print!("K =\t");
2886 for i in 0..32
2887 { print!("{:02X}", key[i]); }
2888 println!();
2889 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
2890 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2891 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
2892
2893 let message = "In the beginning God created the heavens and the earth.";
2894 println!("M =\t{}", message);
2895 let mut cipher = [0_u8; 55];
2896 a_rijndael.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2897 print!("C =\t");
2898 for c in cipher.clone()
2899 { print!("{:02X} ", c); }
2900 println!();
2901 let mut txt = String::new();
2902 for c in cipher.clone()
2903 { write!(txt, "{:02X} ", c); }
2904 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
2905
2906 let mut converted= String::new();
2907 a_rijndael.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut converted);
2908 println!("B =\t{}", converted);
2909 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2910 assert_eq!(converted, message);
2911 println!();
2912
2913 // Normal case for Rijndael-512-512 for post-quantum
2914 use cryptocol::number::SharedArrays;
2915 use cryptocol::hash::SHA3_512;
2916 let mut sha3 = SHA3_512::new();
2917 sha3.absorb_str("Post-quantum");
2918 let key: [u8; 64] = sha3.get_hash_value_in_array();
2919 print!("K =\t");
2920 for i in 0..64
2921 { print!("{:02X}", key[i]); }
2922 println!();
2923 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
2924 sha3.absorb_str("Initialize");
2925 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
2926 iv.src = sha3.get_hash_value_in_array();
2927 let iv = unsafe { iv.des };
2928 print!("IV =\t");
2929 for i in 0..16
2930 { print!("{:08X}", iv[i].to_be()); }
2931 println!();
2932 let message = "In the beginning God created the heavens and the earth.";
2933 println!("M =\t{}", message);
2934 let mut cipher = [0_u8; 55];
2935 a_rijndael.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2936 print!("C =\t");
2937 for c in cipher.clone()
2938 { print!("{:02X} ", c); }
2939 println!();
2940 let mut txt = String::new();
2941 for c in cipher.clone()
2942 { write!(txt, "{:02X} ", c); }
2943 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
2944
2945 let mut converted= String::new();
2946 a_rijndael.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut converted);
2947 println!("B =\t{}", converted);
2948 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2949 assert_eq!(converted, message);
2950 println!("-------------------------------");
2951}
2952
2953fn aes_decrypt_vec_ofb()
2954{
2955 println!("aes_decrypt_vec_ofb()");
2956 use std::io::Write;
2957 use std::fmt::Write as _;
2958 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
2959
2960 // Normal case for AES-128
2961 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
2962 println!("K =\t{:#016X}", key);
2963 let mut a_aes = AES_128::new_with_key_u128(key);
2964 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2965 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2966
2967 let message = "In the beginning God created the heavens and the earth.";
2968 println!("M =\t{}", message);
2969 let mut cipher = Vec::<u8>::new();
2970 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
2971 print!("C =\t");
2972 for c in cipher.clone()
2973 { print!("{:02X} ", c); }
2974 println!();
2975 let mut txt = String::new();
2976 for c in cipher.clone()
2977 { write!(txt, "{:02X} ", c); }
2978 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
2979
2980 let mut recovered = vec![0; 55];
2981 a_aes.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
2982 print!("Ba =\t");
2983 for b in recovered.clone()
2984 { print!("{:02X} ", b); }
2985 println!();
2986 let mut txt = String::new();
2987 for c in recovered.clone()
2988 { write!(txt, "{:02X} ", c); }
2989 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 ");
2990
2991 let mut converted = String::new();
2992 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2993
2994 println!("Bb =\t{}", converted);
2995 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2996 assert_eq!(converted, message);
2997 println!();
2998
2999 // Normal case for AES-192
3000 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3001 print!("K =\t");
3002 for i in 0..24
3003 { print!("{:02X}", key[i]); }
3004 println!();
3005 let mut a_aes = AES_192::new_with_key(&key);
3006 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3007 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3008
3009 let message = "In the beginning God created the heavens and the earth.";
3010 println!("M =\t{}", message);
3011 let mut cipher = Vec::<u8>::new();
3012 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3013 print!("C =\t");
3014 for c in cipher.clone()
3015 { print!("{:02X} ", c); }
3016 println!();
3017 let mut txt = String::new();
3018 for c in cipher.clone()
3019 { write!(txt, "{:02X} ", c); }
3020 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
3021
3022 let mut recovered = vec![0; 55];
3023 a_aes.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
3024 print!("Ba =\t");
3025 for b in recovered.clone()
3026 { print!("{:02X} ", b); }
3027 println!();
3028 let mut txt = String::new();
3029 for c in recovered.clone()
3030 { write!(txt, "{:02X} ", c); }
3031 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 ");
3032
3033 let mut converted = String::new();
3034 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3035
3036 println!("Bb =\t{}", converted);
3037 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3038 assert_eq!(converted, message);
3039 println!();
3040
3041 // Normal case for AES-256
3042 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3043 print!("K =\t");
3044 for i in 0..32
3045 { print!("{:02X}", key[i]); }
3046 println!();
3047 let mut a_aes = AES_256::new_with_key(&key);
3048 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3049 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3050
3051 let message = "In the beginning God created the heavens and the earth.";
3052 println!("M =\t{}", message);
3053 let mut cipher = Vec::<u8>::new();
3054 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3055 print!("C =\t");
3056 for c in cipher.clone()
3057 { print!("{:02X} ", c); }
3058 println!();
3059 let mut txt = String::new();
3060 for c in cipher.clone()
3061 { write!(txt, "{:02X} ", c); }
3062 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
3063
3064 let mut recovered = vec![0; 55];
3065 a_aes.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
3066 print!("Ba =\t");
3067 for b in recovered.clone()
3068 { print!("{:02X} ", b); }
3069 println!();
3070 let mut txt = String::new();
3071 for c in recovered.clone()
3072 { write!(txt, "{:02X} ", c); }
3073 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 ");
3074
3075 let mut converted = String::new();
3076 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3077
3078 println!("Bb =\t{}", converted);
3079 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3080 assert_eq!(converted, message);
3081 println!();
3082
3083 // Normal case for Rijndael-256-256
3084 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3085 print!("K =\t");
3086 for i in 0..32
3087 { print!("{:02X}", key[i]); }
3088 println!();
3089 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
3090 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3091 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
3092
3093 let message = "In the beginning God created the heavens and the earth.";
3094 println!("M =\t{}", message);
3095 let mut cipher = Vec::<u8>::new();
3096 a_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
3097 print!("C =\t");
3098 for c in cipher.clone()
3099 { print!("{:02X} ", c); }
3100 println!();
3101 let mut txt = String::new();
3102 for c in cipher.clone()
3103 { write!(txt, "{:02X} ", c); }
3104 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
3105
3106 let mut recovered = vec![0; 55];
3107 a_rijndael.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
3108 print!("Ba =\t");
3109 for b in recovered.clone()
3110 { print!("{:02X} ", b); }
3111 println!();
3112 let mut txt = String::new();
3113 for c in recovered.clone()
3114 { write!(txt, "{:02X} ", c); }
3115 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 ");
3116
3117 let mut converted = String::new();
3118 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3119
3120 println!("Bb =\t{}", converted);
3121 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3122 assert_eq!(converted, message);
3123 println!();
3124
3125 // Normal case for Rijndael-512-512 for post-quantum
3126 use cryptocol::number::SharedArrays;
3127 use cryptocol::hash::SHA3_512;
3128 let mut sha3 = SHA3_512::new();
3129 sha3.absorb_str("Post-quantum");
3130 let key: [u8; 64] = sha3.get_hash_value_in_array();
3131 print!("K =\t");
3132 for i in 0..64
3133 { print!("{:02X}", key[i]); }
3134 println!();
3135 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
3136 sha3.absorb_str("Initialize");
3137 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
3138 iv.src = sha3.get_hash_value_in_array();
3139 let iv = unsafe { iv.des };
3140 print!("IV =\t");
3141 for i in 0..16
3142 { print!("{:08X}", iv[i].to_be()); }
3143 println!();
3144 let message = "In the beginning God created the heavens and the earth.";
3145 println!("M =\t{}", message);
3146 let mut cipher = Vec::<u8>::new();
3147 a_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
3148 print!("C =\t");
3149 for c in cipher.clone()
3150 { print!("{:02X} ", c); }
3151 println!();
3152 let mut txt = String::new();
3153 for c in cipher.clone()
3154 { write!(txt, "{:02X} ", c); }
3155 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
3156
3157 let mut recovered = vec![0; 55];
3158 a_rijndael.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
3159 print!("Ba =\t");
3160 for b in recovered.clone()
3161 { print!("{:02X} ", b); }
3162 println!();
3163 let mut txt = String::new();
3164 for c in recovered.clone()
3165 { write!(txt, "{:02X} ", c); }
3166 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 ");
3167
3168 let mut converted = String::new();
3169 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3170
3171 println!("Bb =\t{}", converted);
3172 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3173 assert_eq!(converted, message);
3174 println!("-------------------------------");
3175}
3176
3177fn aes_decrypt_vec_ofb_into_vec()
3178{
3179 println!("aes_decrypt_vec_ofb_into_vec()");
3180 use std::io::Write;
3181 use std::fmt::Write as _;
3182 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
3183
3184 // Normal case for AES-128
3185 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
3186 println!("K =\t{:#016X}", key);
3187 let mut a_aes = AES_128::new_with_key_u128(key);
3188 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3189 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3190
3191 let message = "In the beginning God created the heavens and the earth.";
3192 println!("M =\t{}", message);
3193 let mut cipher = Vec::<u8>::new();
3194 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3195 print!("C =\t");
3196 for c in cipher.clone()
3197 { print!("{:02X} ", c); }
3198 println!();
3199 let mut txt = String::new();
3200 for c in cipher.clone()
3201 { write!(txt, "{:02X} ", c); }
3202 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
3203
3204 let mut recovered = Vec::<u8>::new();
3205 a_aes.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
3206 print!("Ba =\t");
3207 for b in recovered.clone()
3208 { print!("{:02X} ", b); }
3209 println!();
3210 let mut txt = String::new();
3211 for c in recovered.clone()
3212 { write!(txt, "{:02X} ", c); }
3213 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 ");
3214
3215 let mut converted = String::new();
3216 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3217
3218 println!("Bb =\t{}", converted);
3219 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3220 assert_eq!(converted, message);
3221 println!();
3222
3223 // Normal case for AES-192
3224 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3225 print!("K =\t");
3226 for i in 0..24
3227 { print!("{:02X}", key[i]); }
3228 println!();
3229 let mut a_aes = AES_192::new_with_key(&key);
3230 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3231 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3232
3233 let message = "In the beginning God created the heavens and the earth.";
3234 println!("M =\t{}", message);
3235 let mut cipher = Vec::<u8>::new();
3236 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3237 print!("C =\t");
3238 for c in cipher.clone()
3239 { print!("{:02X} ", c); }
3240 println!();
3241 let mut txt = String::new();
3242 for c in cipher.clone()
3243 { write!(txt, "{:02X} ", c); }
3244 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
3245
3246 let mut recovered = Vec::<u8>::new();
3247 a_aes.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
3248 print!("Ba =\t");
3249 for b in recovered.clone()
3250 { print!("{:02X} ", b); }
3251 println!();
3252 let mut txt = String::new();
3253 for c in recovered.clone()
3254 { write!(txt, "{:02X} ", c); }
3255 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
3256
3257 let mut converted = String::new();
3258 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3259
3260 println!("Bb =\t{}", converted);
3261 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3262 assert_eq!(converted, message);
3263 println!();
3264
3265 // Normal case for AES-256
3266 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3267 print!("K =\t");
3268 for i in 0..32
3269 { print!("{:02X}", key[i]); }
3270 println!();
3271 let mut a_aes = AES_256::new_with_key(&key);
3272 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3273 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3274
3275 let message = "In the beginning God created the heavens and the earth.";
3276 println!("M =\t{}", message);
3277 let mut cipher = Vec::<u8>::new();
3278 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3279 print!("C =\t");
3280 for c in cipher.clone()
3281 { print!("{:02X} ", c); }
3282 println!();
3283 let mut txt = String::new();
3284 for c in cipher.clone()
3285 { write!(txt, "{:02X} ", c); }
3286 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
3287
3288 let mut recovered = Vec::<u8>::new();
3289 a_aes.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
3290 print!("Ba =\t");
3291 for b in recovered.clone()
3292 { print!("{:02X} ", b); }
3293 println!();
3294 let mut txt = String::new();
3295 for c in recovered.clone()
3296 { write!(txt, "{:02X} ", c); }
3297 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 ");
3298
3299 let mut converted = String::new();
3300 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3301
3302 println!("Bb =\t{}", converted);
3303 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3304 assert_eq!(converted, message);
3305 println!();
3306
3307 // Normal case for Rijndael-256-256
3308 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3309 print!("K =\t");
3310 for i in 0..32
3311 { print!("{:02X}", key[i]); }
3312 println!();
3313 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
3314 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3315 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
3316
3317 let message = "In the beginning God created the heavens and the earth.";
3318 println!("M =\t{}", message);
3319 let mut cipher = Vec::<u8>::new();
3320 a_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
3321 print!("C =\t");
3322 for c in cipher.clone()
3323 { print!("{:02X} ", c); }
3324 println!();
3325 let mut txt = String::new();
3326 for c in cipher.clone()
3327 { write!(txt, "{:02X} ", c); }
3328 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
3329
3330 let mut recovered = Vec::<u8>::new();
3331 a_rijndael.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
3332 print!("Ba =\t");
3333 for b in recovered.clone()
3334 { print!("{:02X} ", b); }
3335 println!();
3336 let mut txt = String::new();
3337 for c in recovered.clone()
3338 { write!(txt, "{:02X} ", c); }
3339 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 ");
3340
3341 let mut converted = String::new();
3342 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3343
3344 println!("Bb =\t{}", converted);
3345 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3346 assert_eq!(converted, message);
3347 println!();
3348
3349 // Normal case for Rijndael-512-512 for post-quantum
3350 use cryptocol::number::SharedArrays;
3351 use cryptocol::hash::SHA3_512;
3352 let mut sha3 = SHA3_512::new();
3353 sha3.absorb_str("Post-quantum");
3354 let key: [u8; 64] = sha3.get_hash_value_in_array();
3355 print!("K =\t");
3356 for i in 0..64
3357 { print!("{:02X}", key[i]); }
3358 println!();
3359 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
3360 sha3.absorb_str("Initialize");
3361 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
3362 iv.src = sha3.get_hash_value_in_array();
3363 let iv = unsafe { iv.des };
3364 print!("IV =\t");
3365 for i in 0..16
3366 { print!("{:08X}", iv[i].to_be()); }
3367 println!();
3368
3369 let message = "In the beginning God created the heavens and the earth.";
3370 println!("M =\t{}", message);
3371 let mut cipher = Vec::<u8>::new();
3372 a_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
3373 print!("C =\t");
3374 for c in cipher.clone()
3375 { print!("{:02X} ", c); }
3376 println!();
3377 let mut txt = String::new();
3378 for c in cipher.clone()
3379 { write!(txt, "{:02X} ", c); }
3380 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
3381
3382 let mut recovered = Vec::<u8>::new();
3383 a_rijndael.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
3384 print!("Ba =\t");
3385 for b in recovered.clone()
3386 { print!("{:02X} ", b); }
3387 println!();
3388 let mut txt = String::new();
3389 for c in recovered.clone()
3390 { write!(txt, "{:02X} ", c); }
3391 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
3392
3393 let mut converted = String::new();
3394 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3395
3396 println!("Bb =\t{}", converted);
3397 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3398 assert_eq!(converted, message);
3399 println!("-------------------------------");
3400}
3401
3402fn aes_decrypt_vec_ofb_into_array()
3403{
3404 println!("aes_decrypt_vec_ofb_into_array()");
3405 use std::io::Write;
3406 use std::fmt::Write as _;
3407 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
3408
3409 // Normal case for AES-128
3410 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
3411 println!("K =\t{:#016X}", key);
3412 let mut a_aes = AES_128::new_with_key_u128(key);
3413 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3414 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3415
3416 let message = "In the beginning God created the heavens and the earth.";
3417 println!("M =\t{}", message);
3418 let mut cipher = Vec::<u8>::new();
3419 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3420 print!("C =\t");
3421 for c in cipher.clone()
3422 { print!("{:02X} ", c); }
3423 println!();
3424 let mut txt = String::new();
3425 for c in cipher.clone()
3426 { write!(txt, "{:02X} ", c); }
3427 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
3428
3429 let mut recovered = [0; 64];
3430 let len = a_aes.decrypt_vec_into_array(iv, &cipher, &mut recovered);
3431 print!("Ba =\t");
3432 for b in recovered.clone()
3433 { print!("{:02X} ", b); }
3434 println!();
3435 let mut txt = String::new();
3436 for c in recovered.clone()
3437 { write!(txt, "{:02X} ", c); }
3438 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
3439
3440 let mut converted = String::new();
3441 unsafe { converted.as_mut_vec() }.write(&recovered);
3442 unsafe { converted.as_mut_vec() }.truncate(len as usize);
3443 println!("Bb =\t{}", converted);
3444 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3445 assert_eq!(converted, message);
3446 println!();
3447
3448 // Normal case for AES-192
3449 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3450 print!("K =\t");
3451 for i in 0..24
3452 { print!("{:02X}", key[i]); }
3453 println!();
3454 let mut a_aes = AES_192::new_with_key(&key);
3455 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3456 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3457
3458 let message = "In the beginning God created the heavens and the earth.";
3459 println!("M =\t{}", message);
3460 let mut cipher = Vec::<u8>::new();
3461 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3462 print!("C =\t");
3463 for c in cipher.clone()
3464 { print!("{:02X} ", c); }
3465 println!();
3466 let mut txt = String::new();
3467 for c in cipher.clone()
3468 { write!(txt, "{:02X} ", c); }
3469 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
3470
3471 let mut recovered = [0; 64];
3472 a_aes.decrypt_vec_into_array(iv, &cipher, &mut recovered);
3473 print!("Ba =\t");
3474 for b in recovered.clone()
3475 { print!("{:02X} ", b); }
3476 println!();
3477 let mut txt = String::new();
3478 for c in recovered.clone()
3479 { write!(txt, "{:02X} ", c); }
3480 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
3481
3482 let mut converted = String::new();
3483 unsafe { converted.as_mut_vec() }.write(&recovered);
3484 unsafe { converted.as_mut_vec() }.truncate(len as usize);
3485 println!("Bb =\t{}", converted);
3486 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3487 assert_eq!(converted, message);
3488 println!();
3489
3490 // Normal case for AES-256
3491 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3492 print!("K =\t");
3493 for i in 0..32
3494 { print!("{:02X}", key[i]); }
3495 println!();
3496 let mut a_aes = AES_256::new_with_key(&key);
3497 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3498 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3499
3500 let message = "In the beginning God created the heavens and the earth.";
3501 println!("M =\t{}", message);
3502 let mut cipher = Vec::<u8>::new();
3503 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3504 print!("C =\t");
3505 for c in cipher.clone()
3506 { print!("{:02X} ", c); }
3507 println!();
3508 let mut txt = String::new();
3509 for c in cipher.clone()
3510 { write!(txt, "{:02X} ", c); }
3511 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
3512
3513 let mut recovered = [0; 64];
3514 a_aes.decrypt_vec_into_array(iv, &cipher, &mut recovered);
3515 print!("Ba =\t");
3516 for b in recovered.clone()
3517 { print!("{:02X} ", b); }
3518 println!();
3519 let mut txt = String::new();
3520 for c in recovered.clone()
3521 { write!(txt, "{:02X} ", c); }
3522 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
3523
3524 let mut converted = String::new();
3525 unsafe { converted.as_mut_vec() }.write(&recovered);
3526 unsafe { converted.as_mut_vec() }.truncate(len as usize);
3527 println!("Bb =\t{}", converted);
3528 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3529 assert_eq!(converted, message);
3530 println!();
3531
3532 // Normal case for Rijndael-256-256
3533 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3534 print!("K =\t");
3535 for i in 0..32
3536 { print!("{:02X}", key[i]); }
3537 println!();
3538 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
3539 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3540 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
3541
3542 let message = "In the beginning God created the heavens and the earth.";
3543 println!("M =\t{}", message);
3544 let mut cipher = Vec::<u8>::new();
3545 a_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
3546 print!("C =\t");
3547 for c in cipher.clone()
3548 { print!("{:02X} ", c); }
3549 println!();
3550 let mut txt = String::new();
3551 for c in cipher.clone()
3552 { write!(txt, "{:02X} ", c); }
3553 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
3554
3555 let mut recovered = [0; 64];
3556 a_rijndael.decrypt_vec_into_array(iv, &cipher, &mut recovered);
3557 print!("Ba =\t");
3558 for b in recovered.clone()
3559 { print!("{:02X} ", b); }
3560 println!();
3561 let mut txt = String::new();
3562 for c in recovered.clone()
3563 { write!(txt, "{:02X} ", c); }
3564 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
3565
3566 let mut converted = String::new();
3567 unsafe { converted.as_mut_vec() }.write(&recovered);
3568 unsafe { converted.as_mut_vec() }.truncate(len as usize);
3569 println!("Bb =\t{}", converted);
3570 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3571 assert_eq!(converted, message);
3572 println!();
3573
3574 // Normal case for Rijndael-512-512 for post-quantum
3575 use cryptocol::number::SharedArrays;
3576 use cryptocol::hash::SHA3_512;
3577 let mut sha3 = SHA3_512::new();
3578 sha3.absorb_str("Post-quantum");
3579 let key: [u8; 64] = sha3.get_hash_value_in_array();
3580 print!("K =\t");
3581 for i in 0..64
3582 { print!("{:02X}", key[i]); }
3583 println!();
3584 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
3585 sha3.absorb_str("Initialize");
3586 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
3587 iv.src = sha3.get_hash_value_in_array();
3588 let iv = unsafe { iv.des };
3589 print!("IV =\t");
3590 for i in 0..16
3591 { print!("{:08X}", iv[i].to_be()); }
3592 println!();
3593
3594 let message = "In the beginning God created the heavens and the earth.";
3595 println!("M =\t{}", message);
3596 let mut cipher = Vec::<u8>::new();
3597 a_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
3598 print!("C =\t");
3599 for c in cipher.clone()
3600 { print!("{:02X} ", c); }
3601 println!();
3602 let mut txt = String::new();
3603 for c in cipher.clone()
3604 { write!(txt, "{:02X} ", c); }
3605 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
3606
3607 let mut recovered = [0; 64];
3608 a_rijndael.decrypt_vec_into_array(iv, &cipher, &mut recovered);
3609 print!("Ba =\t");
3610 for b in recovered.clone()
3611 { print!("{:02X} ", b); }
3612 println!();
3613 let mut txt = String::new();
3614 for c in recovered.clone()
3615 { write!(txt, "{:02X} ", c); }
3616 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
3617
3618 let mut converted = String::new();
3619 unsafe { converted.as_mut_vec() }.write(&recovered);
3620 unsafe { converted.as_mut_vec() }.truncate(len as usize);
3621 println!("Bb =\t{}", converted);
3622 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3623 assert_eq!(converted, message);
3624 println!("-------------------------------");
3625}
3626
3627fn aes_decrypt_vec_ofb_into_string()
3628{
3629 println!("aes_decrypt_vec_ofb_into_string()");
3630 use std::io::Write;
3631 use std::fmt::Write as _;
3632 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
3633
3634 // Normal case for AES-128
3635 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
3636 println!("K =\t{:#016X}", key);
3637 let mut a_aes = AES_128::new_with_key_u128(key);
3638 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3639 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3640
3641 let message = "In the beginning God created the heavens and the earth.";
3642 println!("M =\t{}", message);
3643 let mut cipher = Vec::<u8>::new();
3644 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3645 print!("C =\t");
3646 for c in cipher.clone()
3647 { print!("{:02X} ", c); }
3648 println!();
3649 let mut txt = String::new();
3650 for c in cipher.clone()
3651 { write!(txt, "{:02X} ", c); }
3652 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
3653
3654 let mut converted= String::new();
3655 a_aes.decrypt_vec_into_string(iv, &cipher, &mut converted);
3656 println!("B =\t{}", converted);
3657 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3658 assert_eq!(converted, message);
3659 println!();
3660
3661 // Normal case for AES-192
3662 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3663 print!("K =\t");
3664 for i in 0..24
3665 { print!("{:02X}", key[i]); }
3666 println!();
3667 let mut a_aes = AES_192::new_with_key(&key);
3668 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3669 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3670
3671 let message = "In the beginning God created the heavens and the earth.";
3672 println!("M =\t{}", message);
3673 let mut cipher = Vec::<u8>::new();
3674 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3675 print!("C =\t");
3676 for c in cipher.clone()
3677 { print!("{:02X} ", c); }
3678 println!();
3679 let mut txt = String::new();
3680 for c in cipher.clone()
3681 { write!(txt, "{:02X} ", c); }
3682 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
3683
3684 let mut converted= String::new();
3685 a_aes.decrypt_vec_into_string(iv, &cipher, &mut converted);
3686 println!("B =\t{}", converted);
3687 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3688 assert_eq!(converted, message);
3689 println!();
3690
3691 // Normal case for AES-256
3692 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3693 print!("K =\t");
3694 for i in 0..32
3695 { print!("{:02X}", key[i]); }
3696 println!();
3697 let mut a_aes = AES_256::new_with_key(&key);
3698 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3699 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3700
3701 let message = "In the beginning God created the heavens and the earth.";
3702 println!("M =\t{}", message);
3703 let mut cipher = Vec::<u8>::new();
3704 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3705 print!("C =\t");
3706 for c in cipher.clone()
3707 { print!("{:02X} ", c); }
3708 println!();
3709 let mut txt = String::new();
3710 for c in cipher.clone()
3711 { write!(txt, "{:02X} ", c); }
3712 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
3713
3714 let mut converted= String::new();
3715 a_aes.decrypt_vec_into_string(iv, &cipher, &mut converted);
3716 println!("B =\t{}", converted);
3717 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3718 assert_eq!(converted, message);
3719 println!();
3720
3721 // Normal case for Rijndael-256-256
3722 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3723 print!("K =\t");
3724 for i in 0..32
3725 { print!("{:02X}", key[i]); }
3726 println!();
3727 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
3728 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3729 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
3730
3731 let message = "In the beginning God created the heavens and the earth.";
3732 println!("M =\t{}", message);
3733 let mut cipher = Vec::<u8>::new();
3734 a_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
3735 print!("C =\t");
3736 for c in cipher.clone()
3737 { print!("{:02X} ", c); }
3738 println!();
3739 let mut txt = String::new();
3740 for c in cipher.clone()
3741 { write!(txt, "{:02X} ", c); }
3742 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
3743
3744 let mut converted= String::new();
3745 a_rijndael.decrypt_vec_into_string(iv, &cipher, &mut converted);
3746 println!("B =\t{}", converted);
3747 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3748 assert_eq!(converted, message);
3749 println!();
3750
3751 // Normal case for Rijndael-512-512 for post-quantum
3752 use cryptocol::number::SharedArrays;
3753 use cryptocol::hash::SHA3_512;
3754 let mut sha3 = SHA3_512::new();
3755 sha3.absorb_str("Post-quantum");
3756 let key: [u8; 64] = sha3.get_hash_value_in_array();
3757 print!("K =\t");
3758 for i in 0..64
3759 { print!("{:02X}", key[i]); }
3760 println!();
3761 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
3762 sha3.absorb_str("Initialize");
3763 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
3764 iv.src = sha3.get_hash_value_in_array();
3765 let iv = unsafe { iv.des };
3766 print!("IV =\t");
3767 for i in 0..16
3768 { print!("{:08X}", iv[i].to_be()); }
3769 println!();
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_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
3775 print!("C =\t");
3776 for c in cipher.clone()
3777 { print!("{:02X} ", c); }
3778 println!();
3779 let mut txt = String::new();
3780 for c in cipher.clone()
3781 { write!(txt, "{:02X} ", c); }
3782 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
3783
3784 let mut converted= String::new();
3785 a_rijndael.decrypt_vec_into_string(iv, &cipher, &mut converted);
3786 println!("B =\t{}", converted);
3787 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3788 assert_eq!(converted, message);
3789 println!("-------------------------------");
3790}807fn des_encrypt_str_ofb_into_vec()
808{
809 println!("des_encrypt_str_ofb_into_vec()");
810 use std::io::Write;
811 use std::fmt::Write as _;
812 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
813
814 // Normal case
815 let key = 0x_1234567890ABCDEF_u64;
816 println!("K =\t{:#016X}", key);
817 let mut a_des = DES::new_with_key_u64(key);
818
819 let message = "In the beginning God created the heavens and the earth.";
820 println!("M =\t{}", message);
821 let iv = 0x_FEDCBA0987654321_u64;
822 println!("IV = {}", iv);
823 let mut cipher = Vec::<u8>::new();
824 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
825 print!("C (16 rounds) =\t");
826 for c in cipher.clone()
827 { print!("{:02X} ", c); }
828 println!();
829 let mut txt = String::new();
830 for c in cipher.clone()
831 { write!(txt, "{:02X} ", c); }
832 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
833 println!();
834
835 // Expanded case for 128 rounds
836 let key = 0x_1234567890ABCDEF_u64;
837 println!("K =\t{:#016X}", key);
838 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
839
840 let message = "In the beginning God created the heavens and the earth.";
841 println!("M =\t{}", message);
842 let iv = 0x_FEDCBA0987654321_u64;
843 println!("IV = {}", iv);
844 let mut cipher = Vec::<u8>::new();
845 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
846 print!("C (128 rounds) =\t");
847 for c in cipher.clone()
848 { print!("{:02X} ", c); }
849 println!();
850 let mut txt = String::new();
851 for c in cipher.clone()
852 { write!(txt, "{:02X} ", c); }
853 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
854 println!();
855
856 // Expanded case for 0 rounds which means that key is meaningless
857 let key1 = 0x_1234567890ABCDEF_u64;
858 let key2 = 0_u64;
859 println!("K =\t{:#016X}", key);
860 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
861 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
862
863 let message = "In the beginning God created the heavens and the earth.";
864 println!("M =\t{}", message);
865 let iv = 0x_FEDCBA0987654321_u64;
866 println!("IV = {}", iv);
867 let mut cipher1 = Vec::<u8>::new();
868 let mut cipher2 = Vec::<u8>::new();
869 c_des.encrypt_str_into_vec(iv, &message, &mut cipher1);
870 d_des.encrypt_str_into_vec(iv, &message, &mut cipher2);
871 print!("C (0 rounds) =\t");
872 for c in cipher1.clone()
873 { print!("{:02X} ", c); }
874 println!();
875 let mut txt = String::new();
876 for c in cipher1.clone()
877 { write!(txt, "{:02X} ", c); }
878 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
879 print!("D (0 rounds) =\t");
880 for c in cipher2.clone()
881 { print!("{:02X} ", c); }
882 println!();
883 let mut txt = String::new();
884 for c in cipher2.clone()
885 { write!(txt, "{:02X} ", c); }
886 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
887 println!();
888
889 // Normal case for the message of 0 bytes
890 let key = 0x_1234567890ABCDEF_u64;
891 println!("K =\t{:#016X}", key);
892 let mut a_des = DES::new_with_key_u64(key);
893
894 let message = "";
895 println!("M =\t{}", message);
896 let iv = 0x_FEDCBA0987654321_u64;
897 println!("IV = {}", iv);
898 let mut cipher = Vec::<u8>::new();
899 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
900 print!("C =\t");
901 for c in cipher.clone()
902 { print!("{:02X} ", c); }
903 println!();
904 let mut txt = String::new();
905 for c in cipher.clone()
906 { write!(txt, "{:02X} ", c); }
907 assert_eq!(txt, "");
908 println!();
909
910 // Normal case for the message shorter than 8 bytes
911 let key = 0x_1234567890ABCDEF_u64;
912 println!("K =\t{:#016X}", key);
913 let mut a_des = DES::new_with_key_u64(key);
914
915 let message = "7 bytes";
916 println!("M =\t{}", message);
917 let iv = 0x_FEDCBA0987654321_u64;
918 println!("IV = {}", iv);
919 let mut cipher = Vec::<u8>::new();
920 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
921 print!("C =\t");
922 for c in cipher.clone()
923 { print!("{:02X} ", c); }
924 println!();
925 let mut txt = String::new();
926 for c in cipher.clone()
927 { write!(txt, "{:02X} ", c); }
928 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
929 println!();
930
931 // Normal case for the message of 8 bytes
932 let key = 0x_1234567890ABCDEF_u64;
933 println!("K =\t{:#016X}", key);
934 let mut a_des = DES::new_with_key_u64(key);
935
936 let message = "I am OK.";
937 println!("M =\t{}", message);
938 let iv = 0x_FEDCBA0987654321_u64;
939 println!("IV = {}", iv);
940 let mut cipher = Vec::<u8>::new();
941 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
942 print!("C =\t");
943 for c in cipher.clone()
944 { print!("{:02X} ", c); }
945 println!();
946 let mut txt = String::new();
947 for c in cipher.clone()
948 { write!(txt, "{:02X} ", c); }
949 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
950 println!();
951
952 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
953 let key = 0x_1234567890ABCDEF_u64;
954 println!("K =\t{:#016X}", key);
955 let mut a_des = DES::new_with_key_u64(key);
956
957 let message = "PARK Youngho";
958 println!("M =\t{}", message);
959 let iv = 0x_FEDCBA0987654321_u64;
960 println!("IV = {}", iv);
961 let mut cipher = Vec::<u8>::new();
962 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
963 print!("C =\t");
964 for c in cipher.clone()
965 { print!("{:02X} ", c); }
966 println!();
967 let mut txt = String::new();
968 for c in cipher.clone()
969 { write!(txt, "{:02X} ", c); }
970 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
971 println!();
972
973 // Normal case for the message of 16 bytes
974 let key = 0x_1234567890ABCDEF_u64;
975 println!("K =\t{:#016X}", key);
976 let mut a_des = DES::new_with_key_u64(key);
977
978 let message = "고맙습니다.";
979 println!("M =\t{}", message);
980 let iv = 0x_FEDCBA0987654321_u64;
981 println!("IV = {}", iv);
982 let mut cipher = Vec::<u8>::new();
983 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
984 print!("C =\t");
985 for c in cipher.clone()
986 { print!("{:02X} ", c); }
987 println!();
988 let mut txt = String::new();
989 for c in cipher.clone()
990 { write!(txt, "{:02X} ", c); }
991 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
992 println!("-------------------------------");
993}
994
995fn des_encrypt_str_ofb_into_array()
996{
997 println!("des_encrypt_str_ofb_into_array()");
998 use std::io::Write;
999 use std::fmt::Write as _;
1000 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
1001
1002 // Normal case
1003 let key = 0x_1234567890ABCDEF_u64;
1004 println!("K =\t{:#016X}", key);
1005 let mut a_des = DES::new_with_key_u64(key);
1006
1007 let message = "In the beginning God created the heavens and the earth.";
1008 println!("M =\t{}", message);
1009 let iv = 0x_FEDCBA0987654321_u64;
1010 println!("IV = {}", iv);
1011 let mut cipher = [0_u8; 55];
1012 a_des.encrypt_str_into_array(iv, &message, &mut cipher);
1013 print!("C (16 rounds) =\t");
1014 for c in cipher.clone()
1015 { print!("{:02X} ", c); }
1016 println!();
1017 let mut txt = String::new();
1018 for c in cipher.clone()
1019 { write!(txt, "{:02X} ", c); }
1020 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
1021 println!();
1022
1023 // Expanded case for 128 rounds
1024 let key = 0x_1234567890ABCDEF_u64;
1025 println!("K =\t{:#016X}", key);
1026 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
1027
1028 let message = "In the beginning God created the heavens and the earth.";
1029 println!("M =\t{}", message);
1030 let iv = 0x_FEDCBA0987654321_u64;
1031 println!("IV = {}", iv);
1032 let mut cipher = [0_u8; 55];
1033 a_des.encrypt_str_into_array(iv, &message, &mut cipher);
1034 print!("C (128 rounds) =\t");
1035 for c in cipher.clone()
1036 { print!("{:02X} ", c); }
1037 println!();
1038 let mut txt = String::new();
1039 for c in cipher.clone()
1040 { write!(txt, "{:02X} ", c); }
1041 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
1042 println!();
1043
1044 // Expanded case for 0 rounds which means that key is meaningless
1045 let key1 = 0x_1234567890ABCDEF_u64;
1046 let key2 = 0_u64;
1047 println!("K =\t{:#016X}", key);
1048 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
1049 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
1050
1051 let message = "In the beginning God created the heavens and the earth.";
1052 println!("M =\t{}", message);
1053 let iv = 0x_FEDCBA0987654321_u64;
1054 println!("IV = {}", iv);
1055 let mut cipher1 = [0_u8; 55];
1056 let mut cipher2 = [0_u8; 55];
1057 c_des.encrypt_str_into_array(iv, &message, &mut cipher1);
1058 d_des.encrypt_str_into_array(iv, &message, &mut cipher2);
1059 print!("C (0 rounds) =\t");
1060 for c in cipher1.clone()
1061 { print!("{:02X} ", c); }
1062 println!();
1063 let mut txt = String::new();
1064 for c in cipher1.clone()
1065 { write!(txt, "{:02X} ", c); }
1066 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
1067 print!("D (0 rounds) =\t");
1068 for c in cipher2.clone()
1069 { print!("{:02X} ", c); }
1070 println!();
1071 let mut txt = String::new();
1072 for c in cipher2.clone()
1073 { write!(txt, "{:02X} ", c); }
1074 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
1075 println!();
1076
1077 // Normal case for the message of 0 bytes
1078 let key = 0x_1234567890ABCDEF_u64;
1079 println!("K =\t{:#016X}", key);
1080 let mut a_des = DES::new_with_key_u64(key);
1081
1082 let message = "";
1083 println!("M =\t{}", message);
1084 let iv = 0x_FEDCBA0987654321_u64;
1085 println!("IV = {}", iv);
1086 let mut cipher = [0_u8; 0];
1087 a_des.encrypt_str_into_array(iv, &message, &mut cipher);
1088 print!("C =\t");
1089 for c in cipher.clone()
1090 { print!("{:02X} ", c); }
1091 println!();
1092 let mut txt = String::new();
1093 for c in cipher.clone()
1094 { write!(txt, "{:02X} ", c); }
1095 assert_eq!(txt, "");
1096 println!();
1097
1098 // Normal case for the message shorter than 8 bytes
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 = "7 bytes";
1104 println!("M =\t{}", message);
1105 let iv = 0x_FEDCBA0987654321_u64;
1106 println!("IV = {}", iv);
1107 let mut cipher = [0_u8; 7];
1108 a_des.encrypt_str_into_array(iv, &message, &mut cipher);
1109 print!("C =\t");
1110 for c in cipher.clone()
1111 { print!("{:02X} ", c); }
1112 println!();
1113 let mut txt = String::new();
1114 for c in cipher.clone()
1115 { write!(txt, "{:02X} ", c); }
1116 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
1117 println!();
1118
1119 // Normal case for the message of 8 bytes
1120 let key = 0x_1234567890ABCDEF_u64;
1121 println!("K =\t{:#016X}", key);
1122 let mut a_des = DES::new_with_key_u64(key);
1123
1124 let message = "I am OK.";
1125 println!("M =\t{}", message);
1126 let iv = 0x_FEDCBA0987654321_u64;
1127 println!("IV = {}", iv);
1128 let mut cipher = [0_u8; 8];
1129 a_des.encrypt_str_into_array(iv, &message, &mut cipher);
1130 print!("C =\t");
1131 for c in cipher.clone()
1132 { print!("{:02X} ", c); }
1133 println!();
1134 let mut txt = String::new();
1135 for c in cipher.clone()
1136 { write!(txt, "{:02X} ", c); }
1137 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
1138 println!();
1139
1140 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
1141 let key = 0x_1234567890ABCDEF_u64;
1142 println!("K =\t{:#016X}", key);
1143 let mut a_des = DES::new_with_key_u64(key);
1144
1145 let message = "PARK Youngho";
1146 println!("M =\t{}", message);
1147 let iv = 0x_FEDCBA0987654321_u64;
1148 println!("IV = {}", iv);
1149 let mut cipher = [0_u8; 12];
1150 a_des.encrypt_str_into_array(iv, &message, &mut cipher);
1151 print!("C =\t");
1152 for c in cipher.clone()
1153 { print!("{:02X} ", c); }
1154 println!();
1155 let mut txt = String::new();
1156 for c in cipher.clone()
1157 { write!(txt, "{:02X} ", c); }
1158 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
1159 println!();
1160
1161 // Normal case for the message of 16 bytes
1162 let key = 0x_1234567890ABCDEF_u64;
1163 println!("K =\t{:#016X}", key);
1164 let mut a_des = DES::new_with_key_u64(key);
1165
1166 let message = "고맙습니다.";
1167 println!("M =\t{}", message);
1168 let iv = 0x_FEDCBA0987654321_u64;
1169 println!("IV = {}", iv);
1170 let mut cipher = [0_u8; 16];
1171 a_des.encrypt_str_into_array(iv, &message, &mut cipher);
1172 print!("C =\t");
1173 for c in cipher.clone()
1174 { print!("{:02X} ", c); }
1175 println!();
1176 let mut txt = String::new();
1177 for c in cipher.clone()
1178 { write!(txt, "{:02X} ", c); }
1179 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
1180 println!("-------------------------------");
1181}
1182
1183fn des_encrypt_string_ofb()
1184{
1185 println!("des_encrypt_string_ofb()");
1186 use std::io::Write;
1187 use std::fmt::Write as _;
1188 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
1189
1190 // Normal case
1191 let key = 0x_1234567890ABCDEF_u64;
1192 println!("K =\t{:#016X}", key);
1193 let mut a_des = DES::new_with_key_u64(key);
1194
1195 let message = "In the beginning God created the heavens and the earth.".to_string();
1196 println!("M =\t{}", message);
1197 let iv = 0x_FEDCBA0987654321_u64;
1198 println!("IV = {}", iv);
1199 let mut cipher = [0_u8; 55];
1200 a_des.encrypt_string(iv, &message, cipher.as_mut_ptr());
1201 print!("C (16 rounds) =\t");
1202 for c in cipher.clone()
1203 { print!("{:02X} ", c); }
1204 println!();
1205 let mut txt = String::new();
1206 for c in cipher.clone()
1207 { write!(txt, "{:02X} ", c); }
1208 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
1209 println!();
1210
1211 // Expanded case for 128 rounds
1212 let key = 0x_1234567890ABCDEF_u64;
1213 println!("K =\t{:#016X}", key);
1214 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
1215
1216 let message = "In the beginning God created the heavens and the earth.".to_string();
1217 println!("M =\t{}", message);
1218 let iv = 0x_FEDCBA0987654321_u64;
1219 println!("IV = {}", iv);
1220 let mut cipher = [0_u8; 55];
1221 a_des.encrypt_string(iv, &message, cipher.as_mut_ptr());
1222 print!("C (128 rounds) =\t");
1223 for c in cipher.clone()
1224 { print!("{:02X} ", c); }
1225 println!();
1226 let mut txt = String::new();
1227 for c in cipher.clone()
1228 { write!(txt, "{:02X} ", c); }
1229 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
1230 println!();
1231
1232 // Expanded case for 0 rounds which means that key is meaningless
1233 let key1 = 0x_1234567890ABCDEF_u64;
1234 let key2 = 0_u64;
1235 println!("K =\t{:#016X}", key);
1236 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
1237 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
1238
1239 let message = "In the beginning God created the heavens and the earth.".to_string();
1240 println!("M =\t{}", message);
1241 let iv = 0x_FEDCBA0987654321_u64;
1242 println!("IV = {}", iv);
1243 let mut cipher1 = [0_u8; 55];
1244 let mut cipher2 = [0_u8; 55];
1245 c_des.encrypt_string(iv, &message, cipher1.as_mut_ptr());
1246 d_des.encrypt_string(iv, &message, cipher2.as_mut_ptr());
1247 print!("C (0 rounds) =\t");
1248 for c in cipher1.clone()
1249 { print!("{:02X} ", c); }
1250 println!();
1251 let mut txt = String::new();
1252 for c in cipher1.clone()
1253 { write!(txt, "{:02X} ", c); }
1254 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
1255 print!("D (0 rounds) =\t");
1256 for c in cipher2.clone()
1257 { print!("{:02X} ", c); }
1258 println!();
1259 let mut txt = String::new();
1260 for c in cipher2.clone()
1261 { write!(txt, "{:02X} ", c); }
1262 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
1263 println!();
1264
1265 // Normal case for the message of 0 bytes
1266 let key = 0x_1234567890ABCDEF_u64;
1267 println!("K =\t{:#016X}", key);
1268 let mut a_des = DES::new_with_key_u64(key);
1269
1270 let message = "".to_string();
1271 println!("M =\t{}", message);
1272 let iv = 0x_FEDCBA0987654321_u64;
1273 println!("IV = {}", iv);
1274 let mut cipher = [0_u8; 0];
1275 a_des.encrypt_string(iv, &message, cipher.as_mut_ptr());
1276 print!("C =\t");
1277 for c in cipher.clone()
1278 { print!("{:02X} ", c); }
1279 println!();
1280 let mut txt = String::new();
1281 for c in cipher.clone()
1282 { write!(txt, "{:02X} ", c); }
1283 assert_eq!(txt, "");
1284 println!();
1285
1286 // Normal case for the message shorter than 8 bytes
1287 let key = 0x_1234567890ABCDEF_u64;
1288 println!("K =\t{:#016X}", key);
1289 let mut a_des = DES::new_with_key_u64(key);
1290
1291 let message = "7 bytes".to_string();
1292 println!("M =\t{}", message);
1293 let iv = 0x_FEDCBA0987654321_u64;
1294 println!("IV = {}", iv);
1295 let mut cipher = [0_u8; 7];
1296 a_des.encrypt_string(iv, &message, cipher.as_mut_ptr());
1297 print!("C =\t");
1298 for c in cipher.clone()
1299 { print!("{:02X} ", c); }
1300 println!();
1301 let mut txt = String::new();
1302 for c in cipher.clone()
1303 { write!(txt, "{:02X} ", c); }
1304 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
1305 println!();
1306
1307 // Normal case for the message of 8 bytes
1308 let key = 0x_1234567890ABCDEF_u64;
1309 println!("K =\t{:#016X}", key);
1310 let mut a_des = DES::new_with_key_u64(key);
1311
1312 let message = "I am OK.".to_string();
1313 println!("M =\t{}", message);
1314 let iv = 0x_FEDCBA0987654321_u64;
1315 println!("IV = {}", iv);
1316 let mut cipher = [0_u8; 8];
1317 a_des.encrypt_string(iv, &message, cipher.as_mut_ptr());
1318 print!("C =\t");
1319 for c in cipher.clone()
1320 { print!("{:02X} ", c); }
1321 println!();
1322 let mut txt = String::new();
1323 for c in cipher.clone()
1324 { write!(txt, "{:02X} ", c); }
1325 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
1326 println!();
1327
1328 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
1329 let key = 0x_1234567890ABCDEF_u64;
1330 println!("K =\t{:#016X}", key);
1331 let mut a_des = DES::new_with_key_u64(key);
1332
1333 let message = "PARK Youngho".to_string();
1334 println!("M =\t{}", message);
1335 let iv = 0x_FEDCBA0987654321_u64;
1336 println!("IV = {}", iv);
1337 let mut cipher = [0_u8; 12];
1338 a_des.encrypt_string(iv, &message, cipher.as_mut_ptr());
1339 print!("C =\t");
1340 for c in cipher.clone()
1341 { print!("{:02X} ", c); }
1342 println!();
1343 let mut txt = String::new();
1344 for c in cipher.clone()
1345 { write!(txt, "{:02X} ", c); }
1346 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
1347 println!();
1348
1349 // Normal case for the message of 16 bytes
1350 let key = 0x_1234567890ABCDEF_u64;
1351 println!("K =\t{:#016X}", key);
1352 let mut a_des = DES::new_with_key_u64(key);
1353
1354 let message = "고맙습니다.".to_string();
1355 println!("M =\t{}", message);
1356 let iv = 0x_FEDCBA0987654321_u64;
1357 println!("IV = {}", iv);
1358 let mut cipher = [0_u8; 16];
1359 a_des.encrypt_string(iv, &message, cipher.as_mut_ptr());
1360 print!("C =\t");
1361 for c in cipher.clone()
1362 { print!("{:02X} ", c); }
1363 println!();
1364 let mut txt = String::new();
1365 for c in cipher.clone()
1366 { write!(txt, "{:02X} ", c); }
1367 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
1368 println!("-------------------------------");
1369}
1370
1371fn des_encrypt_string_ofb_into_vec()
1372{
1373 println!("des_encrypt_string_ofb_into_vec()");
1374 use std::io::Write;
1375 use std::fmt::Write as _;
1376 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
1377
1378 // Normal case
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 = "In the beginning God created the heavens and the earth.".to_string();
1384 println!("M =\t{}", message);
1385 let iv = 0x_FEDCBA0987654321_u64;
1386 println!("IV = {}", iv);
1387 let mut cipher = Vec::<u8>::new();
1388 a_des.encrypt_string_into_vec(iv, &message, &mut cipher);
1389 print!("C (16 rounds) =\t");
1390 for c in cipher.clone()
1391 { print!("{:02X} ", c); }
1392 println!();
1393 let mut txt = String::new();
1394 for c in cipher.clone()
1395 { write!(txt, "{:02X} ", c); }
1396 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
1397 println!();
1398
1399 // Expanded case for 128 rounds
1400 let key = 0x_1234567890ABCDEF_u64;
1401 println!("K =\t{:#016X}", key);
1402 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
1403
1404 let message = "In the beginning God created the heavens and the earth.".to_string();
1405 println!("M =\t{}", message);
1406 let iv = 0x_FEDCBA0987654321_u64;
1407 println!("IV = {}", iv);
1408 let mut cipher = Vec::<u8>::new();
1409 a_des.encrypt_string_into_vec(iv, &message, &mut cipher);
1410 print!("C (128 rounds) =\t");
1411 for c in cipher.clone()
1412 { print!("{:02X} ", c); }
1413 println!();
1414 let mut txt = String::new();
1415 for c in cipher.clone()
1416 { write!(txt, "{:02X} ", c); }
1417 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
1418 println!();
1419
1420 // Expanded case for 0 rounds which means that key is meaningless
1421 let key1 = 0x_1234567890ABCDEF_u64;
1422 let key2 = 0_u64;
1423 println!("K =\t{:#016X}", key);
1424 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
1425 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
1426
1427 let message = "In the beginning God created the heavens and the earth.".to_string();
1428 println!("M =\t{}", message);
1429 let iv = 0x_FEDCBA0987654321_u64;
1430 println!("IV = {}", iv);
1431 let mut cipher1 = Vec::<u8>::new();
1432 let mut cipher2 = Vec::<u8>::new();
1433 c_des.encrypt_string_into_vec(iv, &message, &mut cipher1);
1434 d_des.encrypt_string_into_vec(iv, &message, &mut cipher2);
1435 print!("C (0 rounds) =\t");
1436 for c in cipher1.clone()
1437 { print!("{:02X} ", c); }
1438 println!();
1439 let mut txt = String::new();
1440 for c in cipher1.clone()
1441 { write!(txt, "{:02X} ", c); }
1442 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
1443 print!("D (0 rounds) =\t");
1444 for c in cipher2.clone()
1445 { print!("{:02X} ", c); }
1446 println!();
1447 let mut txt = String::new();
1448 for c in cipher2.clone()
1449 { write!(txt, "{:02X} ", c); }
1450 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
1451 println!();
1452
1453 // Normal case for the message of 0 bytes
1454 let key = 0x_1234567890ABCDEF_u64;
1455 println!("K =\t{:#016X}", key);
1456 let mut a_des = DES::new_with_key_u64(key);
1457
1458 let message = "".to_string();
1459 println!("M =\t{}", message);
1460 let iv = 0x_FEDCBA0987654321_u64;
1461 println!("IV = {}", iv);
1462 let mut cipher = Vec::<u8>::new();
1463 a_des.encrypt_string_into_vec(iv, &message, &mut cipher);
1464 print!("C =\t");
1465 for c in cipher.clone()
1466 { print!("{:02X} ", c); }
1467 println!();
1468 let mut txt = String::new();
1469 for c in cipher.clone()
1470 { write!(txt, "{:02X} ", c); }
1471 assert_eq!(txt, "");
1472 println!();
1473
1474 // Normal case for the message shorter than 8 bytes
1475 let key = 0x_1234567890ABCDEF_u64;
1476 println!("K =\t{:#016X}", key);
1477 let mut a_des = DES::new_with_key_u64(key);
1478
1479 let message = "7 bytes".to_string();
1480 println!("M =\t{}", message);
1481 let iv = 0x_FEDCBA0987654321_u64;
1482 println!("IV = {}", iv);
1483 let mut cipher = Vec::<u8>::new();
1484 a_des.encrypt_string_into_vec(iv, &message, &mut cipher);
1485 print!("C =\t");
1486 for c in cipher.clone()
1487 { print!("{:02X} ", c); }
1488 println!();
1489 let mut txt = String::new();
1490 for c in cipher.clone()
1491 { write!(txt, "{:02X} ", c); }
1492 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
1493 println!();
1494
1495 // Normal case for the message of 8 bytes
1496 let key = 0x_1234567890ABCDEF_u64;
1497 println!("K =\t{:#016X}", key);
1498 let mut a_des = DES::new_with_key_u64(key);
1499
1500 let message = "I am OK.".to_string();
1501 println!("M =\t{}", message);
1502 let iv = 0x_FEDCBA0987654321_u64;
1503 println!("IV = {}", iv);
1504 let mut cipher = Vec::<u8>::new();
1505 a_des.encrypt_string_into_vec(iv, &message, &mut cipher);
1506 print!("C =\t");
1507 for c in cipher.clone()
1508 { print!("{:02X} ", c); }
1509 println!();
1510 let mut txt = String::new();
1511 for c in cipher.clone()
1512 { write!(txt, "{:02X} ", c); }
1513 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
1514 println!();
1515
1516 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
1517 let key = 0x_1234567890ABCDEF_u64;
1518 println!("K =\t{:#016X}", key);
1519 let mut a_des = DES::new_with_key_u64(key);
1520
1521 let message = "PARK Youngho".to_string();
1522 println!("M =\t{}", message);
1523 let iv = 0x_FEDCBA0987654321_u64;
1524 println!("IV = {}", iv);
1525 let mut cipher = Vec::<u8>::new();
1526 a_des.encrypt_string_into_vec(iv, &message, &mut cipher);
1527 print!("C =\t");
1528 for c in cipher.clone()
1529 { print!("{:02X} ", c); }
1530 println!();
1531 let mut txt = String::new();
1532 for c in cipher.clone()
1533 { write!(txt, "{:02X} ", c); }
1534 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
1535 println!();
1536
1537 // Normal case for the message of 16 bytes
1538 let key = 0x_1234567890ABCDEF_u64;
1539 println!("K =\t{:#016X}", key);
1540 let mut a_des = DES::new_with_key_u64(key);
1541
1542 let message = "고맙습니다.".to_string();
1543 println!("M =\t{}", message);
1544 let iv = 0x_FEDCBA0987654321_u64;
1545 println!("IV = {}", iv);
1546 let mut cipher = Vec::<u8>::new();
1547 a_des.encrypt_string_into_vec(iv, &message, &mut cipher);
1548 print!("C =\t");
1549 for c in cipher.clone()
1550 { print!("{:02X} ", c); }
1551 println!();
1552 let mut txt = String::new();
1553 for c in cipher.clone()
1554 { write!(txt, "{:02X} ", c); }
1555 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
1556 println!("-------------------------------");
1557}
1558
1559fn des_encrypt_string_ofb_into_array()
1560{
1561 println!("des_encrypt_string_ofb_into_array()");
1562 use std::io::Write;
1563 use std::fmt::Write as _;
1564 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
1565
1566 // Normal case
1567 let key = 0x_1234567890ABCDEF_u64;
1568 println!("K =\t{:#016X}", key);
1569 let mut a_des = DES::new_with_key_u64(key);
1570
1571 let message = "In the beginning God created the heavens and the earth.".to_string();
1572 println!("M =\t{}", message);
1573 let iv = 0x_FEDCBA0987654321_u64;
1574 println!("IV = {}", iv);
1575 let mut cipher = [0_u8; 55];
1576 a_des.encrypt_string_into_array(iv, &message, &mut cipher);
1577 print!("C (16 rounds) =\t");
1578 for c in cipher.clone()
1579 { print!("{:02X} ", c); }
1580 println!();
1581 let mut txt = String::new();
1582 for c in cipher.clone()
1583 { write!(txt, "{:02X} ", c); }
1584 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
1585 println!();
1586
1587 // Expanded case for 128 rounds
1588 let key = 0x_1234567890ABCDEF_u64;
1589 println!("K =\t{:#016X}", key);
1590 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
1591
1592 let message = "In the beginning God created the heavens and the earth.".to_string();
1593 println!("M =\t{}", message);
1594 let iv = 0x_FEDCBA0987654321_u64;
1595 println!("IV = {}", iv);
1596 let mut cipher = [0_u8; 55];
1597 a_des.encrypt_string_into_array(iv, &message, &mut cipher);
1598 print!("C (128 rounds) =\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, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
1606 println!();
1607
1608 // Expanded case for 0 rounds which means that key is meaningless
1609 let key1 = 0x_1234567890ABCDEF_u64;
1610 let key2 = 0_u64;
1611 println!("K =\t{:#016X}", key);
1612 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
1613 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
1614
1615 let message = "In the beginning God created the heavens and the earth.".to_string();
1616 println!("M =\t{}", message);
1617 let iv = 0x_FEDCBA0987654321_u64;
1618 println!("IV = {}", iv);
1619 let mut cipher1 = [0_u8; 55];
1620 let mut cipher2 = [0_u8; 55];
1621 c_des.encrypt_string_into_array(iv, &message, &mut cipher1);
1622 d_des.encrypt_string_into_array(iv, &message, &mut cipher2);
1623 print!("C (0 rounds) =\t");
1624 for c in cipher1.clone()
1625 { print!("{:02X} ", c); }
1626 println!();
1627 let mut txt = String::new();
1628 for c in cipher1.clone()
1629 { write!(txt, "{:02X} ", c); }
1630 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
1631 print!("D (0 rounds) =\t");
1632 for c in cipher2.clone()
1633 { print!("{:02X} ", c); }
1634 println!();
1635 let mut txt = String::new();
1636 for c in cipher2.clone()
1637 { write!(txt, "{:02X} ", c); }
1638 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
1639 println!();
1640
1641 // Normal case for the message of 0 bytes
1642 let key = 0x_1234567890ABCDEF_u64;
1643 println!("K =\t{:#016X}", key);
1644 let mut a_des = DES::new_with_key_u64(key);
1645
1646 let message = "".to_string();
1647 println!("M =\t{}", message);
1648 let iv = 0x_FEDCBA0987654321_u64;
1649 println!("IV = {}", iv);
1650 let mut cipher = [0_u8; 0];
1651 a_des.encrypt_string_into_array(iv, &message, &mut cipher);
1652 print!("C =\t");
1653 for c in cipher.clone()
1654 { print!("{:02X} ", c); }
1655 println!();
1656 let mut txt = String::new();
1657 for c in cipher.clone()
1658 { write!(txt, "{:02X} ", c); }
1659 assert_eq!(txt, "");
1660 println!();
1661
1662 // Normal case for the message shorter than 8 bytes
1663 let key = 0x_1234567890ABCDEF_u64;
1664 println!("K =\t{:#016X}", key);
1665 let mut a_des = DES::new_with_key_u64(key);
1666
1667 let message = "7 bytes".to_string();
1668 println!("M =\t{}", message);
1669 let iv = 0x_FEDCBA0987654321_u64;
1670 println!("IV = {}", iv);
1671 let mut cipher = [0_u8; 7];
1672 a_des.encrypt_string_into_array(iv, &message, &mut cipher);
1673 print!("C =\t");
1674 for c in cipher.clone()
1675 { print!("{:02X} ", c); }
1676 println!();
1677 let mut txt = String::new();
1678 for c in cipher.clone()
1679 { write!(txt, "{:02X} ", c); }
1680 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
1681 println!();
1682
1683 // Normal case for the message of 8 bytes
1684 let key = 0x_1234567890ABCDEF_u64;
1685 println!("K =\t{:#016X}", key);
1686 let mut a_des = DES::new_with_key_u64(key);
1687
1688 let message = "I am OK.".to_string();
1689 println!("M =\t{}", message);
1690 let iv = 0x_FEDCBA0987654321_u64;
1691 println!("IV = {}", iv);
1692 let mut cipher = [0_u8; 8];
1693 a_des.encrypt_string_into_array(iv, &message, &mut cipher);
1694 print!("C =\t");
1695 for c in cipher.clone()
1696 { print!("{:02X} ", c); }
1697 println!();
1698 let mut txt = String::new();
1699 for c in cipher.clone()
1700 { write!(txt, "{:02X} ", c); }
1701 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
1702 println!();
1703
1704 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
1705 let key = 0x_1234567890ABCDEF_u64;
1706 println!("K =\t{:#016X}", key);
1707 let mut a_des = DES::new_with_key_u64(key);
1708
1709 let message = "PARK Youngho".to_string();
1710 println!("M =\t{}", message);
1711 let iv = 0x_FEDCBA0987654321_u64;
1712 println!("IV = {}", iv);
1713 let mut cipher = [0_u8; 12];
1714 a_des.encrypt_string_into_array(iv, &message, &mut cipher);
1715 print!("C =\t");
1716 for c in cipher.clone()
1717 { print!("{:02X} ", c); }
1718 println!();
1719 let mut txt = String::new();
1720 for c in cipher.clone()
1721 { write!(txt, "{:02X} ", c); }
1722 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
1723 println!();
1724
1725 // Normal case for the message of 16 bytes
1726 let key = 0x_1234567890ABCDEF_u64;
1727 println!("K =\t{:#016X}", key);
1728 let mut a_des = DES::new_with_key_u64(key);
1729
1730 let message = "고맙습니다.".to_string();
1731 println!("M =\t{}", message);
1732 let iv = 0x_FEDCBA0987654321_u64;
1733 println!("IV = {}", iv);
1734 let mut cipher = [0_u8; 16];
1735 a_des.encrypt_string_into_array(iv, &message, &mut cipher);
1736 print!("C =\t");
1737 for c in cipher.clone()
1738 { print!("{:02X} ", c); }
1739 println!();
1740 let mut txt = String::new();
1741 for c in cipher.clone()
1742 { write!(txt, "{:02X} ", c); }
1743 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
1744 println!("-------------------------------");
1745}
1746
1747fn des_encrypt_vec_ofb()
1748{
1749 println!("des_encrypt_vec_ofb()");
1750 use std::io::Write;
1751 use std::fmt::Write as _;
1752 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
1753
1754 // Normal case
1755 let key = 0x_1234567890ABCDEF_u64;
1756 println!("K =\t{:#016X}", key);
1757 let mut a_des = DES::new_with_key_u64(key);
1758
1759 let message = "In the beginning God created the heavens and the earth.";
1760 println!("M =\t{}", message);
1761 let message = unsafe { message.to_string().as_mut_vec().clone() };
1762 let iv = 0x_FEDCBA0987654321_u64;
1763 println!("IV = {}", iv);
1764 let mut cipher = [0_u8; 55];
1765 a_des.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1766 print!("C (16 rounds) =\t");
1767 for c in cipher.clone()
1768 { print!("{:02X} ", c); }
1769 println!();
1770 let mut txt = String::new();
1771 for c in cipher.clone()
1772 { write!(txt, "{:02X} ", c); }
1773 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
1774 println!();
1775
1776 // Expanded case for 128 rounds
1777 let key = 0x_1234567890ABCDEF_u64;
1778 println!("K =\t{:#016X}", key);
1779 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
1780
1781 let message = "In the beginning God created the heavens and the earth.";
1782 println!("M =\t{}", message);
1783 let message = unsafe { message.to_string().as_mut_vec().clone() };
1784 let iv = 0x_FEDCBA0987654321_u64;
1785 println!("IV = {}", iv);
1786 let mut cipher = [0_u8; 55];
1787 a_des.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1788 print!("C (128 rounds) =\t");
1789 for c in cipher.clone()
1790 { print!("{:02X} ", c); }
1791 println!();
1792 let mut txt = String::new();
1793 for c in cipher.clone()
1794 { write!(txt, "{:02X} ", c); }
1795 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
1796 println!();
1797
1798 // Expanded case for 0 rounds which means that key is meaningless
1799 let key1 = 0x_1234567890ABCDEF_u64;
1800 let key2 = 0_u64;
1801 println!("K1 =\t{:#016X}", key1);
1802 println!("K2 =\t{:#016X}", key2);
1803 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
1804 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
1805
1806 let message = "In the beginning God created the heavens and the earth.";
1807 println!("M =\t{}", message);
1808 let message = unsafe { message.to_string().as_mut_vec().clone() };
1809 let iv = 0x_FEDCBA0987654321_u64;
1810 println!("IV = {}", iv);
1811 let mut cipher1 = [0_u8; 55];
1812 let mut cipher2 = [0_u8; 55];
1813 c_des.encrypt_vec(iv, &message, cipher1.as_mut_ptr());
1814 d_des.encrypt_vec(iv, &message, cipher2.as_mut_ptr());
1815 print!("C (0 rounds) =\t");
1816 for c in cipher1.clone()
1817 { print!("{:02X} ", c); }
1818 println!();
1819 let mut txt = String::new();
1820 for c in cipher1.clone()
1821 { write!(txt, "{:02X} ", c); }
1822 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
1823 print!("D (0 rounds) =\t");
1824 for c in cipher2.clone()
1825 { print!("{:02X} ", c); }
1826 println!();
1827 let mut txt = String::new();
1828 for c in cipher2.clone()
1829 { write!(txt, "{:02X} ", c); }
1830 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
1831 println!();
1832
1833 // Normal case for the message of 0 bytes
1834 let key = 0x_1234567890ABCDEF_u64;
1835 println!("K =\t{:#016X}", key);
1836 let mut a_des = DES::new_with_key_u64(key);
1837
1838 let message = "";
1839 println!("M =\t{}", message);
1840 let message = unsafe { message.to_string().as_mut_vec().clone() };
1841 let iv = 0x_FEDCBA0987654321_u64;
1842 println!("IV = {}", iv);
1843 let mut cipher = [0_u8; 0];
1844 a_des.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1845 print!("C =\t");
1846 for c in cipher.clone()
1847 { print!("{:02X} ", c); }
1848 println!();
1849 let mut txt = String::new();
1850 for c in cipher.clone()
1851 { write!(txt, "{:02X} ", c); }
1852 assert_eq!(txt, "");
1853 println!();
1854
1855 // Normal case for the message shorter than 8 bytes
1856 let key = 0x_1234567890ABCDEF_u64;
1857 println!("K =\t{:#016X}", key);
1858 let mut a_des = DES::new_with_key_u64(key);
1859
1860 let message = "7 bytes";
1861 println!("M =\t{}", message);
1862 let message = unsafe { message.to_string().as_mut_vec().clone() };
1863 let iv = 0x_FEDCBA0987654321_u64;
1864 println!("IV = {}", iv);
1865 let mut cipher = [0_u8; 7];
1866 a_des.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1867 print!("C =\t");
1868 for c in cipher.clone()
1869 { print!("{:02X} ", c); }
1870 println!();
1871 let mut txt = String::new();
1872 for c in cipher.clone()
1873 { write!(txt, "{:02X} ", c); }
1874 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
1875 println!();
1876
1877 // Normal case for the message of 8 bytes
1878 let key = 0x_1234567890ABCDEF_u64;
1879 println!("K =\t{:#016X}", key);
1880 let mut a_des = DES::new_with_key_u64(key);
1881
1882 let message = "I am OK.";
1883 println!("M =\t{}", message);
1884 let message = unsafe { message.to_string().as_mut_vec().clone() };
1885 let iv = 0x_FEDCBA0987654321_u64;
1886 println!("IV = {}", iv);
1887 let mut cipher = [0_u8; 8];
1888 a_des.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1889 print!("C =\t");
1890 for c in cipher.clone()
1891 { print!("{:02X} ", c); }
1892 println!();
1893 let mut txt = String::new();
1894 for c in cipher.clone()
1895 { write!(txt, "{:02X} ", c); }
1896 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
1897 println!();
1898
1899 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
1900 let key = 0x_1234567890ABCDEF_u64;
1901 println!("K =\t{:#016X}", key);
1902 let mut a_des = DES::new_with_key_u64(key);
1903
1904 let message = "PARK Youngho";
1905 println!("M =\t{}", message);
1906 let message = unsafe { message.to_string().as_mut_vec().clone() };
1907 let iv = 0x_FEDCBA0987654321_u64;
1908 println!("IV = {}", iv);
1909 let mut cipher = [0_u8; 12];
1910 a_des.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1911 print!("C =\t");
1912 for c in cipher.clone()
1913 { print!("{:02X} ", c); }
1914 println!();
1915 let mut txt = String::new();
1916 for c in cipher.clone()
1917 { write!(txt, "{:02X} ", c); }
1918 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
1919 println!();
1920
1921 // Normal case for the message of 16 bytes
1922 let key = 0x_1234567890ABCDEF_u64;
1923 println!("K =\t{:#016X}", key);
1924 let mut a_des = DES::new_with_key_u64(key);
1925
1926 let message = "고맙습니다.";
1927 println!("M =\t{}", message);
1928 let message = unsafe { message.to_string().as_mut_vec().clone() };
1929 let iv = 0x_FEDCBA0987654321_u64;
1930 println!("IV = {}", iv);
1931 let mut cipher = [0_u8; 16];
1932 a_des.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1933 print!("C =\t");
1934 for c in cipher.clone()
1935 { print!("{:02X} ", c); }
1936 println!();
1937 let mut txt = String::new();
1938 for c in cipher.clone()
1939 { write!(txt, "{:02X} ", c); }
1940 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
1941 println!("-------------------------------");
1942}
1943
1944fn des_encrypt_vec_ofb_into_vec()
1945{
1946 println!("des_encrypt_vec_ofb_into_vec()");
1947 use std::io::Write;
1948 use std::fmt::Write as _;
1949 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
1950
1951 // Normal case
1952 let key = 0x_1234567890ABCDEF_u64;
1953 println!("K =\t{:#016X}", key);
1954 let mut a_des = DES::new_with_key_u64(key);
1955
1956 let message = "In the beginning God created the heavens and the earth.";
1957 println!("M =\t{}", message);
1958 let message = unsafe { message.to_string().as_mut_vec().clone() };
1959 let iv = 0x_FEDCBA0987654321_u64;
1960 println!("IV = {}", iv);
1961 let mut cipher = Vec::<u8>::new();
1962 a_des.encrypt_vec_into_vec(iv, &message, &mut cipher);
1963 print!("C (16 rounds) =\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, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
1971 println!();
1972
1973 // Expanded case for 128 rounds
1974 let key = 0x_1234567890ABCDEF_u64;
1975 println!("K =\t{:#016X}", key);
1976 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
1977
1978 let message = "In the beginning God created the heavens and the earth.";
1979 println!("M =\t{}", message);
1980 let message = unsafe { message.to_string().as_mut_vec().clone() };
1981 let iv = 0x_FEDCBA0987654321_u64;
1982 println!("IV = {}", iv);
1983 let mut cipher = Vec::<u8>::new();
1984 a_des.encrypt_vec_into_vec(iv, &message, &mut cipher);
1985 print!("C (128 rounds) =\t");
1986 for c in cipher.clone()
1987 { print!("{:02X} ", c); }
1988 println!();
1989 let mut txt = String::new();
1990 for c in cipher.clone()
1991 { write!(txt, "{:02X} ", c); }
1992 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
1993 println!();
1994
1995 // Expanded case for 0 rounds which means that key is meaningless
1996 let key1 = 0x_1234567890ABCDEF_u64;
1997 let key2 = 0_u64;
1998 println!("K1 =\t{:#016X}", key1);
1999 println!("K2 =\t{:#016X}", key2);
2000 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
2001 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
2002
2003 let message = "In the beginning God created the heavens and the earth.";
2004 println!("M =\t{}", message);
2005 let message = unsafe { message.to_string().as_mut_vec().clone() };
2006
2007 let iv = 0x_FEDCBA0987654321_u64;
2008 println!("IV = {}", iv);
2009 let mut cipher1 = Vec::<u8>::new();
2010 let mut cipher2 = Vec::<u8>::new();
2011 c_des.encrypt_vec_into_vec(iv, &message, &mut cipher1);
2012 d_des.encrypt_vec_into_vec(iv, &message, &mut cipher2);
2013 print!("C (0 rounds) =\t");
2014 for c in cipher1.clone()
2015 { print!("{:02X} ", c); }
2016 println!();
2017 let mut txt = String::new();
2018 for c in cipher1.clone()
2019 { write!(txt, "{:02X} ", c); }
2020 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
2021 print!("D (0 rounds) =\t");
2022 for c in cipher2.clone()
2023 { print!("{:02X} ", c); }
2024 println!();
2025 let mut txt = String::new();
2026 for c in cipher2.clone()
2027 { write!(txt, "{:02X} ", c); }
2028 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
2029 println!();
2030
2031 // Normal case for the message of 0 bytes
2032 let key = 0x_1234567890ABCDEF_u64;
2033 println!("K =\t{:#016X}", key);
2034 let mut a_des = DES::new_with_key_u64(key);
2035
2036 let message = "";
2037 println!("M =\t{}", message);
2038 let message = unsafe { message.to_string().as_mut_vec().clone() };
2039 let iv = 0x_FEDCBA0987654321_u64;
2040 println!("IV = {}", iv);
2041 let mut cipher = Vec::<u8>::new();
2042 a_des.encrypt_vec_into_vec(iv, &message, &mut cipher);
2043 print!("C =\t");
2044 for c in cipher.clone()
2045 { print!("{:02X} ", c); }
2046 println!();
2047 let mut txt = String::new();
2048 for c in cipher.clone()
2049 { write!(txt, "{:02X} ", c); }
2050 assert_eq!(txt, "");
2051 println!();
2052
2053 // Normal case for the message shorter than 8 bytes
2054 let key = 0x_1234567890ABCDEF_u64;
2055 println!("K =\t{:#016X}", key);
2056 let mut a_des = DES::new_with_key_u64(key);
2057
2058 let message = "7 bytes";
2059 println!("M =\t{}", message);
2060 let message = unsafe { message.to_string().as_mut_vec().clone() };
2061 let iv = 0x_FEDCBA0987654321_u64;
2062 println!("IV = {}", iv);
2063 let mut cipher = Vec::<u8>::new();
2064 a_des.encrypt_vec_into_vec(iv, &message, &mut cipher);
2065 print!("C =\t");
2066 for c in cipher.clone()
2067 { print!("{:02X} ", c); }
2068 println!();
2069 let mut txt = String::new();
2070 for c in cipher.clone()
2071 { write!(txt, "{:02X} ", c); }
2072 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
2073 println!();
2074
2075 // Normal case for the message of 8 bytes
2076 let key = 0x_1234567890ABCDEF_u64;
2077 println!("K =\t{:#016X}", key);
2078 let mut a_des = DES::new_with_key_u64(key);
2079
2080 let message = "I am OK.";
2081 println!("M =\t{}", message);
2082 let message = unsafe { message.to_string().as_mut_vec().clone() };
2083 let iv = 0x_FEDCBA0987654321_u64;
2084 println!("IV = {}", iv);
2085 let mut cipher = Vec::<u8>::new();
2086 a_des.encrypt_vec_into_vec(iv, &message, &mut cipher);
2087 print!("C =\t");
2088 for c in cipher.clone()
2089 { print!("{:02X} ", c); }
2090 println!();
2091 let mut txt = String::new();
2092 for c in cipher.clone()
2093 { write!(txt, "{:02X} ", c); }
2094 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
2095 println!();
2096
2097 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
2098 let key = 0x_1234567890ABCDEF_u64;
2099 println!("K =\t{:#016X}", key);
2100 let mut a_des = DES::new_with_key_u64(key);
2101
2102 let message = "PARK Youngho";
2103 println!("M =\t{}", message);
2104 let message = unsafe { message.to_string().as_mut_vec().clone() };
2105 let iv = 0x_FEDCBA0987654321_u64;
2106 println!("IV = {}", iv);
2107 let mut cipher = Vec::<u8>::new();
2108 a_des.encrypt_vec_into_vec(iv, &message, &mut cipher);
2109 print!("C =\t");
2110 for c in cipher.clone()
2111 { print!("{:02X} ", c); }
2112 println!();
2113 let mut txt = String::new();
2114 for c in cipher.clone()
2115 { write!(txt, "{:02X} ", c); }
2116 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
2117 println!();
2118
2119 // Normal case for the message of 16 bytes
2120 let key = 0x_1234567890ABCDEF_u64;
2121 println!("K =\t{:#016X}", key);
2122 let mut a_des = DES::new_with_key_u64(key);
2123
2124 let message = "고맙습니다.";
2125 println!("M =\t{}", message);
2126 let message = unsafe { message.to_string().as_mut_vec().clone() };
2127 let iv = 0x_FEDCBA0987654321_u64;
2128 println!("IV = {}", iv);
2129 let mut cipher = Vec::<u8>::new();
2130 a_des.encrypt_vec_into_vec(iv, &message, &mut cipher);
2131 print!("C =\t");
2132 for c in cipher.clone()
2133 { print!("{:02X} ", c); }
2134 println!();
2135 let mut txt = String::new();
2136 for c in cipher.clone()
2137 { write!(txt, "{:02X} ", c); }
2138 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
2139 println!("-------------------------------");
2140}
2141
2142fn des_encrypt_vec_ofb_into_array()
2143{
2144 println!("des_encrypt_vec_ofb_into_array()");
2145 use std::io::Write;
2146 use std::fmt::Write as _;
2147 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
2148
2149 // Normal case
2150 let key = 0x_1234567890ABCDEF_u64;
2151 println!("K =\t{:#016X}", key);
2152 let mut a_des = DES::new_with_key_u64(key);
2153
2154 let message = "In the beginning God created the heavens and the earth.";
2155 println!("M =\t{}", message);
2156 let message = unsafe { message.to_string().as_mut_vec().clone() };
2157 let iv = 0x_FEDCBA0987654321_u64;
2158 println!("IV = {}", iv);
2159 let mut cipher = [0_u8; 55];
2160 a_des.encrypt_vec_into_array(iv, &message, &mut cipher);
2161 print!("C (16 rounds) =\t");
2162 for c in cipher.clone()
2163 { print!("{:02X} ", c); }
2164 println!();
2165 let mut txt = String::new();
2166 for c in cipher.clone()
2167 { write!(txt, "{:02X} ", c); }
2168 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
2169 println!();
2170
2171 // Expanded case for 128 rounds
2172 let key = 0x_1234567890ABCDEF_u64;
2173 println!("K =\t{:#016X}", key);
2174 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
2175
2176 let message = "In the beginning God created the heavens and the earth.";
2177 println!("M =\t{}", message);
2178 let message = unsafe { message.to_string().as_mut_vec().clone() };
2179 let iv = 0x_FEDCBA0987654321_u64;
2180 println!("IV = {}", iv);
2181 let mut cipher = [0_u8; 55];
2182 a_des.encrypt_vec_into_array(iv, &message, &mut cipher);
2183 print!("C (128 rounds) =\t");
2184 for c in cipher.clone()
2185 { print!("{:02X} ", c); }
2186 println!();
2187 let mut txt = String::new();
2188 for c in cipher.clone()
2189 { write!(txt, "{:02X} ", c); }
2190 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
2191 println!();
2192
2193 // Expanded case for 0 rounds which means that key is meaningless
2194 let key1 = 0x_1234567890ABCDEF_u64;
2195 let key2 = 0_u64;
2196 println!("K1 =\t{:#016X}", key1);
2197 println!("K2 =\t{:#016X}", key2);
2198 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
2199 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
2200
2201 let message = "In the beginning God created the heavens and the earth.";
2202 println!("M =\t{}", message);
2203 let message = unsafe { message.to_string().as_mut_vec().clone() };
2204 let iv = 0x_FEDCBA0987654321_u64;
2205 println!("IV = {}", iv);
2206 let mut cipher1 = [0_u8; 55];
2207 let mut cipher2 = [0_u8; 55];
2208 c_des.encrypt_vec_into_array(iv, &message, &mut cipher1);
2209 d_des.encrypt_vec_into_array(iv, &message, &mut cipher2);
2210 print!("C (0 rounds) =\t");
2211 for c in cipher1.clone()
2212 { print!("{:02X} ", c); }
2213 println!();
2214 let mut txt = String::new();
2215 for c in cipher1.clone()
2216 { write!(txt, "{:02X} ", c); }
2217 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
2218 print!("D (0 rounds) =\t");
2219 for c in cipher2.clone()
2220 { print!("{:02X} ", c); }
2221 println!();
2222 let mut txt = String::new();
2223 for c in cipher2.clone()
2224 { write!(txt, "{:02X} ", c); }
2225 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
2226 println!();
2227
2228 // Normal case for the message of 0 bytes
2229 let key = 0x_1234567890ABCDEF_u64;
2230 println!("K =\t{:#016X}", key);
2231 let mut a_des = DES::new_with_key_u64(key);
2232
2233 let message = "";
2234 println!("M =\t{}", message);
2235 let message = unsafe { message.to_string().as_mut_vec().clone() };
2236 let iv = 0x_FEDCBA0987654321_u64;
2237 println!("IV = {}", iv);
2238 let mut cipher = [0_u8; 0];
2239 a_des.encrypt_vec_into_array(iv, &message, &mut cipher);
2240 print!("C =\t");
2241 for c in cipher.clone()
2242 { print!("{:02X} ", c); }
2243 println!();
2244 let mut txt = String::new();
2245 for c in cipher.clone()
2246 { write!(txt, "{:02X} ", c); }
2247 assert_eq!(txt, "");
2248 println!();
2249
2250 // Normal case for the message shorter than 8 bytes
2251 let key = 0x_1234567890ABCDEF_u64;
2252 println!("K =\t{:#016X}", key);
2253 let mut a_des = DES::new_with_key_u64(key);
2254
2255 let message = "7 bytes";
2256 println!("M =\t{}", message);
2257 let message = unsafe { message.to_string().as_mut_vec().clone() };
2258 let iv = 0x_FEDCBA0987654321_u64;
2259 println!("IV = {}", iv);
2260 let mut cipher = [0_u8; 7];
2261 a_des.encrypt_vec_into_array(iv, &message, &mut cipher);
2262 print!("C =\t");
2263 for c in cipher.clone()
2264 { print!("{:02X} ", c); }
2265 println!();
2266 let mut txt = String::new();
2267 for c in cipher.clone()
2268 { write!(txt, "{:02X} ", c); }
2269 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
2270 println!();
2271
2272 // Normal case for the message of 8 bytes
2273 let key = 0x_1234567890ABCDEF_u64;
2274 println!("K =\t{:#016X}", key);
2275 let mut a_des = DES::new_with_key_u64(key);
2276
2277 let message = "I am OK.";
2278 println!("M =\t{}", message);
2279 let message = unsafe { message.to_string().as_mut_vec().clone() };
2280 let iv = 0x_FEDCBA0987654321_u64;
2281 println!("IV = {}", iv);
2282 let mut cipher = [0_u8; 8];
2283 a_des.encrypt_vec_into_array(iv, &message, &mut cipher);
2284 print!("C =\t");
2285 for c in cipher.clone()
2286 { print!("{:02X} ", c); }
2287 println!();
2288 let mut txt = String::new();
2289 for c in cipher.clone()
2290 { write!(txt, "{:02X} ", c); }
2291 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
2292 println!();
2293
2294 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
2295 let key = 0x_1234567890ABCDEF_u64;
2296 println!("K =\t{:#016X}", key);
2297 let mut a_des = DES::new_with_key_u64(key);
2298
2299 let message = "PARK Youngho";
2300 println!("M =\t{}", message);
2301 let message = unsafe { message.to_string().as_mut_vec().clone() };
2302 let iv = 0x_FEDCBA0987654321_u64;
2303 println!("IV = {}", iv);
2304 let mut cipher = [0_u8; 12];
2305 a_des.encrypt_vec_into_array(iv, &message, &mut cipher);
2306 print!("C =\t");
2307 for c in cipher.clone()
2308 { print!("{:02X} ", c); }
2309 println!();
2310 let mut txt = String::new();
2311 for c in cipher.clone()
2312 { write!(txt, "{:02X} ", c); }
2313 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
2314 println!();
2315
2316 // Normal case for the message of 16 bytes
2317 let key = 0x_1234567890ABCDEF_u64;
2318 println!("K =\t{:#016X}", key);
2319 let mut a_des = DES::new_with_key_u64(key);
2320
2321 let message = "고맙습니다.";
2322 println!("M =\t{}", message);
2323 let message = unsafe { message.to_string().as_mut_vec().clone() };
2324 let iv = 0x_FEDCBA0987654321_u64;
2325 println!("IV = {}", iv);
2326 let mut cipher = [0_u8; 16];
2327 a_des.encrypt_vec_into_array(iv, &message, &mut cipher);
2328 print!("C =\t");
2329 for c in cipher.clone()
2330 { print!("{:02X} ", c); }
2331 println!();
2332 let mut txt = String::new();
2333 for c in cipher.clone()
2334 { write!(txt, "{:02X} ", c); }
2335 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
2336 println!("-------------------------------");
2337}
2338
2339fn des_encrypt_array_ofb()
2340{
2341 println!("des_encrypt_array_ofb()");
2342 use std::io::Write;
2343 use std::fmt::Write as _;
2344 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
2345
2346 // Normal case
2347 let key = 0x_1234567890ABCDEF_u64;
2348 println!("K =\t{:#016X}", key);
2349 let mut a_des = DES::new_with_key_u64(key);
2350
2351 let mes = "In the beginning God created the heavens and the earth.";
2352 println!("M =\t{}", mes);
2353 let mut message = [0_u8; 55];
2354 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2355 let iv = 0x_FEDCBA0987654321_u64;
2356 println!("IV = {}", iv);
2357 let mut cipher = [0_u8; 55];
2358 a_des.encrypt_array(iv, &message, cipher.as_mut_ptr());
2359 print!("C (16 rounds) =\t");
2360 for c in cipher.clone()
2361 { print!("{:02X} ", c); }
2362 println!();
2363 let mut txt = String::new();
2364 for c in cipher.clone()
2365 { write!(txt, "{:02X} ", c); }
2366 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
2367 println!();
2368
2369 // Expanded case for 128 rounds
2370 let key = 0x_1234567890ABCDEF_u64;
2371 println!("K =\t{:#016X}", key);
2372 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
2373
2374 let mes = "In the beginning God created the heavens and the earth.";
2375 println!("M =\t{}", mes);
2376 let mut message = [0_u8; 55];
2377 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2378 let iv = 0x_FEDCBA0987654321_u64;
2379 println!("IV = {}", iv);
2380 let mut cipher = [0_u8; 55];
2381 a_des.encrypt_array(iv, &message, cipher.as_mut_ptr());
2382 print!("C (128 rounds) =\t");
2383 for c in cipher.clone()
2384 { print!("{:02X} ", c); }
2385 println!();
2386 let mut txt = String::new();
2387 for c in cipher.clone()
2388 { write!(txt, "{:02X} ", c); }
2389 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
2390 println!();
2391
2392 // Expanded case for 0 rounds which means that key is meaningless
2393 let key1 = 0x_1234567890ABCDEF_u64;
2394 let key2 = 0_u64;
2395 println!("K1 =\t{:#016X}", key1);
2396 println!("K2 =\t{:#016X}", key2);
2397 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
2398 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
2399
2400 let mes = "In the beginning God created the heavens and the earth.";
2401 println!("M =\t{}", mes);
2402 let mut message = [0_u8; 55];
2403 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2404 let iv = 0x_FEDCBA0987654321_u64;
2405 println!("IV = {}", iv);
2406 let mut cipher1 = [0_u8; 55];
2407 let mut cipher2 = [0_u8; 55];
2408 c_des.encrypt_array(iv, &message, cipher1.as_mut_ptr());
2409 d_des.encrypt_array(iv, &message, cipher2.as_mut_ptr());
2410 print!("C (0 rounds) =\t");
2411 for c in cipher1.clone()
2412 { print!("{:02X} ", c); }
2413 println!();
2414 let mut txt = String::new();
2415 for c in cipher1.clone()
2416 { write!(txt, "{:02X} ", c); }
2417 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
2418 print!("D (0 rounds) =\t");
2419 for c in cipher2.clone()
2420 { print!("{:02X} ", c); }
2421 println!();
2422 let mut txt = String::new();
2423 for c in cipher2.clone()
2424 { write!(txt, "{:02X} ", c); }
2425 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
2426 println!();
2427
2428 // Normal case for the message of 0 bytes
2429 let key = 0x_1234567890ABCDEF_u64;
2430 println!("K =\t{:#016X}", key);
2431 let mut a_des = DES::new_with_key_u64(key);
2432
2433 let mes = "";
2434 println!("M =\t{}", mes);
2435 let mut message = [0_u8; 0];
2436 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2437 let iv = 0x_FEDCBA0987654321_u64;
2438 println!("IV = {}", iv);
2439 let mut cipher = [0_u8; 0];
2440 a_des.encrypt_array(iv, &message, cipher.as_mut_ptr());
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, "");
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 iv = 0x_FEDCBA0987654321_u64;
2461 println!("IV = {}", iv);
2462 let mut cipher = [0_u8; 7];
2463 a_des.encrypt_array(iv, &message, cipher.as_mut_ptr());
2464 print!("C =\t");
2465 for c in cipher.clone()
2466 { print!("{:02X} ", c); }
2467 println!();
2468 let mut txt = String::new();
2469 for c in cipher.clone()
2470 { write!(txt, "{:02X} ", c); }
2471 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
2472 println!();
2473
2474 // Normal case for the message of 8 bytes
2475 let key = 0x_1234567890ABCDEF_u64;
2476 println!("K =\t{:#016X}", key);
2477 let mut a_des = DES::new_with_key_u64(key);
2478
2479 let mes = "I am OK.";
2480 println!("M =\t{}", mes);
2481 let mut message = [0_u8; 8];
2482 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2483 let iv = 0x_FEDCBA0987654321_u64;
2484 println!("IV = {}", iv);
2485 let mut cipher = [0_u8; 8];
2486 a_des.encrypt_array(iv, &message, cipher.as_mut_ptr());
2487 print!("C =\t");
2488 for c in cipher.clone()
2489 { print!("{:02X} ", c); }
2490 println!();
2491 let mut txt = String::new();
2492 for c in cipher.clone()
2493 { write!(txt, "{:02X} ", c); }
2494 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
2495 println!();
2496
2497 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
2498 let key = 0x_1234567890ABCDEF_u64;
2499 println!("K =\t{:#016X}", key);
2500 let mut a_des = DES::new_with_key_u64(key);
2501
2502 let mes = "PARK Youngho";
2503 println!("M =\t{}", mes);
2504 let mut message = [0_u8; 12];
2505 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2506 let iv = 0x_FEDCBA0987654321_u64;
2507 println!("IV = {}", iv);
2508 let mut cipher = [0_u8; 12];
2509 a_des.encrypt_array(iv, &message, cipher.as_mut_ptr());
2510 print!("C =\t");
2511 for c in cipher.clone()
2512 { print!("{:02X} ", c); }
2513 println!();
2514 let mut txt = String::new();
2515 for c in cipher.clone()
2516 { write!(txt, "{:02X} ", c); }
2517 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
2518 println!();
2519
2520 // Normal case for the message of 16 bytes
2521 let key = 0x_1234567890ABCDEF_u64;
2522 println!("K =\t{:#016X}", key);
2523 let mut a_des = DES::new_with_key_u64(key);
2524
2525 let mes = "고맙습니다.";
2526 println!("M =\t{}", mes);
2527 let mut message = [0_u8; 16];
2528 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2529 let iv = 0x_FEDCBA0987654321_u64;
2530 println!("IV = {}", iv);
2531 let mut cipher = [0_u8; 16];
2532 a_des.encrypt_array(iv, &message, cipher.as_mut_ptr());
2533 print!("C =\t");
2534 for c in cipher.clone()
2535 { print!("{:02X} ", c); }
2536 println!();
2537 let mut txt = String::new();
2538 for c in cipher.clone()
2539 { write!(txt, "{:02X} ", c); }
2540 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
2541 println!("-------------------------------");
2542}
2543
2544fn des_encrypt_array_ofb_into_vec()
2545{
2546 println!("des_encrypt_array_ofb_into_vec()");
2547 use std::io::Write;
2548 use std::fmt::Write as _;
2549 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
2550
2551 // Normal case
2552 let key = 0x_1234567890ABCDEF_u64;
2553 println!("K =\t{:#016X}", key);
2554 let mut a_des = DES::new_with_key_u64(key);
2555
2556 let mes = "In the beginning God created the heavens and the earth.";
2557 println!("M =\t{}", mes);
2558 let mut message = [0_u8; 55];
2559 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2560 let iv = 0x_FEDCBA0987654321_u64;
2561 println!("IV = {}", iv);
2562 let mut cipher = Vec::<u8>::new();
2563 a_des.encrypt_array_into_vec(iv, &message, &mut cipher);
2564 print!("C (16 rounds) =\t");
2565 for c in cipher.clone()
2566 { print!("{:02X} ", c); }
2567 println!();
2568 let mut txt = String::new();
2569 for c in cipher.clone()
2570 { write!(txt, "{:02X} ", c); }
2571 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
2572 println!();
2573
2574 // Expanded case for 128 rounds
2575 let key = 0x_1234567890ABCDEF_u64;
2576 println!("K =\t{:#016X}", key);
2577 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
2578
2579 let mes = "In the beginning God created the heavens and the earth.";
2580 println!("M =\t{}", mes);
2581 let mut message = [0_u8; 55];
2582 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2583 let iv = 0x_FEDCBA0987654321_u64;
2584 println!("IV = {}", iv);
2585 let mut cipher = Vec::<u8>::new();
2586 a_des.encrypt_array_into_vec(iv, &message, &mut cipher);
2587 print!("C (128 rounds) =\t");
2588 for c in cipher.clone()
2589 { print!("{:02X} ", c); }
2590 println!();
2591 let mut txt = String::new();
2592 for c in cipher.clone()
2593 { write!(txt, "{:02X} ", c); }
2594 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
2595 println!();
2596
2597 // Expanded case for 0 rounds which means that key is meaningless
2598 let key1 = 0x_1234567890ABCDEF_u64;
2599 let key2 = 0_u64;
2600 println!("K1 =\t{:#016X}", key1);
2601 println!("K2 =\t{:#016X}", key2);
2602 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
2603 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
2604
2605 let mes = "In the beginning God created the heavens and the earth.";
2606 println!("M =\t{}", mes);
2607 let mut message = [0_u8; 55];
2608 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2609
2610 let iv = 0x_FEDCBA0987654321_u64;
2611 println!("IV = {}", iv);
2612 let mut cipher1 = Vec::<u8>::new();
2613 let mut cipher2 = Vec::<u8>::new();
2614 c_des.encrypt_array_into_vec(iv, &message, &mut cipher1);
2615 d_des.encrypt_array_into_vec(iv, &message, &mut cipher2);
2616 print!("C (0 rounds) =\t");
2617 for c in cipher1.clone()
2618 { print!("{:02X} ", c); }
2619 println!();
2620 let mut txt = String::new();
2621 for c in cipher1.clone()
2622 { write!(txt, "{:02X} ", c); }
2623 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
2624 print!("D (0 rounds) =\t");
2625 for c in cipher2.clone()
2626 { print!("{:02X} ", c); }
2627 println!();
2628 let mut txt = String::new();
2629 for c in cipher2.clone()
2630 { write!(txt, "{:02X} ", c); }
2631 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
2632 println!();
2633
2634 // Normal case for the message of 0 bytes
2635 let key = 0x_1234567890ABCDEF_u64;
2636 println!("K =\t{:#016X}", key);
2637 let mut a_des = DES::new_with_key_u64(key);
2638
2639 let mes = "";
2640 println!("M =\t{}", mes);
2641 let mut message = [0_u8; 0];
2642 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2643 let iv = 0x_FEDCBA0987654321_u64;
2644 println!("IV = {}", iv);
2645 let mut cipher = Vec::<u8>::new();
2646 a_des.encrypt_array_into_vec(iv, &message, &mut cipher);
2647 print!("C =\t");
2648 for c in cipher.clone()
2649 { print!("{:02X} ", c); }
2650 println!();
2651 let mut txt = String::new();
2652 for c in cipher.clone()
2653 { write!(txt, "{:02X} ", c); }
2654 assert_eq!(txt, "");
2655 println!();
2656
2657 // Normal case for the message shorter than 8 bytes
2658 let key = 0x_1234567890ABCDEF_u64;
2659 println!("K =\t{:#016X}", key);
2660 let mut a_des = DES::new_with_key_u64(key);
2661
2662 let mes = "7 bytes";
2663 println!("M =\t{}", mes);
2664 let mut message = [0_u8; 7];
2665 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2666 let iv = 0x_FEDCBA0987654321_u64;
2667 println!("IV = {}", iv);
2668 let mut cipher = Vec::<u8>::new();
2669 a_des.encrypt_array_into_vec(iv, &message, &mut cipher);
2670 print!("C =\t");
2671 for c in cipher.clone()
2672 { print!("{:02X} ", c); }
2673 println!();
2674 let mut txt = String::new();
2675 for c in cipher.clone()
2676 { write!(txt, "{:02X} ", c); }
2677 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
2678 println!();
2679
2680 // Normal case for the message of 8 bytes
2681 let key = 0x_1234567890ABCDEF_u64;
2682 println!("K =\t{:#016X}", key);
2683 let mut a_des = DES::new_with_key_u64(key);
2684
2685 let mes = "I am OK.";
2686 println!("M =\t{}", mes);
2687 let mut message = [0_u8; 8];
2688 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2689 let iv = 0x_FEDCBA0987654321_u64;
2690 println!("IV = {}", iv);
2691 let mut cipher = Vec::<u8>::new();
2692 a_des.encrypt_array_into_vec(iv, &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, "2E 50 A0 48 B5 99 DB 07 ");
2701 println!();
2702
2703 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
2704 let key = 0x_1234567890ABCDEF_u64;
2705 println!("K =\t{:#016X}", key);
2706 let mut a_des = DES::new_with_key_u64(key);
2707
2708 let mes = "PARK Youngho";
2709 println!("M =\t{}", mes);
2710 let mut message = [0_u8; 12];
2711 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2712 let iv = 0x_FEDCBA0987654321_u64;
2713 println!("IV = {}", iv);
2714 let mut cipher = Vec::<u8>::new();
2715 a_des.encrypt_array_into_vec(iv, &message, &mut cipher);
2716 print!("C =\t");
2717 for c in cipher.clone()
2718 { print!("{:02X} ", c); }
2719 println!();
2720 let mut txt = String::new();
2721 for c in cipher.clone()
2722 { write!(txt, "{:02X} ", c); }
2723 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
2724 println!();
2725
2726 // Normal case for the message of 16 bytes
2727 let key = 0x_1234567890ABCDEF_u64;
2728 println!("K =\t{:#016X}", key);
2729 let mut a_des = DES::new_with_key_u64(key);
2730
2731 let mes = "고맙습니다.";
2732 println!("M =\t{}", mes);
2733 let mut message = [0_u8; 16];
2734 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2735 let iv = 0x_FEDCBA0987654321_u64;
2736 println!("IV = {}", iv);
2737 let mut cipher = Vec::<u8>::new();
2738 a_des.encrypt_array_into_vec(iv, &message, &mut cipher);
2739 print!("C =\t");
2740 for c in cipher.clone()
2741 { print!("{:02X} ", c); }
2742 println!();
2743 let mut txt = String::new();
2744 for c in cipher.clone()
2745 { write!(txt, "{:02X} ", c); }
2746 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
2747 println!("-------------------------------");
2748}
2749
2750fn des_encrypt_array_ofb_into_array()
2751{
2752 println!("des_encrypt_array_ofb_into_array()");
2753 use std::io::Write;
2754 use std::fmt::Write as _;
2755 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
2756
2757 // Normal case
2758 let key = 0x_1234567890ABCDEF_u64;
2759 println!("K =\t{:#016X}", key);
2760 let mut a_des = DES::new_with_key_u64(key);
2761
2762 let mes = "In the beginning God created the heavens and the earth.";
2763 println!("M =\t{}", mes);
2764 let mut message = [0_u8; 55];
2765 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2766 let iv = 0x_FEDCBA0987654321_u64;
2767 println!("IV = {}", iv);
2768 let mut cipher = [0_u8; 55];
2769 a_des.encrypt_array_into_array(iv, &message, &mut cipher);
2770 for c in cipher.clone()
2771 { print!("{:02X} ", c); }
2772 println!();
2773 let mut txt = String::new();
2774 for c in cipher.clone()
2775 { write!(txt, "{:02X} ", c); }
2776 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
2777 println!();
2778
2779 // Expanded case for 128 rounds
2780 let key = 0x_1234567890ABCDEF_u64;
2781 println!("K =\t{:#016X}", key);
2782 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
2783
2784 let mes = "In the beginning God created the heavens and the earth.";
2785 println!("M =\t{}", mes);
2786 let mut message = [0_u8; 55];
2787 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2788 let iv = 0x_FEDCBA0987654321_u64;
2789 println!("IV = {}", iv);
2790 let mut cipher = [0_u8; 55];
2791 a_des.encrypt_array_into_array(iv, &message, &mut cipher);
2792 print!("C (128 rounds) =\t");
2793 for c in cipher.clone()
2794 { print!("{:02X} ", c); }
2795 println!();
2796 let mut txt = String::new();
2797 for c in cipher.clone()
2798 { write!(txt, "{:02X} ", c); }
2799 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
2800 println!();
2801
2802 // Expanded case for 0 rounds which means that key is meaningless
2803 let key1 = 0x_1234567890ABCDEF_u64;
2804 let key2 = 0_u64;
2805 println!("K1 =\t{:#016X}", key1);
2806 println!("K2 =\t{:#016X}", key2);
2807 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
2808 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
2809
2810 let mes = "In the beginning God created the heavens and the earth.";
2811 println!("M =\t{}", mes);
2812 let mut message = [0_u8; 55];
2813 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2814 let iv = 0x_FEDCBA0987654321_u64;
2815 println!("IV = {}", iv);
2816 let mut cipher1 = [0_u8; 55];
2817 let mut cipher2 = [0_u8; 55];
2818 c_des.encrypt_array_into_array(iv, &message, &mut cipher1);
2819 d_des.encrypt_array_into_array(iv, &message, &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, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
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, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
2836 println!();
2837
2838 // Normal case for the message of 0 bytes
2839 let key = 0x_1234567890ABCDEF_u64;
2840 println!("K =\t{:#016X}", key);
2841 let mut a_des = DES::new_with_key_u64(key);
2842
2843 let mes = "";
2844 println!("M =\t{}", mes);
2845 let mut message = [0_u8; 0];
2846 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2847 let iv = 0x_FEDCBA0987654321_u64;
2848 println!("IV = {}", iv);
2849 let mut cipher = [0_u8; 0];
2850 a_des.encrypt_array_into_array(iv, &message, &mut cipher);
2851 print!("C =\t");
2852 for c in cipher.clone()
2853 { print!("{:02X} ", c); }
2854 println!();
2855 let mut txt = String::new();
2856 for c in cipher.clone()
2857 { write!(txt, "{:02X} ", c); }
2858 assert_eq!(txt, "");
2859 println!();
2860
2861 // Normal case for the message shorter than 8 bytes
2862 let key = 0x_1234567890ABCDEF_u64;
2863 println!("K =\t{:#016X}", key);
2864 let mut a_des = DES::new_with_key_u64(key);
2865
2866 let mes = "7 bytes";
2867 println!("M =\t{}", mes);
2868 let mut message = [0_u8; 7];
2869 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2870 let iv = 0x_FEDCBA0987654321_u64;
2871 println!("IV = {}", iv);
2872 let mut cipher = [0_u8; 7];
2873 a_des.encrypt_array_into_array(iv, &message, &mut cipher);
2874 print!("C =\t");
2875 for c in cipher.clone()
2876 { print!("{:02X} ", c); }
2877 println!();
2878 let mut txt = String::new();
2879 for c in cipher.clone()
2880 { write!(txt, "{:02X} ", c); }
2881 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
2882 println!();
2883
2884 // Normal case for the message of 8 bytes
2885 let key = 0x_1234567890ABCDEF_u64;
2886 println!("K =\t{:#016X}", key);
2887 let mut a_des = DES::new_with_key_u64(key);
2888
2889 let mes = "I am OK.";
2890 println!("M =\t{}", mes);
2891 let mut message = [0_u8; 8];
2892 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2893 let iv = 0x_FEDCBA0987654321_u64;
2894 println!("IV = {}", iv);
2895 let mut cipher = [0_u8; 8];
2896 a_des.encrypt_array_into_array(iv, &message, &mut cipher);
2897 print!("C =\t");
2898 for c in cipher.clone()
2899 { print!("{:02X} ", c); }
2900 println!();
2901 let mut txt = String::new();
2902 for c in cipher.clone()
2903 { write!(txt, "{:02X} ", c); }
2904 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
2905 println!();
2906
2907 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
2908 let key = 0x_1234567890ABCDEF_u64;
2909 println!("K =\t{:#016X}", key);
2910 let mut a_des = DES::new_with_key_u64(key);
2911
2912 let mes = "PARK Youngho";
2913 println!("M =\t{}", mes);
2914 let mut message = [0_u8; 12];
2915 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2916 let iv = 0x_FEDCBA0987654321_u64;
2917 println!("IV = {}", iv);
2918 let mut cipher = [0_u8; 12];
2919 a_des.encrypt_array_into_array(iv, &message, &mut cipher);
2920 print!("C =\t");
2921 for c in cipher.clone()
2922 { print!("{:02X} ", c); }
2923 println!();
2924 let mut txt = String::new();
2925 for c in cipher.clone()
2926 { write!(txt, "{:02X} ", c); }
2927 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
2928 println!();
2929
2930 // Normal case for the message of 16 bytes
2931 let key = 0x_1234567890ABCDEF_u64;
2932 println!("K =\t{:#016X}", key);
2933 let mut a_des = DES::new_with_key_u64(key);
2934
2935 let mes = "고맙습니다.";
2936 println!("M =\t{}", mes);
2937 let mut message = [0_u8; 16];
2938 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2939 let iv = 0x_FEDCBA0987654321_u64;
2940 println!("IV = {}", iv);
2941 let mut cipher = [0_u8; 16];
2942 a_des.encrypt_array_into_array(iv, &message, &mut cipher);
2943 print!("C =\t");
2944 for c in cipher.clone()
2945 { print!("{:02X} ", c); }
2946 println!();
2947 let mut txt = String::new();
2948 for c in cipher.clone()
2949 { write!(txt, "{:02X} ", c); }
2950 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
2951 println!("-------------------------------");
2952}
2953
2954fn des_decrypt_ofb()
2955{
2956 println!("des_decrypt_ofb()");
2957 use std::io::Write;
2958 use std::fmt::Write as _;
2959 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
2960
2961 // Normal case
2962 let key = 0x_1234567890ABCDEF_u64;
2963 println!("K =\t{:#016X}", key);
2964 let mut a_des = DES::new_with_key_u64(key);
2965
2966 let message = "In the beginning God created the heavens and the earth.";
2967 println!("M =\t{}", message);
2968 let iv = 0x_FEDCBA0987654321_u64;
2969 println!("IV = {}", iv);
2970 let mut cipher = Vec::<u8>::new();
2971 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
2972 print!("C (16 rounds) =\t");
2973 for c in cipher.clone()
2974 { print!("{:02X} ", c); }
2975 println!();
2976 let mut txt = String::new();
2977 for c in cipher.clone()
2978 { write!(txt, "{:02X} ", c); }
2979 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
2980
2981 let mut recovered = vec![0; 55];
2982 a_des.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2983 print!("Ba (16 rounds) =\t");
2984 for b in recovered.clone()
2985 { print!("{:02X} ", b); }
2986 println!();
2987 let mut txt = String::new();
2988 for c in recovered.clone()
2989 { write!(txt, "{:02X} ", c); }
2990 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 ");
2991
2992 let mut converted = String::new();
2993 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2994
2995 println!("Bb (16 rounds) =\t{}", converted);
2996 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2997 assert_eq!(converted, message);
2998 println!();
2999
3000 // Expanded case for 128 rounds
3001 let key = 0x_1234567890ABCDEF_u64;
3002 println!("K =\t{:#016X}", key);
3003 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
3004
3005 let message = "In the beginning God created the heavens and the earth.";
3006 println!("M =\t{}", message);
3007 let iv = 0x_FEDCBA0987654321_u64;
3008 println!("IV = {}", iv);
3009 let mut cipher = Vec::<u8>::new();
3010 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3011 print!("C (128 rounds) =\t");
3012 for c in cipher.clone()
3013 { print!("{:02X} ", c); }
3014 println!();
3015 let mut txt = String::new();
3016 for c in cipher.clone()
3017 { write!(txt, "{:02X} ", c); }
3018 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
3019
3020 let mut recovered = vec![0; 55];
3021 a_des.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
3022 print!("Ba (128 rounds) =\t");
3023 for b in recovered.clone()
3024 { print!("{:02X} ", b); }
3025 println!();
3026 let mut txt = String::new();
3027 for c in recovered.clone()
3028 { write!(txt, "{:02X} ", c); }
3029 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 ");
3030
3031 let mut converted = String::new();
3032 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3033
3034 println!("Bb (128 rounds) =\t{}", converted);
3035 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3036 assert_eq!(converted, message);
3037 println!();
3038
3039 // Expanded case for 0 rounds which means that key is meaningless
3040 let key1 = 0x_1234567890ABCDEF_u64;
3041 let key2 = 0_u64;
3042 println!("K =\t{:#016X}", key);
3043 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
3044 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
3045
3046 let message = "In the beginning God created the heavens and the earth.";
3047 println!("M =\t{}", message);
3048 let iv = 0x_FEDCBA0987654321_u64;
3049 println!("IV = {}", iv);
3050 let mut cipher1 = Vec::<u8>::new();
3051 let mut cipher2 = Vec::<u8>::new();
3052 c_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
3053 d_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
3054 print!("C (0 rounds) =\t");
3055 for c in cipher1.clone()
3056 { print!("{:02X} ", c); }
3057 println!();
3058 let mut txt = String::new();
3059 for c in cipher1.clone()
3060 { write!(txt, "{:02X} ", c); }
3061 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
3062 print!("D (0 rounds) =\t");
3063 for c in cipher2.clone()
3064 { print!("{:02X} ", c); }
3065 println!();
3066 let mut txt = String::new();
3067 for c in cipher2.clone()
3068 { write!(txt, "{:02X} ", c); }
3069 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
3070
3071 let mut recovered1 = vec![0; 55];
3072 let mut recovered2 = vec![0; 55];
3073 c_des.decrypt(iv, cipher1.as_ptr(), cipher1.len() as u64, recovered1.as_mut_ptr());
3074 d_des.decrypt(iv, cipher2.as_ptr(), cipher2.len() as u64, recovered2.as_mut_ptr());
3075 print!("B1a (0 rounds) =\t");
3076 for b in recovered1.clone()
3077 { print!("{:02X} ", b); }
3078 println!();
3079 let mut txt = String::new();
3080 for c in recovered1.clone()
3081 { write!(txt, "{:02X} ", c); }
3082 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 ");
3083 print!("B2a (0 rounds) =\t");
3084 for b in recovered2.clone()
3085 { print!("{:02X} ", b); }
3086 println!();
3087 let mut txt = String::new();
3088 for c in recovered2.clone()
3089 { write!(txt, "{:02X} ", c); }
3090 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 ");
3091
3092 let mut converted1 = String::new();
3093 let mut converted2 = String::new();
3094 unsafe { converted1.as_mut_vec() }.append(&mut recovered1);
3095 unsafe { converted2.as_mut_vec() }.append(&mut recovered2);
3096
3097 println!("B1b (0 rounds) =\t{}", converted1);
3098 assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
3099 assert_eq!(converted1, message);
3100 println!("B2b (0 rounds) =\t{}", converted2);
3101 assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
3102 assert_eq!(converted2, message);
3103 assert_eq!(converted1, converted1);
3104 println!();
3105
3106 // Normal case for the message of 0 bytes
3107 let key = 0x_1234567890ABCDEF_u64;
3108 println!("K =\t{:#016X}", key);
3109 let mut a_des = DES::new_with_key_u64(key);
3110
3111 let message = "";
3112 println!("M =\t{}", message);
3113 let iv = 0x_FEDCBA0987654321_u64;
3114 println!("IV = {}", iv);
3115 let mut cipher = Vec::<u8>::new();
3116 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3117 print!("C =\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, "");
3125
3126 let mut recovered = vec![0; 8];
3127 let len = a_des.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
3128 print!("Ba =\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, "00 00 00 00 00 00 00 00 ");
3136
3137 let mut converted = String::new();
3138 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3139 converted.truncate(len as usize);
3140
3141 println!("Bb =\t{}", converted);
3142 assert_eq!(converted, "");
3143 assert_eq!(converted, message);
3144 println!();
3145
3146 // Normal case for the message shorter than 8 bytes
3147 let key = 0x_1234567890ABCDEF_u64;
3148 println!("K =\t{:#016X}", key);
3149 let mut a_des = DES::new_with_key_u64(key);
3150
3151 let message = "7 bytes";
3152 println!("M =\t{}", message);
3153 let iv = 0x_FEDCBA0987654321_u64;
3154 println!("IV = {}", iv);
3155 let mut cipher = Vec::<u8>::new();
3156 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3157 print!("C =\t");
3158 for c in cipher.clone()
3159 { print!("{:02X} ", c); }
3160 println!();
3161 let mut txt = String::new();
3162 for c in cipher.clone()
3163 { write!(txt, "{:02X} ", c); }
3164 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
3165
3166 let mut recovered = vec![0; 8];
3167 let len = a_des.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
3168 print!("Ba =\t");
3169 for b in recovered.clone()
3170 { print!("{:02X} ", b); }
3171 println!();
3172 let mut txt = String::new();
3173 for c in recovered.clone()
3174 { write!(txt, "{:02X} ", c); }
3175 assert_eq!(txt, "37 20 62 79 74 65 73 00 ");
3176
3177 let mut converted = String::new();
3178 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3179 converted.truncate(len as usize);
3180
3181 println!("Bb =\t{}", converted);
3182 assert_eq!(converted, "7 bytes");
3183 assert_eq!(converted, message);
3184 println!();
3185
3186 // Normal case for the message of 8 bytes
3187 let key = 0x_1234567890ABCDEF_u64;
3188 println!("K =\t{:#016X}", key);
3189 let mut a_des = DES::new_with_key_u64(key);
3190
3191 let message = "I am OK.";
3192 println!("M =\t{}", message);
3193 let iv = 0x_FEDCBA0987654321_u64;
3194 println!("IV = {}", iv);
3195 let mut cipher = Vec::<u8>::new();
3196 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3197 print!("C =\t");
3198 for c in cipher.clone()
3199 { print!("{:02X} ", c); }
3200 println!();
3201 let mut txt = String::new();
3202 for c in cipher.clone()
3203 { write!(txt, "{:02X} ", c); }
3204 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
3205
3206 let mut recovered = vec![0; 16];
3207 let len = a_des.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
3208 print!("Ba =\t");
3209 for b in recovered.clone()
3210 { print!("{:02X} ", b); }
3211 println!();
3212 let mut txt = String::new();
3213 for c in recovered.clone()
3214 { write!(txt, "{:02X} ", c); }
3215 assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E 00 00 00 00 00 00 00 00 ");
3216
3217 let mut converted = String::new();
3218 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3219 converted.truncate(len as usize);
3220
3221 println!("Bb =\t{}", converted);
3222 assert_eq!(converted, "I am OK.");
3223 assert_eq!(converted, message);
3224 println!();
3225
3226 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
3227 let key = 0x_1234567890ABCDEF_u64;
3228 println!("K =\t{:#016X}", key);
3229 let mut a_des = DES::new_with_key_u64(key);
3230
3231 let message = "PARK Youngho";
3232 println!("M =\t{}", message);
3233 let iv = 0x_FEDCBA0987654321_u64;
3234 println!("IV = {}", iv);
3235 let mut cipher = Vec::<u8>::new();
3236 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3237 print!("C =\t");
3238 for c in cipher.clone()
3239 { print!("{:02X} ", c); }
3240 println!();
3241 let mut txt = String::new();
3242 for c in cipher.clone()
3243 { write!(txt, "{:02X} ", c); }
3244 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
3245
3246 let mut recovered = vec![0; 16];
3247 let len = a_des.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
3248 print!("Ba =\t");
3249 for b in recovered.clone()
3250 { print!("{:02X} ", b); }
3251 println!();
3252 let mut txt = String::new();
3253 for c in recovered.clone()
3254 { write!(txt, "{:02X} ", c); }
3255 assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F 00 00 00 00 ");
3256
3257 let mut converted = String::new();
3258 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3259 converted.truncate(len as usize);
3260
3261 println!("Bb =\t{}", converted);
3262 assert_eq!(converted, "PARK Youngho");
3263 assert_eq!(converted, message);
3264 println!();
3265
3266 // Normal case for the message of 16 bytes
3267 let key = 0x_1234567890ABCDEF_u64;
3268 println!("K =\t{:#016X}", key);
3269 let mut a_des = DES::new_with_key_u64(key);
3270
3271 let message = "고맙습니다.";
3272 println!("M =\t{}", message);
3273 let iv = 0x_FEDCBA0987654321_u64;
3274 println!("IV = {}", iv);
3275 let mut cipher = Vec::<u8>::new();
3276 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3277 print!("C =\t");
3278 for c in cipher.clone()
3279 { print!("{:02X} ", c); }
3280 println!();
3281 let mut txt = String::new();
3282 for c in cipher.clone()
3283 { write!(txt, "{:02X} ", c); }
3284 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
3285
3286 let mut recovered = vec![0; 24];
3287 let len = a_des.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
3288 print!("Ba =\t");
3289 for b in recovered.clone()
3290 { print!("{:02X} ", b); }
3291 println!();
3292 let mut txt = String::new();
3293 for c in recovered.clone()
3294 { write!(txt, "{:02X} ", c); }
3295 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 ");
3296
3297 let mut converted = String::new();
3298 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3299 converted.truncate(len as usize);
3300
3301 println!("Bb =\t{}", converted);
3302 assert_eq!(converted, "고맙습니다.");
3303 assert_eq!(converted, message);
3304 println!("-------------------------------");
3305}
3306
3307fn des_decrypt_ofb_into_vec()
3308{
3309 println!("des_decrypt_ofb_into_vec()");
3310 use std::io::Write;
3311 use std::fmt::Write as _;
3312 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
3313
3314 // Normal case
3315 let key = 0x_1234567890ABCDEF_u64;
3316 println!("K =\t{:#016X}", key);
3317 let mut a_des = DES::new_with_key_u64(key);
3318
3319 let message = "In the beginning God created the heavens and the earth.";
3320 println!("M =\t{}", message);
3321 let iv = 0x_FEDCBA0987654321_u64;
3322 println!("IV = {}", iv);
3323 let mut cipher = Vec::<u8>::new();
3324 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3325 print!("C (16 rounds) =\t");
3326 for c in cipher.clone()
3327 { print!("{:02X} ", c); }
3328 println!();
3329 let mut txt = String::new();
3330 for c in cipher.clone()
3331 { write!(txt, "{:02X} ", c); }
3332 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
3333
3334 let mut recovered = Vec::<u8>::new();
3335 a_des.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3336 print!("Ba (16 rounds) =\t");
3337 for b in recovered.clone()
3338 { print!("{:02X} ", b); }
3339 println!();
3340 let mut txt = String::new();
3341 for c in recovered.clone()
3342 { write!(txt, "{:02X} ", c); }
3343 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 ");
3344
3345 let mut converted = String::new();
3346 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3347
3348 println!("Bb (16 rounds) =\t{}", converted);
3349 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3350 assert_eq!(converted, message);
3351 println!();
3352
3353 // Expanded case for 128 rounds
3354 let key = 0x_1234567890ABCDEF_u64;
3355 println!("K =\t{:#016X}", key);
3356 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
3357
3358 let message = "In the beginning God created the heavens and the earth.";
3359 println!("M =\t{}", message);
3360 let iv = 0x_FEDCBA0987654321_u64;
3361 println!("IV = {}", iv);
3362 let mut cipher = Vec::<u8>::new();
3363 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3364 print!("C (128 rounds) =\t");
3365 for c in cipher.clone()
3366 { print!("{:02X} ", c); }
3367 println!();
3368 let mut txt = String::new();
3369 for c in cipher.clone()
3370 { write!(txt, "{:02X} ", c); }
3371 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
3372
3373 let mut recovered = Vec::<u8>::new();
3374 a_des.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3375 print!("Ba (128 rounds) =\t");
3376 for b in recovered.clone()
3377 { print!("{:02X} ", b); }
3378 println!();
3379 let mut txt = String::new();
3380 for c in recovered.clone()
3381 { write!(txt, "{:02X} ", c); }
3382 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 ");
3383
3384 let mut converted = String::new();
3385 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3386
3387 println!("Bb (128 rounds) =\t{}", converted);
3388 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3389 assert_eq!(converted, message);
3390 println!();
3391
3392 // Expanded case for 0 rounds which means that key is meaningless
3393 let key1 = 0x_1234567890ABCDEF_u64;
3394 let key2 = 0_u64;
3395 println!("K =\t{:#016X}", key);
3396 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
3397 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
3398
3399 let message = "In the beginning God created the heavens and the earth.";
3400 println!("M =\t{}", message);
3401 let iv = 0x_FEDCBA0987654321_u64;
3402 println!("IV = {}", iv);
3403 let mut cipher1 = Vec::<u8>::new();
3404 let mut cipher2 = Vec::<u8>::new();
3405 c_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
3406 d_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
3407 print!("C (0 rounds) =\t");
3408 for c in cipher1.clone()
3409 { print!("{:02X} ", c); }
3410 println!();
3411 let mut txt = String::new();
3412 for c in cipher1.clone()
3413 { write!(txt, "{:02X} ", c); }
3414 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
3415 print!("D (0 rounds) =\t");
3416 for c in cipher2.clone()
3417 { print!("{:02X} ", c); }
3418 println!();
3419 let mut txt = String::new();
3420 for c in cipher2.clone()
3421 { write!(txt, "{:02X} ", c); }
3422 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
3423
3424 let mut recovered1 = Vec::<u8>::new();
3425 let mut recovered2 = Vec::<u8>::new();
3426 c_des.decrypt_into_vec(iv, cipher1.as_ptr(), cipher1.len() as u64, &mut recovered1);
3427 d_des.decrypt_into_vec(iv, cipher2.as_ptr(), cipher2.len() as u64, &mut recovered2);
3428 print!("B1a (0 rounds) =\t");
3429 for b in recovered1.clone()
3430 { print!("{:02X} ", b); }
3431 println!();
3432 let mut txt = String::new();
3433 for c in recovered1.clone()
3434 { write!(txt, "{:02X} ", c); }
3435 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 ");
3436 print!("B2a (0 rounds) =\t");
3437 for b in recovered2.clone()
3438 { print!("{:02X} ", b); }
3439 println!();
3440 let mut txt = String::new();
3441 for c in recovered2.clone()
3442 { write!(txt, "{:02X} ", c); }
3443 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 ");
3444
3445 let mut converted1 = String::new();
3446 let mut converted2 = String::new();
3447 unsafe { converted1.as_mut_vec() }.append(&mut recovered1);
3448 unsafe { converted2.as_mut_vec() }.append(&mut recovered2);
3449
3450 println!("B1b (0 rounds) =\t{}", converted1);
3451 assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
3452 assert_eq!(converted1, message);
3453 println!("B2b (0 rounds) =\t{}", converted2);
3454 assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
3455 assert_eq!(converted2, message);
3456 assert_eq!(converted1, converted1);
3457 println!();
3458
3459 // Normal case for the message of 0 bytes
3460 let key = 0x_1234567890ABCDEF_u64;
3461 println!("K =\t{:#016X}", key);
3462 let mut a_des = DES::new_with_key_u64(key);
3463
3464 let message = "";
3465 println!("M =\t{}", message);
3466 let iv = 0x_FEDCBA0987654321_u64;
3467 println!("IV = {}", iv);
3468 let mut cipher = Vec::<u8>::new();
3469 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3470 print!("C =\t");
3471 for c in cipher.clone()
3472 { print!("{:02X} ", c); }
3473 println!();
3474 let mut txt = String::new();
3475 for c in cipher.clone()
3476 { write!(txt, "{:02X} ", c); }
3477 assert_eq!(txt, "");
3478
3479 let mut recovered = Vec::<u8>::new();
3480 a_des.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3481 print!("Ba =\t");
3482 for b in recovered.clone()
3483 { print!("{:02X} ", b); }
3484 println!();
3485 let mut txt = String::new();
3486 for c in recovered.clone()
3487 { write!(txt, "{:02X} ", c); }
3488 assert_eq!(txt, "");
3489
3490 let mut converted = String::new();
3491 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3492
3493 println!("Bb =\t{}", converted);
3494 assert_eq!(converted, "");
3495 assert_eq!(converted, message);
3496 println!();
3497
3498 // Normal case for the message shorter than 8 bytes
3499 let key = 0x_1234567890ABCDEF_u64;
3500 println!("K =\t{:#016X}", key);
3501 let mut a_des = DES::new_with_key_u64(key);
3502
3503 let message = "7 bytes";
3504 println!("M =\t{}", message);
3505 let iv = 0x_FEDCBA0987654321_u64;
3506 println!("IV = {}", iv);
3507 let mut cipher = Vec::<u8>::new();
3508 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3509 print!("C =\t");
3510 for c in cipher.clone()
3511 { print!("{:02X} ", c); }
3512 println!();
3513 let mut txt = String::new();
3514 for c in cipher.clone()
3515 { write!(txt, "{:02X} ", c); }
3516 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
3517
3518 let mut recovered = Vec::<u8>::new();
3519 a_des.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3520 print!("Ba =\t");
3521 for b in recovered.clone()
3522 { print!("{:02X} ", b); }
3523 println!();
3524 let mut txt = String::new();
3525 for c in recovered.clone()
3526 { write!(txt, "{:02X} ", c); }
3527 assert_eq!(txt, "37 20 62 79 74 65 73 ");
3528
3529 let mut converted = String::new();
3530 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3531
3532 println!("Bb =\t{}", converted);
3533 assert_eq!(converted, "7 bytes");
3534 assert_eq!(converted, message);
3535 println!();
3536
3537 // Normal case for the message of 8 bytes
3538 let key = 0x_1234567890ABCDEF_u64;
3539 println!("K =\t{:#016X}", key);
3540 let mut a_des = DES::new_with_key_u64(key);
3541
3542 let message = "I am OK.";
3543 println!("M =\t{}", message);
3544 let iv = 0x_FEDCBA0987654321_u64;
3545 println!("IV = {}", iv);
3546 let mut cipher = Vec::<u8>::new();
3547 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3548 print!("C =\t");
3549 for c in cipher.clone()
3550 { print!("{:02X} ", c); }
3551 println!();
3552 let mut txt = String::new();
3553 for c in cipher.clone()
3554 { write!(txt, "{:02X} ", c); }
3555 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
3556
3557 let mut recovered = Vec::<u8>::new();
3558 a_des.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3559 print!("Ba =\t");
3560 for b in recovered.clone()
3561 { print!("{:02X} ", b); }
3562 println!();
3563 let mut txt = String::new();
3564 for c in recovered.clone()
3565 { write!(txt, "{:02X} ", c); }
3566 assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E ");
3567
3568 let mut converted = String::new();
3569 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3570
3571 println!("Bb =\t{}", converted);
3572 assert_eq!(converted, "I am OK.");
3573 assert_eq!(converted, message);
3574 println!();
3575
3576 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
3577 let key = 0x_1234567890ABCDEF_u64;
3578 println!("K =\t{:#016X}", key);
3579 let mut a_des = DES::new_with_key_u64(key);
3580
3581 let message = "PARK Youngho";
3582 println!("M =\t{}", message);
3583 let iv = 0x_FEDCBA0987654321_u64;
3584 println!("IV = {}", iv);
3585 let mut cipher = Vec::<u8>::new();
3586 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3587 print!("C =\t");
3588 for c in cipher.clone()
3589 { print!("{:02X} ", c); }
3590 println!();
3591 let mut txt = String::new();
3592 for c in cipher.clone()
3593 { write!(txt, "{:02X} ", c); }
3594 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
3595
3596 let mut recovered = Vec::<u8>::new();
3597 a_des.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3598 print!("Ba =\t");
3599 for b in recovered.clone()
3600 { print!("{:02X} ", b); }
3601 println!();
3602 let mut txt = String::new();
3603 for c in recovered.clone()
3604 { write!(txt, "{:02X} ", c); }
3605 assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F ");
3606
3607 let mut converted = String::new();
3608 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3609
3610 println!("Bb =\t{}", converted);
3611 assert_eq!(converted, "PARK Youngho");
3612 assert_eq!(converted, message);
3613 println!();
3614
3615 // Normal case for the message of 16 bytes
3616 let key = 0x_1234567890ABCDEF_u64;
3617 println!("K =\t{:#016X}", key);
3618 let mut a_des = DES::new_with_key_u64(key);
3619
3620 let message = "고맙습니다.";
3621 println!("M =\t{}", message);
3622 let iv = 0x_FEDCBA0987654321_u64;
3623 println!("IV = {}", iv);
3624 let mut cipher = Vec::<u8>::new();
3625 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3626 print!("C =\t");
3627 for c in cipher.clone()
3628 { print!("{:02X} ", c); }
3629 println!();
3630 let mut txt = String::new();
3631 for c in cipher.clone()
3632 { write!(txt, "{:02X} ", c); }
3633 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
3634
3635 let mut recovered = Vec::<u8>::new();
3636 a_des.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3637 print!("Ba =\t");
3638 for b in recovered.clone()
3639 { print!("{:02X} ", b); }
3640 println!();
3641 let mut txt = String::new();
3642 for c in recovered.clone()
3643 { write!(txt, "{:02X} ", c); }
3644 assert_eq!(txt, "EA B3 A0 EB A7 99 EC 8A B5 EB 8B 88 EB 8B A4 2E ");
3645
3646 let mut converted = String::new();
3647 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3648
3649 println!("Bb =\t{}", converted);
3650 assert_eq!(converted, "고맙습니다.");
3651 assert_eq!(converted, message);
3652 println!("-------------------------------");
3653}
3654
3655fn des_decrypt_ofb_into_array()
3656{
3657 println!("des_decrypt_ofb_into_array()");
3658 use std::io::Write;
3659 use std::fmt::Write as _;
3660 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
3661
3662 // Normal case
3663 let key = 0x_1234567890ABCDEF_u64;
3664 println!("K =\t{:#016X}", key);
3665 let mut a_des = DES::new_with_key_u64(key);
3666
3667 let message = "In the beginning God created the heavens and the earth.";
3668 println!("M =\t{}", message);
3669 let iv = 0x_FEDCBA0987654321_u64;
3670 println!("IV = {}", iv);
3671 let mut cipher = Vec::<u8>::new();
3672 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3673 print!("C (16 rounds) =\t");
3674 for c in cipher.clone()
3675 { print!("{:02X} ", c); }
3676 println!();
3677 let mut txt = String::new();
3678 for c in cipher.clone()
3679 { write!(txt, "{:02X} ", c); }
3680 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
3681
3682 let mut recovered = [0u8; 56];
3683 let len = a_des.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3684 print!("Ba (16 rounds) =\t");
3685 for b in recovered.clone()
3686 { print!("{:02X} ", b); }
3687 println!();
3688 let mut txt = String::new();
3689 for c in recovered.clone()
3690 { write!(txt, "{:02X} ", c); }
3691 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 ");
3692
3693 let mut converted = String::new();
3694 unsafe { converted.as_mut_vec() }.write(&recovered);
3695 unsafe { converted.as_mut_vec() }.truncate(len as usize);
3696 println!("Bb (16 rounds) =\t{}", converted);
3697 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3698 assert_eq!(converted, message);
3699 println!();
3700
3701 // Expanded case for 128 rounds
3702 let key = 0x_1234567890ABCDEF_u64;
3703 println!("K =\t{:#016X}", key);
3704 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
3705
3706 let message = "In the beginning God created the heavens and the earth.";
3707 println!("M =\t{}", message);
3708 let iv = 0x_FEDCBA0987654321_u64;
3709 println!("IV = {}", iv);
3710 let mut cipher = Vec::<u8>::new();
3711 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3712 print!("C (128 rounds) =\t");
3713 for c in cipher.clone()
3714 { print!("{:02X} ", c); }
3715 println!();
3716 let mut txt = String::new();
3717 for c in cipher.clone()
3718 { write!(txt, "{:02X} ", c); }
3719 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
3720
3721 let mut recovered = [0u8; 56];
3722 let len = a_des.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3723 print!("Ba (16 rounds) =\t");
3724 for b in recovered.clone()
3725 { print!("{:02X} ", b); }
3726 println!();
3727 let mut txt = String::new();
3728 for c in recovered.clone()
3729 { write!(txt, "{:02X} ", c); }
3730 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 ");
3731
3732 let mut converted = String::new();
3733 unsafe { converted.as_mut_vec() }.write(&recovered);
3734 unsafe { converted.as_mut_vec() }.truncate(len as usize);
3735 println!("Bb (16 rounds) =\t{}", converted);
3736 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3737 assert_eq!(converted, message);
3738 println!();
3739
3740 // Expanded case for 0 rounds which means that key is meaningless
3741 let key1 = 0x_1234567890ABCDEF_u64;
3742 let key2 = 0_u64;
3743 println!("K =\t{:#016X}", key);
3744 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
3745 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
3746
3747 let message = "In the beginning God created the heavens and the earth.";
3748 println!("M =\t{}", message);
3749 let iv = 0x_FEDCBA0987654321_u64;
3750 println!("IV = {}", iv);
3751 let mut cipher1 = Vec::<u8>::new();
3752 let mut cipher2 = Vec::<u8>::new();
3753 c_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
3754 d_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
3755 print!("C (0 rounds) =\t");
3756 for c in cipher1.clone()
3757 { print!("{:02X} ", c); }
3758 println!();
3759 let mut txt = String::new();
3760 for c in cipher1.clone()
3761 { write!(txt, "{:02X} ", c); }
3762 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
3763 print!("D (0 rounds) =\t");
3764 for c in cipher2.clone()
3765 { print!("{:02X} ", c); }
3766 println!();
3767 let mut txt = String::new();
3768 for c in cipher2.clone()
3769 { write!(txt, "{:02X} ", c); }
3770 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
3771
3772 let mut recovered1 = [0u8; 56];
3773 let mut recovered2 = [0u8; 56];
3774 let len1 = c_des.decrypt_into_array(iv, cipher1.as_ptr(), cipher1.len() as u64, &mut recovered1);
3775 let len2 = d_des.decrypt_into_array(iv, cipher2.as_ptr(), cipher2.len() as u64, &mut recovered2);
3776 print!("B1a (0 rounds) =\t");
3777 for b in recovered1.clone()
3778 { print!("{:02X} ", b); }
3779 println!();
3780 let mut txt = String::new();
3781 for c in recovered1.clone()
3782 { write!(txt, "{:02X} ", c); }
3783 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 ");
3784 print!("B2a (0 rounds) =\t");
3785 for b in recovered2.clone()
3786 { print!("{:02X} ", b); }
3787 println!();
3788 let mut txt = String::new();
3789 for c in recovered.clone()
3790 { write!(txt, "{:02X} ", c); }
3791 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 ");
3792
3793 let mut converted1 = String::new();
3794 let mut converted2 = String::new();
3795 unsafe { converted1.as_mut_vec() }.write(&recovered1);
3796 unsafe { converted2.as_mut_vec() }.write(&recovered2);
3797 unsafe { converted1.as_mut_vec() }.truncate(len1 as usize);
3798 unsafe { converted2.as_mut_vec() }.truncate(len2 as usize);
3799 println!("B1b (0 rounds) =\t{}", converted1);
3800 println!("B2b (0 rounds) =\t{}", converted2);
3801 assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
3802 assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
3803 assert_eq!(converted1, message);
3804 assert_eq!(converted2, message);
3805 assert_eq!(converted1, converted2);
3806 println!();
3807
3808 // Normal case for the message of 0 bytes
3809 let key = 0x_1234567890ABCDEF_u64;
3810 println!("K =\t{:#016X}", key);
3811 let mut a_des = DES::new_with_key_u64(key);
3812
3813 let message = "";
3814 println!("M =\t{}", message);
3815 let iv = 0x_FEDCBA0987654321_u64;
3816 println!("IV = {}", iv);
3817 let mut cipher = Vec::<u8>::new();
3818 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3819 print!("C =\t");
3820 for c in cipher.clone()
3821 { print!("{:02X} ", c); }
3822 println!();
3823 let mut txt = String::new();
3824 for c in cipher.clone()
3825 { write!(txt, "{:02X} ", c); }
3826 assert_eq!(txt, "");
3827
3828 let mut recovered = [0u8; 8];
3829 let len = a_des.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3830
3831 print!("Ba =\t");
3832 for b in recovered.clone()
3833 { print!("{:02X} ", b); }
3834 println!();
3835 let mut txt = String::new();
3836 for c in recovered.clone()
3837 { write!(txt, "{:02X} ", c); }
3838 assert_eq!(txt, "00 00 00 00 00 00 00 00 ");
3839
3840 let mut converted = String::new();
3841 unsafe { converted.as_mut_vec() }.write(&recovered);
3842 unsafe { converted.as_mut_vec() }.truncate(len as usize);
3843 println!("Bb =\t{}", converted);
3844 assert_eq!(converted, "");
3845 assert_eq!(converted, message);
3846 println!();
3847
3848 // Normal case for the message shorter than 8 bytes
3849 let key = 0x_1234567890ABCDEF_u64;
3850 println!("K =\t{:#016X}", key);
3851 let mut a_des = DES::new_with_key_u64(key);
3852
3853 let message = "7 bytes";
3854 println!("M =\t{}", message);
3855 let iv = 0x_FEDCBA0987654321_u64;
3856 println!("IV = {}", iv);
3857 let mut cipher = Vec::<u8>::new();
3858 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3859 print!("C =\t");
3860 for c in cipher.clone()
3861 { print!("{:02X} ", c); }
3862 println!();
3863 let mut txt = String::new();
3864 for c in cipher.clone()
3865 { write!(txt, "{:02X} ", c); }
3866 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
3867
3868 let mut recovered = [0u8; 8];
3869 let len = a_des.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3870
3871 print!("Ba =\t");
3872 for b in recovered.clone()
3873 { print!("{:02X} ", b); }
3874 println!();
3875 let mut txt = String::new();
3876 for c in recovered.clone()
3877 { write!(txt, "{:02X} ", c); }
3878 assert_eq!(txt, "37 20 62 79 74 65 73 00 ");
3879
3880 let mut converted = String::new();
3881 unsafe { converted.as_mut_vec() }.write(&recovered);
3882 unsafe { converted.as_mut_vec() }.truncate(len as usize);
3883 println!("Bb =\t{}", converted);
3884 assert_eq!(converted, "7 bytes");
3885 assert_eq!(converted, message);
3886 println!();
3887
3888 // Normal case for the message of 8 bytes
3889 let key = 0x_1234567890ABCDEF_u64;
3890 println!("K =\t{:#016X}", key);
3891 let mut a_des = DES::new_with_key_u64(key);
3892
3893 let message = "I am OK.";
3894 println!("M =\t{}", message);
3895 let iv = 0x_FEDCBA0987654321_u64;
3896 println!("IV = {}", iv);
3897 let mut cipher = Vec::<u8>::new();
3898 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3899 print!("C =\t");
3900 for c in cipher.clone()
3901 { print!("{:02X} ", c); }
3902 println!();
3903 let mut txt = String::new();
3904 for c in cipher.clone()
3905 { write!(txt, "{:02X} ", c); }
3906 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
3907
3908 let mut recovered = [0u8; 16];
3909 let len = a_des.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3910
3911 print!("Ba =\t");
3912 for b in recovered.clone()
3913 { print!("{:02X} ", b); }
3914 println!();
3915 let mut txt = String::new();
3916 for c in recovered.clone()
3917 { write!(txt, "{:02X} ", c); }
3918 assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E 00 00 00 00 00 00 00 00 ");
3919
3920 let mut converted = String::new();
3921 unsafe { converted.as_mut_vec() }.write(&recovered);
3922 unsafe { converted.as_mut_vec() }.truncate(len as usize);
3923 println!("Bb =\t{}", converted);
3924 assert_eq!(converted, "I am OK.");
3925 assert_eq!(converted, message);
3926 println!();
3927
3928 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
3929 let key = 0x_1234567890ABCDEF_u64;
3930 println!("K =\t{:#016X}", key);
3931 let mut a_des = DES::new_with_key_u64(key);
3932
3933 let message = "PARK Youngho";
3934 println!("M =\t{}", message);
3935 let iv = 0x_FEDCBA0987654321_u64;
3936 println!("IV = {}", iv);
3937 let mut cipher = Vec::<u8>::new();
3938 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3939 print!("C =\t");
3940 for c in cipher.clone()
3941 { print!("{:02X} ", c); }
3942 println!();
3943 let mut txt = String::new();
3944 for c in cipher.clone()
3945 { write!(txt, "{:02X} ", c); }
3946 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
3947
3948 let mut recovered = [0u8; 16];
3949 let len = a_des.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3950
3951 print!("Ba =\t");
3952 for b in recovered.clone()
3953 { print!("{:02X} ", b); }
3954 println!();
3955 let mut txt = String::new();
3956 for c in recovered.clone()
3957 { write!(txt, "{:02X} ", c); }
3958 assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F 00 00 00 00 ");
3959
3960 let mut converted = String::new();
3961 unsafe { converted.as_mut_vec() }.write(&recovered);
3962 unsafe { converted.as_mut_vec() }.truncate(len as usize);
3963 println!("Bb =\t{}", converted);
3964 assert_eq!(converted, "PARK Youngho");
3965 assert_eq!(converted, message);
3966 println!();
3967
3968 // Normal case for the message of 16 bytes
3969 let key = 0x_1234567890ABCDEF_u64;
3970 println!("K =\t{:#016X}", key);
3971 let mut a_des = DES::new_with_key_u64(key);
3972
3973 let message = "고맙습니다.";
3974 println!("M =\t{}", message);
3975 let iv = 0x_FEDCBA0987654321_u64;
3976 println!("IV = {}", iv);
3977 let mut cipher = Vec::<u8>::new();
3978 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3979 print!("C =\t");
3980 for c in cipher.clone()
3981 { print!("{:02X} ", c); }
3982 println!();
3983 let mut txt = String::new();
3984 for c in cipher.clone()
3985 { write!(txt, "{:02X} ", c); }
3986 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
3987
3988 let mut recovered = [0u8; 24];
3989 let len = a_des.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3990
3991 print!("Ba =\t");
3992 for b in recovered.clone()
3993 { print!("{:02X} ", b); }
3994 println!();
3995 let mut txt = String::new();
3996 for c in recovered.clone()
3997 { write!(txt, "{:02X} ", c); }
3998 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 ");
3999
4000 let mut converted = String::new();
4001 unsafe { converted.as_mut_vec() }.write(&recovered);
4002 unsafe { converted.as_mut_vec() }.truncate(len as usize);
4003 println!("Bb =\t{}", converted);
4004 assert_eq!(converted, "고맙습니다.");
4005 assert_eq!(converted, message);
4006 println!("-------------------------------");
4007}
4008
4009fn des_decrypt_ofb_into_string()
4010{
4011 println!("des_decrypt_ofb_into_string()");
4012 use std::io::Write;
4013 use std::fmt::Write as _;
4014 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
4015
4016 // Normal case
4017 let key = 0x_1234567890ABCDEF_u64;
4018 println!("K =\t{:#016X}", key);
4019 let mut a_des = DES::new_with_key_u64(key);
4020
4021 let message = "In the beginning God created the heavens and the earth.";
4022 println!("M =\t{}", message);
4023 let iv = 0x_FEDCBA0987654321_u64;
4024 println!("IV = {}", iv);
4025 let mut cipher = Vec::<u8>::new();
4026 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4027 print!("C (16 rounds) =\t");
4028 for c in cipher.clone()
4029 { print!("{:02X} ", c); }
4030 println!();
4031 let mut txt = String::new();
4032 for c in cipher.clone()
4033 { write!(txt, "{:02X} ", c); }
4034 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
4035
4036 let mut recovered = String::new();
4037 a_des.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
4038 println!("B (16 rounds) =\t{}", recovered);
4039 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
4040 assert_eq!(recovered, message);
4041 println!();
4042
4043 // Expanded case for 128 rounds
4044 let key = 0x_1234567890ABCDEF_u64;
4045 println!("K =\t{:#016X}", key);
4046 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
4047
4048 let message = "In the beginning God created the heavens and the earth.";
4049 println!("M =\t{}", message);
4050 let iv = 0x_FEDCBA0987654321_u64;
4051 println!("IV = {}", iv);
4052 let mut cipher = Vec::<u8>::new();
4053 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4054 print!("C (128 rounds) =\t");
4055 for c in cipher.clone()
4056 { print!("{:02X} ", c); }
4057 println!();
4058 let mut txt = String::new();
4059 for c in cipher.clone()
4060 { write!(txt, "{:02X} ", c); }
4061 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
4062
4063 let mut recovered = String::new();
4064 a_des.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
4065 println!("B (128 rounds) =\t{}", recovered);
4066 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
4067 assert_eq!(recovered, message);
4068 println!();
4069
4070 // Expanded case for 0 rounds which means that key is meaningless
4071 let key1 = 0x_1234567890ABCDEF_u64;
4072 let key2 = 0_u64;
4073 println!("K =\t{:#016X}", key);
4074 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
4075 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
4076
4077 let message = "In the beginning God created the heavens and the earth.";
4078 println!("M =\t{}", message);
4079 let iv = 0x_FEDCBA0987654321_u64;
4080 println!("IV = {}", iv);
4081 let mut cipher1 = Vec::<u8>::new();
4082 let mut cipher2 = Vec::<u8>::new();
4083 c_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
4084 d_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
4085 print!("C (0 rounds) =\t");
4086 for c in cipher1.clone()
4087 { print!("{:02X} ", c); }
4088 println!();
4089 let mut txt = String::new();
4090 for c in cipher1.clone()
4091 { write!(txt, "{:02X} ", c); }
4092 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
4093 print!("D (0 rounds) =\t");
4094 for c in cipher2.clone()
4095 { print!("{:02X} ", c); }
4096 println!();
4097 let mut txt = String::new();
4098 for c in cipher2.clone()
4099 { write!(txt, "{:02X} ", c); }
4100 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
4101
4102 let mut recovered1 = String::new();
4103 let mut recovered2 = String::new();
4104 c_des.decrypt_into_string(iv, cipher1.as_ptr(), cipher1.len() as u64, &mut recovered1);
4105 d_des.decrypt_into_string(iv, cipher2.as_ptr(), cipher2.len() as u64, &mut recovered2);
4106 println!("B1 (0 rounds) =\t{}", recovered1);
4107 println!("B2 (0 rounds) =\t{}", recovered2);
4108 assert_eq!(recovered1, "In the beginning God created the heavens and the earth.");
4109 assert_eq!(recovered2, "In the beginning God created the heavens and the earth.");
4110 assert_eq!(recovered1, message);
4111 assert_eq!(recovered2, message);
4112 assert_eq!(recovered1, recovered2);
4113 println!();
4114
4115 // Normal case for the message of 0 bytes
4116 let key = 0x_1234567890ABCDEF_u64;
4117 println!("K =\t{:#016X}", key);
4118 let mut a_des = DES::new_with_key_u64(key);
4119
4120 let message = "";
4121 println!("M =\t{}", message);
4122 let iv = 0x_FEDCBA0987654321_u64;
4123 println!("IV = {}", iv);
4124 let mut cipher = Vec::<u8>::new();
4125 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4126 print!("C =\t");
4127 for c in cipher.clone()
4128 { print!("{:02X} ", c); }
4129 println!();
4130 let mut txt = String::new();
4131 for c in cipher.clone()
4132 { write!(txt, "{:02X} ", c); }
4133 assert_eq!(txt, "");
4134
4135 let mut recovered = String::new();
4136 a_des.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
4137 println!("B =\t{}", recovered);
4138 assert_eq!(recovered, "");
4139 assert_eq!(recovered, message);
4140 println!();
4141
4142 // Normal case for the message shorter than 8 bytes
4143 let key = 0x_1234567890ABCDEF_u64;
4144 println!("K =\t{:#016X}", key);
4145 let mut a_des = DES::new_with_key_u64(key);
4146
4147 let message = "7 bytes";
4148 println!("M =\t{}", message);
4149 let iv = 0x_FEDCBA0987654321_u64;
4150 println!("IV = {}", iv);
4151 let mut cipher = Vec::<u8>::new();
4152 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4153 print!("C =\t");
4154 for c in cipher.clone()
4155 { print!("{:02X} ", c); }
4156 println!();
4157 let mut txt = String::new();
4158 for c in cipher.clone()
4159 { write!(txt, "{:02X} ", c); }
4160 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
4161
4162 let mut recovered = String::new();
4163 a_des.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
4164 println!("B =\t{}", recovered);
4165 assert_eq!(recovered, "7 bytes");
4166 assert_eq!(recovered, message);
4167 println!();
4168
4169 // Normal case for the message of 8 bytes
4170 let key = 0x_1234567890ABCDEF_u64;
4171 println!("K =\t{:#016X}", key);
4172 let mut a_des = DES::new_with_key_u64(key);
4173
4174 let message = "I am OK.";
4175 println!("M =\t{}", message);
4176 let iv = 0x_FEDCBA0987654321_u64;
4177 println!("IV = {}", iv);
4178 let mut cipher = Vec::<u8>::new();
4179 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4180 print!("C =\t");
4181 for c in cipher.clone()
4182 { print!("{:02X} ", c); }
4183 println!();
4184 let mut txt = String::new();
4185 for c in cipher.clone()
4186 { write!(txt, "{:02X} ", c); }
4187 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
4188
4189 let mut recovered = String::new();
4190 a_des.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
4191 println!("B =\t{}", recovered);
4192 assert_eq!(recovered, "I am OK.");
4193 assert_eq!(recovered, message);
4194 println!();
4195
4196 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
4197 let key = 0x_1234567890ABCDEF_u64;
4198 println!("K =\t{:#016X}", key);
4199 let mut a_des = DES::new_with_key_u64(key);
4200
4201 let message = "PARK Youngho";
4202 println!("M =\t{}", message);
4203 let iv = 0x_FEDCBA0987654321_u64;
4204 println!("IV = {}", iv);
4205 let mut cipher = Vec::<u8>::new();
4206 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4207 print!("C =\t");
4208 for c in cipher.clone()
4209 { print!("{:02X} ", c); }
4210 println!();
4211 let mut txt = String::new();
4212 for c in cipher.clone()
4213 { write!(txt, "{:02X} ", c); }
4214 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
4215
4216 let mut recovered = String::new();
4217 a_des.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
4218 println!("B =\t{}", recovered);
4219 assert_eq!(recovered, "PARK Youngho");
4220 assert_eq!(recovered, message);
4221 println!();
4222
4223 // Normal case for the message of 16 bytes
4224 let key = 0x_1234567890ABCDEF_u64;
4225 println!("K =\t{:#016X}", key);
4226 let mut a_des = DES::new_with_key_u64(key);
4227
4228 let message = "고맙습니다.";
4229 println!("M =\t{}", message);
4230 let iv = 0x_FEDCBA0987654321_u64;
4231 println!("IV = {}", iv);
4232 let mut cipher = Vec::<u8>::new();
4233 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4234 print!("C =\t");
4235 for c in cipher.clone()
4236 { print!("{:02X} ", c); }
4237 println!();
4238 let mut txt = String::new();
4239 for c in cipher.clone()
4240 { write!(txt, "{:02X} ", c); }
4241 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
4242
4243 let mut recovered = String::new();
4244 a_des.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
4245 println!("B =\t{}", recovered);
4246 assert_eq!(recovered, "고맙습니다.");
4247 assert_eq!(recovered, message);
4248 println!("-------------------------------");
4249}
4250
4251fn des_decrypt_vec_ofb()
4252{
4253 println!("des_decrypt_vec_ofb()");
4254 use std::io::Write;
4255 use std::fmt::Write as _;
4256 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
4257
4258 // Normal case
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 = "In the beginning God created the heavens and the earth.";
4264 println!("M =\t{}", message);
4265 let iv = 0x_FEDCBA0987654321_u64;
4266 println!("IV = {}", iv);
4267 let mut cipher = Vec::<u8>::new();
4268 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4269 print!("C (16 rounds) =\t");
4270 for c in cipher.clone()
4271 { print!("{:02X} ", c); }
4272 println!();
4273 let mut txt = String::new();
4274 for c in cipher.clone()
4275 { write!(txt, "{:02X} ", c); }
4276 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
4277
4278 let mut recovered = vec![0; 55];
4279 a_des.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
4280 print!("Ba (16 rounds) =\t");
4281 for b in recovered.clone()
4282 { print!("{:02X} ", b); }
4283 println!();
4284 let mut txt = String::new();
4285 for c in recovered.clone()
4286 { write!(txt, "{:02X} ", c); }
4287 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 ");
4288
4289 let mut converted = String::new();
4290 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4291
4292 println!("Bb (16 rounds) =\t{}", converted);
4293 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4294 assert_eq!(converted, message);
4295 println!();
4296
4297 // Expanded case for 128 rounds
4298 let key = 0x_1234567890ABCDEF_u64;
4299 println!("K =\t{:#016X}", key);
4300 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
4301
4302 let message = "In the beginning God created the heavens and the earth.";
4303 println!("M =\t{}", message);
4304 let iv = 0x_FEDCBA0987654321_u64;
4305 println!("IV = {}", iv);
4306 let mut cipher = Vec::<u8>::new();
4307 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4308 print!("C (128 rounds) =\t");
4309 for c in cipher.clone()
4310 { print!("{:02X} ", c); }
4311 println!();
4312 let mut txt = String::new();
4313 for c in cipher.clone()
4314 { write!(txt, "{:02X} ", c); }
4315 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
4316
4317 let mut recovered = vec![0; 55];
4318 a_des.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
4319 print!("Ba (128 rounds) =\t");
4320 for b in recovered.clone()
4321 { print!("{:02X} ", b); }
4322 println!();
4323 let mut txt = String::new();
4324 for c in recovered.clone()
4325 { write!(txt, "{:02X} ", c); }
4326 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 ");
4327
4328 let mut converted = String::new();
4329 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4330
4331 println!("Bb (128 rounds) =\t{}", converted);
4332 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4333 assert_eq!(converted, message);
4334 println!();
4335
4336 // Expanded case for 0 rounds which means that key is meaningless
4337 let key1 = 0x_1234567890ABCDEF_u64;
4338 let key2 = 0_u64;
4339 println!("K =\t{:#016X}", key);
4340 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
4341 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
4342
4343 let message = "In the beginning God created the heavens and the earth.";
4344 println!("M =\t{}", message);
4345 let iv = 0x_FEDCBA0987654321_u64;
4346 println!("IV = {}", iv);
4347 let mut cipher1 = Vec::<u8>::new();
4348 let mut cipher2 = Vec::<u8>::new();
4349 c_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
4350 d_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
4351 print!("C (0 rounds) =\t");
4352 for c in cipher1.clone()
4353 { print!("{:02X} ", c); }
4354 println!();
4355 let mut txt = String::new();
4356 for c in cipher1.clone()
4357 { write!(txt, "{:02X} ", c); }
4358 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
4359 print!("D (0 rounds) =\t");
4360 for c in cipher2.clone()
4361 { print!("{:02X} ", c); }
4362 println!();
4363 let mut txt = String::new();
4364 for c in cipher2.clone()
4365 { write!(txt, "{:02X} ", c); }
4366 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
4367
4368 let mut recovered1 = vec![0; 55];
4369 let mut recovered2 = vec![0; 55];
4370 c_des.decrypt_vec(iv, &cipher1, recovered1.as_mut_ptr());
4371 d_des.decrypt_vec(iv, &cipher2, recovered2.as_mut_ptr());
4372 print!("B1a (0 rounds) =\t");
4373 for b in recovered1.clone()
4374 { print!("{:02X} ", b); }
4375 println!();
4376 let mut txt = String::new();
4377 for c in recovered1.clone()
4378 { write!(txt, "{:02X} ", c); }
4379 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 ");
4380 print!("B2a (0 rounds) =\t");
4381 for b in recovered2.clone()
4382 { print!("{:02X} ", b); }
4383 println!();
4384 let mut txt = String::new();
4385 for c in recovered2.clone()
4386 { write!(txt, "{:02X} ", c); }
4387 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 ");
4388
4389 let mut converted1 = String::new();
4390 let mut converted2 = String::new();
4391 unsafe { converted1.as_mut_vec() }.append(&mut recovered1);
4392 unsafe { converted2.as_mut_vec() }.append(&mut recovered2);
4393
4394 println!("B1b (0 rounds) =\t{}", converted1);
4395 assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
4396 assert_eq!(converted1, message);
4397 println!("B2b (0 rounds) =\t{}", converted2);
4398 assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
4399 assert_eq!(converted2, message);
4400 assert_eq!(converted1, converted1);
4401 println!();
4402
4403 // Normal case for the message of 0 bytes
4404 let key = 0x_1234567890ABCDEF_u64;
4405 println!("K =\t{:#016X}", key);
4406 let mut a_des = DES::new_with_key_u64(key);
4407
4408 let message = "";
4409 println!("M =\t{}", message);
4410 let iv = 0x_FEDCBA0987654321_u64;
4411 println!("IV = {}", iv);
4412 let mut cipher = Vec::<u8>::new();
4413 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4414 print!("C =\t");
4415 for c in cipher.clone()
4416 { print!("{:02X} ", c); }
4417 println!();
4418 let mut txt = String::new();
4419 for c in cipher.clone()
4420 { write!(txt, "{:02X} ", c); }
4421 assert_eq!(txt, "");
4422
4423 let mut recovered = vec![0; 8];
4424 let len = a_des.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
4425 print!("Ba =\t");
4426 for b in recovered.clone()
4427 { print!("{:02X} ", b); }
4428 println!();
4429 let mut txt = String::new();
4430 for c in recovered.clone()
4431 { write!(txt, "{:02X} ", c); }
4432 assert_eq!(txt, "00 00 00 00 00 00 00 00 ");
4433
4434 let mut converted = String::new();
4435 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4436 converted.truncate(len as usize);
4437
4438 println!("Bb =\t{}", converted);
4439 assert_eq!(converted, "");
4440 assert_eq!(converted, message);
4441 println!();
4442
4443 // Normal case for the message shorter than 8 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 = "7 bytes";
4449 println!("M =\t{}", message);
4450 let iv = 0x_FEDCBA0987654321_u64;
4451 println!("IV = {}", iv);
4452 let mut cipher = Vec::<u8>::new();
4453 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4454 print!("C =\t");
4455 for c in cipher.clone()
4456 { print!("{:02X} ", c); }
4457 println!();
4458 let mut txt = String::new();
4459 for c in cipher.clone()
4460 { write!(txt, "{:02X} ", c); }
4461 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
4462
4463 let mut recovered = vec![0; 8];
4464 let len = a_des.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
4465 print!("Ba =\t");
4466 for b in recovered.clone()
4467 { print!("{:02X} ", b); }
4468 println!();
4469 let mut txt = String::new();
4470 for c in recovered.clone()
4471 { write!(txt, "{:02X} ", c); }
4472 assert_eq!(txt, "37 20 62 79 74 65 73 00 ");
4473
4474 let mut converted = String::new();
4475 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4476 converted.truncate(len as usize);
4477
4478 println!("Bb =\t{}", converted);
4479 assert_eq!(converted, "7 bytes");
4480 assert_eq!(converted, message);
4481 println!();
4482
4483 // Normal case for the message of 8 bytes
4484 let key = 0x_1234567890ABCDEF_u64;
4485 println!("K =\t{:#016X}", key);
4486 let mut a_des = DES::new_with_key_u64(key);
4487
4488 let message = "I am OK.";
4489 println!("M =\t{}", message);
4490 let iv = 0x_FEDCBA0987654321_u64;
4491 println!("IV = {}", iv);
4492 let mut cipher = Vec::<u8>::new();
4493 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4494 print!("C =\t");
4495 for c in cipher.clone()
4496 { print!("{:02X} ", c); }
4497 println!();
4498 let mut txt = String::new();
4499 for c in cipher.clone()
4500 { write!(txt, "{:02X} ", c); }
4501 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
4502
4503 let mut recovered = vec![0; 16];
4504 let len = a_des.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
4505 print!("Ba =\t");
4506 for b in recovered.clone()
4507 { print!("{:02X} ", b); }
4508 println!();
4509 let mut txt = String::new();
4510 for c in recovered.clone()
4511 { write!(txt, "{:02X} ", c); }
4512 assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E 00 00 00 00 00 00 00 00 ");
4513
4514 let mut converted = String::new();
4515 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4516 converted.truncate(len as usize);
4517
4518 println!("Bb =\t{}", converted);
4519 assert_eq!(converted, "I am OK.");
4520 assert_eq!(converted, message);
4521 println!();
4522
4523 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
4524 let key = 0x_1234567890ABCDEF_u64;
4525 println!("K =\t{:#016X}", key);
4526 let mut a_des = DES::new_with_key_u64(key);
4527
4528 let message = "PARK Youngho";
4529 println!("M =\t{}", message);
4530 let iv = 0x_FEDCBA0987654321_u64;
4531 println!("IV = {}", iv);
4532 let mut cipher = Vec::<u8>::new();
4533 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4534 print!("C =\t");
4535 for c in cipher.clone()
4536 { print!("{:02X} ", c); }
4537 println!();
4538 let mut txt = String::new();
4539 for c in cipher.clone()
4540 { write!(txt, "{:02X} ", c); }
4541 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
4542
4543 let mut recovered = vec![0; 16];
4544 let len = a_des.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
4545 print!("Ba =\t");
4546 for b in recovered.clone()
4547 { print!("{:02X} ", b); }
4548 println!();
4549 let mut txt = String::new();
4550 for c in recovered.clone()
4551 { write!(txt, "{:02X} ", c); }
4552 assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F 00 00 00 00 ");
4553
4554 let mut converted = String::new();
4555 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4556 converted.truncate(len as usize);
4557
4558 println!("Bb =\t{}", converted);
4559 assert_eq!(converted, "PARK Youngho");
4560 assert_eq!(converted, message);
4561 println!();
4562
4563 // Normal case for the message of 16 bytes
4564 let key = 0x_1234567890ABCDEF_u64;
4565 println!("K =\t{:#016X}", key);
4566 let mut a_des = DES::new_with_key_u64(key);
4567
4568 let message = "고맙습니다.";
4569 println!("M =\t{}", message);
4570 let iv = 0x_FEDCBA0987654321_u64;
4571 println!("IV = {}", iv);
4572 let mut cipher = Vec::<u8>::new();
4573 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4574 print!("C =\t");
4575 for c in cipher.clone()
4576 { print!("{:02X} ", c); }
4577 println!();
4578 let mut txt = String::new();
4579 for c in cipher.clone()
4580 { write!(txt, "{:02X} ", c); }
4581 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
4582
4583 let mut recovered = vec![0; 24];
4584 let len = a_des.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
4585 print!("Ba =\t");
4586 for b in recovered.clone()
4587 { print!("{:02X} ", b); }
4588 println!();
4589 let mut txt = String::new();
4590 for c in recovered.clone()
4591 { write!(txt, "{:02X} ", c); }
4592 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 ");
4593
4594 let mut converted = String::new();
4595 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4596 converted.truncate(len as usize);
4597
4598 println!("Bb =\t{}", converted);
4599 assert_eq!(converted, "고맙습니다.");
4600 assert_eq!(converted, message);
4601 println!("-------------------------------");
4602}
4603
4604fn des_decrypt_vec_ofb_into_vec()
4605{
4606 println!("des_decrypt_vec_ofb_into_vec()");
4607 use std::io::Write;
4608 use std::fmt::Write as _;
4609 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
4610
4611 // Normal case
4612 let key = 0x_1234567890ABCDEF_u64;
4613 println!("K =\t{:#016X}", key);
4614 let mut a_des = DES::new_with_key_u64(key);
4615
4616 let message = "In the beginning God created the heavens and the earth.";
4617 println!("M =\t{}", message);
4618 let iv = 0x_FEDCBA0987654321_u64;
4619 println!("IV = {}", iv);
4620 let mut cipher = Vec::<u8>::new();
4621 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4622 print!("C (16 rounds) =\t");
4623 for c in cipher.clone()
4624 { print!("{:02X} ", c); }
4625 println!();
4626 let mut txt = String::new();
4627 for c in cipher.clone()
4628 { write!(txt, "{:02X} ", c); }
4629 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
4630
4631 let mut recovered = Vec::<u8>::new();
4632 a_des.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
4633 print!("Ba (16 rounds) =\t");
4634 for b in recovered.clone()
4635 { print!("{:02X} ", b); }
4636 println!();
4637 let mut txt = String::new();
4638 for c in recovered.clone()
4639 { write!(txt, "{:02X} ", c); }
4640 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 ");
4641
4642 let mut converted = String::new();
4643 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4644
4645 println!("Bb (16 rounds) =\t{}", converted);
4646 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4647 assert_eq!(converted, message);
4648 println!();
4649
4650 // Expanded case for 128 rounds
4651 let key = 0x_1234567890ABCDEF_u64;
4652 println!("K =\t{:#016X}", key);
4653 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
4654
4655 let message = "In the beginning God created the heavens and the earth.";
4656 println!("M =\t{}", message);
4657 let iv = 0x_FEDCBA0987654321_u64;
4658 println!("IV = {}", iv);
4659 let mut cipher = Vec::<u8>::new();
4660 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4661 print!("C (128 rounds) =\t");
4662 for c in cipher.clone()
4663 { print!("{:02X} ", c); }
4664 println!();
4665 let mut txt = String::new();
4666 for c in cipher.clone()
4667 { write!(txt, "{:02X} ", c); }
4668 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
4669
4670 let mut recovered = Vec::<u8>::new();
4671 a_des.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
4672 print!("Ba (128 rounds) =\t");
4673 for b in recovered.clone()
4674 { print!("{:02X} ", b); }
4675 println!();
4676 let mut txt = String::new();
4677 for c in recovered.clone()
4678 { write!(txt, "{:02X} ", c); }
4679 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 ");
4680
4681 let mut converted = String::new();
4682 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4683
4684 println!("Bb (128 rounds) =\t{}", converted);
4685 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4686 assert_eq!(converted, message);
4687 println!();
4688
4689 // Expanded case for 0 rounds which means that key is meaningless
4690 let key1 = 0x_1234567890ABCDEF_u64;
4691 let key2 = 0_u64;
4692 println!("K =\t{:#016X}", key);
4693 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
4694 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
4695
4696 let message = "In the beginning God created the heavens and the earth.";
4697 println!("M =\t{}", message);
4698 let iv = 0x_FEDCBA0987654321_u64;
4699 println!("IV = {}", iv);
4700 let mut cipher1 = Vec::<u8>::new();
4701 let mut cipher2 = Vec::<u8>::new();
4702 c_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
4703 d_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
4704 print!("C (0 rounds) =\t");
4705 for c in cipher1.clone()
4706 { print!("{:02X} ", c); }
4707 println!();
4708 let mut txt = String::new();
4709 for c in cipher1.clone()
4710 { write!(txt, "{:02X} ", c); }
4711 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
4712 print!("D (0 rounds) =\t");
4713 for c in cipher2.clone()
4714 { print!("{:02X} ", c); }
4715 println!();
4716 let mut txt = String::new();
4717 for c in cipher2.clone()
4718 { write!(txt, "{:02X} ", c); }
4719 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
4720
4721 let mut recovered1 = Vec::<u8>::new();
4722 let mut recovered2 = Vec::<u8>::new();
4723 c_des.decrypt_vec_into_vec(iv, &cipher1, &mut recovered1);
4724 d_des.decrypt_vec_into_vec(iv, &cipher2, &mut recovered2);
4725 print!("B1a (0 rounds) =\t");
4726 for b in recovered1.clone()
4727 { print!("{:02X} ", b); }
4728 println!();
4729 let mut txt = String::new();
4730 for c in recovered1.clone()
4731 { write!(txt, "{:02X} ", c); }
4732 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 ");
4733 print!("B2a (0 rounds) =\t");
4734 for b in recovered2.clone()
4735 { print!("{:02X} ", b); }
4736 println!();
4737 let mut txt = String::new();
4738 for c in recovered2.clone()
4739 { write!(txt, "{:02X} ", c); }
4740 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 ");
4741
4742 let mut converted1 = String::new();
4743 let mut converted2 = String::new();
4744 unsafe { converted1.as_mut_vec() }.append(&mut recovered1);
4745 unsafe { converted2.as_mut_vec() }.append(&mut recovered2);
4746
4747 println!("B1b (0 rounds) =\t{}", converted1);
4748 assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
4749 assert_eq!(converted1, message);
4750 println!("B2b (0 rounds) =\t{}", converted2);
4751 assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
4752 assert_eq!(converted2, message);
4753 assert_eq!(converted1, converted1);
4754 println!();
4755
4756 // Normal case for the message of 0 bytes
4757 let key = 0x_1234567890ABCDEF_u64;
4758 println!("K =\t{:#016X}", key);
4759 let mut a_des = DES::new_with_key_u64(key);
4760
4761 let message = "";
4762 println!("M =\t{}", message);
4763 let iv = 0x_FEDCBA0987654321_u64;
4764 println!("IV = {}", iv);
4765 let mut cipher = Vec::<u8>::new();
4766 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4767 print!("C =\t");
4768 for c in cipher.clone()
4769 { print!("{:02X} ", c); }
4770 println!();
4771 let mut txt = String::new();
4772 for c in cipher.clone()
4773 { write!(txt, "{:02X} ", c); }
4774 assert_eq!(txt, "");
4775
4776 let mut recovered = Vec::<u8>::new();
4777 a_des.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
4778 print!("Ba =\t");
4779 for b in recovered.clone()
4780 { print!("{:02X} ", b); }
4781 println!();
4782 let mut txt = String::new();
4783 for c in recovered.clone()
4784 { write!(txt, "{:02X} ", c); }
4785 assert_eq!(txt, "");
4786
4787 let mut converted = String::new();
4788 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4789
4790 println!("Bb =\t{}", converted);
4791 assert_eq!(converted, "");
4792 assert_eq!(converted, message);
4793 println!();
4794
4795 // Normal case for the message shorter than 8 bytes
4796 let key = 0x_1234567890ABCDEF_u64;
4797 println!("K =\t{:#016X}", key);
4798 let mut a_des = DES::new_with_key_u64(key);
4799
4800 let message = "7 bytes";
4801 println!("M =\t{}", message);
4802 let iv = 0x_FEDCBA0987654321_u64;
4803 println!("IV = {}", iv);
4804 let mut cipher = Vec::<u8>::new();
4805 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4806 print!("C =\t");
4807 for c in cipher.clone()
4808 { print!("{:02X} ", c); }
4809 println!();
4810 let mut txt = String::new();
4811 for c in cipher.clone()
4812 { write!(txt, "{:02X} ", c); }
4813 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
4814
4815 let mut recovered = Vec::<u8>::new();
4816 a_des.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
4817 print!("Ba =\t");
4818 for b in recovered.clone()
4819 { print!("{:02X} ", b); }
4820 println!();
4821 let mut txt = String::new();
4822 for c in recovered.clone()
4823 { write!(txt, "{:02X} ", c); }
4824 assert_eq!(txt, "37 20 62 79 74 65 73 ");
4825
4826 let mut converted = String::new();
4827 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4828
4829 println!("Bb =\t{}", converted);
4830 assert_eq!(converted, "7 bytes");
4831 assert_eq!(converted, message);
4832 println!();
4833
4834 // Normal case for the message of 8 bytes
4835 let key = 0x_1234567890ABCDEF_u64;
4836 println!("K =\t{:#016X}", key);
4837 let mut a_des = DES::new_with_key_u64(key);
4838
4839 let message = "I am OK.";
4840 println!("M =\t{}", message);
4841 let iv = 0x_FEDCBA0987654321_u64;
4842 println!("IV = {}", iv);
4843 let mut cipher = Vec::<u8>::new();
4844 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4845 print!("C =\t");
4846 for c in cipher.clone()
4847 { print!("{:02X} ", c); }
4848 println!();
4849 let mut txt = String::new();
4850 for c in cipher.clone()
4851 { write!(txt, "{:02X} ", c); }
4852 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
4853
4854 let mut recovered = Vec::<u8>::new();
4855 a_des.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
4856 print!("Ba =\t");
4857 for b in recovered.clone()
4858 { print!("{:02X} ", b); }
4859 println!();
4860 let mut txt = String::new();
4861 for c in recovered.clone()
4862 { write!(txt, "{:02X} ", c); }
4863 assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E ");
4864
4865 let mut converted = String::new();
4866 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4867
4868 println!("Bb =\t{}", converted);
4869 assert_eq!(converted, "I am OK.");
4870 assert_eq!(converted, message);
4871 println!();
4872
4873 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
4874 let key = 0x_1234567890ABCDEF_u64;
4875 println!("K =\t{:#016X}", key);
4876 let mut a_des = DES::new_with_key_u64(key);
4877
4878 let message = "PARK Youngho";
4879 println!("M =\t{}", message);
4880 let iv = 0x_FEDCBA0987654321_u64;
4881 println!("IV = {}", iv);
4882 let mut cipher = Vec::<u8>::new();
4883 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4884 print!("C =\t");
4885 for c in cipher.clone()
4886 { print!("{:02X} ", c); }
4887 println!();
4888 let mut txt = String::new();
4889 for c in cipher.clone()
4890 { write!(txt, "{:02X} ", c); }
4891 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
4892
4893 let mut recovered = Vec::<u8>::new();
4894 a_des.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
4895 print!("Ba =\t");
4896 for b in recovered.clone()
4897 { print!("{:02X} ", b); }
4898 println!();
4899 let mut txt = String::new();
4900 for c in recovered.clone()
4901 { write!(txt, "{:02X} ", c); }
4902 assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F ");
4903
4904 let mut converted = String::new();
4905 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4906
4907 println!("Bb =\t{}", converted);
4908 assert_eq!(converted, "PARK Youngho");
4909 assert_eq!(converted, message);
4910 println!();
4911
4912 // Normal case for the message of 16 bytes
4913 let key = 0x_1234567890ABCDEF_u64;
4914 println!("K =\t{:#016X}", key);
4915 let mut a_des = DES::new_with_key_u64(key);
4916
4917 let message = "고맙습니다.";
4918 println!("M =\t{}", message);
4919 let iv = 0x_FEDCBA0987654321_u64;
4920 println!("IV = {}", iv);
4921 let mut cipher = Vec::<u8>::new();
4922 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4923 print!("C =\t");
4924 for c in cipher.clone()
4925 { print!("{:02X} ", c); }
4926 println!();
4927 let mut txt = String::new();
4928 for c in cipher.clone()
4929 { write!(txt, "{:02X} ", c); }
4930 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
4931
4932 let mut recovered = Vec::<u8>::new();
4933 a_des.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
4934 print!("Ba =\t");
4935 for b in recovered.clone()
4936 { print!("{:02X} ", b); }
4937 println!();
4938 let mut txt = String::new();
4939 for c in recovered.clone()
4940 { write!(txt, "{:02X} ", c); }
4941 assert_eq!(txt, "EA B3 A0 EB A7 99 EC 8A B5 EB 8B 88 EB 8B A4 2E ");
4942
4943 let mut converted = String::new();
4944 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4945
4946 println!("Bb =\t{}", converted);
4947 assert_eq!(converted, "고맙습니다.");
4948 assert_eq!(converted, message);
4949 println!("-------------------------------");
4950}
4951
4952fn des_decrypt_vec_ofb_into_array()
4953{
4954 println!("des_decrypt_vec_ofb_into_array()");
4955 use std::io::Write;
4956 use std::fmt::Write as _;
4957 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
4958
4959 // Normal case
4960 let key = 0x_1234567890ABCDEF_u64;
4961 println!("K =\t{:#016X}", key);
4962 let mut a_des = DES::new_with_key_u64(key);
4963
4964 let message = "In the beginning God created the heavens and the earth.";
4965 println!("M =\t{}", message);
4966 let iv = 0x_FEDCBA0987654321_u64;
4967 println!("IV = {}", iv);
4968 let mut cipher = Vec::<u8>::new();
4969 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4970 print!("C (16 rounds) =\t");
4971 for c in cipher.clone()
4972 { print!("{:02X} ", c); }
4973 println!();
4974 let mut txt = String::new();
4975 for c in cipher.clone()
4976 { write!(txt, "{:02X} ", c); }
4977 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
4978
4979 let mut recovered = [0u8; 56];
4980 let len = a_des.decrypt_vec_into_array(iv, &cipher, &mut recovered);
4981 print!("Ba (16 rounds) =\t");
4982 for b in recovered.clone()
4983 { print!("{:02X} ", b); }
4984 println!();
4985 let mut txt = String::new();
4986 for c in recovered.clone()
4987 { write!(txt, "{:02X} ", c); }
4988 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 ");
4989
4990 let mut converted = String::new();
4991 unsafe { converted.as_mut_vec() }.write(&recovered);
4992 unsafe { converted.as_mut_vec() }.truncate(len as usize);
4993 println!("Bb (16 rounds) =\t{}", converted);
4994 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4995 assert_eq!(converted, message);
4996 println!();
4997
4998 // Expanded case for 128 rounds
4999 let key = 0x_1234567890ABCDEF_u64;
5000 println!("K =\t{:#016X}", key);
5001 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
5002
5003 let message = "In the beginning God created the heavens and the earth.";
5004 println!("M =\t{}", message);
5005 let iv = 0x_FEDCBA0987654321_u64;
5006 println!("IV = {}", iv);
5007 let mut cipher = Vec::<u8>::new();
5008 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5009 print!("C (128 rounds) =\t");
5010 for c in cipher.clone()
5011 { print!("{:02X} ", c); }
5012 println!();
5013 let mut txt = String::new();
5014 for c in cipher.clone()
5015 { write!(txt, "{:02X} ", c); }
5016 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
5017
5018 let mut recovered = [0u8; 56];
5019 let len = a_des.decrypt_vec_into_array(iv, &cipher, &mut recovered);
5020 print!("Ba (16 rounds) =\t");
5021 for b in recovered.clone()
5022 { print!("{:02X} ", b); }
5023 println!();
5024 let mut txt = String::new();
5025 for c in recovered.clone()
5026 { write!(txt, "{:02X} ", c); }
5027 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 ");
5028
5029 let mut converted = String::new();
5030 unsafe { converted.as_mut_vec() }.write(&recovered);
5031 unsafe { converted.as_mut_vec() }.truncate(len as usize);
5032 println!("Bb (16 rounds) =\t{}", converted);
5033 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
5034 assert_eq!(converted, message);
5035 println!();
5036
5037 // Expanded case for 0 rounds which means that key is meaningless
5038 let key1 = 0x_1234567890ABCDEF_u64;
5039 let key2 = 0_u64;
5040 println!("K =\t{:#016X}", key);
5041 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
5042 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
5043
5044 let message = "In the beginning God created the heavens and the earth.";
5045 println!("M =\t{}", message);
5046 let iv = 0x_FEDCBA0987654321_u64;
5047 println!("IV = {}", iv);
5048 let mut cipher1 = Vec::<u8>::new();
5049 let mut cipher2 = Vec::<u8>::new();
5050 c_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
5051 d_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
5052 print!("C (0 rounds) =\t");
5053 for c in cipher1.clone()
5054 { print!("{:02X} ", c); }
5055 println!();
5056 let mut txt = String::new();
5057 for c in cipher1.clone()
5058 { write!(txt, "{:02X} ", c); }
5059 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
5060 print!("D (0 rounds) =\t");
5061 for c in cipher2.clone()
5062 { print!("{:02X} ", c); }
5063 println!();
5064 let mut txt = String::new();
5065 for c in cipher2.clone()
5066 { write!(txt, "{:02X} ", c); }
5067 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
5068
5069 let mut recovered1 = [0u8; 56];
5070 let mut recovered2 = [0u8; 56];
5071 let len1 = c_des.decrypt_vec_into_array(iv, &cipher1, &mut recovered1);
5072 let len2 = d_des.decrypt_vec_into_array(iv, &cipher2, &mut recovered2);
5073 print!("B1a (0 rounds) =\t");
5074 for b in recovered1.clone()
5075 { print!("{:02X} ", b); }
5076 println!();
5077 let mut txt = String::new();
5078 for c in recovered1.clone()
5079 { write!(txt, "{:02X} ", c); }
5080 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 ");
5081 print!("B2a (0 rounds) =\t");
5082 for b in recovered2.clone()
5083 { print!("{:02X} ", b); }
5084 println!();
5085 let mut txt = String::new();
5086 for c in recovered.clone()
5087 { write!(txt, "{:02X} ", c); }
5088 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 ");
5089
5090 let mut converted1 = String::new();
5091 let mut converted2 = String::new();
5092 unsafe { converted1.as_mut_vec() }.write(&recovered1);
5093 unsafe { converted2.as_mut_vec() }.write(&recovered2);
5094 unsafe { converted1.as_mut_vec() }.truncate(len1 as usize);
5095 unsafe { converted2.as_mut_vec() }.truncate(len2 as usize);
5096 println!("B1b (0 rounds) =\t{}", converted1);
5097 println!("B2b (0 rounds) =\t{}", converted2);
5098 assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
5099 assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
5100 assert_eq!(converted1, message);
5101 assert_eq!(converted2, message);
5102 assert_eq!(converted1, converted2);
5103 println!();
5104
5105 // Normal case for the message of 0 bytes
5106 let key = 0x_1234567890ABCDEF_u64;
5107 println!("K =\t{:#016X}", key);
5108 let mut a_des = DES::new_with_key_u64(key);
5109
5110 let message = "";
5111 println!("M =\t{}", message);
5112 let iv = 0x_FEDCBA0987654321_u64;
5113 println!("IV = {}", iv);
5114 let mut cipher = Vec::<u8>::new();
5115 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5116 print!("C =\t");
5117 for c in cipher.clone()
5118 { print!("{:02X} ", c); }
5119 println!();
5120 let mut txt = String::new();
5121 for c in cipher.clone()
5122 { write!(txt, "{:02X} ", c); }
5123 assert_eq!(txt, "");
5124
5125 let mut recovered = [0u8; 8];
5126 let len = a_des.decrypt_vec_into_array(iv, &cipher, &mut recovered);
5127
5128 print!("Ba =\t");
5129 for b in recovered.clone()
5130 { print!("{:02X} ", b); }
5131 println!();
5132 let mut txt = String::new();
5133 for c in recovered.clone()
5134 { write!(txt, "{:02X} ", c); }
5135 assert_eq!(txt, "00 00 00 00 00 00 00 00 ");
5136
5137 let mut converted = String::new();
5138 unsafe { converted.as_mut_vec() }.write(&recovered);
5139 unsafe { converted.as_mut_vec() }.truncate(len as usize);
5140 println!("Bb =\t{}", converted);
5141 assert_eq!(converted, "");
5142 assert_eq!(converted, message);
5143 println!();
5144
5145 // Normal case for the message shorter than 8 bytes
5146 let key = 0x_1234567890ABCDEF_u64;
5147 println!("K =\t{:#016X}", key);
5148 let mut a_des = DES::new_with_key_u64(key);
5149
5150 let message = "7 bytes";
5151 println!("M =\t{}", message);
5152 let iv = 0x_FEDCBA0987654321_u64;
5153 println!("IV = {}", iv);
5154 let mut cipher = Vec::<u8>::new();
5155 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5156 print!("C =\t");
5157 for c in cipher.clone()
5158 { print!("{:02X} ", c); }
5159 println!();
5160 let mut txt = String::new();
5161 for c in cipher.clone()
5162 { write!(txt, "{:02X} ", c); }
5163 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
5164
5165 let mut recovered = [0u8; 8];
5166 let len = a_des.decrypt_vec_into_array(iv, &cipher, &mut recovered);
5167
5168 print!("Ba =\t");
5169 for b in recovered.clone()
5170 { print!("{:02X} ", b); }
5171 println!();
5172 let mut txt = String::new();
5173 for c in recovered.clone()
5174 { write!(txt, "{:02X} ", c); }
5175 assert_eq!(txt, "37 20 62 79 74 65 73 00 ");
5176
5177 let mut converted = String::new();
5178 unsafe { converted.as_mut_vec() }.write(&recovered);
5179 unsafe { converted.as_mut_vec() }.truncate(len as usize);
5180 println!("Bb =\t{}", converted);
5181 assert_eq!(converted, "7 bytes");
5182 assert_eq!(converted, message);
5183 println!();
5184
5185 // Normal case for the message of 8 bytes
5186 let key = 0x_1234567890ABCDEF_u64;
5187 println!("K =\t{:#016X}", key);
5188 let mut a_des = DES::new_with_key_u64(key);
5189
5190 let message = "I am OK.";
5191 println!("M =\t{}", message);
5192 let iv = 0x_FEDCBA0987654321_u64;
5193 println!("IV = {}", iv);
5194 let mut cipher = Vec::<u8>::new();
5195 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5196 print!("C =\t");
5197 for c in cipher.clone()
5198 { print!("{:02X} ", c); }
5199 println!();
5200 let mut txt = String::new();
5201 for c in cipher.clone()
5202 { write!(txt, "{:02X} ", c); }
5203 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
5204
5205 let mut recovered = [0u8; 16];
5206 let len = a_des.decrypt_vec_into_array(iv, &cipher, &mut recovered);
5207
5208 print!("Ba =\t");
5209 for b in recovered.clone()
5210 { print!("{:02X} ", b); }
5211 println!();
5212 let mut txt = String::new();
5213 for c in recovered.clone()
5214 { write!(txt, "{:02X} ", c); }
5215 assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E 00 00 00 00 00 00 00 00 ");
5216
5217 let mut converted = String::new();
5218 unsafe { converted.as_mut_vec() }.write(&recovered);
5219 unsafe { converted.as_mut_vec() }.truncate(len as usize);
5220 println!("Bb =\t{}", converted);
5221 assert_eq!(converted, "I am OK.");
5222 assert_eq!(converted, message);
5223 println!();
5224
5225 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
5226 let key = 0x_1234567890ABCDEF_u64;
5227 println!("K =\t{:#016X}", key);
5228 let mut a_des = DES::new_with_key_u64(key);
5229
5230 let message = "PARK Youngho";
5231 println!("M =\t{}", message);
5232 let iv = 0x_FEDCBA0987654321_u64;
5233 println!("IV = {}", iv);
5234 let mut cipher = Vec::<u8>::new();
5235 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5236 print!("C =\t");
5237 for c in cipher.clone()
5238 { print!("{:02X} ", c); }
5239 println!();
5240 let mut txt = String::new();
5241 for c in cipher.clone()
5242 { write!(txt, "{:02X} ", c); }
5243 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
5244
5245 let mut recovered = [0u8; 16];
5246 let len = a_des.decrypt_vec_into_array(iv, &cipher, &mut recovered);
5247
5248 print!("Ba =\t");
5249 for b in recovered.clone()
5250 { print!("{:02X} ", b); }
5251 println!();
5252 let mut txt = String::new();
5253 for c in recovered.clone()
5254 { write!(txt, "{:02X} ", c); }
5255 assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F 00 00 00 00 ");
5256
5257 let mut converted = String::new();
5258 unsafe { converted.as_mut_vec() }.write(&recovered);
5259 unsafe { converted.as_mut_vec() }.truncate(len as usize);
5260 println!("Bb =\t{}", converted);
5261 assert_eq!(converted, "PARK Youngho");
5262 assert_eq!(converted, message);
5263 println!();
5264
5265 // Normal case for the message of 16 bytes
5266 let key = 0x_1234567890ABCDEF_u64;
5267 println!("K =\t{:#016X}", key);
5268 let mut a_des = DES::new_with_key_u64(key);
5269
5270 let message = "고맙습니다.";
5271 println!("M =\t{}", message);
5272 let iv = 0x_FEDCBA0987654321_u64;
5273 println!("IV = {}", iv);
5274 let mut cipher = Vec::<u8>::new();
5275 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5276 print!("C =\t");
5277 for c in cipher.clone()
5278 { print!("{:02X} ", c); }
5279 println!();
5280 let mut txt = String::new();
5281 for c in cipher.clone()
5282 { write!(txt, "{:02X} ", c); }
5283 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
5284
5285 let mut recovered = [0u8; 24];
5286 let len = a_des.decrypt_vec_into_array(iv, &cipher, &mut recovered);
5287
5288 print!("Ba =\t");
5289 for b in recovered.clone()
5290 { print!("{:02X} ", b); }
5291 println!();
5292 let mut txt = String::new();
5293 for c in recovered.clone()
5294 { write!(txt, "{:02X} ", c); }
5295 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 ");
5296
5297 let mut converted = String::new();
5298 unsafe { converted.as_mut_vec() }.write(&recovered);
5299 unsafe { converted.as_mut_vec() }.truncate(len as usize);
5300 println!("Bb =\t{}", converted);
5301 assert_eq!(converted, "고맙습니다.");
5302 assert_eq!(converted, message);
5303 println!("-------------------------------");
5304}
5305
5306fn des_decrypt_vec_ofb_into_string()
5307{
5308 println!("des_decrypt_vec_ofb_into_string()");
5309 use std::io::Write;
5310 use std::fmt::Write as _;
5311 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
5312
5313 // Normal case
5314 let key = 0x_1234567890ABCDEF_u64;
5315 println!("K =\t{:#016X}", key);
5316 let mut a_des = DES::new_with_key_u64(key);
5317
5318 let message = "In the beginning God created the heavens and the earth.";
5319 println!("M =\t{}", message);
5320 let iv = 0x_FEDCBA0987654321_u64;
5321 println!("IV = {}", iv);
5322 let mut cipher = Vec::<u8>::new();
5323 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5324 print!("C (16 rounds) =\t");
5325 for c in cipher.clone()
5326 { print!("{:02X} ", c); }
5327 println!();
5328 let mut txt = String::new();
5329 for c in cipher.clone()
5330 { write!(txt, "{:02X} ", c); }
5331 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
5332
5333 let mut recovered = String::new();
5334 a_des.decrypt_vec_into_string(iv, &cipher, &mut recovered);
5335 println!("B (16 rounds) =\t{}", recovered);
5336 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
5337 assert_eq!(recovered, message);
5338 println!();
5339
5340 // Expanded case for 128 rounds
5341 let key = 0x_1234567890ABCDEF_u64;
5342 println!("K =\t{:#016X}", key);
5343 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
5344
5345 let message = "In the beginning God created the heavens and the earth.";
5346 println!("M =\t{}", message);
5347 let iv = 0x_FEDCBA0987654321_u64;
5348 println!("IV = {}", iv);
5349 let mut cipher = Vec::<u8>::new();
5350 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5351 print!("C (128 rounds) =\t");
5352 for c in cipher.clone()
5353 { print!("{:02X} ", c); }
5354 println!();
5355 let mut txt = String::new();
5356 for c in cipher.clone()
5357 { write!(txt, "{:02X} ", c); }
5358 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
5359
5360 let mut recovered = String::new();
5361 a_des.decrypt_vec_into_string(iv, &cipher, &mut recovered);
5362 println!("B (128 rounds) =\t{}", recovered);
5363 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
5364 assert_eq!(recovered, message);
5365 println!();
5366
5367 // Expanded case for 0 rounds which means that key is meaningless
5368 let key1 = 0x_1234567890ABCDEF_u64;
5369 let key2 = 0_u64;
5370 println!("K =\t{:#016X}", key);
5371 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
5372 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
5373
5374 let message = "In the beginning God created the heavens and the earth.";
5375 println!("M =\t{}", message);
5376 let iv = 0x_FEDCBA0987654321_u64;
5377 println!("IV = {}", iv);
5378 let mut cipher1 = Vec::<u8>::new();
5379 let mut cipher2 = Vec::<u8>::new();
5380 c_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
5381 d_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
5382 print!("C (0 rounds) =\t");
5383 for c in cipher1.clone()
5384 { print!("{:02X} ", c); }
5385 println!();
5386 let mut txt = String::new();
5387 for c in cipher1.clone()
5388 { write!(txt, "{:02X} ", c); }
5389 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
5390 print!("D (0 rounds) =\t");
5391 for c in cipher2.clone()
5392 { print!("{:02X} ", c); }
5393 println!();
5394 let mut txt = String::new();
5395 for c in cipher2.clone()
5396 { write!(txt, "{:02X} ", c); }
5397 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
5398
5399 let mut recovered1 = String::new();
5400 let mut recovered2 = String::new();
5401 c_des.decrypt_vec_into_string(iv, &cipher1, &mut recovered1);
5402 d_des.decrypt_vec_into_string(iv, &cipher2, &mut recovered2);
5403 println!("B1 (0 rounds) =\t{}", recovered1);
5404 println!("B2 (0 rounds) =\t{}", recovered2);
5405 assert_eq!(recovered1, "In the beginning God created the heavens and the earth.");
5406 assert_eq!(recovered2, "In the beginning God created the heavens and the earth.");
5407 assert_eq!(recovered1, message);
5408 assert_eq!(recovered2, message);
5409 assert_eq!(recovered1, recovered2);
5410 println!();
5411
5412 // Normal case for the message of 0 bytes
5413 let key = 0x_1234567890ABCDEF_u64;
5414 println!("K =\t{:#016X}", key);
5415 let mut a_des = DES::new_with_key_u64(key);
5416
5417 let message = "";
5418 println!("M =\t{}", message);
5419 let iv = 0x_FEDCBA0987654321_u64;
5420 println!("IV = {}", iv);
5421 let mut cipher = Vec::<u8>::new();
5422 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5423 print!("C =\t");
5424 for c in cipher.clone()
5425 { print!("{:02X} ", c); }
5426 println!();
5427 let mut txt = String::new();
5428 for c in cipher.clone()
5429 { write!(txt, "{:02X} ", c); }
5430 assert_eq!(txt, "");
5431
5432 let mut recovered = String::new();
5433 a_des.decrypt_vec_into_string(iv, &cipher, &mut recovered);
5434 println!("B =\t{}", recovered);
5435 assert_eq!(recovered, "");
5436 assert_eq!(recovered, message);
5437 println!();
5438
5439 // Normal case for the message shorter than 8 bytes
5440 let key = 0x_1234567890ABCDEF_u64;
5441 println!("K =\t{:#016X}", key);
5442 let mut a_des = DES::new_with_key_u64(key);
5443
5444 let message = "7 bytes";
5445 println!("M =\t{}", message);
5446 let iv = 0x_FEDCBA0987654321_u64;
5447 println!("IV = {}", iv);
5448 let mut cipher = Vec::<u8>::new();
5449 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5450 print!("C =\t");
5451 for c in cipher.clone()
5452 { print!("{:02X} ", c); }
5453 println!();
5454 let mut txt = String::new();
5455 for c in cipher.clone()
5456 { write!(txt, "{:02X} ", c); }
5457 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
5458
5459 let mut recovered = String::new();
5460 a_des.decrypt_vec_into_string(iv, &cipher, &mut recovered);
5461 println!("B =\t{}", recovered);
5462 assert_eq!(recovered, "7 bytes");
5463 assert_eq!(recovered, message);
5464 println!();
5465
5466 // Normal case for the message of 8 bytes
5467 let key = 0x_1234567890ABCDEF_u64;
5468 println!("K =\t{:#016X}", key);
5469 let mut a_des = DES::new_with_key_u64(key);
5470
5471 let message = "I am OK.";
5472 println!("M =\t{}", message);
5473 let iv = 0x_FEDCBA0987654321_u64;
5474 println!("IV = {}", iv);
5475 let mut cipher = Vec::<u8>::new();
5476 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5477 print!("C =\t");
5478 for c in cipher.clone()
5479 { print!("{:02X} ", c); }
5480 println!();
5481 let mut txt = String::new();
5482 for c in cipher.clone()
5483 { write!(txt, "{:02X} ", c); }
5484 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
5485
5486 let mut recovered = String::new();
5487 a_des.decrypt_vec_into_string(iv, &cipher, &mut recovered);
5488 println!("B =\t{}", recovered);
5489 assert_eq!(recovered, "I am OK.");
5490 assert_eq!(recovered, message);
5491 println!();
5492
5493 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
5494 let key = 0x_1234567890ABCDEF_u64;
5495 println!("K =\t{:#016X}", key);
5496 let mut a_des = DES::new_with_key_u64(key);
5497
5498 let message = "PARK Youngho";
5499 println!("M =\t{}", message);
5500 let iv = 0x_FEDCBA0987654321_u64;
5501 println!("IV = {}", iv);
5502 let mut cipher = Vec::<u8>::new();
5503 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5504 print!("C =\t");
5505 for c in cipher.clone()
5506 { print!("{:02X} ", c); }
5507 println!();
5508 let mut txt = String::new();
5509 for c in cipher.clone()
5510 { write!(txt, "{:02X} ", c); }
5511 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
5512
5513 let mut recovered = String::new();
5514 a_des.decrypt_vec_into_string(iv, &cipher, &mut recovered);
5515 println!("B =\t{}", recovered);
5516 assert_eq!(recovered, "PARK Youngho");
5517 assert_eq!(recovered, message);
5518 println!();
5519
5520 // Normal case for the message of 16 bytes
5521 let key = 0x_1234567890ABCDEF_u64;
5522 println!("K =\t{:#016X}", key);
5523 let mut a_des = DES::new_with_key_u64(key);
5524
5525 let message = "고맙습니다.";
5526 println!("M =\t{}", message);
5527 let iv = 0x_FEDCBA0987654321_u64;
5528 println!("IV = {}", iv);
5529 let mut cipher = Vec::<u8>::new();
5530 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5531 print!("C =\t");
5532 for c in cipher.clone()
5533 { print!("{:02X} ", c); }
5534 println!();
5535 let mut txt = String::new();
5536 for c in cipher.clone()
5537 { write!(txt, "{:02X} ", c); }
5538 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
5539
5540 let mut recovered = String::new();
5541 a_des.decrypt_vec_into_string(iv, &cipher, &mut recovered);
5542 println!("B =\t{}", recovered);
5543 assert_eq!(recovered, "고맙습니다.");
5544 assert_eq!(recovered, message);
5545 println!("-------------------------------");
5546}Sourcefn encrypt_str_into_array<U, const N: usize>(
&mut self,
iv: T,
message: &str,
cipher: &mut [U; N],
) -> u64
fn encrypt_str_into_array<U, const N: usize>( &mut self, iv: T, message: &str, cipher: &mut [U; N], ) -> u64
Encrypts the data in a str object without any padding in OFB (Output
FeedBack) mode, and stores the encrypted data in array [U; N].
§Arguments
ivis an initialization vector for OFB mode.messageis an immutable reference tostrobject which is&str, and is the place where the plaintext to be encrypted is stored.cipheris a mutable reference to an array[U; N]object, and is the place where the encrypted data will be stored.
§Output
- This method returns the size of ciphertext.
- If this method failed in encryption or
message.len()is0, this method returnszero.
§Features
- If
messageis a null string “”, nothing will be encrypted, and stored in the array[U; N]objectcipher. - If
U::size_in_bytes()*Nis less thanmessage.len(), this method does not perform encryption but returnszero. - If
U::size_in_bytes()*Nis equal tomessage.len(), this method performs encryption, fills the arraycipherwith the encrypted data, and returns the size of the ciphertext in bytes. - If
U::size_in_bytes()*Nis greater thanmessage.len(), this method performs encryption, fills the arraycipherwith the encrypted data, and then fills the rest of the elements of the arraycipherwith zeros, and returns the size of the ciphertext in bytes. - The size of the area for ciphertext should be prepared to be
message.len()at least. So, it is responsible for you to prepare thecipherarea big enough!
§For Rijndael or AES, and its variants
§Example 1 for AES-128
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ AES_128, CBC_ISO };
let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
println!("K =\t{:#016X}", key);
let mut a_aes = AES_128::new_with_key_u128(key);
let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = [0_u8; 55];
a_aes.encrypt_str_into_array(iv, &message, &mut cipher);
print!("C =\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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 07 AF 4E C8 EF 5D 0A 74 97 3F 90 9E 05 9E 9E 32 FB 55 54 45 7C ED A2 2B F8 07 66 C0 7B CB 98 F3 BF 93 15 BA 26 1C 47 ");§For more examples,
click here
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, OFB };
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 iv = 0x_FEDCBA0987654321_u64;
println!("IV = {}", iv);
let mut cipher = [0_u8; 55];
a_des.encrypt_str_into_array(iv, &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, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");§For more examples,
click here
§For BigCryptor128
§Example 1 for TAES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
+ AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
+ AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
println!("IV = {:#034X}", iv);
let message = "In the beginning God created the heavens and the earth.";
let mut cipher = [0_u8; 55];
taes.encrypt_str_into_array(iv, &message, &mut cipher);
print!("C =\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, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");§For more examples,
click here
§For BigCryptor64
§Example 1 for TDES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
+ DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
+ DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
let iv = 0x_FEDCBA0987654321_u64;
println!("IV = {:#018X}", iv);
let message = "In the beginning God created the heavens and the earth.";
let mut cipher = [0_u8; 55];
tdes.encrypt_str_into_array(iv, &message, &mut cipher);
print!("C =\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, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");§For more examples,
click here
Examples found in repository?
193fn bigcryptor64_encrypt_str_ofb_into_array()
194{
195 println!("bigcryptor64_encrypt_str_ofb_into_array()");
196 use std::io::Write;
197 use std::fmt::Write as _;
198 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
199
200 // TDES case
201 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
202 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
203 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
204 let iv = 0x_FEDCBA0987654321_u64;
205 println!("IV = {:#018X}", iv);
206 let message = "In the beginning God created the heavens and the earth.";
207 let mut cipher = [0_u8; 55];
208 tdes.encrypt_str_into_array(iv, &message, &mut cipher);
209 print!("C =\t");
210 for c in cipher.clone()
211 { print!("{:02X} ", c); }
212 println!();
213 let mut txt = String::new();
214 for c in cipher.clone()
215 { write!(txt, "{:02X} ", c); }
216 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
217 println!("-------------------------------");
218}More examples
193fn bigcryptor128_encrypt_str_ofb_into_array()
194{
195 println!("bigcryptor128_encrypt_str_ofb_into_array()");
196 use std::io::Write;
197 use std::fmt::Write as _;
198 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
199
200 // TAES_128 case
201 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
202 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
203 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
204 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
205 println!("IV = {:#034X}", iv);
206 let message = "In the beginning God created the heavens and the earth.";
207 let mut cipher = [0_u8; 55];
208 taes.encrypt_str_into_array(iv, &message, &mut cipher);
209 print!("C =\t");
210 for c in cipher.clone()
211 { print!("{:02X} ", c); }
212 println!();
213 let mut txt = String::new();
214 for c in cipher.clone()
215 { write!(txt, "{:02X} ", c); }
216 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
217 println!("-------------------------------");
218}
219
220fn bigcryptor128_encrypt_string_ofb()
221{
222 println!("bigcryptor128_encrypt_string_ofb()");
223 use std::io::Write;
224 use std::fmt::Write as _;
225 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
226
227 // TAES_128 case
228 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
229 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
230 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
231 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
232 println!("IV = {:#034X}", iv);
233 let message = "In the beginning God created the heavens and the earth.".to_string();
234 let mut cipher = [0_u8; 55];
235 taes.encrypt_string(iv, &message, cipher.as_mut_ptr());
236 print!("C =\t");
237 for c in cipher.clone()
238 { print!("{:02X} ", c); }
239 println!();
240 let mut txt = String::new();
241 for c in cipher.clone()
242 { write!(txt, "{:02X} ", c); }
243 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
244 println!("-------------------------------");
245}
246
247fn bigcryptor128_encrypt_string_ofb_into_vec()
248{
249 println!("bigcryptor128_encrypt_string_ofb_into_vec()");
250 use std::io::Write;
251 use std::fmt::Write as _;
252 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
253
254 // TAES_128 case
255 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
256 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
257 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
258 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
259 println!("IV = {:#034X}", iv);
260 let message = "In the beginning God created the heavens and the earth.".to_string();
261 let mut cipher = Vec::<u8>::new();
262 taes.encrypt_string_into_vec(iv, &message, &mut cipher);
263 print!("C =\t");
264 for c in cipher.clone()
265 { print!("{:02X} ", c); }
266 println!();
267 let mut txt = String::new();
268 for c in cipher.clone()
269 { write!(txt, "{:02X} ", c); }
270 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
271 println!("-------------------------------");
272}
273
274fn bigcryptor128_encrypt_string_ofb_into_array()
275{
276 println!("bigcryptor128_encrypt_string_ofb_into_array()");
277 use std::io::Write;
278 use std::fmt::Write as _;
279 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
280
281 // TAES_128 case
282 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
283 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
284 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
285 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
286 println!("IV = {:#034X}", iv);
287 let message = "In the beginning God created the heavens and the earth.".to_string();
288 let mut cipher = [0_u8; 55];
289 taes.encrypt_string_into_array(iv, &message, &mut cipher);
290 print!("C =\t");
291 for c in cipher.clone()
292 { print!("{:02X} ", c); }
293 println!();
294 let mut txt = String::new();
295 for c in cipher.clone()
296 { write!(txt, "{:02X} ", c); }
297 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
298 println!("-------------------------------");
299}
300
301fn bigcryptor128_encrypt_vec_ofb()
302{
303 println!("bigcryptor128_encrypt_vec_ofb()");
304 use std::io::Write;
305 use std::fmt::Write as _;
306 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
307
308 // TAES_128 case
309 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
310 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
311 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
312 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
313 println!("IV = {:#034X}", iv);
314 let message = "In the beginning God created the heavens and the earth.";
315 println!("M =\t{}", message);
316 let message = unsafe { message.to_string().as_mut_vec().clone() };
317 let mut cipher = [0_u8; 55];
318 taes.encrypt_vec(iv, &message, cipher.as_mut_ptr());
319 print!("C =\t");
320 for c in cipher.clone()
321 { print!("{:02X} ", c); }
322 println!();
323 let mut txt = String::new();
324 for c in cipher.clone()
325 { write!(txt, "{:02X} ", c); }
326 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
327 println!("-------------------------------");
328}
329
330fn bigcryptor128_encrypt_vec_ofb_into_vec()
331{
332 println!("bigcryptor128_encrypt_vec_ofb_into_vec()");
333 use std::io::Write;
334 use std::fmt::Write as _;
335 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
336
337 // TAES_128 case
338 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
339 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
340 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
341 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
342 println!("IV = {:#034X}", iv);
343 let message = "In the beginning God created the heavens and the earth.";
344 println!("M =\t{}", message);
345 let message = unsafe { message.to_string().as_mut_vec().clone() };
346 let mut cipher = Vec::<u8>::new();
347 taes.encrypt_vec_into_vec(iv, &message, &mut cipher);
348 print!("C =\t");
349 for c in cipher.clone()
350 { print!("{:02X} ", c); }
351 println!();
352 let mut txt = String::new();
353 for c in cipher.clone()
354 { write!(txt, "{:02X} ", c); }
355 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
356 println!("-------------------------------");
357}
358
359fn bigcryptor128_encrypt_vec_ofb_into_array()
360{
361 println!("bigcryptor128_encrypt_vec_ofb_into_array()");
362 use std::io::Write;
363 use std::fmt::Write as _;
364 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
365
366 // TAES_128 case
367 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
368 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
369 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
370 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
371 println!("IV = {:#034X}", iv);
372 let message = "In the beginning God created the heavens and the earth.";
373 println!("M =\t{}", message);
374 let message = unsafe { message.to_string().as_mut_vec().clone() };
375 let mut cipher = [0_u8; 55];
376 taes.encrypt_vec_into_array(iv, &message, &mut cipher);
377 print!("C =\t");
378 for c in cipher.clone()
379 { print!("{:02X} ", c); }
380 println!();
381 let mut txt = String::new();
382 for c in cipher.clone()
383 { write!(txt, "{:02X} ", c); }
384 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
385 println!("-------------------------------");
386}
387
388fn bigcryptor128_encrypt_array_ofb()
389{
390 println!("bigcryptor128_encrypt_array_ofb()");
391 use std::io::Write;
392 use std::fmt::Write as _;
393 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
394
395 // TAES_128 case
396 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
397 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
398 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
399 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
400 println!("IV = {:#034X}", iv);
401 let mes = "In the beginning God created the heavens and the earth.";
402 println!("M =\t{}", mes);
403 let mut message = [0_u8; 55];
404 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
405 let mut cipher = [0_u8; 55];
406 taes.encrypt_array(iv, &message, cipher.as_mut_ptr());
407 print!("C =\t");
408 for c in cipher.clone()
409 { print!("{:02X} ", c); }
410 println!();
411 let mut txt = String::new();
412 for c in cipher.clone()
413 { write!(txt, "{:02X} ", c); }
414 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
415 println!("-------------------------------");
416}
417
418fn bigcryptor128_encrypt_array_ofb_into_vec()
419{
420 println!("bigcryptor128_encrypt_array_ofb_into_vec()");
421 use std::io::Write;
422 use std::fmt::Write as _;
423 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
424
425 // TAES_128 case
426 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
427 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
428 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
429 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
430 println!("IV = {:#034X}", iv);
431 let mes = "In the beginning God created the heavens and the earth.";
432 println!("M =\t{}", mes);
433 let mut message = [0_u8; 55];
434 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
435 let mut cipher = Vec::<u8>::new();
436 taes.encrypt_array_into_vec(iv, &message, &mut cipher);
437 print!("C =\t");
438 for c in cipher.clone()
439 { print!("{:02X} ", c); }
440 println!();
441 let mut txt = String::new();
442 for c in cipher.clone()
443 { write!(txt, "{:02X} ", c); }
444 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
445 println!("-------------------------------");
446}
447
448fn bigcryptor128_encrypt_array_ofb_into_array()
449{
450 println!("bigcryptor128_encrypt_array_ofb_into_array()");
451 use std::io::Write;
452 use std::fmt::Write as _;
453 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
454
455 // TAES_128 case
456 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
457 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
458 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
459 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
460 println!("IV = {:#034X}", iv);
461 let mes = "In the beginning God created the heavens and the earth.";
462 println!("M =\t{}", mes);
463 let mut message = [0_u8; 55];
464 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
465 let mut cipher = [0_u8; 55];
466 taes.encrypt_array_into_array(iv, &message, &mut cipher);
467 for c in cipher.clone()
468 { print!("{:02X} ", c); }
469 println!();
470 let mut txt = String::new();
471 for c in cipher.clone()
472 { write!(txt, "{:02X} ", c); }
473 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
474 println!("-------------------------------");
475}
476
477fn bigcryptor128_decrypt_ofb()
478{
479 println!("bigcryptor128_decrypt_ofb()");
480 use std::io::Write;
481 use std::fmt::Write as _;
482 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
483
484 // TAES_128 case
485 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
486 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
487 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
488 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
489 println!("IV = {:#034X}", iv);
490 let message = "In the beginning God created the heavens and the earth.";
491 println!("M =\t{}", message);
492 let mut cipher = Vec::<u8>::new();
493 taes.encrypt_str_into_vec(iv, &message, &mut cipher);
494 print!("C =\t");
495 for c in cipher.clone()
496 { print!("{:02X} ", c); }
497 println!();
498 let mut txt = String::new();
499 for c in cipher.clone()
500 { write!(txt, "{:02X} ", c); }
501 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
502
503 let mut recovered = vec![0; 55];
504 taes.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
505 print!("Ba =\t");
506 for b in recovered.clone()
507 { print!("{:02X} ", b); }
508 println!();
509 let mut txt = String::new();
510 for c in recovered.clone()
511 { write!(txt, "{:02X} ", c); }
512 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 ");
513
514 let mut converted = String::new();
515 unsafe { converted.as_mut_vec() }.append(&mut recovered);
516
517 println!("Bb =\t{}", converted);
518 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
519 assert_eq!(converted, message);
520 println!("-------------------------------");
521}
522
523fn bigcryptor128_decrypt_ofb_into_vec()
524{
525 println!("bigcryptor128_decrypt_ofb_into_vec()");
526 use std::io::Write;
527 use std::fmt::Write as _;
528 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
529
530 // TAES_128 case
531 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
532 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
533 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
534 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
535 println!("IV = {:#034X}", iv);
536 let message = "In the beginning God created the heavens and the earth.";
537 println!("M =\t{}", message);
538 let mut cipher = Vec::<u8>::new();
539 taes.encrypt_str_into_vec(iv, &message, &mut cipher);
540 print!("C =\t");
541 for c in cipher.clone()
542 { print!("{:02X} ", c); }
543 println!();
544 let mut txt = String::new();
545 for c in cipher.clone()
546 { write!(txt, "{:02X} ", c); }
547 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
548
549 let mut recovered = Vec::<u8>::new();
550 taes.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
551 print!("Ba =\t");
552 for b in recovered.clone()
553 { print!("{:02X} ", b); }
554 println!();
555 let mut txt = String::new();
556 for c in recovered.clone()
557 { write!(txt, "{:02X} ", c); }
558 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 ");
559
560 let mut converted = String::new();
561 unsafe { converted.as_mut_vec() }.append(&mut recovered);
562
563 println!("Bb =\t{}", converted);
564 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
565 assert_eq!(converted, message);
566 println!("-------------------------------");
567}
568
569fn bigcryptor128_decrypt_ofb_into_array()
570{
571 println!("bigcryptor128_decrypt_ofb_into_array()");
572 use std::io::Write;
573 use std::fmt::Write as _;
574 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
575
576 // TAES_128 case
577 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
578 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
579 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
580 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
581 println!("IV = {:#034X}", iv);
582 let message = "In the beginning God created the heavens and the earth.";
583 println!("M =\t{}", message);
584 let mut cipher = Vec::<u8>::new();
585 taes.encrypt_str_into_vec(iv, &message, &mut cipher);
586 print!("C =\t");
587 for c in cipher.clone()
588 { print!("{:02X} ", c); }
589 println!();
590 let mut txt = String::new();
591 for c in cipher.clone()
592 { write!(txt, "{:02X} ", c); }
593 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
594
595 let mut recovered = [0u8; 56];
596 let len = taes.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
597 print!("Ba =\t");
598 for b in recovered.clone()
599 { print!("{:02X} ", b); }
600 println!();
601 let mut txt = String::new();
602 for c in recovered.clone()
603 { write!(txt, "{:02X} ", c); }
604 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 ");
605
606 let mut converted = String::new();
607 unsafe { converted.as_mut_vec() }.write(&recovered);
608 unsafe { converted.as_mut_vec() }.truncate(len as usize);
609 println!("Bb =\t{}", converted);
610 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
611 assert_eq!(converted, message);
612 println!("-------------------------------");
613}
614
615fn bigcryptor128_decrypt_ofb_into_string()
616{
617 println!("bigcryptor128_decrypt_ofb_into_string()");
618 use std::io::Write;
619 use std::fmt::Write as _;
620 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
621
622 // TAES_128 case
623 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
624 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
625 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
626 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
627 println!("IV = {:#034X}", iv);
628 let message = "In the beginning God created the heavens and the earth.";
629 println!("M =\t{}", message);
630 let mut cipher = Vec::<u8>::new();
631 taes.encrypt_str_into_vec(iv, &message, &mut cipher);
632 print!("C =\t");
633 for c in cipher.clone()
634 { print!("{:02X} ", c); }
635 println!();
636 let mut txt = String::new();
637 for c in cipher.clone()
638 { write!(txt, "{:02X} ", c); }
639 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
640
641 let mut recovered = String::new();
642 taes.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
643 println!("B =\t{}", recovered);
644 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
645 assert_eq!(recovered, message);
646 println!("-------------------------------");
647}
648
649fn bigcryptor128_decrypt_vec_ofb()
650{
651 println!("bigcryptor128_decrypt_vec_ofb()");
652 use std::io::Write;
653 use std::fmt::Write as _;
654 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
655
656 // TAES_128 case
657 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
658 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
659 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
660 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
661 println!("IV = {:#034X}", iv);
662 let message = "In the beginning God created the heavens and the earth.";
663 println!("M =\t{}", message);
664 let mut cipher = Vec::<u8>::new();
665 taes.encrypt_str_into_vec(iv, &message, &mut cipher);
666 print!("C =\t");
667 for c in cipher.clone()
668 { print!("{:02X} ", c); }
669 println!();
670 let mut txt = String::new();
671 for c in cipher.clone()
672 { write!(txt, "{:02X} ", c); }
673 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
674
675 let mut recovered = vec![0; 55];
676 taes.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
677 print!("Ba =\t");
678 for b in recovered.clone()
679 { print!("{:02X} ", b); }
680 println!();
681 let mut txt = String::new();
682 for c in recovered.clone()
683 { write!(txt, "{:02X} ", c); }
684 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 ");
685
686 let mut converted = String::new();
687 unsafe { converted.as_mut_vec() }.append(&mut recovered);
688
689 println!("Bb =\t{}", converted);
690 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
691 assert_eq!(converted, message);
692 println!("-------------------------------");
693}
694
695fn bigcryptor128_decrypt_vec_ofb_into_vec()
696{
697 println!("bigcryptor128_decrypt_vec_ofb_into_vec()");
698 use std::io::Write;
699 use std::fmt::Write as _;
700 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
701
702 // TAES_128 case
703 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
704 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
705 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
706 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
707 println!("IV = {:#034X}", iv);
708 let message = "In the beginning God created the heavens and the earth.";
709 println!("M =\t{}", message);
710 let mut cipher = Vec::<u8>::new();
711 taes.encrypt_str_into_vec(iv, &message, &mut cipher);
712 print!("C =\t");
713 for c in cipher.clone()
714 { print!("{:02X} ", c); }
715 println!();
716 let mut txt = String::new();
717 for c in cipher.clone()
718 { write!(txt, "{:02X} ", c); }
719 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
720
721 let mut recovered = Vec::<u8>::new();
722 taes.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
723 print!("Ba =\t");
724 for b in recovered.clone()
725 { print!("{:02X} ", b); }
726 println!();
727 let mut txt = String::new();
728 for c in recovered.clone()
729 { write!(txt, "{:02X} ", c); }
730 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 ");
731
732 let mut converted = String::new();
733 unsafe { converted.as_mut_vec() }.append(&mut recovered);
734
735 println!("Bb =\t{}", converted);
736 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
737 assert_eq!(converted, message);
738 println!("-------------------------------");
739}
740
741fn bigcryptor128_decrypt_vec_ofb_into_array()
742{
743 println!("bigcryptor128_decrypt_vec_ofb_into_array()");
744 use std::io::Write;
745 use std::fmt::Write as _;
746 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
747
748 // TAES_128 case
749 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
750 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
751 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
752 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
753 println!("IV = {:#034X}", iv);
754 let message = "In the beginning God created the heavens and the earth.";
755 println!("M =\t{}", message);
756 let mut cipher = Vec::<u8>::new();
757 taes.encrypt_str_into_vec(iv, &message, &mut cipher);
758 print!("C =\t");
759 for c in cipher.clone()
760 { print!("{:02X} ", c); }
761 println!();
762 let mut txt = String::new();
763 for c in cipher.clone()
764 { write!(txt, "{:02X} ", c); }
765 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
766
767 let mut recovered = [0u8; 56];
768 let len = taes.decrypt_vec_into_array(iv, &cipher, &mut recovered);
769 print!("Ba =\t");
770 for b in recovered.clone()
771 { print!("{:02X} ", b); }
772 println!();
773 let mut txt = String::new();
774 for c in recovered.clone()
775 { write!(txt, "{:02X} ", c); }
776 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 ");
777
778 let mut converted = String::new();
779 unsafe { converted.as_mut_vec() }.write(&recovered);
780 unsafe { converted.as_mut_vec() }.truncate(len as usize);
781 println!("Bb =\t{}", converted);
782 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
783 assert_eq!(converted, message);
784 println!("-------------------------------");
785}
786
787fn bigcryptor128_decrypt_vec_ofb_into_string()
788{
789 println!("bigcryptor128_decrypt_vec_ofb_into_string()");
790 use std::io::Write;
791 use std::fmt::Write as _;
792 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
793
794 // TAES_128 case
795 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
796 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
797 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
798 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
799 println!("IV = {:#034X}", iv);
800 let message = "In the beginning God created the heavens and the earth.";
801 println!("M =\t{}", message);
802 let mut cipher = Vec::<u8>::new();
803 taes.encrypt_str_into_vec(iv, &message, &mut cipher);
804 print!("C =\t");
805 for c in cipher.clone()
806 { print!("{:02X} ", c); }
807 println!();
808 let mut txt = String::new();
809 for c in cipher.clone()
810 { write!(txt, "{:02X} ", c); }
811 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
812
813 let mut recovered = String::new();
814 taes.decrypt_vec_into_string(iv, &cipher, &mut recovered);
815 println!("B =\t{}", recovered);
816 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
817 assert_eq!(recovered, message);
818 println!("-------------------------------");
819}
820
821fn bigcryptor128_decrypt_array_ofb()
822{
823 println!("bigcryptor128_decrypt_array_ofb()");
824 use std::io::Write;
825 use std::fmt::Write as _;
826 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
827
828 // TAES_128 case
829 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
830 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
831 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
832 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
833 println!("IV = {:#034X}", iv);
834 let message = "In the beginning God created the heavens and the earth.";
835 println!("M =\t{}", message); let mut cipher = [0_u8; 55];
836 taes.encrypt_str_into_array(iv, &message, &mut cipher);
837 print!("C =\t");
838 for c in cipher.clone()
839 { print!("{:02X} ", c); }
840 println!();
841 let mut txt = String::new();
842 for c in cipher.clone()
843 { write!(txt, "{:02X} ", c); }
844 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
845
846 let mut recovered = vec![0; 55];
847 let len = taes.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
848 recovered.truncate(len as usize);
849 print!("Ba =\t");
850 for b in recovered.clone()
851 { print!("{:02X} ", b); }
852 println!();
853 let mut txt = String::new();
854 for c in recovered.clone()
855 { write!(txt, "{:02X} ", c); }
856 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 ");
857
858 let mut converted = String::new();
859 unsafe { converted.as_mut_vec() }.append(&mut recovered);
860
861 println!("Bb =\t{}", converted);
862 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
863 assert_eq!(converted, message);
864 println!("-------------------------------");
865}
866
867fn bigcryptor128_decrypt_array_ofb_into_vec()
868{
869 println!("bigcryptor128_decrypt_array_ofb_into_vec()");
870 use std::io::Write;
871 use std::fmt::Write as _;
872 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
873
874 // TAES_128 case
875 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
876 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
877 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
878 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
879 println!("IV = {:#034X}", iv);
880 let message = "In the beginning God created the heavens and the earth.";
881 println!("M =\t{}", message); let mut cipher = [0_u8; 55];
882 taes.encrypt_str_into_array(iv, &message, &mut cipher);
883 print!("C =\t");
884 for c in cipher.clone()
885 { print!("{:02X} ", c); }
886 println!();
887 let mut txt = String::new();
888 for c in cipher.clone()
889 { write!(txt, "{:02X} ", c); }
890 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
891
892 let mut recovered = Vec::<u8>::new();
893 taes.decrypt_array_into_vec(iv, &cipher, &mut recovered);
894 print!("Ba =\t");
895 for b in recovered.clone()
896 { print!("{:02X} ", b); }
897 println!();
898 let mut txt = String::new();
899 for c in recovered.clone()
900 { write!(txt, "{:02X} ", c); }
901 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 ");
902
903 let mut converted = String::new();
904 unsafe { converted.as_mut_vec() }.append(&mut recovered);
905
906 println!("Bb =\t{}", converted);
907 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
908 assert_eq!(converted, message);
909 println!("-------------------------------");
910}
911
912fn bigcryptor128_decrypt_array_ofb_into_array()
913{
914 println!("bigcryptor128_decrypt_array_ofb_into_array()");
915 use std::io::Write;
916 use std::fmt::Write as _;
917 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
918
919 // TAES_128 case
920 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
921 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
922 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
923 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
924 println!("IV = {:#034X}", iv);
925 let message = "In the beginning God created the heavens and the earth.";
926 println!("M =\t{}", message); let mut cipher = [0_u8; 55];
927 taes.encrypt_str_into_array(iv, &message, &mut cipher);
928 print!("C =\t");
929 for c in cipher.clone()
930 { print!("{:02X} ", c); }
931 println!();
932 let mut txt = String::new();
933 for c in cipher.clone()
934 { write!(txt, "{:02X} ", c); }
935 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
936
937 let mut recovered = [0u8; 56];
938 let len = taes.decrypt_array_into_array(iv, &cipher, &mut recovered);
939 print!("Ba =\t");
940 for b in recovered.clone()
941 { print!("{:02X} ", b); }
942 println!();
943 let mut txt = String::new();
944 for c in recovered.clone()
945 { write!(txt, "{:02X} ", c); }
946 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 ");
947
948 let mut converted = String::new();
949 unsafe { converted.as_mut_vec() }.write(&recovered);
950 unsafe { converted.as_mut_vec() }.truncate(len as usize);
951 println!("Bb =\t{}", converted);
952 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
953 assert_eq!(converted, message);
954 println!("-------------------------------");
955}
956
957fn bigcryptor128_decrypt_array_ofb_into_string()
958{
959 println!("bigcryptor128_decrypt_array_ofb_into_string()");
960 use std::io::Write;
961 use std::fmt::Write as _;
962 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
963
964 // TAES_128 case
965 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
966 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
967 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
968 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
969 println!("IV = {:#034X}", iv);
970 let message = "In the beginning God created the heavens and the earth.";
971 println!("M =\t{}", message); let mut cipher = [0_u8; 55];
972 taes.encrypt_str_into_array(iv, &message, &mut cipher);
973 print!("C =\t");
974 for c in cipher.clone()
975 { print!("{:02X} ", c); }
976 println!();
977 let mut txt = String::new();
978 for c in cipher.clone()
979 { write!(txt, "{:02X} ", c); }
980 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
981
982 let mut recovered = String::new();
983 taes.decrypt_array_into_string(iv, &cipher, &mut recovered);
984 println!("B =\t{}", recovered);
985 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
986 assert_eq!(recovered, message);
987 println!("-------------------------------");
988}719fn aes_encrypt_str_ofb_into_array()
720{
721 println!("aes_encrypt_str_ofb_into_array()");
722 use std::io::Write;
723 use std::fmt::Write as _;
724 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
725
726 // Normal case for AES-128
727 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
728 println!("K =\t{:#016X}", key);
729 let mut a_aes = AES_128::new_with_key_u128(key);
730 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
731 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
732
733 let message = "In the beginning God created the heavens and the earth.";
734 println!("M =\t{}", message);
735 let mut cipher = [0_u8; 55];
736 a_aes.encrypt_str_into_array(iv, &message, &mut cipher);
737 print!("C =\t");
738 for c in cipher.clone()
739 { print!("{:02X} ", c); }
740 println!();
741 let mut txt = String::new();
742 for c in cipher.clone()
743 { write!(txt, "{:02X} ", c); }
744 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
745 println!();
746
747 // Normal case for AES-192
748 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
749 print!("K =\t");
750 for i in 0..24
751 { print!("{:02X}", key[i]); }
752 println!();
753 let mut a_aes = AES_192::new_with_key(&key);
754 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
755 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
756
757 let message = "In the beginning God created the heavens and the earth.";
758 println!("M =\t{}", message);
759 let mut cipher = [0_u8; 55];
760 a_aes.encrypt_str_into_array(iv, &message, &mut cipher);
761 print!("C =\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, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
769 println!();
770
771 // Normal case for AES-256
772 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
773 print!("K =\t");
774 for i in 0..32
775 { print!("{:02X}", key[i]); }
776 println!();
777 let mut a_aes = AES_256::new_with_key(&key);
778 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
779 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
780
781 let message = "In the beginning God created the heavens and the earth.";
782 println!("M =\t{}", message);
783 let mut cipher = [0_u8; 55];
784 a_aes.encrypt_str_into_array(iv, &message, &mut cipher);
785 print!("C =\t");
786 for c in cipher.clone()
787 { print!("{:02X} ", c); }
788 println!();
789 let mut txt = String::new();
790 for c in cipher.clone()
791 { write!(txt, "{:02X} ", c); }
792 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
793 println!();
794
795 // Normal case for Rijndael-256-256
796 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
797 print!("K =\t");
798 for i in 0..32
799 { print!("{:02X}", key[i]); }
800 println!();
801 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
802 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
803 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
804
805 let message = "In the beginning God created the heavens and the earth.";
806 println!("M =\t{}", message);
807 let mut cipher = [0_u8; 55];
808 a_rijndael.encrypt_str_into_array(iv, &message, &mut cipher);
809 print!("C =\t");
810 for c in cipher.clone()
811 { print!("{:02X} ", c); }
812 println!();
813 let mut txt = String::new();
814 for c in cipher.clone()
815 { write!(txt, "{:02X} ", c); }
816 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
817 println!();
818
819 // Normal case for Rijndael-512-512 for post-quantum
820 use cryptocol::number::SharedArrays;
821 use cryptocol::hash::SHA3_512;
822 let mut sha3 = SHA3_512::new();
823 sha3.absorb_str("Post-quantum");
824 let key: [u8; 64] = sha3.get_hash_value_in_array();
825 print!("K =\t");
826 for i in 0..64
827 { print!("{:02X}", key[i]); }
828 println!();
829 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
830 sha3.absorb_str("Initialize");
831 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
832 iv.src = sha3.get_hash_value_in_array();
833 let iv = unsafe { iv.des };
834 print!("IV =\t");
835 for i in 0..16
836 { print!("{:08X}", iv[i].to_be()); }
837 println!();
838 let message = "In the beginning God created the heavens and the earth.";
839 println!("M =\t{}", message);
840 let mut cipher = [0_u8; 55];
841 a_rijndael.encrypt_str_into_array(iv, &message, &mut cipher);
842 print!("C =\t");
843 for c in cipher.clone()
844 { print!("{:02X} ", c); }
845 println!();
846 let mut txt = String::new();
847 for c in cipher.clone()
848 { write!(txt, "{:02X} ", c); }
849 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
850 println!("-------------------------------");
851}
852
853fn aes_encrypt_string_ofb()
854{
855 println!("aes_encrypt_string_ofb()");
856 use std::io::Write;
857 use std::fmt::Write as _;
858 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
859
860 // Normal case for AES-128
861 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
862 println!("K =\t{:#016X}", key);
863 let mut a_aes = AES_128::new_with_key_u128(key);
864 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
865 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
866
867 let message = "In the beginning God created the heavens and the earth.".to_string();
868 println!("M =\t{}", message);
869 let mut cipher = [0_u8; 55];
870 a_aes.encrypt_string(iv, &message, cipher.as_mut_ptr());
871 print!("C =\t");
872 for c in cipher.clone()
873 { print!("{:02X} ", c); }
874 println!();
875 let mut txt = String::new();
876 for c in cipher.clone()
877 { write!(txt, "{:02X} ", c); }
878 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
879 println!();
880
881 // Normal case for AES-192
882 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
883 print!("K =\t");
884 for i in 0..24
885 { print!("{:02X}", key[i]); }
886 println!();
887 let mut a_aes = AES_192::new_with_key(&key);
888 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
889 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
890
891 let message = "In the beginning God created the heavens and the earth.".to_string();
892 println!("M =\t{}", message);
893 let mut cipher = [0_u8; 55];
894 a_aes.encrypt_string(iv, &message, cipher.as_mut_ptr());
895 print!("C =\t");
896 for c in cipher.clone()
897 { print!("{:02X} ", c); }
898 println!();
899 let mut txt = String::new();
900 for c in cipher.clone()
901 { write!(txt, "{:02X} ", c); }
902 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
903 println!();
904
905 // Normal case for AES-256
906 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
907 print!("K =\t");
908 for i in 0..32
909 { print!("{:02X}", key[i]); }
910 println!();
911 let mut a_aes = AES_256::new_with_key(&key);
912 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
913 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
914
915 let message = "In the beginning God created the heavens and the earth.".to_string();
916 println!("M =\t{}", message);
917 let mut cipher = [0_u8; 55];
918 a_aes.encrypt_string(iv, &message, cipher.as_mut_ptr());
919 print!("C =\t");
920 for c in cipher.clone()
921 { print!("{:02X} ", c); }
922 println!();
923 let mut txt = String::new();
924 for c in cipher.clone()
925 { write!(txt, "{:02X} ", c); }
926 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
927 println!();
928
929 // Normal case for Rijndael-256-256
930 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
931 print!("K =\t");
932 for i in 0..32
933 { print!("{:02X}", key[i]); }
934 println!();
935 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
936 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
937 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
938
939 let message = "In the beginning God created the heavens and the earth.".to_string();
940 println!("M =\t{}", message);
941 let mut cipher = [0_u8; 55];
942 a_rijndael.encrypt_string(iv, &message, cipher.as_mut_ptr());
943 print!("C =\t");
944 for c in cipher.clone()
945 { print!("{:02X} ", c); }
946 println!();
947 let mut txt = String::new();
948 for c in cipher.clone()
949 { write!(txt, "{:02X} ", c); }
950 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
951 println!();
952
953 // Normal case for Rijndael-512-512 for post-quantum
954 use cryptocol::number::SharedArrays;
955 use cryptocol::hash::SHA3_512;
956 let mut sha3 = SHA3_512::new();
957 sha3.absorb_str("Post-quantum");
958 let key: [u8; 64] = sha3.get_hash_value_in_array();
959 print!("K =\t");
960 for i in 0..64
961 { print!("{:02X}", key[i]); }
962 println!();
963 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
964 sha3.absorb_str("Initialize");
965 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
966 iv.src = sha3.get_hash_value_in_array();
967 let iv = unsafe { iv.des };
968 print!("IV =\t");
969 for i in 0..16
970 { print!("{:08X}", iv[i].to_be()); }
971 println!();
972
973 let message = "In the beginning God created the heavens and the earth.".to_string();
974 println!("M =\t{}", message);
975 let mut cipher = [0_u8; 55];
976 a_rijndael.encrypt_string(iv, &message, cipher.as_mut_ptr());
977 print!("C =\t");
978 for c in cipher.clone()
979 { print!("{:02X} ", c); }
980 println!();
981 let mut txt = String::new();
982 for c in cipher.clone()
983 { write!(txt, "{:02X} ", c); }
984 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
985 println!("-------------------------------");
986}
987
988fn aes_encrypt_string_ofb_into_vec()
989{
990 println!("aes_encrypt_string_ofb_into_vec()");
991 use std::io::Write;
992 use std::fmt::Write as _;
993 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
994
995 // Normal case for AES-128
996 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
997 println!("K =\t{:#016X}", key);
998 let mut a_aes = AES_128::new_with_key_u128(key);
999 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1000 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1001
1002 let message = "In the beginning God created the heavens and the earth.".to_string();
1003 println!("M =\t{}", message);
1004 let mut cipher = Vec::<u8>::new();
1005 a_aes.encrypt_string_into_vec(iv, &message, &mut cipher);
1006 print!("C =\t");
1007 for c in cipher.clone()
1008 { print!("{:02X} ", c); }
1009 println!();
1010 let mut txt = String::new();
1011 for c in cipher.clone()
1012 { write!(txt, "{:02X} ", c); }
1013 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1014 println!();
1015
1016 // Normal case for AES-192
1017 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1018 print!("K =\t");
1019 for i in 0..24
1020 { print!("{:02X}", key[i]); }
1021 println!();
1022 let mut a_aes = AES_192::new_with_key(&key);
1023 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1024 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1025
1026 let message = "In the beginning God created the heavens and the earth.".to_string();
1027 println!("M =\t{}", message);
1028 let mut cipher = Vec::<u8>::new();
1029 a_aes.encrypt_string_into_vec(iv, &message, &mut cipher);
1030 print!("C =\t");
1031 for c in cipher.clone()
1032 { print!("{:02X} ", c); }
1033 println!();
1034 let mut txt = String::new();
1035 for c in cipher.clone()
1036 { write!(txt, "{:02X} ", c); }
1037 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
1038 println!();
1039
1040 // Normal case for AES-256
1041 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1042 print!("K =\t");
1043 for i in 0..32
1044 { print!("{:02X}", key[i]); }
1045 println!();
1046 let mut a_aes = AES_256::new_with_key(&key);
1047 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1048 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1049
1050 let message = "In the beginning God created the heavens and the earth.".to_string();
1051 println!("M =\t{}", message);
1052 let mut cipher = Vec::<u8>::new();
1053 a_aes.encrypt_string_into_vec(iv, &message, &mut cipher);
1054 print!("C =\t");
1055 for c in cipher.clone()
1056 { print!("{:02X} ", c); }
1057 println!();
1058 let mut txt = String::new();
1059 for c in cipher.clone()
1060 { write!(txt, "{:02X} ", c); }
1061 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
1062 println!();
1063
1064 // Normal case for Rijndael-256-256
1065 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1066 print!("K =\t");
1067 for i in 0..32
1068 { print!("{:02X}", key[i]); }
1069 println!();
1070 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
1071 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1072 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
1073
1074 let message = "In the beginning God created the heavens and the earth.".to_string();
1075 println!("M =\t{}", message);
1076 let mut cipher = Vec::<u8>::new();
1077 a_rijndael.encrypt_string_into_vec(iv, &message, &mut cipher);
1078 print!("C =\t");
1079 for c in cipher.clone()
1080 { print!("{:02X} ", c); }
1081 println!();
1082 let mut txt = String::new();
1083 for c in cipher.clone()
1084 { write!(txt, "{:02X} ", c); }
1085 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
1086 println!();
1087
1088 // Normal case for Rijndael-512-512 for post-quantum
1089 use cryptocol::number::SharedArrays;
1090 use cryptocol::hash::SHA3_512;
1091 let mut sha3 = SHA3_512::new();
1092 sha3.absorb_str("Post-quantum");
1093 let key: [u8; 64] = sha3.get_hash_value_in_array();
1094 print!("K =\t");
1095 for i in 0..64
1096 { print!("{:02X}", key[i]); }
1097 println!();
1098 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
1099 sha3.absorb_str("Initialize");
1100 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
1101 iv.src = sha3.get_hash_value_in_array();
1102 let iv = unsafe { iv.des };
1103 print!("IV =\t");
1104 for i in 0..16
1105 { print!("{:08X}", iv[i].to_be()); }
1106 println!();
1107 let message = "In the beginning God created the heavens and the earth.".to_string();
1108 println!("M =\t{}", message);
1109 let mut cipher = Vec::<u8>::new();
1110 a_rijndael.encrypt_string_into_vec(iv, &message, &mut cipher);
1111 print!("C =\t");
1112 for c in cipher.clone()
1113 { print!("{:02X} ", c); }
1114 println!();
1115 let mut txt = String::new();
1116 for c in cipher.clone()
1117 { write!(txt, "{:02X} ", c); }
1118 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
1119 println!("-------------------------------");
1120}
1121
1122fn aes_encrypt_string_ofb_into_array()
1123{
1124 println!("aes_encrypt_string_ofb_into_array()");
1125 use std::io::Write;
1126 use std::fmt::Write as _;
1127 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
1128
1129 // Normal case for AES-128
1130 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1131 println!("K =\t{:#016X}", key);
1132 let mut a_aes = AES_128::new_with_key_u128(key);
1133 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1134 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1135
1136 let message = "In the beginning God created the heavens and the earth.".to_string();
1137 println!("M =\t{}", message);
1138 let mut cipher = [0_u8; 55];
1139 a_aes.encrypt_string_into_array(iv, &message, &mut cipher);
1140 print!("C =\t");
1141 for c in cipher.clone()
1142 { print!("{:02X} ", c); }
1143 println!();
1144 let mut txt = String::new();
1145 for c in cipher.clone()
1146 { write!(txt, "{:02X} ", c); }
1147 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1148 println!();
1149
1150 // Normal case for AES-192
1151 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1152 print!("K =\t");
1153 for i in 0..24
1154 { print!("{:02X}", key[i]); }
1155 println!();
1156 let mut a_aes = AES_192::new_with_key(&key);
1157 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1158 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1159
1160 let message = "In the beginning God created the heavens and the earth.".to_string();
1161 println!("M =\t{}", message);
1162 let mut cipher = [0_u8; 55];
1163 a_aes.encrypt_string_into_array(iv, &message, &mut cipher);
1164 print!("C =\t");
1165 for c in cipher.clone()
1166 { print!("{:02X} ", c); }
1167 println!();
1168 let mut txt = String::new();
1169 for c in cipher.clone()
1170 { write!(txt, "{:02X} ", c); }
1171 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
1172 println!();
1173
1174 // Normal case for AES-256
1175 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1176 print!("K =\t");
1177 for i in 0..32
1178 { print!("{:02X}", key[i]); }
1179 println!();
1180 let mut a_aes = AES_256::new_with_key(&key);
1181 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1182 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1183
1184 let message = "In the beginning God created the heavens and the earth.".to_string();
1185 println!("M =\t{}", message);
1186 let mut cipher = [0_u8; 55];
1187 a_aes.encrypt_string_into_array(iv, &message, &mut cipher);
1188 print!("C =\t");
1189 for c in cipher.clone()
1190 { print!("{:02X} ", c); }
1191 println!();
1192 let mut txt = String::new();
1193 for c in cipher.clone()
1194 { write!(txt, "{:02X} ", c); }
1195 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
1196 println!();
1197
1198 // Normal case for Rijndael-256-256
1199 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1200 print!("K =\t");
1201 for i in 0..32
1202 { print!("{:02X}", key[i]); }
1203 println!();
1204 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
1205 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1206 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
1207
1208 let message = "In the beginning God created the heavens and the earth.".to_string();
1209 println!("M =\t{}", message);
1210 let mut cipher = [0_u8; 55];
1211 a_rijndael.encrypt_string_into_array(iv, &message, &mut cipher);
1212 print!("C =\t");
1213 for c in cipher.clone()
1214 { print!("{:02X} ", c); }
1215 println!();
1216 let mut txt = String::new();
1217 for c in cipher.clone()
1218 { write!(txt, "{:02X} ", c); }
1219 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
1220 println!();
1221
1222 // Normal case for Rijndael-512-512 for post-quantum
1223 use cryptocol::number::SharedArrays;
1224 use cryptocol::hash::SHA3_512;
1225 let mut sha3 = SHA3_512::new();
1226 sha3.absorb_str("Post-quantum");
1227 let key: [u8; 64] = sha3.get_hash_value_in_array();
1228 print!("K =\t");
1229 for i in 0..64
1230 { print!("{:02X}", key[i]); }
1231 println!();
1232 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
1233 sha3.absorb_str("Initialize");
1234 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
1235 iv.src = sha3.get_hash_value_in_array();
1236 let iv = unsafe { iv.des };
1237 print!("IV =\t");
1238 for i in 0..16
1239 { print!("{:08X}", iv[i].to_be()); }
1240 println!();
1241 let message = "In the beginning God created the heavens and the earth.".to_string();
1242 println!("M =\t{}", message);
1243 let mut cipher = [0_u8; 55];
1244 a_rijndael.encrypt_string_into_array(iv, &message, &mut cipher);
1245 print!("C =\t");
1246 for c in cipher.clone()
1247 { print!("{:02X} ", c); }
1248 println!();
1249 let mut txt = String::new();
1250 for c in cipher.clone()
1251 { write!(txt, "{:02X} ", c); }
1252 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
1253 println!("-------------------------------");
1254}
1255
1256fn aes_encrypt_vec_ofb()
1257{
1258 println!("aes_encrypt_vec_ofb()");
1259 use std::io::Write;
1260 use std::fmt::Write as _;
1261 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
1262
1263 // Normal case for AES-128
1264 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1265 println!("K =\t{:#016X}", key);
1266 let mut a_aes = AES_128::new_with_key_u128(key);
1267 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1268 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1269
1270 let message = "In the beginning God created the heavens and the earth.";
1271 println!("M =\t{}", message);
1272 let message = unsafe { message.to_string().as_mut_vec().clone() };
1273 let mut cipher = [0_u8; 55];
1274 a_aes.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1275 print!("C =\t");
1276 for c in cipher.clone()
1277 { print!("{:02X} ", c); }
1278 println!();
1279 let mut txt = String::new();
1280 for c in cipher.clone()
1281 { write!(txt, "{:02X} ", c); }
1282 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1283 println!();
1284
1285 // Normal case for AES-192
1286 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1287 print!("K =\t");
1288 for i in 0..24
1289 { print!("{:02X}", key[i]); }
1290 println!();
1291 let mut a_aes = AES_192::new_with_key(&key);
1292 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1293 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1294
1295 let message = "In the beginning God created the heavens and the earth.";
1296 println!("M =\t{}", message);
1297 let message = unsafe { message.to_string().as_mut_vec().clone() };
1298 let mut cipher = [0_u8; 55];
1299 a_aes.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1300 print!("C =\t");
1301 for c in cipher.clone()
1302 { print!("{:02X} ", c); }
1303 println!();
1304 let mut txt = String::new();
1305 for c in cipher.clone()
1306 { write!(txt, "{:02X} ", c); }
1307 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
1308 println!();
1309
1310 // Normal case for AES-256
1311 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1312 print!("K =\t");
1313 for i in 0..32
1314 { print!("{:02X}", key[i]); }
1315 println!();
1316 let mut a_aes = AES_256::new_with_key(&key);
1317 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1318 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1319
1320 let message = "In the beginning God created the heavens and the earth.";
1321 println!("M =\t{}", message);
1322 let message = unsafe { message.to_string().as_mut_vec().clone() };
1323 let mut cipher = [0_u8; 55];
1324 a_aes.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1325 print!("C =\t");
1326 for c in cipher.clone()
1327 { print!("{:02X} ", c); }
1328 println!();
1329 let mut txt = String::new();
1330 for c in cipher.clone()
1331 { write!(txt, "{:02X} ", c); }
1332 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
1333 println!();
1334
1335 // Normal case for Rijndael-256-256
1336 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1337 print!("K =\t");
1338 for i in 0..32
1339 { print!("{:02X}", key[i]); }
1340 println!();
1341 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
1342 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1343 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
1344
1345 let message = "In the beginning God created the heavens and the earth.";
1346 println!("M =\t{}", message);
1347 let message = unsafe { message.to_string().as_mut_vec().clone() };
1348 let mut cipher = [0_u8; 55];
1349 a_rijndael.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1350 print!("C =\t");
1351 for c in cipher.clone()
1352 { print!("{:02X} ", c); }
1353 println!();
1354 let mut txt = String::new();
1355 for c in cipher.clone()
1356 { write!(txt, "{:02X} ", c); }
1357 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
1358 println!();
1359
1360 // Normal case for Rijndael-512-512 for post-quantum
1361 use cryptocol::number::SharedArrays;
1362 use cryptocol::hash::SHA3_512;
1363 let mut sha3 = SHA3_512::new();
1364 sha3.absorb_str("Post-quantum");
1365 let key: [u8; 64] = sha3.get_hash_value_in_array();
1366 print!("K =\t");
1367 for i in 0..64
1368 { print!("{:02X}", key[i]); }
1369 println!();
1370 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
1371 sha3.absorb_str("Initialize");
1372 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
1373 iv.src = sha3.get_hash_value_in_array();
1374 let iv = unsafe { iv.des };
1375 print!("IV =\t");
1376 for i in 0..16
1377 { print!("{:08X}", iv[i].to_be()); }
1378 println!();
1379 let message = "In the beginning God created the heavens and the earth.";
1380 println!("M =\t{}", message);
1381 let message = unsafe { message.to_string().as_mut_vec().clone() };
1382 let mut cipher = [0_u8; 55];
1383 a_rijndael.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1384 print!("C =\t");
1385 for c in cipher.clone()
1386 { print!("{:02X} ", c); }
1387 println!();
1388 let mut txt = String::new();
1389 for c in cipher.clone()
1390 { write!(txt, "{:02X} ", c); }
1391 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
1392 println!("-------------------------------");
1393}
1394
1395fn aes_encrypt_vec_ofb_into_vec()
1396{
1397 println!("aes_encrypt_vec_ofb_into_vec()");
1398 use std::io::Write;
1399 use std::fmt::Write as _;
1400 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
1401
1402 // Normal case for AES-128
1403 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1404 println!("K =\t{:#016X}", key);
1405 let mut a_aes = AES_128::new_with_key_u128(key);
1406 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1407 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1408
1409 let message = "In the beginning God created the heavens and the earth.";
1410 println!("M =\t{}", message);
1411 let message = unsafe { message.to_string().as_mut_vec().clone() };
1412 let mut cipher = Vec::<u8>::new();
1413 a_aes.encrypt_vec_into_vec(iv, &message, &mut cipher);
1414 print!("C =\t");
1415 for c in cipher.clone()
1416 { print!("{:02X} ", c); }
1417 println!();
1418 let mut txt = String::new();
1419 for c in cipher.clone()
1420 { write!(txt, "{:02X} ", c); }
1421 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1422 println!();
1423
1424 // Normal case for AES-192
1425 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1426 print!("K =\t");
1427 for i in 0..24
1428 { print!("{:02X}", key[i]); }
1429 println!();
1430 let mut a_aes = AES_192::new_with_key(&key);
1431 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1432 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1433
1434 let message = "In the beginning God created the heavens and the earth.";
1435 println!("M =\t{}", message);
1436 let message = unsafe { message.to_string().as_mut_vec().clone() };
1437 let mut cipher = Vec::<u8>::new();
1438 a_aes.encrypt_vec_into_vec(iv, &message, &mut cipher);
1439 print!("C =\t");
1440 for c in cipher.clone()
1441 { print!("{:02X} ", c); }
1442 println!();
1443 let mut txt = String::new();
1444 for c in cipher.clone()
1445 { write!(txt, "{:02X} ", c); }
1446 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
1447 println!();
1448
1449 // Normal case for AES-256
1450 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1451 print!("K =\t");
1452 for i in 0..32
1453 { print!("{:02X}", key[i]); }
1454 println!();
1455 let mut a_aes = AES_256::new_with_key(&key);
1456 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1457 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1458
1459 let message = "In the beginning God created the heavens and the earth.";
1460 println!("M =\t{}", message);
1461 let message = unsafe { message.to_string().as_mut_vec().clone() };
1462 let mut cipher = Vec::<u8>::new();
1463 a_aes.encrypt_vec_into_vec(iv, &message, &mut cipher);
1464 print!("C =\t");
1465 for c in cipher.clone()
1466 { print!("{:02X} ", c); }
1467 println!();
1468 let mut txt = String::new();
1469 for c in cipher.clone()
1470 { write!(txt, "{:02X} ", c); }
1471 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
1472 println!();
1473
1474 // Normal case for Rijndael-256-256
1475 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1476 print!("K =\t");
1477 for i in 0..32
1478 { print!("{:02X}", key[i]); }
1479 println!();
1480 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
1481 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1482 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
1483
1484 let message = "In the beginning God created the heavens and the earth.";
1485 println!("M =\t{}", message);
1486 let message = unsafe { message.to_string().as_mut_vec().clone() };
1487 let mut cipher = Vec::<u8>::new();
1488 a_rijndael.encrypt_vec_into_vec(iv, &message, &mut cipher);
1489 print!("C =\t");
1490 for c in cipher.clone()
1491 { print!("{:02X} ", c); }
1492 println!();
1493 let mut txt = String::new();
1494 for c in cipher.clone()
1495 { write!(txt, "{:02X} ", c); }
1496 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
1497 println!();
1498
1499 // Normal case for Rijndael-512-512 for post-quantum
1500 use cryptocol::number::SharedArrays;
1501 use cryptocol::hash::SHA3_512;
1502 let mut sha3 = SHA3_512::new();
1503 sha3.absorb_str("Post-quantum");
1504 let key: [u8; 64] = sha3.get_hash_value_in_array();
1505 print!("K =\t");
1506 for i in 0..64
1507 { print!("{:02X}", key[i]); }
1508 println!();
1509 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
1510 sha3.absorb_str("Initialize");
1511 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
1512 iv.src = sha3.get_hash_value_in_array();
1513 let iv = unsafe { iv.des };
1514 print!("IV =\t");
1515 for i in 0..16
1516 { print!("{:08X}", iv[i].to_be()); }
1517 println!();
1518 let message = "In the beginning God created the heavens and the earth.";
1519 println!("M =\t{}", message);
1520 let message = unsafe { message.to_string().as_mut_vec().clone() };
1521 let mut cipher = Vec::<u8>::new();
1522 a_rijndael.encrypt_vec_into_vec(iv, &message, &mut cipher);
1523 print!("C =\t");
1524 for c in cipher.clone()
1525 { print!("{:02X} ", c); }
1526 println!();
1527 let mut txt = String::new();
1528 for c in cipher.clone()
1529 { write!(txt, "{:02X} ", c); }
1530 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
1531 println!("-------------------------------");
1532}
1533
1534fn aes_encrypt_vec_ofb_into_array()
1535{
1536 println!("aes_encrypt_vec_ofb_into_array()");
1537 use std::io::Write;
1538 use std::fmt::Write as _;
1539 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
1540
1541 // Normal case for AES-128
1542 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1543 println!("K =\t{:#016X}", key);
1544 let mut a_aes = AES_128::new_with_key_u128(key);
1545 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1546 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1547
1548 let message = "In the beginning God created the heavens and the earth.";
1549 println!("M =\t{}", message);
1550 let message = unsafe { message.to_string().as_mut_vec().clone() };
1551 let mut cipher = [0_u8; 55];
1552 a_aes.encrypt_vec_into_array(iv, &message, &mut cipher);
1553 print!("C =\t");
1554 for c in cipher.clone()
1555 { print!("{:02X} ", c); }
1556 println!();
1557 let mut txt = String::new();
1558 for c in cipher.clone()
1559 { write!(txt, "{:02X} ", c); }
1560 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1561 println!();
1562
1563 // Normal case for AES-192
1564 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1565 print!("K =\t");
1566 for i in 0..24
1567 { print!("{:02X}", key[i]); }
1568 println!();
1569 let mut a_aes = AES_192::new_with_key(&key);
1570 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1571 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1572
1573 let message = "In the beginning God created the heavens and the earth.";
1574 println!("M =\t{}", message);
1575 let message = unsafe { message.to_string().as_mut_vec().clone() };
1576 let mut cipher = [0_u8; 55];
1577 a_aes.encrypt_vec_into_array(iv, &message, &mut cipher);
1578 print!("C =\t");
1579 for c in cipher.clone()
1580 { print!("{:02X} ", c); }
1581 println!();
1582 let mut txt = String::new();
1583 for c in cipher.clone()
1584 { write!(txt, "{:02X} ", c); }
1585 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
1586 println!();
1587
1588 // Normal case for AES-256
1589 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1590 print!("K =\t");
1591 for i in 0..32
1592 { print!("{:02X}", key[i]); }
1593 println!();
1594 let mut a_aes = AES_256::new_with_key(&key);
1595 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1596 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1597
1598 let message = "In the beginning God created the heavens and the earth.";
1599 println!("M =\t{}", message);
1600 let message = unsafe { message.to_string().as_mut_vec().clone() };
1601 let mut cipher = [0_u8; 55];
1602 a_aes.encrypt_vec_into_array(iv, &message, &mut cipher);
1603 print!("C =\t");
1604 for c in cipher.clone()
1605 { print!("{:02X} ", c); }
1606 println!();
1607 let mut txt = String::new();
1608 for c in cipher.clone()
1609 { write!(txt, "{:02X} ", c); }
1610 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
1611 println!();
1612
1613 // Normal case for Rijndael-256-256
1614 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1615 print!("K =\t");
1616 for i in 0..32
1617 { print!("{:02X}", key[i]); }
1618 println!();
1619 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
1620 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1621 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
1622
1623 let message = "In the beginning God created the heavens and the earth.";
1624 println!("M =\t{}", message);
1625 let message = unsafe { message.to_string().as_mut_vec().clone() };
1626 let mut cipher = [0_u8; 55];
1627 a_rijndael.encrypt_vec_into_array(iv, &message, &mut cipher);
1628 print!("C =\t");
1629 for c in cipher.clone()
1630 { print!("{:02X} ", c); }
1631 println!();
1632 let mut txt = String::new();
1633 for c in cipher.clone()
1634 { write!(txt, "{:02X} ", c); }
1635 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
1636 println!();
1637
1638 // Normal case for Rijndael-512-512 for post-quantum
1639 use cryptocol::number::SharedArrays;
1640 use cryptocol::hash::SHA3_512;
1641 let mut sha3 = SHA3_512::new();
1642 sha3.absorb_str("Post-quantum");
1643 let key: [u8; 64] = sha3.get_hash_value_in_array();
1644 print!("K =\t");
1645 for i in 0..64
1646 { print!("{:02X}", key[i]); }
1647 println!();
1648 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
1649 sha3.absorb_str("Initialize");
1650 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
1651 iv.src = sha3.get_hash_value_in_array();
1652 let iv = unsafe { iv.des };
1653 print!("IV =\t");
1654 for i in 0..16
1655 { print!("{:08X}", iv[i].to_be()); }
1656 println!();
1657 let message = "In the beginning God created the heavens and the earth.";
1658 println!("M =\t{}", message);
1659 let message = unsafe { message.to_string().as_mut_vec().clone() };
1660 let mut cipher = [0_u8; 55];
1661 a_rijndael.encrypt_vec_into_array(iv, &message, &mut cipher);
1662 print!("C =\t");
1663 for c in cipher.clone()
1664 { print!("{:02X} ", c); }
1665 println!();
1666 let mut txt = String::new();
1667 for c in cipher.clone()
1668 { write!(txt, "{:02X} ", c); }
1669 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
1670 println!("-------------------------------");
1671}
1672
1673fn aes_encrypt_array_ofb()
1674{
1675 println!("aes_encrypt_array_ofb()");
1676 use std::io::Write;
1677 use std::fmt::Write as _;
1678 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
1679
1680 // Normal case for AES-128
1681 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1682 println!("K =\t{:#016X}", key);
1683 let mut a_aes = AES_128::new_with_key_u128(key);
1684 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1685 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1686
1687 let mes = "In the beginning God created the heavens and the earth.";
1688 println!("M =\t{}", mes);
1689 let mut message = [0_u8; 55];
1690 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1691 let mut cipher = [0_u8; 55];
1692 a_aes.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
1693 print!("C =\t");
1694 for c in cipher.clone()
1695 { print!("{:02X} ", c); }
1696 println!();
1697 let mut txt = String::new();
1698 for c in cipher.clone()
1699 { write!(txt, "{:02X} ", c); }
1700 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1701 println!();
1702
1703 // Normal case for AES-192
1704 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1705 print!("K =\t");
1706 for i in 0..24
1707 { print!("{:02X}", key[i]); }
1708 println!();
1709 let mut a_aes = AES_192::new_with_key(&key);
1710 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1711 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1712
1713 let mes = "In the beginning God created the heavens and the earth.";
1714 println!("M =\t{}", mes);
1715 let mut message = [0_u8; 55];
1716 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1717 let mut cipher = [0_u8; 55];
1718 a_aes.encrypt(iv, message.as_ptr(), message.len() as u64, 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, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
1727 println!();
1728
1729 // Normal case for AES-256
1730 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1731 print!("K =\t");
1732 for i in 0..32
1733 { print!("{:02X}", key[i]); }
1734 println!();
1735 let mut a_aes = AES_256::new_with_key(&key);
1736 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1737 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1738
1739 let mes = "In the beginning God created the heavens and the earth.";
1740 println!("M =\t{}", mes);
1741 let mut message = [0_u8; 55];
1742 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1743 let mut cipher = [0_u8; 55];
1744 a_aes.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
1745 print!("C =\t");
1746 for c in cipher.clone()
1747 { print!("{:02X} ", c); }
1748 println!();
1749 let mut txt = String::new();
1750 for c in cipher.clone()
1751 { write!(txt, "{:02X} ", c); }
1752 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
1753 println!();
1754
1755 // Normal case for Rijndael-256-256
1756 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1757 print!("K =\t");
1758 for i in 0..32
1759 { print!("{:02X}", key[i]); }
1760 println!();
1761 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
1762 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1763 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
1764
1765 let mes = "In the beginning God created the heavens and the earth.";
1766 println!("M =\t{}", mes);
1767 let mut message = [0_u8; 55];
1768 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1769 let mut cipher = [0_u8; 55];
1770 a_rijndael.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
1771 print!("C =\t");
1772 for c in cipher.clone()
1773 { print!("{:02X} ", c); }
1774 println!();
1775 let mut txt = String::new();
1776 for c in cipher.clone()
1777 { write!(txt, "{:02X} ", c); }
1778 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
1779 println!();
1780
1781 // Normal case for Rijndael-512-512 for post-quantum
1782 use cryptocol::number::SharedArrays;
1783 use cryptocol::hash::SHA3_512;
1784 let mut sha3 = SHA3_512::new();
1785 sha3.absorb_str("Post-quantum");
1786 let key: [u8; 64] = sha3.get_hash_value_in_array();
1787 print!("K =\t");
1788 for i in 0..64
1789 { print!("{:02X}", key[i]); }
1790 println!();
1791 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
1792 sha3.absorb_str("Initialize");
1793 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
1794 iv.src = sha3.get_hash_value_in_array();
1795 let iv = unsafe { iv.des };
1796 print!("IV =\t");
1797 for i in 0..16
1798 { print!("{:08X}", iv[i].to_be()); }
1799 println!();
1800 let mes = "In the beginning God created the heavens and the earth.";
1801 println!("M =\t{}", mes);
1802 let mut message = [0_u8; 55];
1803 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1804 let mut cipher = [0_u8; 55];
1805 a_rijndael.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
1806 print!("C =\t");
1807 for c in cipher.clone()
1808 { print!("{:02X} ", c); }
1809 println!();
1810 let mut txt = String::new();
1811 for c in cipher.clone()
1812 { write!(txt, "{:02X} ", c); }
1813 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
1814 println!("-------------------------------");
1815}
1816
1817fn aes_encrypt_array_ofb_into_vec()
1818{
1819 println!("aes_encrypt_array_ofb_into_vec()");
1820 use std::io::Write;
1821 use std::fmt::Write as _;
1822 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
1823
1824 // Normal case for AES-128
1825 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1826 println!("K =\t{:#016X}", key);
1827 let mut a_aes = AES_128::new_with_key_u128(key);
1828 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1829 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1830
1831 let mes = "In the beginning God created the heavens and the earth.";
1832 println!("M =\t{}", mes);
1833 let mut message = [0_u8; 55];
1834 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1835 let mut cipher = Vec::<u8>::new();
1836 a_aes.encrypt_array_into_vec(iv, &message, &mut cipher);
1837 print!("C =\t");
1838 for c in cipher.clone()
1839 { print!("{:02X} ", c); }
1840 println!();
1841 let mut txt = String::new();
1842 for c in cipher.clone()
1843 { write!(txt, "{:02X} ", c); }
1844 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1845 println!();
1846
1847 // Normal case for AES-192
1848 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1849 print!("K =\t");
1850 for i in 0..24
1851 { print!("{:02X}", key[i]); }
1852 println!();
1853 let mut a_aes = AES_192::new_with_key(&key);
1854 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1855 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1856
1857 let mes = "In the beginning God created the heavens and the earth.";
1858 println!("M =\t{}", mes);
1859 let mut message = [0_u8; 55];
1860 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1861 let mut cipher = Vec::<u8>::new();
1862 a_aes.encrypt_array_into_vec(iv, &message, &mut cipher);
1863 print!("C =\t");
1864 for c in cipher.clone()
1865 { print!("{:02X} ", c); }
1866 println!();
1867 let mut txt = String::new();
1868 for c in cipher.clone()
1869 { write!(txt, "{:02X} ", c); }
1870 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
1871 println!();
1872
1873 // Normal case for AES-256
1874 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1875 print!("K =\t");
1876 for i in 0..32
1877 { print!("{:02X}", key[i]); }
1878 println!();
1879 let mut a_aes = AES_256::new_with_key(&key);
1880 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1881 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1882
1883 let mes = "In the beginning God created the heavens and the earth.";
1884 println!("M =\t{}", mes);
1885 let mut message = [0_u8; 55];
1886 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1887 let mut cipher = Vec::<u8>::new();
1888 a_aes.encrypt_array_into_vec(iv, &message, &mut cipher);
1889 print!("C =\t");
1890 for c in cipher.clone()
1891 { print!("{:02X} ", c); }
1892 println!();
1893 let mut txt = String::new();
1894 for c in cipher.clone()
1895 { write!(txt, "{:02X} ", c); }
1896 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
1897 println!();
1898
1899 // Normal case for Rijndael-256-256
1900 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1901 print!("K =\t");
1902 for i in 0..32
1903 { print!("{:02X}", key[i]); }
1904 println!();
1905 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
1906 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1907 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
1908
1909 let mes = "In the beginning God created the heavens and the earth.";
1910 println!("M =\t{}", mes);
1911 let mut message = [0_u8; 55];
1912 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1913 let mut cipher = Vec::<u8>::new();
1914 a_rijndael.encrypt_array_into_vec(iv, &message, &mut cipher);
1915 print!("C =\t");
1916 for c in cipher.clone()
1917 { print!("{:02X} ", c); }
1918 println!();
1919 let mut txt = String::new();
1920 for c in cipher.clone()
1921 { write!(txt, "{:02X} ", c); }
1922 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
1923 println!();
1924
1925 // Normal case for Rijndael-512-512 for post-quantum
1926 use cryptocol::number::SharedArrays;
1927 use cryptocol::hash::SHA3_512;
1928 let mut sha3 = SHA3_512::new();
1929 sha3.absorb_str("Post-quantum");
1930 let key: [u8; 64] = sha3.get_hash_value_in_array();
1931 print!("K =\t");
1932 for i in 0..64
1933 { print!("{:02X}", key[i]); }
1934 println!();
1935 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
1936 sha3.absorb_str("Initialize");
1937 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
1938 iv.src = sha3.get_hash_value_in_array();
1939 let iv = unsafe { iv.des };
1940 print!("IV =\t");
1941 for i in 0..16
1942 { print!("{:08X}", iv[i].to_be()); }
1943 println!();
1944 let mes = "In the beginning God created the heavens and the earth.";
1945 println!("M =\t{}", mes);
1946 let mut message = [0_u8; 55];
1947 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1948 let mut cipher = Vec::<u8>::new();
1949 a_rijndael.encrypt_array_into_vec(iv, &message, &mut cipher);
1950 print!("C =\t");
1951 for c in cipher.clone()
1952 { print!("{:02X} ", c); }
1953 println!();
1954 let mut txt = String::new();
1955 for c in cipher.clone()
1956 { write!(txt, "{:02X} ", c); }
1957 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
1958 println!("-------------------------------");
1959}
1960
1961fn aes_encrypt_array_ofb_into_array()
1962{
1963 println!("aes_encrypt_array_ofb_into_array()");
1964 use std::io::Write;
1965 use std::fmt::Write as _;
1966 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
1967
1968 // Normal case for AES-128
1969 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1970 println!("K =\t{:#016X}", key);
1971 let mut a_aes = AES_128::new_with_key_u128(key);
1972 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1973 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1974
1975 let mes = "In the beginning God created the heavens and the earth.";
1976 println!("M =\t{}", mes);
1977 let mut message = [0_u8; 55];
1978 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1979 let mut cipher = [0_u8; 55];
1980 a_aes.encrypt_array_into_array(iv, &message, &mut cipher);
1981 print!("C =\t");
1982 for c in cipher.clone()
1983 { print!("{:02X} ", c); }
1984 println!();
1985 let mut txt = String::new();
1986 for c in cipher.clone()
1987 { write!(txt, "{:02X} ", c); }
1988 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1989 println!();
1990
1991 // Normal case for AES-192
1992 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1993 print!("K =\t");
1994 for i in 0..24
1995 { print!("{:02X}", key[i]); }
1996 println!();
1997 let mut a_aes = AES_192::new_with_key(&key);
1998 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1999 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2000
2001 let mes = "In the beginning God created the heavens and the earth.";
2002 println!("M =\t{}", mes);
2003 let mut message = [0_u8; 55];
2004 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2005 let mut cipher = [0_u8; 55];
2006 a_aes.encrypt_array_into_array(iv, &message, &mut cipher);
2007 print!("C =\t");
2008 for c in cipher.clone()
2009 { print!("{:02X} ", c); }
2010 println!();
2011 let mut txt = String::new();
2012 for c in cipher.clone()
2013 { write!(txt, "{:02X} ", c); }
2014 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
2015 println!();
2016
2017 // Normal case for AES-256
2018 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2019 print!("K =\t");
2020 for i in 0..32
2021 { print!("{:02X}", key[i]); }
2022 println!();
2023 let mut a_aes = AES_256::new_with_key(&key);
2024 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2025 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2026
2027 let mes = "In the beginning God created the heavens and the earth.";
2028 println!("M =\t{}", mes);
2029 let mut message = [0_u8; 55];
2030 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2031 let mut cipher = [0_u8; 55];
2032 a_aes.encrypt_array_into_array(iv, &message, &mut cipher);
2033 print!("C =\t");
2034 for c in cipher.clone()
2035 { print!("{:02X} ", c); }
2036 println!();
2037 let mut txt = String::new();
2038 for c in cipher.clone()
2039 { write!(txt, "{:02X} ", c); }
2040 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
2041 println!();
2042
2043 // Normal case for Rijndael-256-256
2044 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2045 print!("K =\t");
2046 for i in 0..32
2047 { print!("{:02X}", key[i]); }
2048 println!();
2049 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
2050 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2051 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
2052
2053 let mes = "In the beginning God created the heavens and the earth.";
2054 println!("M =\t{}", mes);
2055 let mut message = [0_u8; 55];
2056 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2057 let mut cipher = [0_u8; 55];
2058 a_rijndael.encrypt_array_into_array(iv, &message, &mut cipher);
2059 print!("C =\t");
2060 for c in cipher.clone()
2061 { print!("{:02X} ", c); }
2062 println!();
2063 let mut txt = String::new();
2064 for c in cipher.clone()
2065 { write!(txt, "{:02X} ", c); }
2066 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
2067 println!();
2068
2069 // Normal case for Rijndael-512-512 for post-quantum
2070 use cryptocol::number::SharedArrays;
2071 use cryptocol::hash::SHA3_512;
2072 let mut sha3 = SHA3_512::new();
2073 sha3.absorb_str("Post-quantum");
2074 let key: [u8; 64] = sha3.get_hash_value_in_array();
2075 print!("K =\t");
2076 for i in 0..64
2077 { print!("{:02X}", key[i]); }
2078 println!();
2079 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
2080 sha3.absorb_str("Initialize");
2081 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
2082 iv.src = sha3.get_hash_value_in_array();
2083 let iv = unsafe { iv.des };
2084 print!("IV =\t");
2085 for i in 0..16
2086 { print!("{:08X}", iv[i].to_be()); }
2087 println!();
2088 let mes = "In the beginning God created the heavens and the earth.";
2089 println!("M =\t{}", mes);
2090 let mut message = [0_u8; 55];
2091 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2092 let mut cipher = [0_u8; 55];
2093 a_rijndael.encrypt_array_into_array(iv, &message, &mut cipher);
2094 print!("C =\t");
2095 for c in cipher.clone()
2096 { print!("{:02X} ", c); }
2097 println!();
2098 let mut txt = String::new();
2099 for c in cipher.clone()
2100 { write!(txt, "{:02X} ", c); }
2101 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
2102 println!("-------------------------------");
2103}
2104
2105
2106fn aes_decrypt_ofb()
2107{
2108 println!("aes_decrypt_ofb");
2109 use std::io::Write;
2110 use std::fmt::Write as _;
2111 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
2112
2113 // Normal case for AES-128
2114 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
2115 println!("K =\t{:#016X}", key);
2116 let mut a_aes = AES_128::new_with_key_u128(key);
2117 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2118 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0], iv[1], iv[2], iv[3]);
2119
2120 let message = "In the beginning God created the heavens and the earth.";
2121 println!("M =\t{}", message);
2122 let mut cipher = [0_u8; 55];
2123 a_aes.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
2132
2133 let mut recovered = vec![0; 55];
2134 a_aes.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2135 print!("Ba =\t");
2136 for b in recovered.clone()
2137 { print!("{:02X} ", b); }
2138 println!();
2139 let mut txt = String::new();
2140 for c in recovered.clone()
2141 { write!(txt, "{:02X} ", c); }
2142 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 ");
2143
2144 let mut converted = String::new();
2145 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2146
2147 println!("Bb =\t{}", converted);
2148 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2149 assert_eq!(converted, message);
2150 println!();
2151
2152 // Normal case for AES-192
2153 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2154 print!("K =\t");
2155 for i in 0..24
2156 { print!("{:02X}", key[i]); }
2157 println!();
2158 let mut a_aes = AES_192::new_with_key(&key);
2159 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2160 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2161
2162 let message = "In the beginning God created the heavens and the earth.";
2163 println!("M =\t{}", message);
2164 let mut cipher = [0_u8; 55];
2165 a_aes.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
2166 print!("C =\t");
2167 for c in cipher.clone()
2168 { print!("{:02X} ", c); }
2169 println!();
2170 let mut txt = String::new();
2171 for c in cipher.clone()
2172 { write!(txt, "{:02X} ", c); }
2173 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
2174
2175 let mut recovered = vec![0; 55];
2176 a_aes.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2177 print!("Ba =\t");
2178 for b in recovered.clone()
2179 { print!("{:02X} ", b); }
2180 println!();
2181 let mut txt = String::new();
2182 for c in recovered.clone()
2183 { write!(txt, "{:02X} ", c); }
2184 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 ");
2185
2186 let mut converted = String::new();
2187 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2188
2189 println!("Bb =\t{}", converted);
2190 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2191 assert_eq!(converted, message);
2192 println!();
2193
2194 // Normal case for AES-256
2195 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2196 print!("K =\t");
2197 for i in 0..32
2198 { print!("{:02X}", key[i]); }
2199 println!();
2200 let mut a_aes = AES_256::new_with_key(&key);
2201 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2202 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2203
2204 let message = "In the beginning God created the heavens and the earth.";
2205 println!("M =\t{}", message);
2206 let mut cipher = [0_u8; 55];
2207 a_aes.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
2208 print!("C =\t");
2209 for c in cipher.clone()
2210 { print!("{:02X} ", c); }
2211 println!();
2212 let mut txt = String::new();
2213 for c in cipher.clone()
2214 { write!(txt, "{:02X} ", c); }
2215 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
2216
2217 let mut recovered = vec![0; 55];
2218 a_aes.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2219 print!("Ba =\t");
2220 for b in recovered.clone()
2221 { print!("{:02X} ", b); }
2222 println!();
2223 let mut txt = String::new();
2224 for c in recovered.clone()
2225 { write!(txt, "{:02X} ", c); }
2226 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 ");
2227
2228 let mut converted = String::new();
2229 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2230
2231 println!("Bb =\t{}", converted);
2232 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2233 assert_eq!(converted, message);
2234 println!();
2235
2236 // Normal case for Rijndael-256-256
2237 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2238 print!("K =\t");
2239 for i in 0..32
2240 { print!("{:02X}", key[i]); }
2241 println!();
2242 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
2243 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2244 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
2245
2246 let message = "In the beginning God created the heavens and the earth.";
2247 println!("M =\t{}", message);
2248 let mut cipher = [0_u8; 55];
2249 a_rijndael.encrypt(iv, message.as_ptr(), message.len() as u64, 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, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
2258
2259 let mut recovered = vec![0; 55];
2260 a_rijndael.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2261 print!("Ba =\t");
2262 for b in recovered.clone()
2263 { print!("{:02X} ", b); }
2264 println!();
2265 let mut txt = String::new();
2266 for c in recovered.clone()
2267 { write!(txt, "{:02X} ", c); }
2268 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 ");
2269
2270 let mut converted = String::new();
2271 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2272
2273 println!("Bb =\t{}", converted);
2274 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2275 assert_eq!(converted, message);
2276 println!();
2277
2278 // Normal case for Rijndael-512-512 for post-quantum
2279 use cryptocol::number::SharedArrays;
2280 use cryptocol::hash::SHA3_512;
2281 let mut sha3 = SHA3_512::new();
2282 sha3.absorb_str("Post-quantum");
2283 let key: [u8; 64] = sha3.get_hash_value_in_array();
2284 print!("K =\t");
2285 for i in 0..64
2286 { print!("{:02X}", key[i]); }
2287 println!();
2288 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
2289 sha3.absorb_str("Initialize");
2290 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
2291 iv.src = sha3.get_hash_value_in_array();
2292 let iv = unsafe { iv.des };
2293 print!("IV =\t");
2294 for i in 0..16
2295 { print!("{:08X}", iv[i].to_be()); }
2296 println!();
2297 let message = "In the beginning God created the heavens and the earth.";
2298 println!("M =\t{}", message);
2299 let mut cipher = [0_u8; 55];
2300 a_rijndael.encrypt(iv.clone(), message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
2301 print!("C =\t");
2302 for c in cipher.clone()
2303 { print!("{:02X} ", c); }
2304 println!();
2305 let mut txt = String::new();
2306 for c in cipher.clone()
2307 { write!(txt, "{:02X} ", c); }
2308 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
2309
2310 let mut recovered = vec![0; 55];
2311 a_rijndael.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2312 print!("Ba =\t");
2313 for b in recovered.clone()
2314 { print!("{:02X} ", b); }
2315 println!();
2316 let mut txt = String::new();
2317 for c in recovered.clone()
2318 { write!(txt, "{:02X} ", c); }
2319 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 ");
2320
2321 let mut converted = String::new();
2322 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2323
2324 println!("Bb =\t{}", converted);
2325 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2326 assert_eq!(converted, message);
2327 println!("-------------------------------");
2328}
2329
2330
2331fn aes_decrypt_ofb_into_vec()
2332{
2333 println!("aes_decrypt_ofb_into_vec()");
2334 use std::io::Write;
2335 use std::fmt::Write as _;
2336 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
2337
2338 // Normal case for AES-128
2339 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
2340 println!("K =\t{:#016X}", key);
2341 let mut a_aes = AES_128::new_with_key_u128(key);
2342 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2343 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2344
2345 let message = "In the beginning God created the heavens and the earth.";
2346 println!("M =\t{}", message);
2347 let mut cipher = [0_u8; 55];
2348 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2349 print!("C =\t");
2350 for c in cipher.clone()
2351 { print!("{:02X} ", c); }
2352 println!();
2353 let mut txt = String::new();
2354 for c in cipher.clone()
2355 { write!(txt, "{:02X} ", c); }
2356 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
2357 println!();
2358
2359 let mut recovered = Vec::<u8>::new();
2360 a_aes.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2361 print!("Ba =\t");
2362 for b in recovered.clone()
2363 { print!("{:02X} ", b); }
2364 println!();
2365 let mut txt = String::new();
2366 for c in recovered.clone()
2367 { write!(txt, "{:02X} ", c); }
2368 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 ");
2369
2370 let mut converted = String::new();
2371 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2372
2373 println!("Bb =\t{}", converted);
2374 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2375 assert_eq!(converted, message);
2376 println!();
2377
2378 // Normal case for AES-192
2379 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2380 print!("K =\t");
2381 for i in 0..24
2382 { print!("{:02X}", key[i]); }
2383 println!();
2384 let mut a_aes = AES_192::new_with_key(&key);
2385 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2386 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2387
2388 let message = "In the beginning God created the heavens and the earth.";
2389 println!("M =\t{}", message);
2390 let mut cipher = [0_u8; 55];
2391 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2392 print!("C =\t");
2393 for c in cipher.clone()
2394 { print!("{:02X} ", c); }
2395 println!();
2396 let mut txt = String::new();
2397 for c in cipher.clone()
2398 { write!(txt, "{:02X} ", c); }
2399 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
2400 println!();
2401
2402 let mut recovered = Vec::<u8>::new();
2403 a_aes.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2404 print!("Ba =\t");
2405 for b in recovered.clone()
2406 { print!("{:02X} ", b); }
2407 println!();
2408 let mut txt = String::new();
2409 for c in recovered.clone()
2410 { write!(txt, "{:02X} ", c); }
2411 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 ");
2412
2413 let mut converted = String::new();
2414 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2415
2416 println!("Bb =\t{}", converted);
2417 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2418 assert_eq!(converted, message);
2419 println!();
2420
2421 // Normal case for AES-256
2422 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2423 print!("K =\t");
2424 for i in 0..32
2425 { print!("{:02X}", key[i]); }
2426 println!();
2427 let mut a_aes = AES_256::new_with_key(&key);
2428 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2429 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2430
2431 let message = "In the beginning God created the heavens and the earth.";
2432 println!("M =\t{}", message);
2433 let mut cipher = [0_u8; 55];
2434 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2435 print!("C =\t");
2436 for c in cipher.clone()
2437 { print!("{:02X} ", c); }
2438 println!();
2439 let mut txt = String::new();
2440 for c in cipher.clone()
2441 { write!(txt, "{:02X} ", c); }
2442 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
2443 println!();
2444
2445 let mut recovered = Vec::<u8>::new();
2446 a_aes.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2447 print!("Ba =\t");
2448 for b in recovered.clone()
2449 { print!("{:02X} ", b); }
2450 println!();
2451 let mut txt = String::new();
2452 for c in recovered.clone()
2453 { write!(txt, "{:02X} ", c); }
2454 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 ");
2455
2456 let mut converted = String::new();
2457 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2458
2459 println!("Bb =\t{}", converted);
2460 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2461 assert_eq!(converted, message);
2462 println!();
2463
2464 // Normal case for Rijndael-256-256
2465 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2466 print!("K =\t");
2467 for i in 0..32
2468 { print!("{:02X}", key[i]); }
2469 println!();
2470 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
2471 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2472 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
2473
2474 let message = "In the beginning God created the heavens and the earth.";
2475 println!("M =\t{}", message);
2476 let mut cipher = [0_u8; 55];
2477 a_rijndael.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2478 print!("C =\t");
2479 for c in cipher.clone()
2480 { print!("{:02X} ", c); }
2481 println!();
2482 let mut txt = String::new();
2483 for c in cipher.clone()
2484 { write!(txt, "{:02X} ", c); }
2485 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
2486 println!();
2487
2488 let mut recovered = Vec::<u8>::new();
2489 a_rijndael.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2490 print!("Ba =\t");
2491 for b in recovered.clone()
2492 { print!("{:02X} ", b); }
2493 println!();
2494 let mut txt = String::new();
2495 for c in recovered.clone()
2496 { write!(txt, "{:02X} ", c); }
2497 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 ");
2498
2499 let mut converted = String::new();
2500 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2501
2502 println!("Bb =\t{}", converted);
2503 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2504 assert_eq!(converted, message);
2505 println!();
2506
2507 // Normal case for Rijndael-512-512 for post-quantum
2508 use cryptocol::number::SharedArrays;
2509 use cryptocol::hash::SHA3_512;
2510 let mut sha3 = SHA3_512::new();
2511 sha3.absorb_str("Post-quantum");
2512 let key: [u8; 64] = sha3.get_hash_value_in_array();
2513 print!("K =\t");
2514 for i in 0..64
2515 { print!("{:02X}", key[i]); }
2516 println!();
2517 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
2518 sha3.absorb_str("Initialize");
2519 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
2520 iv.src = sha3.get_hash_value_in_array();
2521 let iv = unsafe { iv.des };
2522 print!("IV =\t");
2523 for i in 0..16
2524 { print!("{:08X}", iv[i].to_be()); }
2525 println!();
2526 let message = "In the beginning God created the heavens and the earth.";
2527 println!("M =\t{}", message);
2528 let mut cipher = [0_u8; 55];
2529 a_rijndael.encrypt(iv.clone(), message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
2530 print!("C =\t");
2531 for c in cipher.clone()
2532 { print!("{:02X} ", c); }
2533 println!();
2534 let mut txt = String::new();
2535 for c in cipher.clone()
2536 { write!(txt, "{:02X} ", c); }
2537 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
2538
2539 let mut recovered = Vec::<u8>::new();
2540 a_rijndael.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2541 print!("Ba =\t");
2542 for b in recovered.clone()
2543 { print!("{:02X} ", b); }
2544 println!();
2545 let mut txt = String::new();
2546 for c in recovered.clone()
2547 { write!(txt, "{:02X} ", c); }
2548 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 ");
2549
2550 let mut converted = String::new();
2551 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2552
2553 println!("Bb =\t{}", converted);
2554 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2555 assert_eq!(converted, message);
2556 println!("-------------------------------");
2557}
2558
2559fn aes_decrypt_ofb_into_array()
2560{
2561 println!("aes_decrypt_ofb_into_array()");
2562 use std::io::Write;
2563 use std::fmt::Write as _;
2564 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
2565
2566 // Normal case for AES-128
2567 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
2568 println!("K =\t{:#016X}", key);
2569 let mut a_aes = AES_128::new_with_key_u128(key);
2570 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2571 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2572
2573 let message = "In the beginning God created the heavens and the earth.";
2574 println!("M =\t{}", message);
2575 let mut cipher = [0_u8; 55];
2576 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2577 print!("C =\t");
2578 for c in cipher.clone()
2579 { print!("{:02X} ", c); }
2580 println!();
2581 let mut txt = String::new();
2582 for c in cipher.clone()
2583 { write!(txt, "{:02X} ", c); }
2584 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
2585
2586 let mut recovered = [0; 64];
2587 let len = a_aes.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2588 print!("Ba =\t");
2589 for b in recovered.clone()
2590 { print!("{:02X} ", b); }
2591 println!();
2592 let mut txt = String::new();
2593 for c in recovered.clone()
2594 { write!(txt, "{:02X} ", c); }
2595 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
2596
2597 let mut converted = String::new();
2598 unsafe { converted.as_mut_vec() }.write(&recovered);
2599 unsafe { converted.as_mut_vec() }.truncate(len as usize);
2600
2601 println!("Bb =\t{}", converted);
2602 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2603 assert_eq!(converted, message);
2604 println!();
2605
2606 // Normal case for AES-192
2607 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2608 print!("K =\t");
2609 for i in 0..24
2610 { print!("{:02X}", key[i]); }
2611 println!();
2612 let mut a_aes = AES_192::new_with_key(&key);
2613 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2614 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2615
2616 let message = "In the beginning God created the heavens and the earth.";
2617 println!("M =\t{}", message);
2618 let mut cipher = [0_u8; 55];
2619 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2620 print!("C =\t");
2621 for c in cipher.clone()
2622 { print!("{:02X} ", c); }
2623 println!();
2624 let mut txt = String::new();
2625 for c in cipher.clone()
2626 { write!(txt, "{:02X} ", c); }
2627 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
2628
2629 let mut recovered = [0; 64];
2630 a_aes.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2631 print!("Ba =\t");
2632 for b in recovered.clone()
2633 { print!("{:02X} ", b); }
2634 println!();
2635 let mut txt = String::new();
2636
2637 for c in recovered.clone()
2638 { write!(txt, "{:02X} ", c); }
2639 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
2640
2641 let mut converted = String::new();
2642 unsafe { converted.as_mut_vec() }.write(&recovered);
2643 unsafe { converted.as_mut_vec() }.truncate(len as usize);
2644
2645 println!("Bb =\t{}", converted);
2646 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2647 assert_eq!(converted, message);
2648 println!();
2649
2650 // Normal case for AES-256
2651 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2652 print!("K =\t");
2653 for i in 0..32
2654 { print!("{:02X}", key[i]); }
2655 println!();
2656 let mut a_aes = AES_256::new_with_key(&key);
2657 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2658 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2659
2660 let message = "In the beginning God created the heavens and the earth.";
2661 println!("M =\t{}", message);
2662 let mut cipher = [0_u8; 55];
2663 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2664 print!("C =\t");
2665 for c in cipher.clone()
2666 { print!("{:02X} ", c); }
2667 println!();
2668 let mut txt = String::new();
2669 for c in cipher.clone()
2670 { write!(txt, "{:02X} ", c); }
2671 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
2672
2673 let mut recovered = [0; 64];
2674 a_aes.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2675 print!("Ba =\t");
2676 for b in recovered.clone()
2677 { print!("{:02X} ", b); }
2678 println!();
2679 let mut txt = String::new();
2680 for c in recovered.clone()
2681 { write!(txt, "{:02X} ", c); }
2682 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
2683
2684 let mut converted = String::new();
2685 unsafe { converted.as_mut_vec() }.write(&recovered);
2686 unsafe { converted.as_mut_vec() }.truncate(len as usize);
2687
2688 println!("Bb =\t{}", converted);
2689 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2690 assert_eq!(converted, message);
2691 println!();
2692
2693 // Normal case for Rijndael-256-256
2694 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2695 print!("K =\t");
2696 for i in 0..32
2697 { print!("{:02X}", key[i]); }
2698 println!();
2699 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
2700 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2701 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
2702
2703 let message = "In the beginning God created the heavens and the earth.";
2704 println!("M =\t{}", message);
2705 let mut cipher = [0_u8; 55];
2706 a_rijndael.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2707 print!("C =\t");
2708 for c in cipher.clone()
2709 { print!("{:02X} ", c); }
2710 println!();
2711 let mut txt = String::new();
2712 for c in cipher.clone()
2713 { write!(txt, "{:02X} ", c); }
2714 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
2715
2716 let mut recovered = [0; 64];
2717 a_rijndael.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2718 for b in recovered.clone()
2719 { print!("{:02X} ", b); }
2720 println!();
2721 let mut txt = String::new();
2722 for c in recovered.clone()
2723 { write!(txt, "{:02X} ", c); }
2724 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
2725
2726 let mut converted = String::new();
2727 unsafe { converted.as_mut_vec() }.write(&recovered);
2728 unsafe { converted.as_mut_vec() }.truncate(len as usize);
2729
2730 println!("Bb =\t{}", converted);
2731 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2732 assert_eq!(converted, message);
2733 println!();
2734
2735 // Normal case for Rijndael-512-512 for post-quantum
2736 use cryptocol::number::SharedArrays;
2737 use cryptocol::hash::SHA3_512;
2738 let mut sha3 = SHA3_512::new();
2739 sha3.absorb_str("Post-quantum");
2740 let key: [u8; 64] = sha3.get_hash_value_in_array();
2741 print!("K =\t");
2742 for i in 0..64
2743 { print!("{:02X}", key[i]); }
2744 println!();
2745 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
2746 sha3.absorb_str("Initialize");
2747 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
2748 iv.src = sha3.get_hash_value_in_array();
2749 let iv = unsafe { iv.des };
2750 print!("IV =\t");
2751 for i in 0..16
2752 { print!("{:08X}", iv[i].to_be()); }
2753 println!();
2754
2755 let message = "In the beginning God created the heavens and the earth.";
2756 println!("M =\t{}", message);
2757 let mut cipher = [0_u8; 55];
2758 a_rijndael.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2759 print!("C =\t");
2760 for c in cipher.clone()
2761 { print!("{:02X} ", c); }
2762 println!();
2763 let mut txt = String::new();
2764 for c in cipher.clone()
2765 { write!(txt, "{:02X} ", c); }
2766 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
2767
2768 let mut recovered = [0; 64];
2769 a_rijndael.decrypt_into_array(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2770 print!("Ba =\t");
2771 for b in recovered.clone()
2772 { print!("{:02X} ", b); }
2773 println!();
2774 let mut txt = String::new();
2775 for c in recovered.clone()
2776 { write!(txt, "{:02X} ", c); }
2777 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
2778
2779 let mut converted = String::new();
2780 unsafe { converted.as_mut_vec() }.write(&recovered);
2781 unsafe { converted.as_mut_vec() }.truncate(len as usize);
2782
2783 println!("Bb =\t{}", converted);
2784 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2785 assert_eq!(converted, message);
2786 println!("-------------------------------");
2787}
2788
2789fn aes_decrypt_ofb_into_string()
2790{
2791 println!("aes_decrypt_ofb_into_string()");
2792 use std::io::Write;
2793 use std::fmt::Write as _;
2794 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
2795
2796 // Normal case for AES-128
2797 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
2798 println!("K =\t{:#016X}", key);
2799 let mut a_aes = AES_128::new_with_key_u128(key);
2800 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2801 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2802
2803 let message = "In the beginning God created the heavens and the earth.";
2804 println!("M =\t{}", message);
2805 let mut cipher = [0_u8; 55];
2806 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2807 print!("C =\t");
2808 for c in cipher.clone()
2809 { print!("{:02X} ", c); }
2810 println!();
2811 let mut txt = String::new();
2812 for c in cipher.clone()
2813 { write!(txt, "{:02X} ", c); }
2814 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
2815
2816 let mut converted= String::new();
2817 a_aes.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut converted);
2818 println!("B =\t{}", converted);
2819 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2820 assert_eq!(converted, message);
2821 println!();
2822
2823 // Normal case for AES-192
2824 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2825 print!("K =\t");
2826 for i in 0..24
2827 { print!("{:02X}", key[i]); }
2828 println!();
2829 let mut a_aes = AES_192::new_with_key(&key);
2830 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2831 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2832
2833 let message = "In the beginning God created the heavens and the earth.";
2834 println!("M =\t{}", message);
2835 let mut cipher = [0_u8; 55];
2836 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2837 print!("C =\t");
2838 for c in cipher.clone()
2839 { print!("{:02X} ", c); }
2840 println!();
2841 let mut txt = String::new();
2842 for c in cipher.clone()
2843 { write!(txt, "{:02X} ", c); }
2844 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
2845
2846 let mut converted= String::new();
2847 a_aes.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut converted);
2848 println!("B =\t{}", converted);
2849 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2850 assert_eq!(converted, message);
2851 println!();
2852
2853 // Normal case for AES-256
2854 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2855 print!("K =\t");
2856 for i in 0..32
2857 { print!("{:02X}", key[i]); }
2858 println!();
2859 let mut a_aes = AES_256::new_with_key(&key);
2860 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2861 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2862
2863 let message = "In the beginning God created the heavens and the earth.";
2864 println!("M =\t{}", message);
2865 let mut cipher = [0_u8; 55];
2866 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2867 print!("C =\t");
2868 for c in cipher.clone()
2869 { print!("{:02X} ", c); }
2870 println!();
2871 let mut txt = String::new();
2872 for c in cipher.clone()
2873 { write!(txt, "{:02X} ", c); }
2874 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
2875
2876 let mut converted= String::new();
2877 a_aes.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut converted);
2878 println!("B =\t{}", converted);
2879 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2880 assert_eq!(converted, message);
2881 println!();
2882
2883 // Normal case for Rijndael-256-256
2884 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2885 print!("K =\t");
2886 for i in 0..32
2887 { print!("{:02X}", key[i]); }
2888 println!();
2889 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
2890 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2891 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
2892
2893 let message = "In the beginning God created the heavens and the earth.";
2894 println!("M =\t{}", message);
2895 let mut cipher = [0_u8; 55];
2896 a_rijndael.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2897 print!("C =\t");
2898 for c in cipher.clone()
2899 { print!("{:02X} ", c); }
2900 println!();
2901 let mut txt = String::new();
2902 for c in cipher.clone()
2903 { write!(txt, "{:02X} ", c); }
2904 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
2905
2906 let mut converted= String::new();
2907 a_rijndael.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut converted);
2908 println!("B =\t{}", converted);
2909 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2910 assert_eq!(converted, message);
2911 println!();
2912
2913 // Normal case for Rijndael-512-512 for post-quantum
2914 use cryptocol::number::SharedArrays;
2915 use cryptocol::hash::SHA3_512;
2916 let mut sha3 = SHA3_512::new();
2917 sha3.absorb_str("Post-quantum");
2918 let key: [u8; 64] = sha3.get_hash_value_in_array();
2919 print!("K =\t");
2920 for i in 0..64
2921 { print!("{:02X}", key[i]); }
2922 println!();
2923 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
2924 sha3.absorb_str("Initialize");
2925 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
2926 iv.src = sha3.get_hash_value_in_array();
2927 let iv = unsafe { iv.des };
2928 print!("IV =\t");
2929 for i in 0..16
2930 { print!("{:08X}", iv[i].to_be()); }
2931 println!();
2932 let message = "In the beginning God created the heavens and the earth.";
2933 println!("M =\t{}", message);
2934 let mut cipher = [0_u8; 55];
2935 a_rijndael.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2936 print!("C =\t");
2937 for c in cipher.clone()
2938 { print!("{:02X} ", c); }
2939 println!();
2940 let mut txt = String::new();
2941 for c in cipher.clone()
2942 { write!(txt, "{:02X} ", c); }
2943 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
2944
2945 let mut converted= String::new();
2946 a_rijndael.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut converted);
2947 println!("B =\t{}", converted);
2948 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2949 assert_eq!(converted, message);
2950 println!("-------------------------------");
2951}
2952
2953fn aes_decrypt_vec_ofb()
2954{
2955 println!("aes_decrypt_vec_ofb()");
2956 use std::io::Write;
2957 use std::fmt::Write as _;
2958 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
2959
2960 // Normal case for AES-128
2961 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
2962 println!("K =\t{:#016X}", key);
2963 let mut a_aes = AES_128::new_with_key_u128(key);
2964 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2965 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2966
2967 let message = "In the beginning God created the heavens and the earth.";
2968 println!("M =\t{}", message);
2969 let mut cipher = Vec::<u8>::new();
2970 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
2971 print!("C =\t");
2972 for c in cipher.clone()
2973 { print!("{:02X} ", c); }
2974 println!();
2975 let mut txt = String::new();
2976 for c in cipher.clone()
2977 { write!(txt, "{:02X} ", c); }
2978 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
2979
2980 let mut recovered = vec![0; 55];
2981 a_aes.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
2982 print!("Ba =\t");
2983 for b in recovered.clone()
2984 { print!("{:02X} ", b); }
2985 println!();
2986 let mut txt = String::new();
2987 for c in recovered.clone()
2988 { write!(txt, "{:02X} ", c); }
2989 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 ");
2990
2991 let mut converted = String::new();
2992 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2993
2994 println!("Bb =\t{}", converted);
2995 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2996 assert_eq!(converted, message);
2997 println!();
2998
2999 // Normal case for AES-192
3000 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3001 print!("K =\t");
3002 for i in 0..24
3003 { print!("{:02X}", key[i]); }
3004 println!();
3005 let mut a_aes = AES_192::new_with_key(&key);
3006 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3007 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3008
3009 let message = "In the beginning God created the heavens and the earth.";
3010 println!("M =\t{}", message);
3011 let mut cipher = Vec::<u8>::new();
3012 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3013 print!("C =\t");
3014 for c in cipher.clone()
3015 { print!("{:02X} ", c); }
3016 println!();
3017 let mut txt = String::new();
3018 for c in cipher.clone()
3019 { write!(txt, "{:02X} ", c); }
3020 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
3021
3022 let mut recovered = vec![0; 55];
3023 a_aes.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
3024 print!("Ba =\t");
3025 for b in recovered.clone()
3026 { print!("{:02X} ", b); }
3027 println!();
3028 let mut txt = String::new();
3029 for c in recovered.clone()
3030 { write!(txt, "{:02X} ", c); }
3031 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 ");
3032
3033 let mut converted = String::new();
3034 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3035
3036 println!("Bb =\t{}", converted);
3037 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3038 assert_eq!(converted, message);
3039 println!();
3040
3041 // Normal case for AES-256
3042 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3043 print!("K =\t");
3044 for i in 0..32
3045 { print!("{:02X}", key[i]); }
3046 println!();
3047 let mut a_aes = AES_256::new_with_key(&key);
3048 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3049 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3050
3051 let message = "In the beginning God created the heavens and the earth.";
3052 println!("M =\t{}", message);
3053 let mut cipher = Vec::<u8>::new();
3054 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3055 print!("C =\t");
3056 for c in cipher.clone()
3057 { print!("{:02X} ", c); }
3058 println!();
3059 let mut txt = String::new();
3060 for c in cipher.clone()
3061 { write!(txt, "{:02X} ", c); }
3062 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
3063
3064 let mut recovered = vec![0; 55];
3065 a_aes.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
3066 print!("Ba =\t");
3067 for b in recovered.clone()
3068 { print!("{:02X} ", b); }
3069 println!();
3070 let mut txt = String::new();
3071 for c in recovered.clone()
3072 { write!(txt, "{:02X} ", c); }
3073 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 ");
3074
3075 let mut converted = String::new();
3076 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3077
3078 println!("Bb =\t{}", converted);
3079 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3080 assert_eq!(converted, message);
3081 println!();
3082
3083 // Normal case for Rijndael-256-256
3084 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3085 print!("K =\t");
3086 for i in 0..32
3087 { print!("{:02X}", key[i]); }
3088 println!();
3089 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
3090 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3091 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
3092
3093 let message = "In the beginning God created the heavens and the earth.";
3094 println!("M =\t{}", message);
3095 let mut cipher = Vec::<u8>::new();
3096 a_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
3097 print!("C =\t");
3098 for c in cipher.clone()
3099 { print!("{:02X} ", c); }
3100 println!();
3101 let mut txt = String::new();
3102 for c in cipher.clone()
3103 { write!(txt, "{:02X} ", c); }
3104 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
3105
3106 let mut recovered = vec![0; 55];
3107 a_rijndael.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
3108 print!("Ba =\t");
3109 for b in recovered.clone()
3110 { print!("{:02X} ", b); }
3111 println!();
3112 let mut txt = String::new();
3113 for c in recovered.clone()
3114 { write!(txt, "{:02X} ", c); }
3115 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 ");
3116
3117 let mut converted = String::new();
3118 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3119
3120 println!("Bb =\t{}", converted);
3121 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3122 assert_eq!(converted, message);
3123 println!();
3124
3125 // Normal case for Rijndael-512-512 for post-quantum
3126 use cryptocol::number::SharedArrays;
3127 use cryptocol::hash::SHA3_512;
3128 let mut sha3 = SHA3_512::new();
3129 sha3.absorb_str("Post-quantum");
3130 let key: [u8; 64] = sha3.get_hash_value_in_array();
3131 print!("K =\t");
3132 for i in 0..64
3133 { print!("{:02X}", key[i]); }
3134 println!();
3135 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
3136 sha3.absorb_str("Initialize");
3137 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
3138 iv.src = sha3.get_hash_value_in_array();
3139 let iv = unsafe { iv.des };
3140 print!("IV =\t");
3141 for i in 0..16
3142 { print!("{:08X}", iv[i].to_be()); }
3143 println!();
3144 let message = "In the beginning God created the heavens and the earth.";
3145 println!("M =\t{}", message);
3146 let mut cipher = Vec::<u8>::new();
3147 a_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
3148 print!("C =\t");
3149 for c in cipher.clone()
3150 { print!("{:02X} ", c); }
3151 println!();
3152 let mut txt = String::new();
3153 for c in cipher.clone()
3154 { write!(txt, "{:02X} ", c); }
3155 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
3156
3157 let mut recovered = vec![0; 55];
3158 a_rijndael.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
3159 print!("Ba =\t");
3160 for b in recovered.clone()
3161 { print!("{:02X} ", b); }
3162 println!();
3163 let mut txt = String::new();
3164 for c in recovered.clone()
3165 { write!(txt, "{:02X} ", c); }
3166 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 ");
3167
3168 let mut converted = String::new();
3169 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3170
3171 println!("Bb =\t{}", converted);
3172 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3173 assert_eq!(converted, message);
3174 println!("-------------------------------");
3175}
3176
3177fn aes_decrypt_vec_ofb_into_vec()
3178{
3179 println!("aes_decrypt_vec_ofb_into_vec()");
3180 use std::io::Write;
3181 use std::fmt::Write as _;
3182 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
3183
3184 // Normal case for AES-128
3185 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
3186 println!("K =\t{:#016X}", key);
3187 let mut a_aes = AES_128::new_with_key_u128(key);
3188 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3189 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3190
3191 let message = "In the beginning God created the heavens and the earth.";
3192 println!("M =\t{}", message);
3193 let mut cipher = Vec::<u8>::new();
3194 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3195 print!("C =\t");
3196 for c in cipher.clone()
3197 { print!("{:02X} ", c); }
3198 println!();
3199 let mut txt = String::new();
3200 for c in cipher.clone()
3201 { write!(txt, "{:02X} ", c); }
3202 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
3203
3204 let mut recovered = Vec::<u8>::new();
3205 a_aes.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
3206 print!("Ba =\t");
3207 for b in recovered.clone()
3208 { print!("{:02X} ", b); }
3209 println!();
3210 let mut txt = String::new();
3211 for c in recovered.clone()
3212 { write!(txt, "{:02X} ", c); }
3213 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 ");
3214
3215 let mut converted = String::new();
3216 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3217
3218 println!("Bb =\t{}", converted);
3219 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3220 assert_eq!(converted, message);
3221 println!();
3222
3223 // Normal case for AES-192
3224 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3225 print!("K =\t");
3226 for i in 0..24
3227 { print!("{:02X}", key[i]); }
3228 println!();
3229 let mut a_aes = AES_192::new_with_key(&key);
3230 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3231 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3232
3233 let message = "In the beginning God created the heavens and the earth.";
3234 println!("M =\t{}", message);
3235 let mut cipher = Vec::<u8>::new();
3236 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3237 print!("C =\t");
3238 for c in cipher.clone()
3239 { print!("{:02X} ", c); }
3240 println!();
3241 let mut txt = String::new();
3242 for c in cipher.clone()
3243 { write!(txt, "{:02X} ", c); }
3244 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
3245
3246 let mut recovered = Vec::<u8>::new();
3247 a_aes.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
3248 print!("Ba =\t");
3249 for b in recovered.clone()
3250 { print!("{:02X} ", b); }
3251 println!();
3252 let mut txt = String::new();
3253 for c in recovered.clone()
3254 { write!(txt, "{:02X} ", c); }
3255 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
3256
3257 let mut converted = String::new();
3258 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3259
3260 println!("Bb =\t{}", converted);
3261 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3262 assert_eq!(converted, message);
3263 println!();
3264
3265 // Normal case for AES-256
3266 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3267 print!("K =\t");
3268 for i in 0..32
3269 { print!("{:02X}", key[i]); }
3270 println!();
3271 let mut a_aes = AES_256::new_with_key(&key);
3272 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3273 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3274
3275 let message = "In the beginning God created the heavens and the earth.";
3276 println!("M =\t{}", message);
3277 let mut cipher = Vec::<u8>::new();
3278 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3279 print!("C =\t");
3280 for c in cipher.clone()
3281 { print!("{:02X} ", c); }
3282 println!();
3283 let mut txt = String::new();
3284 for c in cipher.clone()
3285 { write!(txt, "{:02X} ", c); }
3286 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
3287
3288 let mut recovered = Vec::<u8>::new();
3289 a_aes.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
3290 print!("Ba =\t");
3291 for b in recovered.clone()
3292 { print!("{:02X} ", b); }
3293 println!();
3294 let mut txt = String::new();
3295 for c in recovered.clone()
3296 { write!(txt, "{:02X} ", c); }
3297 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 ");
3298
3299 let mut converted = String::new();
3300 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3301
3302 println!("Bb =\t{}", converted);
3303 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3304 assert_eq!(converted, message);
3305 println!();
3306
3307 // Normal case for Rijndael-256-256
3308 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3309 print!("K =\t");
3310 for i in 0..32
3311 { print!("{:02X}", key[i]); }
3312 println!();
3313 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
3314 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3315 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
3316
3317 let message = "In the beginning God created the heavens and the earth.";
3318 println!("M =\t{}", message);
3319 let mut cipher = Vec::<u8>::new();
3320 a_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
3321 print!("C =\t");
3322 for c in cipher.clone()
3323 { print!("{:02X} ", c); }
3324 println!();
3325 let mut txt = String::new();
3326 for c in cipher.clone()
3327 { write!(txt, "{:02X} ", c); }
3328 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
3329
3330 let mut recovered = Vec::<u8>::new();
3331 a_rijndael.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
3332 print!("Ba =\t");
3333 for b in recovered.clone()
3334 { print!("{:02X} ", b); }
3335 println!();
3336 let mut txt = String::new();
3337 for c in recovered.clone()
3338 { write!(txt, "{:02X} ", c); }
3339 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 ");
3340
3341 let mut converted = String::new();
3342 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3343
3344 println!("Bb =\t{}", converted);
3345 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3346 assert_eq!(converted, message);
3347 println!();
3348
3349 // Normal case for Rijndael-512-512 for post-quantum
3350 use cryptocol::number::SharedArrays;
3351 use cryptocol::hash::SHA3_512;
3352 let mut sha3 = SHA3_512::new();
3353 sha3.absorb_str("Post-quantum");
3354 let key: [u8; 64] = sha3.get_hash_value_in_array();
3355 print!("K =\t");
3356 for i in 0..64
3357 { print!("{:02X}", key[i]); }
3358 println!();
3359 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
3360 sha3.absorb_str("Initialize");
3361 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
3362 iv.src = sha3.get_hash_value_in_array();
3363 let iv = unsafe { iv.des };
3364 print!("IV =\t");
3365 for i in 0..16
3366 { print!("{:08X}", iv[i].to_be()); }
3367 println!();
3368
3369 let message = "In the beginning God created the heavens and the earth.";
3370 println!("M =\t{}", message);
3371 let mut cipher = Vec::<u8>::new();
3372 a_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
3373 print!("C =\t");
3374 for c in cipher.clone()
3375 { print!("{:02X} ", c); }
3376 println!();
3377 let mut txt = String::new();
3378 for c in cipher.clone()
3379 { write!(txt, "{:02X} ", c); }
3380 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
3381
3382 let mut recovered = Vec::<u8>::new();
3383 a_rijndael.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
3384 print!("Ba =\t");
3385 for b in recovered.clone()
3386 { print!("{:02X} ", b); }
3387 println!();
3388 let mut txt = String::new();
3389 for c in recovered.clone()
3390 { write!(txt, "{:02X} ", c); }
3391 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
3392
3393 let mut converted = String::new();
3394 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3395
3396 println!("Bb =\t{}", converted);
3397 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3398 assert_eq!(converted, message);
3399 println!("-------------------------------");
3400}
3401
3402fn aes_decrypt_vec_ofb_into_array()
3403{
3404 println!("aes_decrypt_vec_ofb_into_array()");
3405 use std::io::Write;
3406 use std::fmt::Write as _;
3407 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
3408
3409 // Normal case for AES-128
3410 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
3411 println!("K =\t{:#016X}", key);
3412 let mut a_aes = AES_128::new_with_key_u128(key);
3413 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3414 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3415
3416 let message = "In the beginning God created the heavens and the earth.";
3417 println!("M =\t{}", message);
3418 let mut cipher = Vec::<u8>::new();
3419 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3420 print!("C =\t");
3421 for c in cipher.clone()
3422 { print!("{:02X} ", c); }
3423 println!();
3424 let mut txt = String::new();
3425 for c in cipher.clone()
3426 { write!(txt, "{:02X} ", c); }
3427 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
3428
3429 let mut recovered = [0; 64];
3430 let len = a_aes.decrypt_vec_into_array(iv, &cipher, &mut recovered);
3431 print!("Ba =\t");
3432 for b in recovered.clone()
3433 { print!("{:02X} ", b); }
3434 println!();
3435 let mut txt = String::new();
3436 for c in recovered.clone()
3437 { write!(txt, "{:02X} ", c); }
3438 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
3439
3440 let mut converted = String::new();
3441 unsafe { converted.as_mut_vec() }.write(&recovered);
3442 unsafe { converted.as_mut_vec() }.truncate(len as usize);
3443 println!("Bb =\t{}", converted);
3444 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3445 assert_eq!(converted, message);
3446 println!();
3447
3448 // Normal case for AES-192
3449 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3450 print!("K =\t");
3451 for i in 0..24
3452 { print!("{:02X}", key[i]); }
3453 println!();
3454 let mut a_aes = AES_192::new_with_key(&key);
3455 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3456 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3457
3458 let message = "In the beginning God created the heavens and the earth.";
3459 println!("M =\t{}", message);
3460 let mut cipher = Vec::<u8>::new();
3461 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3462 print!("C =\t");
3463 for c in cipher.clone()
3464 { print!("{:02X} ", c); }
3465 println!();
3466 let mut txt = String::new();
3467 for c in cipher.clone()
3468 { write!(txt, "{:02X} ", c); }
3469 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
3470
3471 let mut recovered = [0; 64];
3472 a_aes.decrypt_vec_into_array(iv, &cipher, &mut recovered);
3473 print!("Ba =\t");
3474 for b in recovered.clone()
3475 { print!("{:02X} ", b); }
3476 println!();
3477 let mut txt = String::new();
3478 for c in recovered.clone()
3479 { write!(txt, "{:02X} ", c); }
3480 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
3481
3482 let mut converted = String::new();
3483 unsafe { converted.as_mut_vec() }.write(&recovered);
3484 unsafe { converted.as_mut_vec() }.truncate(len as usize);
3485 println!("Bb =\t{}", converted);
3486 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3487 assert_eq!(converted, message);
3488 println!();
3489
3490 // Normal case for AES-256
3491 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3492 print!("K =\t");
3493 for i in 0..32
3494 { print!("{:02X}", key[i]); }
3495 println!();
3496 let mut a_aes = AES_256::new_with_key(&key);
3497 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3498 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3499
3500 let message = "In the beginning God created the heavens and the earth.";
3501 println!("M =\t{}", message);
3502 let mut cipher = Vec::<u8>::new();
3503 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3504 print!("C =\t");
3505 for c in cipher.clone()
3506 { print!("{:02X} ", c); }
3507 println!();
3508 let mut txt = String::new();
3509 for c in cipher.clone()
3510 { write!(txt, "{:02X} ", c); }
3511 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
3512
3513 let mut recovered = [0; 64];
3514 a_aes.decrypt_vec_into_array(iv, &cipher, &mut recovered);
3515 print!("Ba =\t");
3516 for b in recovered.clone()
3517 { print!("{:02X} ", b); }
3518 println!();
3519 let mut txt = String::new();
3520 for c in recovered.clone()
3521 { write!(txt, "{:02X} ", c); }
3522 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
3523
3524 let mut converted = String::new();
3525 unsafe { converted.as_mut_vec() }.write(&recovered);
3526 unsafe { converted.as_mut_vec() }.truncate(len as usize);
3527 println!("Bb =\t{}", converted);
3528 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3529 assert_eq!(converted, message);
3530 println!();
3531
3532 // Normal case for Rijndael-256-256
3533 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3534 print!("K =\t");
3535 for i in 0..32
3536 { print!("{:02X}", key[i]); }
3537 println!();
3538 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
3539 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3540 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
3541
3542 let message = "In the beginning God created the heavens and the earth.";
3543 println!("M =\t{}", message);
3544 let mut cipher = Vec::<u8>::new();
3545 a_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
3546 print!("C =\t");
3547 for c in cipher.clone()
3548 { print!("{:02X} ", c); }
3549 println!();
3550 let mut txt = String::new();
3551 for c in cipher.clone()
3552 { write!(txt, "{:02X} ", c); }
3553 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
3554
3555 let mut recovered = [0; 64];
3556 a_rijndael.decrypt_vec_into_array(iv, &cipher, &mut recovered);
3557 print!("Ba =\t");
3558 for b in recovered.clone()
3559 { print!("{:02X} ", b); }
3560 println!();
3561 let mut txt = String::new();
3562 for c in recovered.clone()
3563 { write!(txt, "{:02X} ", c); }
3564 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
3565
3566 let mut converted = String::new();
3567 unsafe { converted.as_mut_vec() }.write(&recovered);
3568 unsafe { converted.as_mut_vec() }.truncate(len as usize);
3569 println!("Bb =\t{}", converted);
3570 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3571 assert_eq!(converted, message);
3572 println!();
3573
3574 // Normal case for Rijndael-512-512 for post-quantum
3575 use cryptocol::number::SharedArrays;
3576 use cryptocol::hash::SHA3_512;
3577 let mut sha3 = SHA3_512::new();
3578 sha3.absorb_str("Post-quantum");
3579 let key: [u8; 64] = sha3.get_hash_value_in_array();
3580 print!("K =\t");
3581 for i in 0..64
3582 { print!("{:02X}", key[i]); }
3583 println!();
3584 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
3585 sha3.absorb_str("Initialize");
3586 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
3587 iv.src = sha3.get_hash_value_in_array();
3588 let iv = unsafe { iv.des };
3589 print!("IV =\t");
3590 for i in 0..16
3591 { print!("{:08X}", iv[i].to_be()); }
3592 println!();
3593
3594 let message = "In the beginning God created the heavens and the earth.";
3595 println!("M =\t{}", message);
3596 let mut cipher = Vec::<u8>::new();
3597 a_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
3598 print!("C =\t");
3599 for c in cipher.clone()
3600 { print!("{:02X} ", c); }
3601 println!();
3602 let mut txt = String::new();
3603 for c in cipher.clone()
3604 { write!(txt, "{:02X} ", c); }
3605 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
3606
3607 let mut recovered = [0; 64];
3608 a_rijndael.decrypt_vec_into_array(iv, &cipher, &mut recovered);
3609 print!("Ba =\t");
3610 for b in recovered.clone()
3611 { print!("{:02X} ", b); }
3612 println!();
3613 let mut txt = String::new();
3614 for c in recovered.clone()
3615 { write!(txt, "{:02X} ", c); }
3616 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
3617
3618 let mut converted = String::new();
3619 unsafe { converted.as_mut_vec() }.write(&recovered);
3620 unsafe { converted.as_mut_vec() }.truncate(len as usize);
3621 println!("Bb =\t{}", converted);
3622 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3623 assert_eq!(converted, message);
3624 println!("-------------------------------");
3625}
3626
3627fn aes_decrypt_vec_ofb_into_string()
3628{
3629 println!("aes_decrypt_vec_ofb_into_string()");
3630 use std::io::Write;
3631 use std::fmt::Write as _;
3632 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
3633
3634 // Normal case for AES-128
3635 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
3636 println!("K =\t{:#016X}", key);
3637 let mut a_aes = AES_128::new_with_key_u128(key);
3638 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3639 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3640
3641 let message = "In the beginning God created the heavens and the earth.";
3642 println!("M =\t{}", message);
3643 let mut cipher = Vec::<u8>::new();
3644 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3645 print!("C =\t");
3646 for c in cipher.clone()
3647 { print!("{:02X} ", c); }
3648 println!();
3649 let mut txt = String::new();
3650 for c in cipher.clone()
3651 { write!(txt, "{:02X} ", c); }
3652 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
3653
3654 let mut converted= String::new();
3655 a_aes.decrypt_vec_into_string(iv, &cipher, &mut converted);
3656 println!("B =\t{}", converted);
3657 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3658 assert_eq!(converted, message);
3659 println!();
3660
3661 // Normal case for AES-192
3662 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3663 print!("K =\t");
3664 for i in 0..24
3665 { print!("{:02X}", key[i]); }
3666 println!();
3667 let mut a_aes = AES_192::new_with_key(&key);
3668 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3669 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3670
3671 let message = "In the beginning God created the heavens and the earth.";
3672 println!("M =\t{}", message);
3673 let mut cipher = Vec::<u8>::new();
3674 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3675 print!("C =\t");
3676 for c in cipher.clone()
3677 { print!("{:02X} ", c); }
3678 println!();
3679 let mut txt = String::new();
3680 for c in cipher.clone()
3681 { write!(txt, "{:02X} ", c); }
3682 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
3683
3684 let mut converted= String::new();
3685 a_aes.decrypt_vec_into_string(iv, &cipher, &mut converted);
3686 println!("B =\t{}", converted);
3687 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3688 assert_eq!(converted, message);
3689 println!();
3690
3691 // Normal case for AES-256
3692 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3693 print!("K =\t");
3694 for i in 0..32
3695 { print!("{:02X}", key[i]); }
3696 println!();
3697 let mut a_aes = AES_256::new_with_key(&key);
3698 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3699 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3700
3701 let message = "In the beginning God created the heavens and the earth.";
3702 println!("M =\t{}", message);
3703 let mut cipher = Vec::<u8>::new();
3704 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3705 print!("C =\t");
3706 for c in cipher.clone()
3707 { print!("{:02X} ", c); }
3708 println!();
3709 let mut txt = String::new();
3710 for c in cipher.clone()
3711 { write!(txt, "{:02X} ", c); }
3712 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
3713
3714 let mut converted= String::new();
3715 a_aes.decrypt_vec_into_string(iv, &cipher, &mut converted);
3716 println!("B =\t{}", converted);
3717 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3718 assert_eq!(converted, message);
3719 println!();
3720
3721 // Normal case for Rijndael-256-256
3722 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3723 print!("K =\t");
3724 for i in 0..32
3725 { print!("{:02X}", key[i]); }
3726 println!();
3727 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
3728 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3729 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
3730
3731 let message = "In the beginning God created the heavens and the earth.";
3732 println!("M =\t{}", message);
3733 let mut cipher = Vec::<u8>::new();
3734 a_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
3735 print!("C =\t");
3736 for c in cipher.clone()
3737 { print!("{:02X} ", c); }
3738 println!();
3739 let mut txt = String::new();
3740 for c in cipher.clone()
3741 { write!(txt, "{:02X} ", c); }
3742 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
3743
3744 let mut converted= String::new();
3745 a_rijndael.decrypt_vec_into_string(iv, &cipher, &mut converted);
3746 println!("B =\t{}", converted);
3747 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3748 assert_eq!(converted, message);
3749 println!();
3750
3751 // Normal case for Rijndael-512-512 for post-quantum
3752 use cryptocol::number::SharedArrays;
3753 use cryptocol::hash::SHA3_512;
3754 let mut sha3 = SHA3_512::new();
3755 sha3.absorb_str("Post-quantum");
3756 let key: [u8; 64] = sha3.get_hash_value_in_array();
3757 print!("K =\t");
3758 for i in 0..64
3759 { print!("{:02X}", key[i]); }
3760 println!();
3761 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
3762 sha3.absorb_str("Initialize");
3763 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
3764 iv.src = sha3.get_hash_value_in_array();
3765 let iv = unsafe { iv.des };
3766 print!("IV =\t");
3767 for i in 0..16
3768 { print!("{:08X}", iv[i].to_be()); }
3769 println!();
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_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
3775 print!("C =\t");
3776 for c in cipher.clone()
3777 { print!("{:02X} ", c); }
3778 println!();
3779 let mut txt = String::new();
3780 for c in cipher.clone()
3781 { write!(txt, "{:02X} ", c); }
3782 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
3783
3784 let mut converted= String::new();
3785 a_rijndael.decrypt_vec_into_string(iv, &cipher, &mut converted);
3786 println!("B =\t{}", converted);
3787 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3788 assert_eq!(converted, message);
3789 println!("-------------------------------");
3790}
3791
3792fn aes_decrypt_array_ofb()
3793{
3794 println!("aes_decrypt_array_ofb()");
3795 use std::io::Write;
3796 use std::fmt::Write as _;
3797 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
3798
3799 // Normal case for AES-128
3800 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
3801 println!("K =\t{:#016X}", key);
3802 let mut a_aes = AES_128::new_with_key_u128(key);
3803 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3804 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3805
3806 let message = "In the beginning God created the heavens and the earth.";
3807 println!("M =\t{}", message);
3808 let mut cipher = [0_u8; 55];
3809 a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
3810 print!("C =\t");
3811 for c in cipher.clone()
3812 { print!("{:02X} ", c); }
3813 println!();
3814 let mut txt = String::new();
3815 for c in cipher.clone()
3816 { write!(txt, "{:02X} ", c); }
3817 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
3818
3819 let mut recovered = vec![0; 55];
3820 a_aes.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
3821 print!("Ba =\t");
3822 for b in recovered.clone()
3823 { print!("{:02X} ", b); }
3824 println!();
3825 let mut txt = String::new();
3826 for c in recovered.clone()
3827 { write!(txt, "{:02X} ", c); }
3828 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 ");
3829
3830 let mut converted = String::new();
3831 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3832
3833 println!("Bb =\t{}", converted);
3834 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3835 assert_eq!(converted, message);
3836 println!();
3837
3838 // Normal case for AES-192
3839 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3840 print!("K =\t");
3841 for i in 0..24
3842 { print!("{:02X}", key[i]); }
3843 println!();
3844 let mut a_aes = AES_192::new_with_key(&key);
3845 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3846 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3847
3848 let message = "In the beginning God created the heavens and the earth.";
3849 println!("M =\t{}", message);
3850 let mut cipher = [0_u8; 55];
3851 a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
3852 print!("C =\t");
3853 for c in cipher.clone()
3854 { print!("{:02X} ", c); }
3855 println!();
3856 let mut txt = String::new();
3857 for c in cipher.clone()
3858 { write!(txt, "{:02X} ", c); }
3859 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
3860
3861 let mut recovered = vec![0; 55];
3862 a_aes.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
3863 print!("Ba =\t");
3864 for b in recovered.clone()
3865 { print!("{:02X} ", b); }
3866 println!();
3867 let mut txt = String::new();
3868 for c in recovered.clone()
3869 { write!(txt, "{:02X} ", c); }
3870 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 ");
3871
3872 let mut converted = String::new();
3873 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3874
3875 println!("Bb =\t{}", converted);
3876 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3877 assert_eq!(converted, message);
3878 println!();
3879
3880 // Normal case for AES-256
3881 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3882 print!("K =\t");
3883 for i in 0..32
3884 { print!("{:02X}", key[i]); }
3885 println!();
3886 let mut a_aes = AES_256::new_with_key(&key);
3887 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3888 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3889
3890 let message = "In the beginning God created the heavens and the earth.";
3891 println!("M =\t{}", message);
3892 let mut cipher = [0_u8; 55];
3893 a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
3894 print!("C =\t");
3895 for c in cipher.clone()
3896 { print!("{:02X} ", c); }
3897 println!();
3898 let mut txt = String::new();
3899 for c in cipher.clone()
3900 { write!(txt, "{:02X} ", c); }
3901 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
3902
3903 let mut recovered = vec![0; 55];
3904 a_aes.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
3905 print!("Ba =\t");
3906 for b in recovered.clone()
3907 { print!("{:02X} ", b); }
3908 println!();
3909 let mut txt = String::new();
3910 for c in recovered.clone()
3911 { write!(txt, "{:02X} ", c); }
3912 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 ");
3913
3914 let mut converted = String::new();
3915 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3916
3917 println!("Bb =\t{}", converted);
3918 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3919 assert_eq!(converted, message);
3920 println!();
3921
3922 // Normal case for Rijndael-256-256
3923 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3924 print!("K =\t");
3925 for i in 0..32
3926 { print!("{:02X}", key[i]); }
3927 println!();
3928 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
3929 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3930 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
3931
3932 let message = "In the beginning God created the heavens and the earth.";
3933 println!("M =\t{}", message);
3934 let mut cipher = [0_u8; 55];
3935 a_rijndael.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
3936 print!("C =\t");
3937 for c in cipher.clone()
3938 { print!("{:02X} ", c); }
3939 println!();
3940 let mut txt = String::new();
3941 for c in cipher.clone()
3942 { write!(txt, "{:02X} ", c); }
3943 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
3944
3945 let mut recovered = vec![0; 55];
3946 a_rijndael.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
3947 print!("Ba =\t");
3948 for b in recovered.clone()
3949 { print!("{:02X} ", b); }
3950 println!();
3951 let mut txt = String::new();
3952 for c in recovered.clone()
3953 { write!(txt, "{:02X} ", c); }
3954 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 ");
3955
3956 let mut converted = String::new();
3957 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3958
3959 println!("Bb =\t{}", converted);
3960 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3961 assert_eq!(converted, message);
3962 println!();
3963
3964 // Normal case for Rijndael-512-512 for post-quantum
3965 use cryptocol::number::SharedArrays;
3966 use cryptocol::hash::SHA3_512;
3967 let mut sha3 = SHA3_512::new();
3968 sha3.absorb_str("Post-quantum");
3969 let key: [u8; 64] = sha3.get_hash_value_in_array();
3970 print!("K =\t");
3971 for i in 0..64
3972 { print!("{:02X}", key[i]); }
3973 println!();
3974 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
3975 sha3.absorb_str("Initialize");
3976 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
3977 iv.src = sha3.get_hash_value_in_array();
3978 let iv = unsafe { iv.des };
3979 print!("IV =\t");
3980 for i in 0..16
3981 { print!("{:08X}", iv[i].to_be()); }
3982 println!();
3983 let message = "In the beginning God created the heavens and the earth.";
3984 println!("M =\t{}", message);
3985 let mut cipher = [0_u8; 55];
3986 a_rijndael.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
3987 print!("C =\t");
3988 for c in cipher.clone()
3989 { print!("{:02X} ", c); }
3990 println!();
3991 let mut txt = String::new();
3992 for c in cipher.clone()
3993 { write!(txt, "{:02X} ", c); }
3994 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
3995
3996 let mut recovered = vec![0; 55];
3997 a_rijndael.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
3998 print!("Ba =\t");
3999 for b in recovered.clone()
4000 { print!("{:02X} ", b); }
4001 println!();
4002 let mut txt = String::new();
4003 for c in recovered.clone()
4004 { write!(txt, "{:02X} ", c); }
4005 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 ");
4006
4007 let mut converted = String::new();
4008 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4009
4010 println!("Bb =\t{}", converted);
4011 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4012 assert_eq!(converted, message);
4013 println!("-------------------------------");
4014}
4015
4016fn aes_decrypt_array_ofb_into_vec()
4017{
4018 println!("aes_decrypt_array_ofb_into_vec()");
4019 use std::io::Write;
4020 use std::fmt::Write as _;
4021 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
4022
4023 // Normal case for AES-128
4024 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
4025 println!("K =\t{:#016X}", key);
4026 let mut a_aes = AES_128::new_with_key_u128(key);
4027 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
4028 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
4029
4030 let message = "In the beginning God created the heavens and the earth.";
4031 println!("M =\t{}", message);
4032 let mut cipher = [0_u8; 55];
4033 a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4034 print!("C =\t");
4035 for c in cipher.clone()
4036 { print!("{:02X} ", c); }
4037 println!();
4038 let mut txt = String::new();
4039 for c in cipher.clone()
4040 { write!(txt, "{:02X} ", c); }
4041 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
4042
4043 let mut recovered = vec![0; 55];
4044 a_aes.decrypt_array_into_vec(iv, &cipher, &mut recovered);
4045 print!("Ba =\t");
4046 for b in recovered.clone()
4047 { print!("{:02X} ", b); }
4048 println!();
4049 let mut txt = String::new();
4050 for c in recovered.clone()
4051 { write!(txt, "{:02X} ", c); }
4052 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 ");
4053
4054 let mut converted = String::new();
4055 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4056
4057 println!("Bb =\t{}", converted);
4058 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4059 assert_eq!(converted, message);
4060 println!();
4061
4062 // Normal case for AES-192
4063 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
4064 print!("K =\t");
4065 for i in 0..24
4066 { print!("{:02X}", key[i]); }
4067 println!();
4068 let mut a_aes = AES_192::new_with_key(&key);
4069 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
4070 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
4071
4072 let message = "In the beginning God created the heavens and the earth.";
4073 println!("M =\t{}", message);
4074 let mut cipher = [0_u8; 55];
4075 a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4076 print!("C =\t");
4077 for c in cipher.clone()
4078 { print!("{:02X} ", c); }
4079 println!();
4080 let mut txt = String::new();
4081 for c in cipher.clone()
4082 { write!(txt, "{:02X} ", c); }
4083 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
4084
4085 let mut recovered = vec![0; 55];
4086 a_aes.decrypt_array_into_vec(iv, &cipher, &mut recovered);
4087 print!("Ba =\t");
4088 for b in recovered.clone()
4089 { print!("{:02X} ", b); }
4090 println!();
4091 let mut txt = String::new();
4092 for c in recovered.clone()
4093 { write!(txt, "{:02X} ", c); }
4094 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 ");
4095
4096 let mut converted = String::new();
4097 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4098
4099 println!("Bb =\t{}", converted);
4100 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4101 assert_eq!(converted, message);
4102 println!();
4103
4104 // Normal case for AES-256
4105 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
4106 print!("K =\t");
4107 for i in 0..32
4108 { print!("{:02X}", key[i]); }
4109 println!();
4110 let mut a_aes = AES_256::new_with_key(&key);
4111 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
4112 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
4113
4114 let message = "In the beginning God created the heavens and the earth.";
4115 println!("M =\t{}", message);
4116 let mut cipher = [0_u8; 55];
4117 a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4118 print!("C =\t");
4119 for c in cipher.clone()
4120 { print!("{:02X} ", c); }
4121 println!();
4122 let mut txt = String::new();
4123 for c in cipher.clone()
4124 { write!(txt, "{:02X} ", c); }
4125 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
4126
4127 let mut recovered = vec![0; 55];
4128 a_aes.decrypt_array_into_vec(iv, &cipher, &mut recovered);
4129 print!("Ba =\t");
4130 for b in recovered.clone()
4131 { print!("{:02X} ", b); }
4132 println!();
4133 let mut txt = String::new();
4134 for c in recovered.clone()
4135 { write!(txt, "{:02X} ", c); }
4136 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 ");
4137
4138 let mut converted = String::new();
4139 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4140
4141 println!("Bb =\t{}", converted);
4142 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4143 assert_eq!(converted, message);
4144 println!();
4145
4146 // Normal case for Rijndael-256-256
4147 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
4148 print!("K =\t");
4149 for i in 0..32
4150 { print!("{:02X}", key[i]); }
4151 println!();
4152 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
4153 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
4154 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
4155
4156 let message = "In the beginning God created the heavens and the earth.";
4157 println!("M =\t{}", message);
4158 let mut cipher = [0_u8; 55];
4159 a_rijndael.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4160 print!("C =\t");
4161 for c in cipher.clone()
4162 { print!("{:02X} ", c); }
4163 println!();
4164 let mut txt = String::new();
4165 for c in cipher.clone()
4166 { write!(txt, "{:02X} ", c); }
4167 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
4168
4169 let mut recovered = vec![0; 55];
4170 a_rijndael.decrypt_array_into_vec(iv, &cipher, &mut recovered);
4171 print!("Ba =\t");
4172 for b in recovered.clone()
4173 { print!("{:02X} ", b); }
4174 println!();
4175 let mut txt = String::new();
4176 for c in recovered.clone()
4177 { write!(txt, "{:02X} ", c); }
4178 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 ");
4179
4180 let mut converted = String::new();
4181 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4182
4183 println!("Bb =\t{}", converted);
4184 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4185 assert_eq!(converted, message);
4186 println!();
4187
4188 // Normal case for Rijndael-512-512 for post-quantum
4189 use cryptocol::number::SharedArrays;
4190 use cryptocol::hash::SHA3_512;
4191 let mut sha3 = SHA3_512::new();
4192 sha3.absorb_str("Post-quantum");
4193 let key: [u8; 64] = sha3.get_hash_value_in_array();
4194 print!("K =\t");
4195 for i in 0..64
4196 { print!("{:02X}", key[i]); }
4197 println!();
4198 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
4199 sha3.absorb_str("Initialize");
4200 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
4201 iv.src = sha3.get_hash_value_in_array();
4202 let iv = unsafe { iv.des };
4203 print!("IV =\t");
4204 for i in 0..16
4205 { print!("{:08X}", iv[i].to_be()); }
4206 println!();
4207 let message = "In the beginning God created the heavens and the earth.";
4208 println!("M =\t{}", message);
4209 let mut cipher = [0_u8; 55];
4210 a_rijndael.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4211 print!("C =\t");
4212 for c in cipher.clone()
4213 { print!("{:02X} ", c); }
4214 println!();
4215 let mut txt = String::new();
4216 for c in cipher.clone()
4217 { write!(txt, "{:02X} ", c); }
4218 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
4219
4220 let mut recovered = vec![0; 55];
4221 a_rijndael.decrypt_array_into_vec(iv, &cipher, &mut recovered);
4222 print!("Ba =\t");
4223 for b in recovered.clone()
4224 { print!("{:02X} ", b); }
4225 println!();
4226 let mut txt = String::new();
4227 for c in recovered.clone()
4228 { write!(txt, "{:02X} ", c); }
4229 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 ");
4230
4231 let mut converted = String::new();
4232 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4233
4234 println!("Bb =\t{}", converted);
4235 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4236 assert_eq!(converted, message);
4237 println!("-------------------------------");
4238}
4239
4240fn aes_decrypt_array_ofb_into_array()
4241{
4242 println!("aes_decrypt_array_ofb_into_array()");
4243 use std::io::Write;
4244 use std::fmt::Write as _;
4245 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
4246
4247 // Normal case for AES-128
4248 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
4249 println!("K =\t{:#016X}", key);
4250 let mut a_aes = AES_128::new_with_key_u128(key);
4251 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
4252 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
4253
4254 let message = "In the beginning God created the heavens and the earth.";
4255 println!("M =\t{}", message);
4256 let mut cipher = [0_u8; 55];
4257 a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4258 print!("C =\t");
4259 for c in cipher.clone()
4260 { print!("{:02X} ", c); }
4261 println!();
4262 let mut txt = String::new();
4263 for c in cipher.clone()
4264 { write!(txt, "{:02X} ", c); }
4265 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
4266
4267 let mut recovered = [0; 64];
4268 let len = a_aes.decrypt_array_into_array(iv, &cipher, &mut recovered);
4269 print!("Ba =\t");
4270 for b in recovered.clone()
4271 { print!("{:02X} ", b); }
4272 println!();
4273 let mut txt = String::new();
4274 for c in recovered.clone()
4275 { write!(txt, "{:02X} ", c); }
4276 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
4277
4278 let mut converted = String::new();
4279 unsafe { converted.as_mut_vec() }.write(&recovered);
4280 unsafe { converted.as_mut_vec() }.truncate(len as usize);
4281 println!("Bb =\t{}", converted);
4282 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4283 assert_eq!(converted, message);
4284 println!();
4285
4286 // Normal case for AES-192
4287 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
4288 print!("K =\t");
4289 for i in 0..24
4290 { print!("{:02X}", key[i]); }
4291 println!();
4292 let mut a_aes = AES_192::new_with_key(&key);
4293 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
4294 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
4295
4296 let message = "In the beginning God created the heavens and the earth.";
4297 println!("M =\t{}", message);
4298 let mut cipher = [0_u8; 55];
4299 a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4300 print!("C =\t");
4301 for c in cipher.clone()
4302 { print!("{:02X} ", c); }
4303 println!();
4304 let mut txt = String::new();
4305 for c in cipher.clone()
4306 { write!(txt, "{:02X} ", c); }
4307 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
4308
4309 let mut recovered = [0; 64];
4310 let len = a_aes.decrypt_array_into_array(iv, &cipher, &mut recovered);
4311 print!("Ba =\t");
4312 for b in recovered.clone()
4313 { print!("{:02X} ", b); }
4314 println!();
4315 let mut txt = String::new();
4316 for c in recovered.clone()
4317 { write!(txt, "{:02X} ", c); }
4318 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
4319
4320 let mut converted = String::new();
4321 unsafe { converted.as_mut_vec() }.write(&recovered);
4322 unsafe { converted.as_mut_vec() }.truncate(len as usize);
4323 println!("Bb =\t{}", converted);
4324 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4325 assert_eq!(converted, message);
4326 println!();
4327
4328 // Normal case for AES-256
4329 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
4330 print!("K =\t");
4331 for i in 0..32
4332 { print!("{:02X}", key[i]); }
4333 println!();
4334 let mut a_aes = AES_256::new_with_key(&key);
4335 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
4336 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
4337
4338 let message = "In the beginning God created the heavens and the earth.";
4339 println!("M =\t{}", message);
4340 let mut cipher = [0_u8; 55];
4341 a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4342 print!("C =\t");
4343 for c in cipher.clone()
4344 { print!("{:02X} ", c); }
4345 println!();
4346 let mut txt = String::new();
4347 for c in cipher.clone()
4348 { write!(txt, "{:02X} ", c); }
4349 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
4350
4351 let mut recovered = [0; 64];
4352 let len = a_aes.decrypt_array_into_array(iv, &cipher, &mut recovered);
4353 print!("Ba =\t");
4354 for b in recovered.clone()
4355 { print!("{:02X} ", b); }
4356 println!();
4357 let mut txt = String::new();
4358 for c in recovered.clone()
4359 { write!(txt, "{:02X} ", c); }
4360 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
4361
4362 let mut converted = String::new();
4363 unsafe { converted.as_mut_vec() }.write(&recovered);
4364 unsafe { converted.as_mut_vec() }.truncate(len as usize);
4365 println!("Bb =\t{}", converted);
4366 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4367 assert_eq!(converted, message);
4368 println!();
4369
4370 // Normal case for Rijndael-256-256
4371 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
4372 print!("K =\t");
4373 for i in 0..32
4374 { print!("{:02X}", key[i]); }
4375 println!();
4376 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
4377 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
4378 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
4379
4380 let message = "In the beginning God created the heavens and the earth.";
4381 println!("M =\t{}", message);
4382 let mut cipher = [0_u8; 55];
4383 a_rijndael.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4384 print!("C =\t");
4385 for c in cipher.clone()
4386 { print!("{:02X} ", c); }
4387 println!();
4388 let mut txt = String::new();
4389 for c in cipher.clone()
4390 { write!(txt, "{:02X} ", c); }
4391 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
4392
4393 let mut recovered = [0; 64];
4394 let len = a_rijndael.decrypt_array_into_array(iv, &cipher, &mut recovered);
4395 print!("Ba =\t");
4396 for b in recovered.clone()
4397 { print!("{:02X} ", b); }
4398 println!();
4399 let mut txt = String::new();
4400 for c in recovered.clone()
4401 { write!(txt, "{:02X} ", c); }
4402 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
4403
4404 let mut converted = String::new();
4405 unsafe { converted.as_mut_vec() }.write(&recovered);
4406 unsafe { converted.as_mut_vec() }.truncate(len as usize);
4407 println!("Bb =\t{}", converted);
4408 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4409 assert_eq!(converted, message);
4410 println!();
4411
4412 // Normal case for Rijndael-512-512 for post-quantum
4413 use cryptocol::number::SharedArrays;
4414 use cryptocol::hash::SHA3_512;
4415 let mut sha3 = SHA3_512::new();
4416 sha3.absorb_str("Post-quantum");
4417 let key: [u8; 64] = sha3.get_hash_value_in_array();
4418 print!("K =\t");
4419 for i in 0..64
4420 { print!("{:02X}", key[i]); }
4421 println!();
4422 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
4423 sha3.absorb_str("Initialize");
4424 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
4425 iv.src = sha3.get_hash_value_in_array();
4426 let iv = unsafe { iv.des };
4427 print!("IV =\t");
4428 for i in 0..16
4429 { print!("{:08X}", iv[i].to_be()); }
4430 println!();
4431 let message = "In the beginning God created the heavens and the earth.";
4432 println!("M =\t{}", message);
4433 let mut cipher = [0_u8; 55];
4434 a_rijndael.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4435 print!("C =\t");
4436 for c in cipher.clone()
4437 { print!("{:02X} ", c); }
4438 println!();
4439 let mut txt = String::new();
4440 for c in cipher.clone()
4441 { write!(txt, "{:02X} ", c); }
4442 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
4443
4444 let mut recovered = [0; 64];
4445 let len = a_rijndael.decrypt_array_into_array(iv, &cipher, &mut recovered);
4446 print!("Ba =\t");
4447 for b in recovered.clone()
4448 { print!("{:02X} ", b); }
4449 println!();
4450 let mut txt = String::new();
4451 for c in recovered.clone()
4452 { write!(txt, "{:02X} ", c); }
4453 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
4454
4455 let mut converted = String::new();
4456 unsafe { converted.as_mut_vec() }.write(&recovered);
4457 unsafe { converted.as_mut_vec() }.truncate(len as usize);
4458 println!("Bb =\t{}", converted);
4459 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4460 assert_eq!(converted, message);
4461 println!("-------------------------------");
4462}
4463
4464fn aes_decrypt_array_ofb_into_string()
4465{
4466 println!("aes_decrypt_array_ofb_into_string()");
4467 use std::io::Write;
4468 use std::fmt::Write as _;
4469 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
4470
4471 // Normal case for AES-128
4472 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
4473 println!("K =\t{:#016X}", key);
4474 let mut a_aes = AES_128::new_with_key_u128(key);
4475 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
4476 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
4477
4478 let message = "In the beginning God created the heavens and the earth.";
4479 println!("M =\t{}", message);
4480 let mut cipher = [0_u8; 55];
4481 a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4482 print!("C =\t");
4483 for c in cipher.clone()
4484 { print!("{:02X} ", c); }
4485 println!();
4486 let mut txt = String::new();
4487 for c in cipher.clone()
4488 { write!(txt, "{:02X} ", c); }
4489 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
4490
4491 let mut converted= String::new();
4492 a_aes.decrypt_array_into_string(iv, &cipher, &mut converted);
4493 println!("B =\t{}", converted);
4494 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4495 assert_eq!(converted, message);
4496 println!();
4497
4498 // Normal case for AES-192
4499 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
4500 print!("K =\t");
4501 for i in 0..24
4502 { print!("{:02X}", key[i]); }
4503 println!();
4504 let mut a_aes = AES_192::new_with_key(&key);
4505 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
4506 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
4507
4508 let message = "In the beginning God created the heavens and the earth.";
4509 println!("M =\t{}", message);
4510 let mut cipher = [0_u8; 55];
4511 a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4512 print!("C =\t");
4513 for c in cipher.clone()
4514 { print!("{:02X} ", c); }
4515 println!();
4516 let mut txt = String::new();
4517 for c in cipher.clone()
4518 { write!(txt, "{:02X} ", c); }
4519 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
4520
4521 let mut converted= String::new();
4522 a_aes.decrypt_array_into_string(iv, &cipher, &mut converted);
4523 println!("B =\t{}", converted);
4524 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4525 assert_eq!(converted, message);
4526 println!();
4527
4528 // Normal case for AES-256
4529 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
4530 print!("K =\t");
4531 for i in 0..32
4532 { print!("{:02X}", key[i]); }
4533 println!();
4534 let mut a_aes = AES_256::new_with_key(&key);
4535 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
4536 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
4537
4538 let message = "In the beginning God created the heavens and the earth.";
4539 println!("M =\t{}", message);
4540 let mut cipher = [0_u8; 55];
4541 a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4542 print!("C =\t");
4543 for c in cipher.clone()
4544 { print!("{:02X} ", c); }
4545 println!();
4546 let mut txt = String::new();
4547 for c in cipher.clone()
4548 { write!(txt, "{:02X} ", c); }
4549 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
4550
4551 let mut converted= String::new();
4552 a_aes.decrypt_array_into_string(iv, &cipher, &mut converted);
4553 println!("B =\t{}", converted);
4554 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4555 assert_eq!(converted, message);
4556 println!();
4557
4558 // Normal case for Rijndael-256-256
4559 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
4560 print!("K =\t");
4561 for i in 0..32
4562 { print!("{:02X}", key[i]); }
4563 println!();
4564 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
4565 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
4566 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
4567
4568 let message = "In the beginning God created the heavens and the earth.";
4569 println!("M =\t{}", message);
4570 let mut cipher = [0_u8; 55];
4571 a_rijndael.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4572 print!("C =\t");
4573 for c in cipher.clone()
4574 { print!("{:02X} ", c); }
4575 println!();
4576 let mut txt = String::new();
4577 for c in cipher.clone()
4578 { write!(txt, "{:02X} ", c); }
4579 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
4580
4581 let mut converted= String::new();
4582 a_rijndael.decrypt_array_into_string(iv, &cipher, &mut converted);
4583 println!("B =\t{}", converted);
4584 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4585 assert_eq!(converted, message);
4586 println!();
4587
4588 // Normal case for Rijndael-512-512 for post-quantum
4589 use cryptocol::number::SharedArrays;
4590 use cryptocol::hash::SHA3_512;
4591 let mut sha3 = SHA3_512::new();
4592 sha3.absorb_str("Post-quantum");
4593 let key: [u8; 64] = sha3.get_hash_value_in_array();
4594 print!("K =\t");
4595 for i in 0..64
4596 { print!("{:02X}", key[i]); }
4597 println!();
4598 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
4599 sha3.absorb_str("Initialize");
4600 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
4601 iv.src = sha3.get_hash_value_in_array();
4602 let iv = unsafe { iv.des };
4603 print!("IV =\t");
4604 for i in 0..16
4605 { print!("{:08X}", iv[i].to_be()); }
4606 println!();
4607 let message = "In the beginning God created the heavens and the earth.";
4608 println!("M =\t{}", message);
4609 let mut cipher = [0_u8; 55];
4610 a_rijndael.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4611 print!("C =\t");
4612 for c in cipher.clone()
4613 { print!("{:02X} ", c); }
4614 println!();
4615 let mut txt = String::new();
4616 for c in cipher.clone()
4617 { write!(txt, "{:02X} ", c); }
4618 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
4619
4620 let mut converted= String::new();
4621 a_rijndael.decrypt_array_into_string(iv, &cipher, &mut converted);
4622 println!("B =\t{}", converted);
4623 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4624 assert_eq!(converted, message);
4625 println!("-------------------------------");
4626}995fn des_encrypt_str_ofb_into_array()
996{
997 println!("des_encrypt_str_ofb_into_array()");
998 use std::io::Write;
999 use std::fmt::Write as _;
1000 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
1001
1002 // Normal case
1003 let key = 0x_1234567890ABCDEF_u64;
1004 println!("K =\t{:#016X}", key);
1005 let mut a_des = DES::new_with_key_u64(key);
1006
1007 let message = "In the beginning God created the heavens and the earth.";
1008 println!("M =\t{}", message);
1009 let iv = 0x_FEDCBA0987654321_u64;
1010 println!("IV = {}", iv);
1011 let mut cipher = [0_u8; 55];
1012 a_des.encrypt_str_into_array(iv, &message, &mut cipher);
1013 print!("C (16 rounds) =\t");
1014 for c in cipher.clone()
1015 { print!("{:02X} ", c); }
1016 println!();
1017 let mut txt = String::new();
1018 for c in cipher.clone()
1019 { write!(txt, "{:02X} ", c); }
1020 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
1021 println!();
1022
1023 // Expanded case for 128 rounds
1024 let key = 0x_1234567890ABCDEF_u64;
1025 println!("K =\t{:#016X}", key);
1026 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
1027
1028 let message = "In the beginning God created the heavens and the earth.";
1029 println!("M =\t{}", message);
1030 let iv = 0x_FEDCBA0987654321_u64;
1031 println!("IV = {}", iv);
1032 let mut cipher = [0_u8; 55];
1033 a_des.encrypt_str_into_array(iv, &message, &mut cipher);
1034 print!("C (128 rounds) =\t");
1035 for c in cipher.clone()
1036 { print!("{:02X} ", c); }
1037 println!();
1038 let mut txt = String::new();
1039 for c in cipher.clone()
1040 { write!(txt, "{:02X} ", c); }
1041 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
1042 println!();
1043
1044 // Expanded case for 0 rounds which means that key is meaningless
1045 let key1 = 0x_1234567890ABCDEF_u64;
1046 let key2 = 0_u64;
1047 println!("K =\t{:#016X}", key);
1048 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
1049 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
1050
1051 let message = "In the beginning God created the heavens and the earth.";
1052 println!("M =\t{}", message);
1053 let iv = 0x_FEDCBA0987654321_u64;
1054 println!("IV = {}", iv);
1055 let mut cipher1 = [0_u8; 55];
1056 let mut cipher2 = [0_u8; 55];
1057 c_des.encrypt_str_into_array(iv, &message, &mut cipher1);
1058 d_des.encrypt_str_into_array(iv, &message, &mut cipher2);
1059 print!("C (0 rounds) =\t");
1060 for c in cipher1.clone()
1061 { print!("{:02X} ", c); }
1062 println!();
1063 let mut txt = String::new();
1064 for c in cipher1.clone()
1065 { write!(txt, "{:02X} ", c); }
1066 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
1067 print!("D (0 rounds) =\t");
1068 for c in cipher2.clone()
1069 { print!("{:02X} ", c); }
1070 println!();
1071 let mut txt = String::new();
1072 for c in cipher2.clone()
1073 { write!(txt, "{:02X} ", c); }
1074 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
1075 println!();
1076
1077 // Normal case for the message of 0 bytes
1078 let key = 0x_1234567890ABCDEF_u64;
1079 println!("K =\t{:#016X}", key);
1080 let mut a_des = DES::new_with_key_u64(key);
1081
1082 let message = "";
1083 println!("M =\t{}", message);
1084 let iv = 0x_FEDCBA0987654321_u64;
1085 println!("IV = {}", iv);
1086 let mut cipher = [0_u8; 0];
1087 a_des.encrypt_str_into_array(iv, &message, &mut cipher);
1088 print!("C =\t");
1089 for c in cipher.clone()
1090 { print!("{:02X} ", c); }
1091 println!();
1092 let mut txt = String::new();
1093 for c in cipher.clone()
1094 { write!(txt, "{:02X} ", c); }
1095 assert_eq!(txt, "");
1096 println!();
1097
1098 // Normal case for the message shorter than 8 bytes
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 = "7 bytes";
1104 println!("M =\t{}", message);
1105 let iv = 0x_FEDCBA0987654321_u64;
1106 println!("IV = {}", iv);
1107 let mut cipher = [0_u8; 7];
1108 a_des.encrypt_str_into_array(iv, &message, &mut cipher);
1109 print!("C =\t");
1110 for c in cipher.clone()
1111 { print!("{:02X} ", c); }
1112 println!();
1113 let mut txt = String::new();
1114 for c in cipher.clone()
1115 { write!(txt, "{:02X} ", c); }
1116 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
1117 println!();
1118
1119 // Normal case for the message of 8 bytes
1120 let key = 0x_1234567890ABCDEF_u64;
1121 println!("K =\t{:#016X}", key);
1122 let mut a_des = DES::new_with_key_u64(key);
1123
1124 let message = "I am OK.";
1125 println!("M =\t{}", message);
1126 let iv = 0x_FEDCBA0987654321_u64;
1127 println!("IV = {}", iv);
1128 let mut cipher = [0_u8; 8];
1129 a_des.encrypt_str_into_array(iv, &message, &mut cipher);
1130 print!("C =\t");
1131 for c in cipher.clone()
1132 { print!("{:02X} ", c); }
1133 println!();
1134 let mut txt = String::new();
1135 for c in cipher.clone()
1136 { write!(txt, "{:02X} ", c); }
1137 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
1138 println!();
1139
1140 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
1141 let key = 0x_1234567890ABCDEF_u64;
1142 println!("K =\t{:#016X}", key);
1143 let mut a_des = DES::new_with_key_u64(key);
1144
1145 let message = "PARK Youngho";
1146 println!("M =\t{}", message);
1147 let iv = 0x_FEDCBA0987654321_u64;
1148 println!("IV = {}", iv);
1149 let mut cipher = [0_u8; 12];
1150 a_des.encrypt_str_into_array(iv, &message, &mut cipher);
1151 print!("C =\t");
1152 for c in cipher.clone()
1153 { print!("{:02X} ", c); }
1154 println!();
1155 let mut txt = String::new();
1156 for c in cipher.clone()
1157 { write!(txt, "{:02X} ", c); }
1158 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
1159 println!();
1160
1161 // Normal case for the message of 16 bytes
1162 let key = 0x_1234567890ABCDEF_u64;
1163 println!("K =\t{:#016X}", key);
1164 let mut a_des = DES::new_with_key_u64(key);
1165
1166 let message = "고맙습니다.";
1167 println!("M =\t{}", message);
1168 let iv = 0x_FEDCBA0987654321_u64;
1169 println!("IV = {}", iv);
1170 let mut cipher = [0_u8; 16];
1171 a_des.encrypt_str_into_array(iv, &message, &mut cipher);
1172 print!("C =\t");
1173 for c in cipher.clone()
1174 { print!("{:02X} ", c); }
1175 println!();
1176 let mut txt = String::new();
1177 for c in cipher.clone()
1178 { write!(txt, "{:02X} ", c); }
1179 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
1180 println!("-------------------------------");
1181}Sourcefn encrypt_string(&mut self, iv: T, message: &String, cipher: *mut u8) -> u64
fn encrypt_string(&mut self, iv: T, message: &String, cipher: *mut u8) -> u64
Encrypts the data stored in a String object without any padding
in OFB (Output FeedBack) mode.
§Arguments
ivis an initialization vector for OFB mode.messageis an immutable reference toStringobject, and is the place where the plaintext to be encrypted is stored.cipheris a mutable pointer tou8which is*mut u8, and is the place where the encrypted data will be stored.
§Output
- This method returns the size of ciphertext.
- If this method failed in encryption or
message.len()is0, this method returnszero.
§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
messageis aStringobject that has a null string “”, nothing will be encrypted, and stored in the memory area that starts fromcipher. - The size of the memory area which starts at
cipheris assumed to be enough to store the ciphertext. - The size of the area for ciphertext should be prepared to be
message.len()at least. So, it is responsible for you to prepare thecipherarea big enough!
§For Rijndael or AES, and its variants
§Example 1 for AES-128
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ AES_128, CBC_ISO };
let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
println!("K =\t{:#016X}", key);
let mut a_aes = AES_128::new_with_key_u128(key);
let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
let message = "In the beginning God created the heavens and the earth.".to_string();
println!("M =\t{}", message);
let mut cipher = [0_u8; 55];
a_aes.encrypt_string(iv, &message, cipher.as_mut_ptr());
print!("C =\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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 07 AF 4E C8 EF 5D 0A 74 97 3F 90 9E 05 9E 9E 32 FB 55 54 45 7C ED A2 2B F8 07 66 C0 7B CB 98 F3 BF 93 15 BA 26 1C 47 ");§For more examples,
click here
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, OFB };
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 iv = 0x_FEDCBA0987654321_u64;
println!("IV = {}", iv);
let mut cipher = [0_u8; 55];
a_des.encrypt_string(iv, &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, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");§For more examples,
click here
§For BigCryptor128
§Example 1 for TAES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
+ AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
+ AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
println!("IV = {:#034X}", iv);
let message = "In the beginning God created the heavens and the earth.".to_string();
let mut cipher = [0_u8; 55];
taes.encrypt_string(iv, &message, cipher.as_mut_ptr());
print!("C =\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, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");§For more examples,
click here
§For BigCryptor64
§Example 1 for TDES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
+ DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
+ DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
let iv = 0x_FEDCBA0987654321_u64;
println!("IV = {:#018X}", iv);
let message = "In the beginning God created the heavens and the earth.".to_string();
let mut cipher = [0_u8; 55];
tdes.encrypt_string(iv, &message, cipher.as_mut_ptr());
print!("C =\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, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");§For more examples,
click here
Examples found in repository?
220fn bigcryptor64_encrypt_string_ofb()
221{
222 println!("bigcryptor64_encrypt_string_ofb()");
223 use std::io::Write;
224 use std::fmt::Write as _;
225 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
226
227 // TDES case
228 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
229 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
230 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
231 let iv = 0x_FEDCBA0987654321_u64;
232 println!("IV = {:#018X}", iv);
233 let message = "In the beginning God created the heavens and the earth.".to_string();
234 let mut cipher = [0_u8; 55];
235 tdes.encrypt_string(iv, &message, cipher.as_mut_ptr());
236 print!("C =\t");
237 for c in cipher.clone()
238 { print!("{:02X} ", c); }
239 println!();
240 let mut txt = String::new();
241 for c in cipher.clone()
242 { write!(txt, "{:02X} ", c); }
243 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
244 println!("-------------------------------");
245}More examples
220fn bigcryptor128_encrypt_string_ofb()
221{
222 println!("bigcryptor128_encrypt_string_ofb()");
223 use std::io::Write;
224 use std::fmt::Write as _;
225 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
226
227 // TAES_128 case
228 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
229 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
230 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
231 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
232 println!("IV = {:#034X}", iv);
233 let message = "In the beginning God created the heavens and the earth.".to_string();
234 let mut cipher = [0_u8; 55];
235 taes.encrypt_string(iv, &message, cipher.as_mut_ptr());
236 print!("C =\t");
237 for c in cipher.clone()
238 { print!("{:02X} ", c); }
239 println!();
240 let mut txt = String::new();
241 for c in cipher.clone()
242 { write!(txt, "{:02X} ", c); }
243 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
244 println!("-------------------------------");
245}853fn aes_encrypt_string_ofb()
854{
855 println!("aes_encrypt_string_ofb()");
856 use std::io::Write;
857 use std::fmt::Write as _;
858 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
859
860 // Normal case for AES-128
861 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
862 println!("K =\t{:#016X}", key);
863 let mut a_aes = AES_128::new_with_key_u128(key);
864 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
865 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
866
867 let message = "In the beginning God created the heavens and the earth.".to_string();
868 println!("M =\t{}", message);
869 let mut cipher = [0_u8; 55];
870 a_aes.encrypt_string(iv, &message, cipher.as_mut_ptr());
871 print!("C =\t");
872 for c in cipher.clone()
873 { print!("{:02X} ", c); }
874 println!();
875 let mut txt = String::new();
876 for c in cipher.clone()
877 { write!(txt, "{:02X} ", c); }
878 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
879 println!();
880
881 // Normal case for AES-192
882 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
883 print!("K =\t");
884 for i in 0..24
885 { print!("{:02X}", key[i]); }
886 println!();
887 let mut a_aes = AES_192::new_with_key(&key);
888 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
889 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
890
891 let message = "In the beginning God created the heavens and the earth.".to_string();
892 println!("M =\t{}", message);
893 let mut cipher = [0_u8; 55];
894 a_aes.encrypt_string(iv, &message, cipher.as_mut_ptr());
895 print!("C =\t");
896 for c in cipher.clone()
897 { print!("{:02X} ", c); }
898 println!();
899 let mut txt = String::new();
900 for c in cipher.clone()
901 { write!(txt, "{:02X} ", c); }
902 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
903 println!();
904
905 // Normal case for AES-256
906 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
907 print!("K =\t");
908 for i in 0..32
909 { print!("{:02X}", key[i]); }
910 println!();
911 let mut a_aes = AES_256::new_with_key(&key);
912 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
913 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
914
915 let message = "In the beginning God created the heavens and the earth.".to_string();
916 println!("M =\t{}", message);
917 let mut cipher = [0_u8; 55];
918 a_aes.encrypt_string(iv, &message, cipher.as_mut_ptr());
919 print!("C =\t");
920 for c in cipher.clone()
921 { print!("{:02X} ", c); }
922 println!();
923 let mut txt = String::new();
924 for c in cipher.clone()
925 { write!(txt, "{:02X} ", c); }
926 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
927 println!();
928
929 // Normal case for Rijndael-256-256
930 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
931 print!("K =\t");
932 for i in 0..32
933 { print!("{:02X}", key[i]); }
934 println!();
935 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
936 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
937 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
938
939 let message = "In the beginning God created the heavens and the earth.".to_string();
940 println!("M =\t{}", message);
941 let mut cipher = [0_u8; 55];
942 a_rijndael.encrypt_string(iv, &message, cipher.as_mut_ptr());
943 print!("C =\t");
944 for c in cipher.clone()
945 { print!("{:02X} ", c); }
946 println!();
947 let mut txt = String::new();
948 for c in cipher.clone()
949 { write!(txt, "{:02X} ", c); }
950 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
951 println!();
952
953 // Normal case for Rijndael-512-512 for post-quantum
954 use cryptocol::number::SharedArrays;
955 use cryptocol::hash::SHA3_512;
956 let mut sha3 = SHA3_512::new();
957 sha3.absorb_str("Post-quantum");
958 let key: [u8; 64] = sha3.get_hash_value_in_array();
959 print!("K =\t");
960 for i in 0..64
961 { print!("{:02X}", key[i]); }
962 println!();
963 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
964 sha3.absorb_str("Initialize");
965 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
966 iv.src = sha3.get_hash_value_in_array();
967 let iv = unsafe { iv.des };
968 print!("IV =\t");
969 for i in 0..16
970 { print!("{:08X}", iv[i].to_be()); }
971 println!();
972
973 let message = "In the beginning God created the heavens and the earth.".to_string();
974 println!("M =\t{}", message);
975 let mut cipher = [0_u8; 55];
976 a_rijndael.encrypt_string(iv, &message, cipher.as_mut_ptr());
977 print!("C =\t");
978 for c in cipher.clone()
979 { print!("{:02X} ", c); }
980 println!();
981 let mut txt = String::new();
982 for c in cipher.clone()
983 { write!(txt, "{:02X} ", c); }
984 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
985 println!("-------------------------------");
986}1183fn des_encrypt_string_ofb()
1184{
1185 println!("des_encrypt_string_ofb()");
1186 use std::io::Write;
1187 use std::fmt::Write as _;
1188 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
1189
1190 // Normal case
1191 let key = 0x_1234567890ABCDEF_u64;
1192 println!("K =\t{:#016X}", key);
1193 let mut a_des = DES::new_with_key_u64(key);
1194
1195 let message = "In the beginning God created the heavens and the earth.".to_string();
1196 println!("M =\t{}", message);
1197 let iv = 0x_FEDCBA0987654321_u64;
1198 println!("IV = {}", iv);
1199 let mut cipher = [0_u8; 55];
1200 a_des.encrypt_string(iv, &message, cipher.as_mut_ptr());
1201 print!("C (16 rounds) =\t");
1202 for c in cipher.clone()
1203 { print!("{:02X} ", c); }
1204 println!();
1205 let mut txt = String::new();
1206 for c in cipher.clone()
1207 { write!(txt, "{:02X} ", c); }
1208 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
1209 println!();
1210
1211 // Expanded case for 128 rounds
1212 let key = 0x_1234567890ABCDEF_u64;
1213 println!("K =\t{:#016X}", key);
1214 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
1215
1216 let message = "In the beginning God created the heavens and the earth.".to_string();
1217 println!("M =\t{}", message);
1218 let iv = 0x_FEDCBA0987654321_u64;
1219 println!("IV = {}", iv);
1220 let mut cipher = [0_u8; 55];
1221 a_des.encrypt_string(iv, &message, cipher.as_mut_ptr());
1222 print!("C (128 rounds) =\t");
1223 for c in cipher.clone()
1224 { print!("{:02X} ", c); }
1225 println!();
1226 let mut txt = String::new();
1227 for c in cipher.clone()
1228 { write!(txt, "{:02X} ", c); }
1229 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
1230 println!();
1231
1232 // Expanded case for 0 rounds which means that key is meaningless
1233 let key1 = 0x_1234567890ABCDEF_u64;
1234 let key2 = 0_u64;
1235 println!("K =\t{:#016X}", key);
1236 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
1237 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
1238
1239 let message = "In the beginning God created the heavens and the earth.".to_string();
1240 println!("M =\t{}", message);
1241 let iv = 0x_FEDCBA0987654321_u64;
1242 println!("IV = {}", iv);
1243 let mut cipher1 = [0_u8; 55];
1244 let mut cipher2 = [0_u8; 55];
1245 c_des.encrypt_string(iv, &message, cipher1.as_mut_ptr());
1246 d_des.encrypt_string(iv, &message, cipher2.as_mut_ptr());
1247 print!("C (0 rounds) =\t");
1248 for c in cipher1.clone()
1249 { print!("{:02X} ", c); }
1250 println!();
1251 let mut txt = String::new();
1252 for c in cipher1.clone()
1253 { write!(txt, "{:02X} ", c); }
1254 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
1255 print!("D (0 rounds) =\t");
1256 for c in cipher2.clone()
1257 { print!("{:02X} ", c); }
1258 println!();
1259 let mut txt = String::new();
1260 for c in cipher2.clone()
1261 { write!(txt, "{:02X} ", c); }
1262 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
1263 println!();
1264
1265 // Normal case for the message of 0 bytes
1266 let key = 0x_1234567890ABCDEF_u64;
1267 println!("K =\t{:#016X}", key);
1268 let mut a_des = DES::new_with_key_u64(key);
1269
1270 let message = "".to_string();
1271 println!("M =\t{}", message);
1272 let iv = 0x_FEDCBA0987654321_u64;
1273 println!("IV = {}", iv);
1274 let mut cipher = [0_u8; 0];
1275 a_des.encrypt_string(iv, &message, cipher.as_mut_ptr());
1276 print!("C =\t");
1277 for c in cipher.clone()
1278 { print!("{:02X} ", c); }
1279 println!();
1280 let mut txt = String::new();
1281 for c in cipher.clone()
1282 { write!(txt, "{:02X} ", c); }
1283 assert_eq!(txt, "");
1284 println!();
1285
1286 // Normal case for the message shorter than 8 bytes
1287 let key = 0x_1234567890ABCDEF_u64;
1288 println!("K =\t{:#016X}", key);
1289 let mut a_des = DES::new_with_key_u64(key);
1290
1291 let message = "7 bytes".to_string();
1292 println!("M =\t{}", message);
1293 let iv = 0x_FEDCBA0987654321_u64;
1294 println!("IV = {}", iv);
1295 let mut cipher = [0_u8; 7];
1296 a_des.encrypt_string(iv, &message, cipher.as_mut_ptr());
1297 print!("C =\t");
1298 for c in cipher.clone()
1299 { print!("{:02X} ", c); }
1300 println!();
1301 let mut txt = String::new();
1302 for c in cipher.clone()
1303 { write!(txt, "{:02X} ", c); }
1304 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
1305 println!();
1306
1307 // Normal case for the message of 8 bytes
1308 let key = 0x_1234567890ABCDEF_u64;
1309 println!("K =\t{:#016X}", key);
1310 let mut a_des = DES::new_with_key_u64(key);
1311
1312 let message = "I am OK.".to_string();
1313 println!("M =\t{}", message);
1314 let iv = 0x_FEDCBA0987654321_u64;
1315 println!("IV = {}", iv);
1316 let mut cipher = [0_u8; 8];
1317 a_des.encrypt_string(iv, &message, cipher.as_mut_ptr());
1318 print!("C =\t");
1319 for c in cipher.clone()
1320 { print!("{:02X} ", c); }
1321 println!();
1322 let mut txt = String::new();
1323 for c in cipher.clone()
1324 { write!(txt, "{:02X} ", c); }
1325 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
1326 println!();
1327
1328 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
1329 let key = 0x_1234567890ABCDEF_u64;
1330 println!("K =\t{:#016X}", key);
1331 let mut a_des = DES::new_with_key_u64(key);
1332
1333 let message = "PARK Youngho".to_string();
1334 println!("M =\t{}", message);
1335 let iv = 0x_FEDCBA0987654321_u64;
1336 println!("IV = {}", iv);
1337 let mut cipher = [0_u8; 12];
1338 a_des.encrypt_string(iv, &message, cipher.as_mut_ptr());
1339 print!("C =\t");
1340 for c in cipher.clone()
1341 { print!("{:02X} ", c); }
1342 println!();
1343 let mut txt = String::new();
1344 for c in cipher.clone()
1345 { write!(txt, "{:02X} ", c); }
1346 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
1347 println!();
1348
1349 // Normal case for the message of 16 bytes
1350 let key = 0x_1234567890ABCDEF_u64;
1351 println!("K =\t{:#016X}", key);
1352 let mut a_des = DES::new_with_key_u64(key);
1353
1354 let message = "고맙습니다.".to_string();
1355 println!("M =\t{}", message);
1356 let iv = 0x_FEDCBA0987654321_u64;
1357 println!("IV = {}", iv);
1358 let mut cipher = [0_u8; 16];
1359 a_des.encrypt_string(iv, &message, cipher.as_mut_ptr());
1360 print!("C =\t");
1361 for c in cipher.clone()
1362 { print!("{:02X} ", c); }
1363 println!();
1364 let mut txt = String::new();
1365 for c in cipher.clone()
1366 { write!(txt, "{:02X} ", c); }
1367 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
1368 println!("-------------------------------");
1369}Sourcefn encrypt_string_into_vec<U>(
&mut self,
iv: T,
message: &String,
cipher: &mut Vec<U>,
) -> u64
fn encrypt_string_into_vec<U>( &mut self, iv: T, message: &String, cipher: &mut Vec<U>, ) -> u64
Encrypts the data stored in a String object without any padding in
OFB (Output FeedBack) mode, and stores the encrypted data in Vec<U>.
§Arguments
ivis an initialization vector for OFB mode.messageis an immutable reference toStringobject, and is the place where the plaintext to be encrypted is stored.cipheris a mutable reference toVec<U>object, and is the place where the encrypted data will be stored.
§Output
- This method returns the size of ciphertext.
- If this method failed in encryption or
message.len()is0, this method returnszero.
§Features
- If
messageis aStringobject that has a null string “”, nothing will be encrypted, and stored in theVec<U>object which is referred to ascipher. - 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 Rijndael or AES, and its variants
§Example 1 for AES-128
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ AES_128, CBC_ISO };
let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
println!("K =\t{:#016X}", key);
let mut a_aes = AES_128::new_with_key_u128(key);
let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
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_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
print!("C =\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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 07 AF 4E C8 EF 5D 0A 74 97 3F 90 9E 05 9E 9E 32 FB 55 54 45 7C ED A2 2B F8 07 66 C0 7B CB 98 F3 BF 93 15 BA 26 1C 47 ");§For more examples,
click here
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, OFB };
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 iv = 0x_FEDCBA0987654321_u64;
println!("IV = {}", iv);
let mut cipher = Vec::<u8>::new();
a_des.encrypt_string_into_vec(iv, &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, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");§For more examples,
click here
§For BigCryptor128
§Example 1 for TAES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
+ AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
+ AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
println!("IV = {:#034X}", iv);
let message = "In the beginning God created the heavens and the earth.".to_string();
let mut cipher = Vec::<u8>::new();
taes.encrypt_string_into_vec(iv, &message, &mut cipher);
print!("C =\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, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");§For more examples,
click here
§For BigCryptor64
§Example 1 for TDES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
+ DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
+ DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
let iv = 0x_FEDCBA0987654321_u64;
println!("IV = {:#018X}", iv);
let message = "In the beginning God created the heavens and the earth.".to_string();
let mut cipher = Vec::<u8>::new();
tdes.encrypt_string_into_vec(iv, &message, &mut cipher);
print!("C =\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, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");§For more examples,
click here
Examples found in repository?
247fn bigcryptor64_encrypt_string_ofb_into_vec()
248{
249 println!("bigcryptor64_encrypt_string_ofb_into_vec()");
250 use std::io::Write;
251 use std::fmt::Write as _;
252 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
253
254 // TDES case
255 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
256 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
257 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
258 let iv = 0x_FEDCBA0987654321_u64;
259 println!("IV = {:#018X}", iv);
260 let message = "In the beginning God created the heavens and the earth.".to_string();
261 let mut cipher = Vec::<u8>::new();
262 tdes.encrypt_string_into_vec(iv, &message, &mut cipher);
263 print!("C =\t");
264 for c in cipher.clone()
265 { print!("{:02X} ", c); }
266 println!();
267 let mut txt = String::new();
268 for c in cipher.clone()
269 { write!(txt, "{:02X} ", c); }
270 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
271 println!("-------------------------------");
272}More examples
247fn bigcryptor128_encrypt_string_ofb_into_vec()
248{
249 println!("bigcryptor128_encrypt_string_ofb_into_vec()");
250 use std::io::Write;
251 use std::fmt::Write as _;
252 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
253
254 // TAES_128 case
255 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
256 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
257 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
258 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
259 println!("IV = {:#034X}", iv);
260 let message = "In the beginning God created the heavens and the earth.".to_string();
261 let mut cipher = Vec::<u8>::new();
262 taes.encrypt_string_into_vec(iv, &message, &mut cipher);
263 print!("C =\t");
264 for c in cipher.clone()
265 { print!("{:02X} ", c); }
266 println!();
267 let mut txt = String::new();
268 for c in cipher.clone()
269 { write!(txt, "{:02X} ", c); }
270 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
271 println!("-------------------------------");
272}988fn aes_encrypt_string_ofb_into_vec()
989{
990 println!("aes_encrypt_string_ofb_into_vec()");
991 use std::io::Write;
992 use std::fmt::Write as _;
993 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
994
995 // Normal case for AES-128
996 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
997 println!("K =\t{:#016X}", key);
998 let mut a_aes = AES_128::new_with_key_u128(key);
999 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1000 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1001
1002 let message = "In the beginning God created the heavens and the earth.".to_string();
1003 println!("M =\t{}", message);
1004 let mut cipher = Vec::<u8>::new();
1005 a_aes.encrypt_string_into_vec(iv, &message, &mut cipher);
1006 print!("C =\t");
1007 for c in cipher.clone()
1008 { print!("{:02X} ", c); }
1009 println!();
1010 let mut txt = String::new();
1011 for c in cipher.clone()
1012 { write!(txt, "{:02X} ", c); }
1013 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1014 println!();
1015
1016 // Normal case for AES-192
1017 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1018 print!("K =\t");
1019 for i in 0..24
1020 { print!("{:02X}", key[i]); }
1021 println!();
1022 let mut a_aes = AES_192::new_with_key(&key);
1023 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1024 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1025
1026 let message = "In the beginning God created the heavens and the earth.".to_string();
1027 println!("M =\t{}", message);
1028 let mut cipher = Vec::<u8>::new();
1029 a_aes.encrypt_string_into_vec(iv, &message, &mut cipher);
1030 print!("C =\t");
1031 for c in cipher.clone()
1032 { print!("{:02X} ", c); }
1033 println!();
1034 let mut txt = String::new();
1035 for c in cipher.clone()
1036 { write!(txt, "{:02X} ", c); }
1037 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
1038 println!();
1039
1040 // Normal case for AES-256
1041 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1042 print!("K =\t");
1043 for i in 0..32
1044 { print!("{:02X}", key[i]); }
1045 println!();
1046 let mut a_aes = AES_256::new_with_key(&key);
1047 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1048 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1049
1050 let message = "In the beginning God created the heavens and the earth.".to_string();
1051 println!("M =\t{}", message);
1052 let mut cipher = Vec::<u8>::new();
1053 a_aes.encrypt_string_into_vec(iv, &message, &mut cipher);
1054 print!("C =\t");
1055 for c in cipher.clone()
1056 { print!("{:02X} ", c); }
1057 println!();
1058 let mut txt = String::new();
1059 for c in cipher.clone()
1060 { write!(txt, "{:02X} ", c); }
1061 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
1062 println!();
1063
1064 // Normal case for Rijndael-256-256
1065 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1066 print!("K =\t");
1067 for i in 0..32
1068 { print!("{:02X}", key[i]); }
1069 println!();
1070 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
1071 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1072 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
1073
1074 let message = "In the beginning God created the heavens and the earth.".to_string();
1075 println!("M =\t{}", message);
1076 let mut cipher = Vec::<u8>::new();
1077 a_rijndael.encrypt_string_into_vec(iv, &message, &mut cipher);
1078 print!("C =\t");
1079 for c in cipher.clone()
1080 { print!("{:02X} ", c); }
1081 println!();
1082 let mut txt = String::new();
1083 for c in cipher.clone()
1084 { write!(txt, "{:02X} ", c); }
1085 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
1086 println!();
1087
1088 // Normal case for Rijndael-512-512 for post-quantum
1089 use cryptocol::number::SharedArrays;
1090 use cryptocol::hash::SHA3_512;
1091 let mut sha3 = SHA3_512::new();
1092 sha3.absorb_str("Post-quantum");
1093 let key: [u8; 64] = sha3.get_hash_value_in_array();
1094 print!("K =\t");
1095 for i in 0..64
1096 { print!("{:02X}", key[i]); }
1097 println!();
1098 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
1099 sha3.absorb_str("Initialize");
1100 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
1101 iv.src = sha3.get_hash_value_in_array();
1102 let iv = unsafe { iv.des };
1103 print!("IV =\t");
1104 for i in 0..16
1105 { print!("{:08X}", iv[i].to_be()); }
1106 println!();
1107 let message = "In the beginning God created the heavens and the earth.".to_string();
1108 println!("M =\t{}", message);
1109 let mut cipher = Vec::<u8>::new();
1110 a_rijndael.encrypt_string_into_vec(iv, &message, &mut cipher);
1111 print!("C =\t");
1112 for c in cipher.clone()
1113 { print!("{:02X} ", c); }
1114 println!();
1115 let mut txt = String::new();
1116 for c in cipher.clone()
1117 { write!(txt, "{:02X} ", c); }
1118 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
1119 println!("-------------------------------");
1120}1371fn des_encrypt_string_ofb_into_vec()
1372{
1373 println!("des_encrypt_string_ofb_into_vec()");
1374 use std::io::Write;
1375 use std::fmt::Write as _;
1376 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
1377
1378 // Normal case
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 = "In the beginning God created the heavens and the earth.".to_string();
1384 println!("M =\t{}", message);
1385 let iv = 0x_FEDCBA0987654321_u64;
1386 println!("IV = {}", iv);
1387 let mut cipher = Vec::<u8>::new();
1388 a_des.encrypt_string_into_vec(iv, &message, &mut cipher);
1389 print!("C (16 rounds) =\t");
1390 for c in cipher.clone()
1391 { print!("{:02X} ", c); }
1392 println!();
1393 let mut txt = String::new();
1394 for c in cipher.clone()
1395 { write!(txt, "{:02X} ", c); }
1396 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
1397 println!();
1398
1399 // Expanded case for 128 rounds
1400 let key = 0x_1234567890ABCDEF_u64;
1401 println!("K =\t{:#016X}", key);
1402 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
1403
1404 let message = "In the beginning God created the heavens and the earth.".to_string();
1405 println!("M =\t{}", message);
1406 let iv = 0x_FEDCBA0987654321_u64;
1407 println!("IV = {}", iv);
1408 let mut cipher = Vec::<u8>::new();
1409 a_des.encrypt_string_into_vec(iv, &message, &mut cipher);
1410 print!("C (128 rounds) =\t");
1411 for c in cipher.clone()
1412 { print!("{:02X} ", c); }
1413 println!();
1414 let mut txt = String::new();
1415 for c in cipher.clone()
1416 { write!(txt, "{:02X} ", c); }
1417 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
1418 println!();
1419
1420 // Expanded case for 0 rounds which means that key is meaningless
1421 let key1 = 0x_1234567890ABCDEF_u64;
1422 let key2 = 0_u64;
1423 println!("K =\t{:#016X}", key);
1424 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
1425 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
1426
1427 let message = "In the beginning God created the heavens and the earth.".to_string();
1428 println!("M =\t{}", message);
1429 let iv = 0x_FEDCBA0987654321_u64;
1430 println!("IV = {}", iv);
1431 let mut cipher1 = Vec::<u8>::new();
1432 let mut cipher2 = Vec::<u8>::new();
1433 c_des.encrypt_string_into_vec(iv, &message, &mut cipher1);
1434 d_des.encrypt_string_into_vec(iv, &message, &mut cipher2);
1435 print!("C (0 rounds) =\t");
1436 for c in cipher1.clone()
1437 { print!("{:02X} ", c); }
1438 println!();
1439 let mut txt = String::new();
1440 for c in cipher1.clone()
1441 { write!(txt, "{:02X} ", c); }
1442 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
1443 print!("D (0 rounds) =\t");
1444 for c in cipher2.clone()
1445 { print!("{:02X} ", c); }
1446 println!();
1447 let mut txt = String::new();
1448 for c in cipher2.clone()
1449 { write!(txt, "{:02X} ", c); }
1450 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
1451 println!();
1452
1453 // Normal case for the message of 0 bytes
1454 let key = 0x_1234567890ABCDEF_u64;
1455 println!("K =\t{:#016X}", key);
1456 let mut a_des = DES::new_with_key_u64(key);
1457
1458 let message = "".to_string();
1459 println!("M =\t{}", message);
1460 let iv = 0x_FEDCBA0987654321_u64;
1461 println!("IV = {}", iv);
1462 let mut cipher = Vec::<u8>::new();
1463 a_des.encrypt_string_into_vec(iv, &message, &mut cipher);
1464 print!("C =\t");
1465 for c in cipher.clone()
1466 { print!("{:02X} ", c); }
1467 println!();
1468 let mut txt = String::new();
1469 for c in cipher.clone()
1470 { write!(txt, "{:02X} ", c); }
1471 assert_eq!(txt, "");
1472 println!();
1473
1474 // Normal case for the message shorter than 8 bytes
1475 let key = 0x_1234567890ABCDEF_u64;
1476 println!("K =\t{:#016X}", key);
1477 let mut a_des = DES::new_with_key_u64(key);
1478
1479 let message = "7 bytes".to_string();
1480 println!("M =\t{}", message);
1481 let iv = 0x_FEDCBA0987654321_u64;
1482 println!("IV = {}", iv);
1483 let mut cipher = Vec::<u8>::new();
1484 a_des.encrypt_string_into_vec(iv, &message, &mut cipher);
1485 print!("C =\t");
1486 for c in cipher.clone()
1487 { print!("{:02X} ", c); }
1488 println!();
1489 let mut txt = String::new();
1490 for c in cipher.clone()
1491 { write!(txt, "{:02X} ", c); }
1492 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
1493 println!();
1494
1495 // Normal case for the message of 8 bytes
1496 let key = 0x_1234567890ABCDEF_u64;
1497 println!("K =\t{:#016X}", key);
1498 let mut a_des = DES::new_with_key_u64(key);
1499
1500 let message = "I am OK.".to_string();
1501 println!("M =\t{}", message);
1502 let iv = 0x_FEDCBA0987654321_u64;
1503 println!("IV = {}", iv);
1504 let mut cipher = Vec::<u8>::new();
1505 a_des.encrypt_string_into_vec(iv, &message, &mut cipher);
1506 print!("C =\t");
1507 for c in cipher.clone()
1508 { print!("{:02X} ", c); }
1509 println!();
1510 let mut txt = String::new();
1511 for c in cipher.clone()
1512 { write!(txt, "{:02X} ", c); }
1513 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
1514 println!();
1515
1516 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
1517 let key = 0x_1234567890ABCDEF_u64;
1518 println!("K =\t{:#016X}", key);
1519 let mut a_des = DES::new_with_key_u64(key);
1520
1521 let message = "PARK Youngho".to_string();
1522 println!("M =\t{}", message);
1523 let iv = 0x_FEDCBA0987654321_u64;
1524 println!("IV = {}", iv);
1525 let mut cipher = Vec::<u8>::new();
1526 a_des.encrypt_string_into_vec(iv, &message, &mut cipher);
1527 print!("C =\t");
1528 for c in cipher.clone()
1529 { print!("{:02X} ", c); }
1530 println!();
1531 let mut txt = String::new();
1532 for c in cipher.clone()
1533 { write!(txt, "{:02X} ", c); }
1534 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
1535 println!();
1536
1537 // Normal case for the message of 16 bytes
1538 let key = 0x_1234567890ABCDEF_u64;
1539 println!("K =\t{:#016X}", key);
1540 let mut a_des = DES::new_with_key_u64(key);
1541
1542 let message = "고맙습니다.".to_string();
1543 println!("M =\t{}", message);
1544 let iv = 0x_FEDCBA0987654321_u64;
1545 println!("IV = {}", iv);
1546 let mut cipher = Vec::<u8>::new();
1547 a_des.encrypt_string_into_vec(iv, &message, &mut cipher);
1548 print!("C =\t");
1549 for c in cipher.clone()
1550 { print!("{:02X} ", c); }
1551 println!();
1552 let mut txt = String::new();
1553 for c in cipher.clone()
1554 { write!(txt, "{:02X} ", c); }
1555 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
1556 println!("-------------------------------");
1557}Sourcefn encrypt_string_into_array<U, const N: usize>(
&mut self,
iv: T,
message: &String,
cipher: &mut [U; N],
) -> u64
fn encrypt_string_into_array<U, const N: usize>( &mut self, iv: T, message: &String, cipher: &mut [U; N], ) -> u64
Encrypts the data stored in a String object without any padding in OFB
(Output FeedBack) mode, and stores the encrypted data in array [U; N].
§Arguments
ivis an initialization vector for OFB mode.messageis an immutable reference toStringobject, and is the place where the plaintext to be encrypted is stored.cipheris a mutable reference to an array[U; N]object, and is the place where the encrypted data will be stored.
§Output
- This method returns the size of ciphertext.
- If this method failed in encryption or
message.len()is0, this method returnszero.
§Features
- If
messageis aStringobject that has a null string “”, nothing will be encrypted, and stored in the array[U; N]objectcipher. - If
U::size_in_bytes()*Nis less thanmessage.len(), this method does not perform encryption but returnszero. - If
U::size_in_bytes()*Nis equal tomessage.len(), this method performs encryption, fills the arraycipherwith the encrypted data, and returns the size of the ciphertext in bytes. - If
U::size_in_bytes()*Nis greater thanmessage.len(), this method performs encryption, fills the arraycipherwith the encrypted data, and then fills the rest of the elements of the arraycipherwith zeros, and returns the size of the ciphertext in bytes. - The size of the area for ciphertext should be prepared to be
message.len()at least. So, it is responsible for you to prepare thecipherarea big enough!
§For Rijndael or AES, and its variants
§Example 1 for AES-128
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, CBC_ISO };
let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
println!("K =\t{:#016X}", key);
let mut a_aes = AES_128::new_with_key_u128(key);
let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
let message = "In the beginning God created the heavens and the earth.".to_string();
println!("M =\t{}", message);
let mut cipher = [0_u8; 55];
a_aes.encrypt_str_into_array(iv, &message, &mut cipher);
print!("C =\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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 07 AF 4E C8 EF 5D 0A 74 97 3F 90 9E 05 9E 9E 32 FB 55 54 45 7C ED A2 2B F8 07 66 C0 7B CB 98 F3 BF 93 15 BA 26 1C 47 ");§For more examples,
click here
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, OFB };
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 iv = 0x_FEDCBA0987654321_u64;
println!("IV = {}", iv);
let mut cipher = [0_u8; 55];
a_des.encrypt_string_into_array(iv, &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, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");§For more examples,
click here
§For BigCryptor128
§Example 1 for TAES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
+ AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
+ AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
println!("IV = {:#034X}", iv);
let message = "In the beginning God created the heavens and the earth.".to_string();
let mut cipher = [0_u8; 55];
taes.encrypt_string_into_array(iv, &message, &mut cipher);
print!("C =\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, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");§For more examples,
click here
§For BigCryptor64
§Example 1 for TDES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
+ DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
+ DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
let iv = 0x_FEDCBA0987654321_u64;
println!("IV = {:#018X}", iv);
let message = "In the beginning God created the heavens and the earth.".to_string();
let mut cipher = [0_u8; 55];
tdes.encrypt_string_into_array(iv, &message, &mut cipher);
print!("C =\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, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");§For more examples,
click here
Examples found in repository?
274fn bigcryptor64_encrypt_string_ofb_into_array()
275{
276 println!("bigcryptor64_encrypt_string_ofb_into_array()");
277 use std::io::Write;
278 use std::fmt::Write as _;
279 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
280
281 // TDES case
282 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
283 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
284 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
285 let iv = 0x_FEDCBA0987654321_u64;
286 println!("IV = {:#018X}", iv);
287 let message = "In the beginning God created the heavens and the earth.".to_string();
288 let mut cipher = [0_u8; 55];
289 tdes.encrypt_string_into_array(iv, &message, &mut cipher);
290 print!("C =\t");
291 for c in cipher.clone()
292 { print!("{:02X} ", c); }
293 println!();
294 let mut txt = String::new();
295 for c in cipher.clone()
296 { write!(txt, "{:02X} ", c); }
297 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
298 println!("-------------------------------");
299}More examples
274fn bigcryptor128_encrypt_string_ofb_into_array()
275{
276 println!("bigcryptor128_encrypt_string_ofb_into_array()");
277 use std::io::Write;
278 use std::fmt::Write as _;
279 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
280
281 // TAES_128 case
282 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
283 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
284 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
285 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
286 println!("IV = {:#034X}", iv);
287 let message = "In the beginning God created the heavens and the earth.".to_string();
288 let mut cipher = [0_u8; 55];
289 taes.encrypt_string_into_array(iv, &message, &mut cipher);
290 print!("C =\t");
291 for c in cipher.clone()
292 { print!("{:02X} ", c); }
293 println!();
294 let mut txt = String::new();
295 for c in cipher.clone()
296 { write!(txt, "{:02X} ", c); }
297 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
298 println!("-------------------------------");
299}1122fn aes_encrypt_string_ofb_into_array()
1123{
1124 println!("aes_encrypt_string_ofb_into_array()");
1125 use std::io::Write;
1126 use std::fmt::Write as _;
1127 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
1128
1129 // Normal case for AES-128
1130 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1131 println!("K =\t{:#016X}", key);
1132 let mut a_aes = AES_128::new_with_key_u128(key);
1133 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1134 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1135
1136 let message = "In the beginning God created the heavens and the earth.".to_string();
1137 println!("M =\t{}", message);
1138 let mut cipher = [0_u8; 55];
1139 a_aes.encrypt_string_into_array(iv, &message, &mut cipher);
1140 print!("C =\t");
1141 for c in cipher.clone()
1142 { print!("{:02X} ", c); }
1143 println!();
1144 let mut txt = String::new();
1145 for c in cipher.clone()
1146 { write!(txt, "{:02X} ", c); }
1147 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1148 println!();
1149
1150 // Normal case for AES-192
1151 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1152 print!("K =\t");
1153 for i in 0..24
1154 { print!("{:02X}", key[i]); }
1155 println!();
1156 let mut a_aes = AES_192::new_with_key(&key);
1157 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1158 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1159
1160 let message = "In the beginning God created the heavens and the earth.".to_string();
1161 println!("M =\t{}", message);
1162 let mut cipher = [0_u8; 55];
1163 a_aes.encrypt_string_into_array(iv, &message, &mut cipher);
1164 print!("C =\t");
1165 for c in cipher.clone()
1166 { print!("{:02X} ", c); }
1167 println!();
1168 let mut txt = String::new();
1169 for c in cipher.clone()
1170 { write!(txt, "{:02X} ", c); }
1171 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
1172 println!();
1173
1174 // Normal case for AES-256
1175 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1176 print!("K =\t");
1177 for i in 0..32
1178 { print!("{:02X}", key[i]); }
1179 println!();
1180 let mut a_aes = AES_256::new_with_key(&key);
1181 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1182 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1183
1184 let message = "In the beginning God created the heavens and the earth.".to_string();
1185 println!("M =\t{}", message);
1186 let mut cipher = [0_u8; 55];
1187 a_aes.encrypt_string_into_array(iv, &message, &mut cipher);
1188 print!("C =\t");
1189 for c in cipher.clone()
1190 { print!("{:02X} ", c); }
1191 println!();
1192 let mut txt = String::new();
1193 for c in cipher.clone()
1194 { write!(txt, "{:02X} ", c); }
1195 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
1196 println!();
1197
1198 // Normal case for Rijndael-256-256
1199 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1200 print!("K =\t");
1201 for i in 0..32
1202 { print!("{:02X}", key[i]); }
1203 println!();
1204 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
1205 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1206 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
1207
1208 let message = "In the beginning God created the heavens and the earth.".to_string();
1209 println!("M =\t{}", message);
1210 let mut cipher = [0_u8; 55];
1211 a_rijndael.encrypt_string_into_array(iv, &message, &mut cipher);
1212 print!("C =\t");
1213 for c in cipher.clone()
1214 { print!("{:02X} ", c); }
1215 println!();
1216 let mut txt = String::new();
1217 for c in cipher.clone()
1218 { write!(txt, "{:02X} ", c); }
1219 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
1220 println!();
1221
1222 // Normal case for Rijndael-512-512 for post-quantum
1223 use cryptocol::number::SharedArrays;
1224 use cryptocol::hash::SHA3_512;
1225 let mut sha3 = SHA3_512::new();
1226 sha3.absorb_str("Post-quantum");
1227 let key: [u8; 64] = sha3.get_hash_value_in_array();
1228 print!("K =\t");
1229 for i in 0..64
1230 { print!("{:02X}", key[i]); }
1231 println!();
1232 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
1233 sha3.absorb_str("Initialize");
1234 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
1235 iv.src = sha3.get_hash_value_in_array();
1236 let iv = unsafe { iv.des };
1237 print!("IV =\t");
1238 for i in 0..16
1239 { print!("{:08X}", iv[i].to_be()); }
1240 println!();
1241 let message = "In the beginning God created the heavens and the earth.".to_string();
1242 println!("M =\t{}", message);
1243 let mut cipher = [0_u8; 55];
1244 a_rijndael.encrypt_string_into_array(iv, &message, &mut cipher);
1245 print!("C =\t");
1246 for c in cipher.clone()
1247 { print!("{:02X} ", c); }
1248 println!();
1249 let mut txt = String::new();
1250 for c in cipher.clone()
1251 { write!(txt, "{:02X} ", c); }
1252 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
1253 println!("-------------------------------");
1254}1559fn des_encrypt_string_ofb_into_array()
1560{
1561 println!("des_encrypt_string_ofb_into_array()");
1562 use std::io::Write;
1563 use std::fmt::Write as _;
1564 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
1565
1566 // Normal case
1567 let key = 0x_1234567890ABCDEF_u64;
1568 println!("K =\t{:#016X}", key);
1569 let mut a_des = DES::new_with_key_u64(key);
1570
1571 let message = "In the beginning God created the heavens and the earth.".to_string();
1572 println!("M =\t{}", message);
1573 let iv = 0x_FEDCBA0987654321_u64;
1574 println!("IV = {}", iv);
1575 let mut cipher = [0_u8; 55];
1576 a_des.encrypt_string_into_array(iv, &message, &mut cipher);
1577 print!("C (16 rounds) =\t");
1578 for c in cipher.clone()
1579 { print!("{:02X} ", c); }
1580 println!();
1581 let mut txt = String::new();
1582 for c in cipher.clone()
1583 { write!(txt, "{:02X} ", c); }
1584 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
1585 println!();
1586
1587 // Expanded case for 128 rounds
1588 let key = 0x_1234567890ABCDEF_u64;
1589 println!("K =\t{:#016X}", key);
1590 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
1591
1592 let message = "In the beginning God created the heavens and the earth.".to_string();
1593 println!("M =\t{}", message);
1594 let iv = 0x_FEDCBA0987654321_u64;
1595 println!("IV = {}", iv);
1596 let mut cipher = [0_u8; 55];
1597 a_des.encrypt_string_into_array(iv, &message, &mut cipher);
1598 print!("C (128 rounds) =\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, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
1606 println!();
1607
1608 // Expanded case for 0 rounds which means that key is meaningless
1609 let key1 = 0x_1234567890ABCDEF_u64;
1610 let key2 = 0_u64;
1611 println!("K =\t{:#016X}", key);
1612 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
1613 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
1614
1615 let message = "In the beginning God created the heavens and the earth.".to_string();
1616 println!("M =\t{}", message);
1617 let iv = 0x_FEDCBA0987654321_u64;
1618 println!("IV = {}", iv);
1619 let mut cipher1 = [0_u8; 55];
1620 let mut cipher2 = [0_u8; 55];
1621 c_des.encrypt_string_into_array(iv, &message, &mut cipher1);
1622 d_des.encrypt_string_into_array(iv, &message, &mut cipher2);
1623 print!("C (0 rounds) =\t");
1624 for c in cipher1.clone()
1625 { print!("{:02X} ", c); }
1626 println!();
1627 let mut txt = String::new();
1628 for c in cipher1.clone()
1629 { write!(txt, "{:02X} ", c); }
1630 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
1631 print!("D (0 rounds) =\t");
1632 for c in cipher2.clone()
1633 { print!("{:02X} ", c); }
1634 println!();
1635 let mut txt = String::new();
1636 for c in cipher2.clone()
1637 { write!(txt, "{:02X} ", c); }
1638 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
1639 println!();
1640
1641 // Normal case for the message of 0 bytes
1642 let key = 0x_1234567890ABCDEF_u64;
1643 println!("K =\t{:#016X}", key);
1644 let mut a_des = DES::new_with_key_u64(key);
1645
1646 let message = "".to_string();
1647 println!("M =\t{}", message);
1648 let iv = 0x_FEDCBA0987654321_u64;
1649 println!("IV = {}", iv);
1650 let mut cipher = [0_u8; 0];
1651 a_des.encrypt_string_into_array(iv, &message, &mut cipher);
1652 print!("C =\t");
1653 for c in cipher.clone()
1654 { print!("{:02X} ", c); }
1655 println!();
1656 let mut txt = String::new();
1657 for c in cipher.clone()
1658 { write!(txt, "{:02X} ", c); }
1659 assert_eq!(txt, "");
1660 println!();
1661
1662 // Normal case for the message shorter than 8 bytes
1663 let key = 0x_1234567890ABCDEF_u64;
1664 println!("K =\t{:#016X}", key);
1665 let mut a_des = DES::new_with_key_u64(key);
1666
1667 let message = "7 bytes".to_string();
1668 println!("M =\t{}", message);
1669 let iv = 0x_FEDCBA0987654321_u64;
1670 println!("IV = {}", iv);
1671 let mut cipher = [0_u8; 7];
1672 a_des.encrypt_string_into_array(iv, &message, &mut cipher);
1673 print!("C =\t");
1674 for c in cipher.clone()
1675 { print!("{:02X} ", c); }
1676 println!();
1677 let mut txt = String::new();
1678 for c in cipher.clone()
1679 { write!(txt, "{:02X} ", c); }
1680 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
1681 println!();
1682
1683 // Normal case for the message of 8 bytes
1684 let key = 0x_1234567890ABCDEF_u64;
1685 println!("K =\t{:#016X}", key);
1686 let mut a_des = DES::new_with_key_u64(key);
1687
1688 let message = "I am OK.".to_string();
1689 println!("M =\t{}", message);
1690 let iv = 0x_FEDCBA0987654321_u64;
1691 println!("IV = {}", iv);
1692 let mut cipher = [0_u8; 8];
1693 a_des.encrypt_string_into_array(iv, &message, &mut cipher);
1694 print!("C =\t");
1695 for c in cipher.clone()
1696 { print!("{:02X} ", c); }
1697 println!();
1698 let mut txt = String::new();
1699 for c in cipher.clone()
1700 { write!(txt, "{:02X} ", c); }
1701 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
1702 println!();
1703
1704 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
1705 let key = 0x_1234567890ABCDEF_u64;
1706 println!("K =\t{:#016X}", key);
1707 let mut a_des = DES::new_with_key_u64(key);
1708
1709 let message = "PARK Youngho".to_string();
1710 println!("M =\t{}", message);
1711 let iv = 0x_FEDCBA0987654321_u64;
1712 println!("IV = {}", iv);
1713 let mut cipher = [0_u8; 12];
1714 a_des.encrypt_string_into_array(iv, &message, &mut cipher);
1715 print!("C =\t");
1716 for c in cipher.clone()
1717 { print!("{:02X} ", c); }
1718 println!();
1719 let mut txt = String::new();
1720 for c in cipher.clone()
1721 { write!(txt, "{:02X} ", c); }
1722 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
1723 println!();
1724
1725 // Normal case for the message of 16 bytes
1726 let key = 0x_1234567890ABCDEF_u64;
1727 println!("K =\t{:#016X}", key);
1728 let mut a_des = DES::new_with_key_u64(key);
1729
1730 let message = "고맙습니다.".to_string();
1731 println!("M =\t{}", message);
1732 let iv = 0x_FEDCBA0987654321_u64;
1733 println!("IV = {}", iv);
1734 let mut cipher = [0_u8; 16];
1735 a_des.encrypt_string_into_array(iv, &message, &mut cipher);
1736 print!("C =\t");
1737 for c in cipher.clone()
1738 { print!("{:02X} ", c); }
1739 println!();
1740 let mut txt = String::new();
1741 for c in cipher.clone()
1742 { write!(txt, "{:02X} ", c); }
1743 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
1744 println!("-------------------------------");
1745}Sourcefn encrypt_vec<U>(&mut self, iv: T, message: &Vec<U>, cipher: *mut u8) -> u64
fn encrypt_vec<U>(&mut self, iv: T, message: &Vec<U>, cipher: *mut u8) -> u64
Encrypts the data stored in a Vec<U> object without any padding
in OFB (Output FeedBack) mode.
§Arguments
ivis an initialization vector for OFB mode.messageis an immutable reference toVec<U>object, and is the place where the plaintext to be encrypted is stored.cipheris a mutable pointer tou8which is*mut u8, and is the place where the encrypted data will be stored.
§Output
- This method returns the size of ciphertext.
- If this method failed in encryption or
message.len()is0, this method returnszero.
§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++.
- The size of the memory area which starts at
cipheris assumed to be enough to store the ciphertext. - The size of the area for ciphertext should be prepared to be
size_of::<U>()*message.len()at least. So, it is responsible for you to prepare thecipherarea big enough! - If
messageis an emptyVec<U>objectVec::<U>::new(), nothing will be encrypted, and stored in the memory area that starts fromcipher.
§For Rijndael or AES, and its variants
§Example 1 for AES-128
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ AES_128, CBC_ISO };
let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
println!("K =\t{:#016X}", key);
let mut a_aes = AES_128::new_with_key_u128(key);
let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
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; 55];
a_aes.encrypt_vec(iv, &message, cipher.as_mut_ptr());
print!("C =\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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 07 AF 4E C8 EF 5D 0A 74 97 3F 90 9E 05 9E 9E 32 FB 55 54 45 7C ED A2 2B F8 07 66 C0 7B CB 98 F3 BF 93 15 BA 26 1C 47 ");§For more examples,
click here
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, OFB };
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 iv = 0x_FEDCBA0987654321_u64;
println!("IV = {}", iv);
let mut cipher = [0_u8; 55];
a_des.encrypt_vec(iv, &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, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");§For more examples,
click here
§For BigCryptor128
§Example 1 for TAES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
+ AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
+ AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
println!("IV = {:#034X}", iv);
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; 55];
taes.encrypt_vec(iv, &message, cipher.as_mut_ptr());
print!("C =\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, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");§For more examples,
click here
§For BigCryptor64
§Example 1 for TDES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
+ DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
+ DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
let iv = 0x_FEDCBA0987654321_u64;
println!("IV = {:#018X}", iv);
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; 55];
tdes.encrypt_vec(iv, &message, cipher.as_mut_ptr());
print!("C =\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, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");§For more examples,
click here
Examples found in repository?
301fn bigcryptor64_encrypt_vec_ofb()
302{
303 println!("bigcryptor64_encrypt_vec_ofb()");
304 use std::io::Write;
305 use std::fmt::Write as _;
306 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
307
308 // TDES case
309 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
310 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
311 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
312 let iv = 0x_FEDCBA0987654321_u64;
313 println!("IV = {:#018X}", iv);
314 let message = "In the beginning God created the heavens and the earth.";
315 println!("M =\t{}", message);
316 let message = unsafe { message.to_string().as_mut_vec().clone() };
317 let mut cipher = [0_u8; 55];
318 tdes.encrypt_vec(iv, &message, cipher.as_mut_ptr());
319 print!("C =\t");
320 for c in cipher.clone()
321 { print!("{:02X} ", c); }
322 println!();
323 let mut txt = String::new();
324 for c in cipher.clone()
325 { write!(txt, "{:02X} ", c); }
326 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
327 println!("-------------------------------");
328}More examples
301fn bigcryptor128_encrypt_vec_ofb()
302{
303 println!("bigcryptor128_encrypt_vec_ofb()");
304 use std::io::Write;
305 use std::fmt::Write as _;
306 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
307
308 // TAES_128 case
309 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
310 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
311 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
312 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
313 println!("IV = {:#034X}", iv);
314 let message = "In the beginning God created the heavens and the earth.";
315 println!("M =\t{}", message);
316 let message = unsafe { message.to_string().as_mut_vec().clone() };
317 let mut cipher = [0_u8; 55];
318 taes.encrypt_vec(iv, &message, cipher.as_mut_ptr());
319 print!("C =\t");
320 for c in cipher.clone()
321 { print!("{:02X} ", c); }
322 println!();
323 let mut txt = String::new();
324 for c in cipher.clone()
325 { write!(txt, "{:02X} ", c); }
326 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
327 println!("-------------------------------");
328}1256fn aes_encrypt_vec_ofb()
1257{
1258 println!("aes_encrypt_vec_ofb()");
1259 use std::io::Write;
1260 use std::fmt::Write as _;
1261 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
1262
1263 // Normal case for AES-128
1264 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1265 println!("K =\t{:#016X}", key);
1266 let mut a_aes = AES_128::new_with_key_u128(key);
1267 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1268 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1269
1270 let message = "In the beginning God created the heavens and the earth.";
1271 println!("M =\t{}", message);
1272 let message = unsafe { message.to_string().as_mut_vec().clone() };
1273 let mut cipher = [0_u8; 55];
1274 a_aes.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1275 print!("C =\t");
1276 for c in cipher.clone()
1277 { print!("{:02X} ", c); }
1278 println!();
1279 let mut txt = String::new();
1280 for c in cipher.clone()
1281 { write!(txt, "{:02X} ", c); }
1282 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1283 println!();
1284
1285 // Normal case for AES-192
1286 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1287 print!("K =\t");
1288 for i in 0..24
1289 { print!("{:02X}", key[i]); }
1290 println!();
1291 let mut a_aes = AES_192::new_with_key(&key);
1292 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1293 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1294
1295 let message = "In the beginning God created the heavens and the earth.";
1296 println!("M =\t{}", message);
1297 let message = unsafe { message.to_string().as_mut_vec().clone() };
1298 let mut cipher = [0_u8; 55];
1299 a_aes.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1300 print!("C =\t");
1301 for c in cipher.clone()
1302 { print!("{:02X} ", c); }
1303 println!();
1304 let mut txt = String::new();
1305 for c in cipher.clone()
1306 { write!(txt, "{:02X} ", c); }
1307 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
1308 println!();
1309
1310 // Normal case for AES-256
1311 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1312 print!("K =\t");
1313 for i in 0..32
1314 { print!("{:02X}", key[i]); }
1315 println!();
1316 let mut a_aes = AES_256::new_with_key(&key);
1317 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1318 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1319
1320 let message = "In the beginning God created the heavens and the earth.";
1321 println!("M =\t{}", message);
1322 let message = unsafe { message.to_string().as_mut_vec().clone() };
1323 let mut cipher = [0_u8; 55];
1324 a_aes.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1325 print!("C =\t");
1326 for c in cipher.clone()
1327 { print!("{:02X} ", c); }
1328 println!();
1329 let mut txt = String::new();
1330 for c in cipher.clone()
1331 { write!(txt, "{:02X} ", c); }
1332 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
1333 println!();
1334
1335 // Normal case for Rijndael-256-256
1336 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1337 print!("K =\t");
1338 for i in 0..32
1339 { print!("{:02X}", key[i]); }
1340 println!();
1341 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
1342 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1343 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
1344
1345 let message = "In the beginning God created the heavens and the earth.";
1346 println!("M =\t{}", message);
1347 let message = unsafe { message.to_string().as_mut_vec().clone() };
1348 let mut cipher = [0_u8; 55];
1349 a_rijndael.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1350 print!("C =\t");
1351 for c in cipher.clone()
1352 { print!("{:02X} ", c); }
1353 println!();
1354 let mut txt = String::new();
1355 for c in cipher.clone()
1356 { write!(txt, "{:02X} ", c); }
1357 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
1358 println!();
1359
1360 // Normal case for Rijndael-512-512 for post-quantum
1361 use cryptocol::number::SharedArrays;
1362 use cryptocol::hash::SHA3_512;
1363 let mut sha3 = SHA3_512::new();
1364 sha3.absorb_str("Post-quantum");
1365 let key: [u8; 64] = sha3.get_hash_value_in_array();
1366 print!("K =\t");
1367 for i in 0..64
1368 { print!("{:02X}", key[i]); }
1369 println!();
1370 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
1371 sha3.absorb_str("Initialize");
1372 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
1373 iv.src = sha3.get_hash_value_in_array();
1374 let iv = unsafe { iv.des };
1375 print!("IV =\t");
1376 for i in 0..16
1377 { print!("{:08X}", iv[i].to_be()); }
1378 println!();
1379 let message = "In the beginning God created the heavens and the earth.";
1380 println!("M =\t{}", message);
1381 let message = unsafe { message.to_string().as_mut_vec().clone() };
1382 let mut cipher = [0_u8; 55];
1383 a_rijndael.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1384 print!("C =\t");
1385 for c in cipher.clone()
1386 { print!("{:02X} ", c); }
1387 println!();
1388 let mut txt = String::new();
1389 for c in cipher.clone()
1390 { write!(txt, "{:02X} ", c); }
1391 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
1392 println!("-------------------------------");
1393}1747fn des_encrypt_vec_ofb()
1748{
1749 println!("des_encrypt_vec_ofb()");
1750 use std::io::Write;
1751 use std::fmt::Write as _;
1752 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
1753
1754 // Normal case
1755 let key = 0x_1234567890ABCDEF_u64;
1756 println!("K =\t{:#016X}", key);
1757 let mut a_des = DES::new_with_key_u64(key);
1758
1759 let message = "In the beginning God created the heavens and the earth.";
1760 println!("M =\t{}", message);
1761 let message = unsafe { message.to_string().as_mut_vec().clone() };
1762 let iv = 0x_FEDCBA0987654321_u64;
1763 println!("IV = {}", iv);
1764 let mut cipher = [0_u8; 55];
1765 a_des.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1766 print!("C (16 rounds) =\t");
1767 for c in cipher.clone()
1768 { print!("{:02X} ", c); }
1769 println!();
1770 let mut txt = String::new();
1771 for c in cipher.clone()
1772 { write!(txt, "{:02X} ", c); }
1773 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
1774 println!();
1775
1776 // Expanded case for 128 rounds
1777 let key = 0x_1234567890ABCDEF_u64;
1778 println!("K =\t{:#016X}", key);
1779 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
1780
1781 let message = "In the beginning God created the heavens and the earth.";
1782 println!("M =\t{}", message);
1783 let message = unsafe { message.to_string().as_mut_vec().clone() };
1784 let iv = 0x_FEDCBA0987654321_u64;
1785 println!("IV = {}", iv);
1786 let mut cipher = [0_u8; 55];
1787 a_des.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1788 print!("C (128 rounds) =\t");
1789 for c in cipher.clone()
1790 { print!("{:02X} ", c); }
1791 println!();
1792 let mut txt = String::new();
1793 for c in cipher.clone()
1794 { write!(txt, "{:02X} ", c); }
1795 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
1796 println!();
1797
1798 // Expanded case for 0 rounds which means that key is meaningless
1799 let key1 = 0x_1234567890ABCDEF_u64;
1800 let key2 = 0_u64;
1801 println!("K1 =\t{:#016X}", key1);
1802 println!("K2 =\t{:#016X}", key2);
1803 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
1804 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
1805
1806 let message = "In the beginning God created the heavens and the earth.";
1807 println!("M =\t{}", message);
1808 let message = unsafe { message.to_string().as_mut_vec().clone() };
1809 let iv = 0x_FEDCBA0987654321_u64;
1810 println!("IV = {}", iv);
1811 let mut cipher1 = [0_u8; 55];
1812 let mut cipher2 = [0_u8; 55];
1813 c_des.encrypt_vec(iv, &message, cipher1.as_mut_ptr());
1814 d_des.encrypt_vec(iv, &message, cipher2.as_mut_ptr());
1815 print!("C (0 rounds) =\t");
1816 for c in cipher1.clone()
1817 { print!("{:02X} ", c); }
1818 println!();
1819 let mut txt = String::new();
1820 for c in cipher1.clone()
1821 { write!(txt, "{:02X} ", c); }
1822 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
1823 print!("D (0 rounds) =\t");
1824 for c in cipher2.clone()
1825 { print!("{:02X} ", c); }
1826 println!();
1827 let mut txt = String::new();
1828 for c in cipher2.clone()
1829 { write!(txt, "{:02X} ", c); }
1830 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
1831 println!();
1832
1833 // Normal case for the message of 0 bytes
1834 let key = 0x_1234567890ABCDEF_u64;
1835 println!("K =\t{:#016X}", key);
1836 let mut a_des = DES::new_with_key_u64(key);
1837
1838 let message = "";
1839 println!("M =\t{}", message);
1840 let message = unsafe { message.to_string().as_mut_vec().clone() };
1841 let iv = 0x_FEDCBA0987654321_u64;
1842 println!("IV = {}", iv);
1843 let mut cipher = [0_u8; 0];
1844 a_des.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1845 print!("C =\t");
1846 for c in cipher.clone()
1847 { print!("{:02X} ", c); }
1848 println!();
1849 let mut txt = String::new();
1850 for c in cipher.clone()
1851 { write!(txt, "{:02X} ", c); }
1852 assert_eq!(txt, "");
1853 println!();
1854
1855 // Normal case for the message shorter than 8 bytes
1856 let key = 0x_1234567890ABCDEF_u64;
1857 println!("K =\t{:#016X}", key);
1858 let mut a_des = DES::new_with_key_u64(key);
1859
1860 let message = "7 bytes";
1861 println!("M =\t{}", message);
1862 let message = unsafe { message.to_string().as_mut_vec().clone() };
1863 let iv = 0x_FEDCBA0987654321_u64;
1864 println!("IV = {}", iv);
1865 let mut cipher = [0_u8; 7];
1866 a_des.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1867 print!("C =\t");
1868 for c in cipher.clone()
1869 { print!("{:02X} ", c); }
1870 println!();
1871 let mut txt = String::new();
1872 for c in cipher.clone()
1873 { write!(txt, "{:02X} ", c); }
1874 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
1875 println!();
1876
1877 // Normal case for the message of 8 bytes
1878 let key = 0x_1234567890ABCDEF_u64;
1879 println!("K =\t{:#016X}", key);
1880 let mut a_des = DES::new_with_key_u64(key);
1881
1882 let message = "I am OK.";
1883 println!("M =\t{}", message);
1884 let message = unsafe { message.to_string().as_mut_vec().clone() };
1885 let iv = 0x_FEDCBA0987654321_u64;
1886 println!("IV = {}", iv);
1887 let mut cipher = [0_u8; 8];
1888 a_des.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1889 print!("C =\t");
1890 for c in cipher.clone()
1891 { print!("{:02X} ", c); }
1892 println!();
1893 let mut txt = String::new();
1894 for c in cipher.clone()
1895 { write!(txt, "{:02X} ", c); }
1896 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
1897 println!();
1898
1899 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
1900 let key = 0x_1234567890ABCDEF_u64;
1901 println!("K =\t{:#016X}", key);
1902 let mut a_des = DES::new_with_key_u64(key);
1903
1904 let message = "PARK Youngho";
1905 println!("M =\t{}", message);
1906 let message = unsafe { message.to_string().as_mut_vec().clone() };
1907 let iv = 0x_FEDCBA0987654321_u64;
1908 println!("IV = {}", iv);
1909 let mut cipher = [0_u8; 12];
1910 a_des.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1911 print!("C =\t");
1912 for c in cipher.clone()
1913 { print!("{:02X} ", c); }
1914 println!();
1915 let mut txt = String::new();
1916 for c in cipher.clone()
1917 { write!(txt, "{:02X} ", c); }
1918 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
1919 println!();
1920
1921 // Normal case for the message of 16 bytes
1922 let key = 0x_1234567890ABCDEF_u64;
1923 println!("K =\t{:#016X}", key);
1924 let mut a_des = DES::new_with_key_u64(key);
1925
1926 let message = "고맙습니다.";
1927 println!("M =\t{}", message);
1928 let message = unsafe { message.to_string().as_mut_vec().clone() };
1929 let iv = 0x_FEDCBA0987654321_u64;
1930 println!("IV = {}", iv);
1931 let mut cipher = [0_u8; 16];
1932 a_des.encrypt_vec(iv, &message, cipher.as_mut_ptr());
1933 print!("C =\t");
1934 for c in cipher.clone()
1935 { print!("{:02X} ", c); }
1936 println!();
1937 let mut txt = String::new();
1938 for c in cipher.clone()
1939 { write!(txt, "{:02X} ", c); }
1940 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
1941 println!("-------------------------------");
1942}Sourcefn encrypt_vec_into_vec<U, V>(
&mut self,
iv: T,
message: &Vec<U>,
cipher: &mut Vec<V>,
) -> u64
fn encrypt_vec_into_vec<U, V>( &mut self, iv: T, message: &Vec<U>, cipher: &mut Vec<V>, ) -> u64
Encrypts the data stored in a Vec<U> object without any padding in
OFB (Output FeedBack) mode, and stores the encrypted data in Vec<V>.
§Arguments
ivis an initialization vector for OFB mode.messageis an immutable reference toVec<U>object, and is the place where the plaintext to be encrypted is stored.cipheris a mutable reference toVec<U>object, and is the place where the encrypted data will be stored.
§Output
- This method returns the size of ciphertext.
- If this method failed in encryption or
message.len()is0, this method returnszero.
§Features
- If
messageis an emptyVec<U>objectVec::<U>::new(), nothing will be encrypted, and stored in theVec<U>object which is referred to ascipher. - 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 Rijndael or AES, and its variants
§Example 1 for AES-128
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ AES_128, CBC_ISO };
let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
println!("K =\t{:#016X}", key);
let mut a_aes = AES_128::new_with_key_u128(key);
let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
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_aes.encrypt_vec_into_vec(iv, &message, &mut cipher);
print!("C =\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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 07 AF 4E C8 EF 5D 0A 74 97 3F 90 9E 05 9E 9E 32 FB 55 54 45 7C ED A2 2B F8 07 66 C0 7B CB 98 F3 BF 93 15 BA 26 1C 47 ");§For more examples,
click here
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, OFB };
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 iv = 0x_FEDCBA0987654321_u64;
println!("IV = {}", iv);
let mut cipher = Vec::<u8>::new();
a_des.encrypt_vec_into_vec(iv, &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, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");§For more examples,
click here
§For BigCryptor128
§Example 1 for TAES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
+ AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
+ AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
println!("IV = {:#034X}", iv);
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();
taes.encrypt_vec_into_vec(iv, &message, &mut cipher);
print!("C =\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, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");§For more examples,
click here
§For BigCryptor64
§Example 1 for TDES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
+ DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
+ DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
let iv = 0x_FEDCBA0987654321_u64;
println!("IV = {:#018X}", iv);
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();
tdes.encrypt_vec_into_vec(iv, &message, &mut cipher);
print!("C =\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, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");§For more examples,
click here
Examples found in repository?
330fn bigcryptor64_encrypt_vec_ofb_into_vec()
331{
332 println!("bigcryptor64_encrypt_vec_ofb_into_vec()");
333 use std::io::Write;
334 use std::fmt::Write as _;
335 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
336
337 // TDES case
338 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
339 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
340 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
341 let iv = 0x_FEDCBA0987654321_u64;
342 println!("IV = {:#018X}", iv);
343 let message = "In the beginning God created the heavens and the earth.";
344 println!("M =\t{}", message);
345 let message = unsafe { message.to_string().as_mut_vec().clone() };
346 let mut cipher = Vec::<u8>::new();
347 tdes.encrypt_vec_into_vec(iv, &message, &mut cipher);
348 print!("C =\t");
349 for c in cipher.clone()
350 { print!("{:02X} ", c); }
351 println!();
352 let mut txt = String::new();
353 for c in cipher.clone()
354 { write!(txt, "{:02X} ", c); }
355 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
356 println!("-------------------------------");
357}More examples
330fn bigcryptor128_encrypt_vec_ofb_into_vec()
331{
332 println!("bigcryptor128_encrypt_vec_ofb_into_vec()");
333 use std::io::Write;
334 use std::fmt::Write as _;
335 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
336
337 // TAES_128 case
338 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
339 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
340 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
341 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
342 println!("IV = {:#034X}", iv);
343 let message = "In the beginning God created the heavens and the earth.";
344 println!("M =\t{}", message);
345 let message = unsafe { message.to_string().as_mut_vec().clone() };
346 let mut cipher = Vec::<u8>::new();
347 taes.encrypt_vec_into_vec(iv, &message, &mut cipher);
348 print!("C =\t");
349 for c in cipher.clone()
350 { print!("{:02X} ", c); }
351 println!();
352 let mut txt = String::new();
353 for c in cipher.clone()
354 { write!(txt, "{:02X} ", c); }
355 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
356 println!("-------------------------------");
357}1395fn aes_encrypt_vec_ofb_into_vec()
1396{
1397 println!("aes_encrypt_vec_ofb_into_vec()");
1398 use std::io::Write;
1399 use std::fmt::Write as _;
1400 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
1401
1402 // Normal case for AES-128
1403 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1404 println!("K =\t{:#016X}", key);
1405 let mut a_aes = AES_128::new_with_key_u128(key);
1406 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1407 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1408
1409 let message = "In the beginning God created the heavens and the earth.";
1410 println!("M =\t{}", message);
1411 let message = unsafe { message.to_string().as_mut_vec().clone() };
1412 let mut cipher = Vec::<u8>::new();
1413 a_aes.encrypt_vec_into_vec(iv, &message, &mut cipher);
1414 print!("C =\t");
1415 for c in cipher.clone()
1416 { print!("{:02X} ", c); }
1417 println!();
1418 let mut txt = String::new();
1419 for c in cipher.clone()
1420 { write!(txt, "{:02X} ", c); }
1421 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1422 println!();
1423
1424 // Normal case for AES-192
1425 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1426 print!("K =\t");
1427 for i in 0..24
1428 { print!("{:02X}", key[i]); }
1429 println!();
1430 let mut a_aes = AES_192::new_with_key(&key);
1431 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1432 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1433
1434 let message = "In the beginning God created the heavens and the earth.";
1435 println!("M =\t{}", message);
1436 let message = unsafe { message.to_string().as_mut_vec().clone() };
1437 let mut cipher = Vec::<u8>::new();
1438 a_aes.encrypt_vec_into_vec(iv, &message, &mut cipher);
1439 print!("C =\t");
1440 for c in cipher.clone()
1441 { print!("{:02X} ", c); }
1442 println!();
1443 let mut txt = String::new();
1444 for c in cipher.clone()
1445 { write!(txt, "{:02X} ", c); }
1446 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
1447 println!();
1448
1449 // Normal case for AES-256
1450 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1451 print!("K =\t");
1452 for i in 0..32
1453 { print!("{:02X}", key[i]); }
1454 println!();
1455 let mut a_aes = AES_256::new_with_key(&key);
1456 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1457 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1458
1459 let message = "In the beginning God created the heavens and the earth.";
1460 println!("M =\t{}", message);
1461 let message = unsafe { message.to_string().as_mut_vec().clone() };
1462 let mut cipher = Vec::<u8>::new();
1463 a_aes.encrypt_vec_into_vec(iv, &message, &mut cipher);
1464 print!("C =\t");
1465 for c in cipher.clone()
1466 { print!("{:02X} ", c); }
1467 println!();
1468 let mut txt = String::new();
1469 for c in cipher.clone()
1470 { write!(txt, "{:02X} ", c); }
1471 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
1472 println!();
1473
1474 // Normal case for Rijndael-256-256
1475 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1476 print!("K =\t");
1477 for i in 0..32
1478 { print!("{:02X}", key[i]); }
1479 println!();
1480 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
1481 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1482 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
1483
1484 let message = "In the beginning God created the heavens and the earth.";
1485 println!("M =\t{}", message);
1486 let message = unsafe { message.to_string().as_mut_vec().clone() };
1487 let mut cipher = Vec::<u8>::new();
1488 a_rijndael.encrypt_vec_into_vec(iv, &message, &mut cipher);
1489 print!("C =\t");
1490 for c in cipher.clone()
1491 { print!("{:02X} ", c); }
1492 println!();
1493 let mut txt = String::new();
1494 for c in cipher.clone()
1495 { write!(txt, "{:02X} ", c); }
1496 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
1497 println!();
1498
1499 // Normal case for Rijndael-512-512 for post-quantum
1500 use cryptocol::number::SharedArrays;
1501 use cryptocol::hash::SHA3_512;
1502 let mut sha3 = SHA3_512::new();
1503 sha3.absorb_str("Post-quantum");
1504 let key: [u8; 64] = sha3.get_hash_value_in_array();
1505 print!("K =\t");
1506 for i in 0..64
1507 { print!("{:02X}", key[i]); }
1508 println!();
1509 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
1510 sha3.absorb_str("Initialize");
1511 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
1512 iv.src = sha3.get_hash_value_in_array();
1513 let iv = unsafe { iv.des };
1514 print!("IV =\t");
1515 for i in 0..16
1516 { print!("{:08X}", iv[i].to_be()); }
1517 println!();
1518 let message = "In the beginning God created the heavens and the earth.";
1519 println!("M =\t{}", message);
1520 let message = unsafe { message.to_string().as_mut_vec().clone() };
1521 let mut cipher = Vec::<u8>::new();
1522 a_rijndael.encrypt_vec_into_vec(iv, &message, &mut cipher);
1523 print!("C =\t");
1524 for c in cipher.clone()
1525 { print!("{:02X} ", c); }
1526 println!();
1527 let mut txt = String::new();
1528 for c in cipher.clone()
1529 { write!(txt, "{:02X} ", c); }
1530 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
1531 println!("-------------------------------");
1532}1944fn des_encrypt_vec_ofb_into_vec()
1945{
1946 println!("des_encrypt_vec_ofb_into_vec()");
1947 use std::io::Write;
1948 use std::fmt::Write as _;
1949 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
1950
1951 // Normal case
1952 let key = 0x_1234567890ABCDEF_u64;
1953 println!("K =\t{:#016X}", key);
1954 let mut a_des = DES::new_with_key_u64(key);
1955
1956 let message = "In the beginning God created the heavens and the earth.";
1957 println!("M =\t{}", message);
1958 let message = unsafe { message.to_string().as_mut_vec().clone() };
1959 let iv = 0x_FEDCBA0987654321_u64;
1960 println!("IV = {}", iv);
1961 let mut cipher = Vec::<u8>::new();
1962 a_des.encrypt_vec_into_vec(iv, &message, &mut cipher);
1963 print!("C (16 rounds) =\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, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
1971 println!();
1972
1973 // Expanded case for 128 rounds
1974 let key = 0x_1234567890ABCDEF_u64;
1975 println!("K =\t{:#016X}", key);
1976 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
1977
1978 let message = "In the beginning God created the heavens and the earth.";
1979 println!("M =\t{}", message);
1980 let message = unsafe { message.to_string().as_mut_vec().clone() };
1981 let iv = 0x_FEDCBA0987654321_u64;
1982 println!("IV = {}", iv);
1983 let mut cipher = Vec::<u8>::new();
1984 a_des.encrypt_vec_into_vec(iv, &message, &mut cipher);
1985 print!("C (128 rounds) =\t");
1986 for c in cipher.clone()
1987 { print!("{:02X} ", c); }
1988 println!();
1989 let mut txt = String::new();
1990 for c in cipher.clone()
1991 { write!(txt, "{:02X} ", c); }
1992 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
1993 println!();
1994
1995 // Expanded case for 0 rounds which means that key is meaningless
1996 let key1 = 0x_1234567890ABCDEF_u64;
1997 let key2 = 0_u64;
1998 println!("K1 =\t{:#016X}", key1);
1999 println!("K2 =\t{:#016X}", key2);
2000 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
2001 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
2002
2003 let message = "In the beginning God created the heavens and the earth.";
2004 println!("M =\t{}", message);
2005 let message = unsafe { message.to_string().as_mut_vec().clone() };
2006
2007 let iv = 0x_FEDCBA0987654321_u64;
2008 println!("IV = {}", iv);
2009 let mut cipher1 = Vec::<u8>::new();
2010 let mut cipher2 = Vec::<u8>::new();
2011 c_des.encrypt_vec_into_vec(iv, &message, &mut cipher1);
2012 d_des.encrypt_vec_into_vec(iv, &message, &mut cipher2);
2013 print!("C (0 rounds) =\t");
2014 for c in cipher1.clone()
2015 { print!("{:02X} ", c); }
2016 println!();
2017 let mut txt = String::new();
2018 for c in cipher1.clone()
2019 { write!(txt, "{:02X} ", c); }
2020 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
2021 print!("D (0 rounds) =\t");
2022 for c in cipher2.clone()
2023 { print!("{:02X} ", c); }
2024 println!();
2025 let mut txt = String::new();
2026 for c in cipher2.clone()
2027 { write!(txt, "{:02X} ", c); }
2028 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
2029 println!();
2030
2031 // Normal case for the message of 0 bytes
2032 let key = 0x_1234567890ABCDEF_u64;
2033 println!("K =\t{:#016X}", key);
2034 let mut a_des = DES::new_with_key_u64(key);
2035
2036 let message = "";
2037 println!("M =\t{}", message);
2038 let message = unsafe { message.to_string().as_mut_vec().clone() };
2039 let iv = 0x_FEDCBA0987654321_u64;
2040 println!("IV = {}", iv);
2041 let mut cipher = Vec::<u8>::new();
2042 a_des.encrypt_vec_into_vec(iv, &message, &mut cipher);
2043 print!("C =\t");
2044 for c in cipher.clone()
2045 { print!("{:02X} ", c); }
2046 println!();
2047 let mut txt = String::new();
2048 for c in cipher.clone()
2049 { write!(txt, "{:02X} ", c); }
2050 assert_eq!(txt, "");
2051 println!();
2052
2053 // Normal case for the message shorter than 8 bytes
2054 let key = 0x_1234567890ABCDEF_u64;
2055 println!("K =\t{:#016X}", key);
2056 let mut a_des = DES::new_with_key_u64(key);
2057
2058 let message = "7 bytes";
2059 println!("M =\t{}", message);
2060 let message = unsafe { message.to_string().as_mut_vec().clone() };
2061 let iv = 0x_FEDCBA0987654321_u64;
2062 println!("IV = {}", iv);
2063 let mut cipher = Vec::<u8>::new();
2064 a_des.encrypt_vec_into_vec(iv, &message, &mut cipher);
2065 print!("C =\t");
2066 for c in cipher.clone()
2067 { print!("{:02X} ", c); }
2068 println!();
2069 let mut txt = String::new();
2070 for c in cipher.clone()
2071 { write!(txt, "{:02X} ", c); }
2072 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
2073 println!();
2074
2075 // Normal case for the message of 8 bytes
2076 let key = 0x_1234567890ABCDEF_u64;
2077 println!("K =\t{:#016X}", key);
2078 let mut a_des = DES::new_with_key_u64(key);
2079
2080 let message = "I am OK.";
2081 println!("M =\t{}", message);
2082 let message = unsafe { message.to_string().as_mut_vec().clone() };
2083 let iv = 0x_FEDCBA0987654321_u64;
2084 println!("IV = {}", iv);
2085 let mut cipher = Vec::<u8>::new();
2086 a_des.encrypt_vec_into_vec(iv, &message, &mut cipher);
2087 print!("C =\t");
2088 for c in cipher.clone()
2089 { print!("{:02X} ", c); }
2090 println!();
2091 let mut txt = String::new();
2092 for c in cipher.clone()
2093 { write!(txt, "{:02X} ", c); }
2094 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
2095 println!();
2096
2097 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
2098 let key = 0x_1234567890ABCDEF_u64;
2099 println!("K =\t{:#016X}", key);
2100 let mut a_des = DES::new_with_key_u64(key);
2101
2102 let message = "PARK Youngho";
2103 println!("M =\t{}", message);
2104 let message = unsafe { message.to_string().as_mut_vec().clone() };
2105 let iv = 0x_FEDCBA0987654321_u64;
2106 println!("IV = {}", iv);
2107 let mut cipher = Vec::<u8>::new();
2108 a_des.encrypt_vec_into_vec(iv, &message, &mut cipher);
2109 print!("C =\t");
2110 for c in cipher.clone()
2111 { print!("{:02X} ", c); }
2112 println!();
2113 let mut txt = String::new();
2114 for c in cipher.clone()
2115 { write!(txt, "{:02X} ", c); }
2116 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
2117 println!();
2118
2119 // Normal case for the message of 16 bytes
2120 let key = 0x_1234567890ABCDEF_u64;
2121 println!("K =\t{:#016X}", key);
2122 let mut a_des = DES::new_with_key_u64(key);
2123
2124 let message = "고맙습니다.";
2125 println!("M =\t{}", message);
2126 let message = unsafe { message.to_string().as_mut_vec().clone() };
2127 let iv = 0x_FEDCBA0987654321_u64;
2128 println!("IV = {}", iv);
2129 let mut cipher = Vec::<u8>::new();
2130 a_des.encrypt_vec_into_vec(iv, &message, &mut cipher);
2131 print!("C =\t");
2132 for c in cipher.clone()
2133 { print!("{:02X} ", c); }
2134 println!();
2135 let mut txt = String::new();
2136 for c in cipher.clone()
2137 { write!(txt, "{:02X} ", c); }
2138 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
2139 println!("-------------------------------");
2140}Sourcefn encrypt_vec_into_array<U, V, const N: usize>(
&mut self,
iv: T,
message: &Vec<U>,
cipher: &mut [V; N],
) -> u64
fn encrypt_vec_into_array<U, V, const N: usize>( &mut self, iv: T, message: &Vec<U>, cipher: &mut [V; N], ) -> u64
Encrypts the data stored in a Vec<U> object without any padding in OFB
(Output FeedBack) mode, and stores the encrypted data in array [V; N].
§Arguments
ivis an initialization vector for OFB mode.messageis an immutable reference toVec<U>object, and is the place where the plaintext to be encrypted is stored.cipheris a mutable reference to an array[U; N]object, and is the place where the encrypted data will be stored.
§Output
- This method returns the size of ciphertext.
- If this method failed in encryption or
message.len()is0, this method returnszero.
§Features
- If
messageis an emptyVec<U>objectVec::<U>::new(), nothing will be encrypted, and stored in the array[U; N]objectcipher. - If
size_of::<V>()*Nis less thansize_of::<U>() * message.len(), this method does not perform encryption but returnszero. - If
size_of::<V>()*Nis equal tosize_of::<U>() * message.len(), this method performs encryption, fills the arraycipherwith the encrypted data, and returns the size of the ciphertext in bytes. - If
size_of::<V>()*Nis greater thansize_of::<U>() * message.len(), this method performs encryption, fills the arraycipherwith the encrypted data, and then fills the rest of the elements of the arraycipherwith zeros, and returns the size of the ciphertext in bytes. - The size of the area for ciphertext should be prepared to be
size_of::<U>()*message.len()at least. So, it is responsible for you to prepare thecipherarea big enough!
§For Rijndael or AES, and its variants
§Example 1 for AES-128
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ AES_128, CBC_ISO };
let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
println!("K =\t{:#016X}", key);
let mut a_aes = AES_128::new_with_key_u128(key);
let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
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; 55];
a_aes.encrypt_vec_into_array(iv, &message, &mut cipher);
print!("C =\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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 07 AF 4E C8 EF 5D 0A 74 97 3F 90 9E 05 9E 9E 32 FB 55 54 45 7C ED A2 2B F8 07 66 C0 7B CB 98 F3 BF 93 15 BA 26 1C 47 ");§For more examples,
click here
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, OFB };
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 iv = 0x_FEDCBA0987654321_u64;
println!("IV = {}", iv);
let mut cipher = [0_u8; 55];
a_des.encrypt_vec_into_array(iv, &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, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");§For more examples,
click here
§For BigCryptor128
§Example 1 for TAES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
+ AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
+ AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
println!("IV = {:#034X}", iv);
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; 55];
taes.encrypt_vec_into_array(iv, &message, &mut cipher);
print!("C =\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, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");§For more examples,
click here
§For BigCryptor64
§Example 1 for TDES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
+ DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
+ DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
let iv = 0x_FEDCBA0987654321_u64;
println!("IV = {:#018X}", iv);
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; 55];
tdes.encrypt_vec_into_array(iv, &message, &mut cipher);
print!("C =\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, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");§For more examples,
click here
Examples found in repository?
359fn bigcryptor64_encrypt_vec_ofb_into_array()
360{
361 println!("bigcryptor64_encrypt_vec_ofb_into_array()");
362 use std::io::Write;
363 use std::fmt::Write as _;
364 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
365
366 // TDES case
367 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
368 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
369 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
370 let iv = 0x_FEDCBA0987654321_u64;
371 println!("IV = {:#018X}", iv);
372 let message = "In the beginning God created the heavens and the earth.";
373 println!("M =\t{}", message);
374 let message = unsafe { message.to_string().as_mut_vec().clone() };
375 let mut cipher = [0_u8; 55];
376 tdes.encrypt_vec_into_array(iv, &message, &mut cipher);
377 print!("C =\t");
378 for c in cipher.clone()
379 { print!("{:02X} ", c); }
380 println!();
381 let mut txt = String::new();
382 for c in cipher.clone()
383 { write!(txt, "{:02X} ", c); }
384 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
385 println!("-------------------------------");
386}More examples
359fn bigcryptor128_encrypt_vec_ofb_into_array()
360{
361 println!("bigcryptor128_encrypt_vec_ofb_into_array()");
362 use std::io::Write;
363 use std::fmt::Write as _;
364 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
365
366 // TAES_128 case
367 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
368 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
369 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
370 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
371 println!("IV = {:#034X}", iv);
372 let message = "In the beginning God created the heavens and the earth.";
373 println!("M =\t{}", message);
374 let message = unsafe { message.to_string().as_mut_vec().clone() };
375 let mut cipher = [0_u8; 55];
376 taes.encrypt_vec_into_array(iv, &message, &mut cipher);
377 print!("C =\t");
378 for c in cipher.clone()
379 { print!("{:02X} ", c); }
380 println!();
381 let mut txt = String::new();
382 for c in cipher.clone()
383 { write!(txt, "{:02X} ", c); }
384 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
385 println!("-------------------------------");
386}1534fn aes_encrypt_vec_ofb_into_array()
1535{
1536 println!("aes_encrypt_vec_ofb_into_array()");
1537 use std::io::Write;
1538 use std::fmt::Write as _;
1539 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
1540
1541 // Normal case for AES-128
1542 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1543 println!("K =\t{:#016X}", key);
1544 let mut a_aes = AES_128::new_with_key_u128(key);
1545 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1546 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1547
1548 let message = "In the beginning God created the heavens and the earth.";
1549 println!("M =\t{}", message);
1550 let message = unsafe { message.to_string().as_mut_vec().clone() };
1551 let mut cipher = [0_u8; 55];
1552 a_aes.encrypt_vec_into_array(iv, &message, &mut cipher);
1553 print!("C =\t");
1554 for c in cipher.clone()
1555 { print!("{:02X} ", c); }
1556 println!();
1557 let mut txt = String::new();
1558 for c in cipher.clone()
1559 { write!(txt, "{:02X} ", c); }
1560 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1561 println!();
1562
1563 // Normal case for AES-192
1564 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1565 print!("K =\t");
1566 for i in 0..24
1567 { print!("{:02X}", key[i]); }
1568 println!();
1569 let mut a_aes = AES_192::new_with_key(&key);
1570 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1571 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1572
1573 let message = "In the beginning God created the heavens and the earth.";
1574 println!("M =\t{}", message);
1575 let message = unsafe { message.to_string().as_mut_vec().clone() };
1576 let mut cipher = [0_u8; 55];
1577 a_aes.encrypt_vec_into_array(iv, &message, &mut cipher);
1578 print!("C =\t");
1579 for c in cipher.clone()
1580 { print!("{:02X} ", c); }
1581 println!();
1582 let mut txt = String::new();
1583 for c in cipher.clone()
1584 { write!(txt, "{:02X} ", c); }
1585 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
1586 println!();
1587
1588 // Normal case for AES-256
1589 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1590 print!("K =\t");
1591 for i in 0..32
1592 { print!("{:02X}", key[i]); }
1593 println!();
1594 let mut a_aes = AES_256::new_with_key(&key);
1595 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1596 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1597
1598 let message = "In the beginning God created the heavens and the earth.";
1599 println!("M =\t{}", message);
1600 let message = unsafe { message.to_string().as_mut_vec().clone() };
1601 let mut cipher = [0_u8; 55];
1602 a_aes.encrypt_vec_into_array(iv, &message, &mut cipher);
1603 print!("C =\t");
1604 for c in cipher.clone()
1605 { print!("{:02X} ", c); }
1606 println!();
1607 let mut txt = String::new();
1608 for c in cipher.clone()
1609 { write!(txt, "{:02X} ", c); }
1610 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
1611 println!();
1612
1613 // Normal case for Rijndael-256-256
1614 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1615 print!("K =\t");
1616 for i in 0..32
1617 { print!("{:02X}", key[i]); }
1618 println!();
1619 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
1620 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1621 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
1622
1623 let message = "In the beginning God created the heavens and the earth.";
1624 println!("M =\t{}", message);
1625 let message = unsafe { message.to_string().as_mut_vec().clone() };
1626 let mut cipher = [0_u8; 55];
1627 a_rijndael.encrypt_vec_into_array(iv, &message, &mut cipher);
1628 print!("C =\t");
1629 for c in cipher.clone()
1630 { print!("{:02X} ", c); }
1631 println!();
1632 let mut txt = String::new();
1633 for c in cipher.clone()
1634 { write!(txt, "{:02X} ", c); }
1635 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
1636 println!();
1637
1638 // Normal case for Rijndael-512-512 for post-quantum
1639 use cryptocol::number::SharedArrays;
1640 use cryptocol::hash::SHA3_512;
1641 let mut sha3 = SHA3_512::new();
1642 sha3.absorb_str("Post-quantum");
1643 let key: [u8; 64] = sha3.get_hash_value_in_array();
1644 print!("K =\t");
1645 for i in 0..64
1646 { print!("{:02X}", key[i]); }
1647 println!();
1648 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
1649 sha3.absorb_str("Initialize");
1650 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
1651 iv.src = sha3.get_hash_value_in_array();
1652 let iv = unsafe { iv.des };
1653 print!("IV =\t");
1654 for i in 0..16
1655 { print!("{:08X}", iv[i].to_be()); }
1656 println!();
1657 let message = "In the beginning God created the heavens and the earth.";
1658 println!("M =\t{}", message);
1659 let message = unsafe { message.to_string().as_mut_vec().clone() };
1660 let mut cipher = [0_u8; 55];
1661 a_rijndael.encrypt_vec_into_array(iv, &message, &mut cipher);
1662 print!("C =\t");
1663 for c in cipher.clone()
1664 { print!("{:02X} ", c); }
1665 println!();
1666 let mut txt = String::new();
1667 for c in cipher.clone()
1668 { write!(txt, "{:02X} ", c); }
1669 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
1670 println!("-------------------------------");
1671}2142fn des_encrypt_vec_ofb_into_array()
2143{
2144 println!("des_encrypt_vec_ofb_into_array()");
2145 use std::io::Write;
2146 use std::fmt::Write as _;
2147 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
2148
2149 // Normal case
2150 let key = 0x_1234567890ABCDEF_u64;
2151 println!("K =\t{:#016X}", key);
2152 let mut a_des = DES::new_with_key_u64(key);
2153
2154 let message = "In the beginning God created the heavens and the earth.";
2155 println!("M =\t{}", message);
2156 let message = unsafe { message.to_string().as_mut_vec().clone() };
2157 let iv = 0x_FEDCBA0987654321_u64;
2158 println!("IV = {}", iv);
2159 let mut cipher = [0_u8; 55];
2160 a_des.encrypt_vec_into_array(iv, &message, &mut cipher);
2161 print!("C (16 rounds) =\t");
2162 for c in cipher.clone()
2163 { print!("{:02X} ", c); }
2164 println!();
2165 let mut txt = String::new();
2166 for c in cipher.clone()
2167 { write!(txt, "{:02X} ", c); }
2168 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
2169 println!();
2170
2171 // Expanded case for 128 rounds
2172 let key = 0x_1234567890ABCDEF_u64;
2173 println!("K =\t{:#016X}", key);
2174 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
2175
2176 let message = "In the beginning God created the heavens and the earth.";
2177 println!("M =\t{}", message);
2178 let message = unsafe { message.to_string().as_mut_vec().clone() };
2179 let iv = 0x_FEDCBA0987654321_u64;
2180 println!("IV = {}", iv);
2181 let mut cipher = [0_u8; 55];
2182 a_des.encrypt_vec_into_array(iv, &message, &mut cipher);
2183 print!("C (128 rounds) =\t");
2184 for c in cipher.clone()
2185 { print!("{:02X} ", c); }
2186 println!();
2187 let mut txt = String::new();
2188 for c in cipher.clone()
2189 { write!(txt, "{:02X} ", c); }
2190 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
2191 println!();
2192
2193 // Expanded case for 0 rounds which means that key is meaningless
2194 let key1 = 0x_1234567890ABCDEF_u64;
2195 let key2 = 0_u64;
2196 println!("K1 =\t{:#016X}", key1);
2197 println!("K2 =\t{:#016X}", key2);
2198 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
2199 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
2200
2201 let message = "In the beginning God created the heavens and the earth.";
2202 println!("M =\t{}", message);
2203 let message = unsafe { message.to_string().as_mut_vec().clone() };
2204 let iv = 0x_FEDCBA0987654321_u64;
2205 println!("IV = {}", iv);
2206 let mut cipher1 = [0_u8; 55];
2207 let mut cipher2 = [0_u8; 55];
2208 c_des.encrypt_vec_into_array(iv, &message, &mut cipher1);
2209 d_des.encrypt_vec_into_array(iv, &message, &mut cipher2);
2210 print!("C (0 rounds) =\t");
2211 for c in cipher1.clone()
2212 { print!("{:02X} ", c); }
2213 println!();
2214 let mut txt = String::new();
2215 for c in cipher1.clone()
2216 { write!(txt, "{:02X} ", c); }
2217 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
2218 print!("D (0 rounds) =\t");
2219 for c in cipher2.clone()
2220 { print!("{:02X} ", c); }
2221 println!();
2222 let mut txt = String::new();
2223 for c in cipher2.clone()
2224 { write!(txt, "{:02X} ", c); }
2225 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
2226 println!();
2227
2228 // Normal case for the message of 0 bytes
2229 let key = 0x_1234567890ABCDEF_u64;
2230 println!("K =\t{:#016X}", key);
2231 let mut a_des = DES::new_with_key_u64(key);
2232
2233 let message = "";
2234 println!("M =\t{}", message);
2235 let message = unsafe { message.to_string().as_mut_vec().clone() };
2236 let iv = 0x_FEDCBA0987654321_u64;
2237 println!("IV = {}", iv);
2238 let mut cipher = [0_u8; 0];
2239 a_des.encrypt_vec_into_array(iv, &message, &mut cipher);
2240 print!("C =\t");
2241 for c in cipher.clone()
2242 { print!("{:02X} ", c); }
2243 println!();
2244 let mut txt = String::new();
2245 for c in cipher.clone()
2246 { write!(txt, "{:02X} ", c); }
2247 assert_eq!(txt, "");
2248 println!();
2249
2250 // Normal case for the message shorter than 8 bytes
2251 let key = 0x_1234567890ABCDEF_u64;
2252 println!("K =\t{:#016X}", key);
2253 let mut a_des = DES::new_with_key_u64(key);
2254
2255 let message = "7 bytes";
2256 println!("M =\t{}", message);
2257 let message = unsafe { message.to_string().as_mut_vec().clone() };
2258 let iv = 0x_FEDCBA0987654321_u64;
2259 println!("IV = {}", iv);
2260 let mut cipher = [0_u8; 7];
2261 a_des.encrypt_vec_into_array(iv, &message, &mut cipher);
2262 print!("C =\t");
2263 for c in cipher.clone()
2264 { print!("{:02X} ", c); }
2265 println!();
2266 let mut txt = String::new();
2267 for c in cipher.clone()
2268 { write!(txt, "{:02X} ", c); }
2269 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
2270 println!();
2271
2272 // Normal case for the message of 8 bytes
2273 let key = 0x_1234567890ABCDEF_u64;
2274 println!("K =\t{:#016X}", key);
2275 let mut a_des = DES::new_with_key_u64(key);
2276
2277 let message = "I am OK.";
2278 println!("M =\t{}", message);
2279 let message = unsafe { message.to_string().as_mut_vec().clone() };
2280 let iv = 0x_FEDCBA0987654321_u64;
2281 println!("IV = {}", iv);
2282 let mut cipher = [0_u8; 8];
2283 a_des.encrypt_vec_into_array(iv, &message, &mut cipher);
2284 print!("C =\t");
2285 for c in cipher.clone()
2286 { print!("{:02X} ", c); }
2287 println!();
2288 let mut txt = String::new();
2289 for c in cipher.clone()
2290 { write!(txt, "{:02X} ", c); }
2291 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
2292 println!();
2293
2294 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
2295 let key = 0x_1234567890ABCDEF_u64;
2296 println!("K =\t{:#016X}", key);
2297 let mut a_des = DES::new_with_key_u64(key);
2298
2299 let message = "PARK Youngho";
2300 println!("M =\t{}", message);
2301 let message = unsafe { message.to_string().as_mut_vec().clone() };
2302 let iv = 0x_FEDCBA0987654321_u64;
2303 println!("IV = {}", iv);
2304 let mut cipher = [0_u8; 12];
2305 a_des.encrypt_vec_into_array(iv, &message, &mut cipher);
2306 print!("C =\t");
2307 for c in cipher.clone()
2308 { print!("{:02X} ", c); }
2309 println!();
2310 let mut txt = String::new();
2311 for c in cipher.clone()
2312 { write!(txt, "{:02X} ", c); }
2313 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
2314 println!();
2315
2316 // Normal case for the message of 16 bytes
2317 let key = 0x_1234567890ABCDEF_u64;
2318 println!("K =\t{:#016X}", key);
2319 let mut a_des = DES::new_with_key_u64(key);
2320
2321 let message = "고맙습니다.";
2322 println!("M =\t{}", message);
2323 let message = unsafe { message.to_string().as_mut_vec().clone() };
2324 let iv = 0x_FEDCBA0987654321_u64;
2325 println!("IV = {}", iv);
2326 let mut cipher = [0_u8; 16];
2327 a_des.encrypt_vec_into_array(iv, &message, &mut cipher);
2328 print!("C =\t");
2329 for c in cipher.clone()
2330 { print!("{:02X} ", c); }
2331 println!();
2332 let mut txt = String::new();
2333 for c in cipher.clone()
2334 { write!(txt, "{:02X} ", c); }
2335 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
2336 println!("-------------------------------");
2337}Sourcefn encrypt_array<U, const N: usize>(
&mut self,
iv: T,
message: &[U; N],
cipher: *mut u8,
) -> u64
fn encrypt_array<U, const N: usize>( &mut self, iv: T, message: &[U; N], cipher: *mut u8, ) -> u64
Encrypts the data stored in an array [U; N] object without any
padding in OFB (Output FeedBack) mode.
§Arguments
ivis an initialization vector for OFB mode.messageis an immutable reference to an array[U; N]object, and is the place where the plaintext to be encrypted is stored.cipheris a mutable pointer tou8which is*mut u8, and is the place where the encrypted data will be stored.
§Output
- This method returns the size of ciphertext.
- If this method failed in encryption or
message.len()is0, this method returnszero.
§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++.
- The size of the memory area which starts at
cipheris assumed to be enough to store the ciphertext. - The size of the area for ciphertext should be prepared to be
size_of::<U>()*Nat least. So, it is responsible for you to prepare thecipherarea big enough! - If
messageis an empty array[U; 0]object, nothing will be encrypted, and stored in the memory area that starts fromcipher.
§For Rijndael or AES, and its variants
§Example 1 for AES-128
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ AES_128, CBC_ISO };
let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
println!("K =\t{:#016X}", key);
let mut a_aes = AES_128::new_with_key_u128(key);
let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
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; 55];
a_aes.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
print!("C =\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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 07 AF 4E C8 EF 5D 0A 74 97 3F 90 9E 05 9E 9E 32 FB 55 54 45 7C ED A2 2B F8 07 66 C0 7B CB 98 F3 BF 93 15 BA 26 1C 47 ");§For more examples,
click here
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, OFB };
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 iv = 0x_FEDCBA0987654321_u64;
println!("IV = {}", iv);
let mut cipher = [0_u8; 55];
a_des.encrypt_array(iv, &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, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");§For more examples,
click here
§For BigCryptor128
§Example 1 for TAES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
+ AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
+ AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
println!("IV = {:#034X}", iv);
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; 55];
taes.encrypt_array(iv, &message, cipher.as_mut_ptr());
print!("C =\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, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");§For more examples,
click here
§For BigCryptor64
§Example 1 for TDES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
+ DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
+ DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
let iv = 0x_FEDCBA0987654321_u64;
println!("IV = {:#018X}", iv);
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; 55];
tdes.encrypt_array(iv, &message, cipher.as_mut_ptr());
print!("C =\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, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");§For more examples,
click here
Examples found in repository?
388fn bigcryptor64_encrypt_array_ofb()
389{
390 println!("bigcryptor64_encrypt_array_ofb()");
391 use std::io::Write;
392 use std::fmt::Write as _;
393 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
394
395 // TDES case
396 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
397 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
398 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
399 let iv = 0x_FEDCBA0987654321_u64;
400 println!("IV = {:#018X}", iv);
401 let mes = "In the beginning God created the heavens and the earth.";
402 println!("M =\t{}", mes);
403 let mut message = [0_u8; 55];
404 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
405 let mut cipher = [0_u8; 55];
406 tdes.encrypt_array(iv, &message, cipher.as_mut_ptr());
407 print!("C =\t");
408 for c in cipher.clone()
409 { print!("{:02X} ", c); }
410 println!();
411 let mut txt = String::new();
412 for c in cipher.clone()
413 { write!(txt, "{:02X} ", c); }
414 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
415 println!("-------------------------------");
416}More examples
388fn bigcryptor128_encrypt_array_ofb()
389{
390 println!("bigcryptor128_encrypt_array_ofb()");
391 use std::io::Write;
392 use std::fmt::Write as _;
393 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
394
395 // TAES_128 case
396 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
397 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
398 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
399 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
400 println!("IV = {:#034X}", iv);
401 let mes = "In the beginning God created the heavens and the earth.";
402 println!("M =\t{}", mes);
403 let mut message = [0_u8; 55];
404 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
405 let mut cipher = [0_u8; 55];
406 taes.encrypt_array(iv, &message, cipher.as_mut_ptr());
407 print!("C =\t");
408 for c in cipher.clone()
409 { print!("{:02X} ", c); }
410 println!();
411 let mut txt = String::new();
412 for c in cipher.clone()
413 { write!(txt, "{:02X} ", c); }
414 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
415 println!("-------------------------------");
416}2339fn des_encrypt_array_ofb()
2340{
2341 println!("des_encrypt_array_ofb()");
2342 use std::io::Write;
2343 use std::fmt::Write as _;
2344 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
2345
2346 // Normal case
2347 let key = 0x_1234567890ABCDEF_u64;
2348 println!("K =\t{:#016X}", key);
2349 let mut a_des = DES::new_with_key_u64(key);
2350
2351 let mes = "In the beginning God created the heavens and the earth.";
2352 println!("M =\t{}", mes);
2353 let mut message = [0_u8; 55];
2354 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2355 let iv = 0x_FEDCBA0987654321_u64;
2356 println!("IV = {}", iv);
2357 let mut cipher = [0_u8; 55];
2358 a_des.encrypt_array(iv, &message, cipher.as_mut_ptr());
2359 print!("C (16 rounds) =\t");
2360 for c in cipher.clone()
2361 { print!("{:02X} ", c); }
2362 println!();
2363 let mut txt = String::new();
2364 for c in cipher.clone()
2365 { write!(txt, "{:02X} ", c); }
2366 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
2367 println!();
2368
2369 // Expanded case for 128 rounds
2370 let key = 0x_1234567890ABCDEF_u64;
2371 println!("K =\t{:#016X}", key);
2372 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
2373
2374 let mes = "In the beginning God created the heavens and the earth.";
2375 println!("M =\t{}", mes);
2376 let mut message = [0_u8; 55];
2377 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2378 let iv = 0x_FEDCBA0987654321_u64;
2379 println!("IV = {}", iv);
2380 let mut cipher = [0_u8; 55];
2381 a_des.encrypt_array(iv, &message, cipher.as_mut_ptr());
2382 print!("C (128 rounds) =\t");
2383 for c in cipher.clone()
2384 { print!("{:02X} ", c); }
2385 println!();
2386 let mut txt = String::new();
2387 for c in cipher.clone()
2388 { write!(txt, "{:02X} ", c); }
2389 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
2390 println!();
2391
2392 // Expanded case for 0 rounds which means that key is meaningless
2393 let key1 = 0x_1234567890ABCDEF_u64;
2394 let key2 = 0_u64;
2395 println!("K1 =\t{:#016X}", key1);
2396 println!("K2 =\t{:#016X}", key2);
2397 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
2398 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
2399
2400 let mes = "In the beginning God created the heavens and the earth.";
2401 println!("M =\t{}", mes);
2402 let mut message = [0_u8; 55];
2403 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2404 let iv = 0x_FEDCBA0987654321_u64;
2405 println!("IV = {}", iv);
2406 let mut cipher1 = [0_u8; 55];
2407 let mut cipher2 = [0_u8; 55];
2408 c_des.encrypt_array(iv, &message, cipher1.as_mut_ptr());
2409 d_des.encrypt_array(iv, &message, cipher2.as_mut_ptr());
2410 print!("C (0 rounds) =\t");
2411 for c in cipher1.clone()
2412 { print!("{:02X} ", c); }
2413 println!();
2414 let mut txt = String::new();
2415 for c in cipher1.clone()
2416 { write!(txt, "{:02X} ", c); }
2417 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
2418 print!("D (0 rounds) =\t");
2419 for c in cipher2.clone()
2420 { print!("{:02X} ", c); }
2421 println!();
2422 let mut txt = String::new();
2423 for c in cipher2.clone()
2424 { write!(txt, "{:02X} ", c); }
2425 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
2426 println!();
2427
2428 // Normal case for the message of 0 bytes
2429 let key = 0x_1234567890ABCDEF_u64;
2430 println!("K =\t{:#016X}", key);
2431 let mut a_des = DES::new_with_key_u64(key);
2432
2433 let mes = "";
2434 println!("M =\t{}", mes);
2435 let mut message = [0_u8; 0];
2436 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2437 let iv = 0x_FEDCBA0987654321_u64;
2438 println!("IV = {}", iv);
2439 let mut cipher = [0_u8; 0];
2440 a_des.encrypt_array(iv, &message, cipher.as_mut_ptr());
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, "");
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 iv = 0x_FEDCBA0987654321_u64;
2461 println!("IV = {}", iv);
2462 let mut cipher = [0_u8; 7];
2463 a_des.encrypt_array(iv, &message, cipher.as_mut_ptr());
2464 print!("C =\t");
2465 for c in cipher.clone()
2466 { print!("{:02X} ", c); }
2467 println!();
2468 let mut txt = String::new();
2469 for c in cipher.clone()
2470 { write!(txt, "{:02X} ", c); }
2471 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
2472 println!();
2473
2474 // Normal case for the message of 8 bytes
2475 let key = 0x_1234567890ABCDEF_u64;
2476 println!("K =\t{:#016X}", key);
2477 let mut a_des = DES::new_with_key_u64(key);
2478
2479 let mes = "I am OK.";
2480 println!("M =\t{}", mes);
2481 let mut message = [0_u8; 8];
2482 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2483 let iv = 0x_FEDCBA0987654321_u64;
2484 println!("IV = {}", iv);
2485 let mut cipher = [0_u8; 8];
2486 a_des.encrypt_array(iv, &message, cipher.as_mut_ptr());
2487 print!("C =\t");
2488 for c in cipher.clone()
2489 { print!("{:02X} ", c); }
2490 println!();
2491 let mut txt = String::new();
2492 for c in cipher.clone()
2493 { write!(txt, "{:02X} ", c); }
2494 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
2495 println!();
2496
2497 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
2498 let key = 0x_1234567890ABCDEF_u64;
2499 println!("K =\t{:#016X}", key);
2500 let mut a_des = DES::new_with_key_u64(key);
2501
2502 let mes = "PARK Youngho";
2503 println!("M =\t{}", mes);
2504 let mut message = [0_u8; 12];
2505 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2506 let iv = 0x_FEDCBA0987654321_u64;
2507 println!("IV = {}", iv);
2508 let mut cipher = [0_u8; 12];
2509 a_des.encrypt_array(iv, &message, cipher.as_mut_ptr());
2510 print!("C =\t");
2511 for c in cipher.clone()
2512 { print!("{:02X} ", c); }
2513 println!();
2514 let mut txt = String::new();
2515 for c in cipher.clone()
2516 { write!(txt, "{:02X} ", c); }
2517 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
2518 println!();
2519
2520 // Normal case for the message of 16 bytes
2521 let key = 0x_1234567890ABCDEF_u64;
2522 println!("K =\t{:#016X}", key);
2523 let mut a_des = DES::new_with_key_u64(key);
2524
2525 let mes = "고맙습니다.";
2526 println!("M =\t{}", mes);
2527 let mut message = [0_u8; 16];
2528 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2529 let iv = 0x_FEDCBA0987654321_u64;
2530 println!("IV = {}", iv);
2531 let mut cipher = [0_u8; 16];
2532 a_des.encrypt_array(iv, &message, cipher.as_mut_ptr());
2533 print!("C =\t");
2534 for c in cipher.clone()
2535 { print!("{:02X} ", c); }
2536 println!();
2537 let mut txt = String::new();
2538 for c in cipher.clone()
2539 { write!(txt, "{:02X} ", c); }
2540 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
2541 println!("-------------------------------");
2542}Sourcefn encrypt_array_into_vec<U, V, const N: usize>(
&mut self,
iv: T,
message: &[U; N],
cipher: &mut Vec<V>,
) -> u64
fn encrypt_array_into_vec<U, V, const N: usize>( &mut self, iv: T, message: &[U; N], cipher: &mut Vec<V>, ) -> u64
Encrypts the data stored in an array [U; N] object without any padding
in OFB (Output FeedBack) mode, and stores the encrypted data in Vec<V>.
§Arguments
ivis an initialization vector for OFB mode.messageis an immutable reference to an array[U; N]object, and is the place where the plaintext to be encrypted is stored.cipheris a mutable reference toVec<U>object, and is the place where the encrypted data will be stored.
§Output
- This method returns the size of ciphertext.
- If this method failed in encryption or
message.len()is0, this method returnszero.
§Features
- If
messageis an empty array[U; 0]object, nothing will be encrypted, and stored in theVec<U>objectcipher. - 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 Rijndael or AES, and its variants
§Example 1 for AES-128
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ AES_128, CBC_ISO };
let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
println!("K =\t{:#016X}", key);
let mut a_aes = AES_128::new_with_key_u128(key);
let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
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_aes.encrypt_array_into_vec(iv, &message, &mut cipher);
print!("C =\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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 07 AF 4E C8 EF 5D 0A 74 97 3F 90 9E 05 9E 9E 32 FB 55 54 45 7C ED A2 2B F8 07 66 C0 7B CB 98 F3 BF 93 15 BA 26 1C 47 ");§For more examples,
click here
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, OFB };
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 iv = 0x_FEDCBA0987654321_u64;
println!("IV = {}", iv);
let mut cipher = Vec::<u8>::new();
a_des.encrypt_array_into_vec(iv, &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, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");§For more examples,
click here
§For BigCryptor128
§Example 1 for TAES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
+ AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
+ AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
println!("IV = {:#034X}", iv);
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();
taes.encrypt_array_into_vec(iv, &message, &mut cipher);
print!("C =\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, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");§For more examples,
click here
§For BigCryptor64
§Example 1 for TDES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
+ DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
+ DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
let iv = 0x_FEDCBA0987654321_u64;
println!("IV = {:#018X}", iv);
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();
tdes.encrypt_array_into_vec(iv, &message, &mut cipher);
print!("C =\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, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");§For more examples,
click here
Examples found in repository?
418fn bigcryptor64_encrypt_array_ofb_into_vec()
419{
420 println!("bigcryptor64_encrypt_array_ofb_into_vec()");
421 use std::io::Write;
422 use std::fmt::Write as _;
423 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
424
425 // TDES case
426 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
427 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
428 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
429 let iv = 0x_FEDCBA0987654321_u64;
430 println!("IV = {:#018X}", iv);
431 let mes = "In the beginning God created the heavens and the earth.";
432 println!("M =\t{}", mes);
433 let mut message = [0_u8; 55];
434 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
435 let mut cipher = Vec::<u8>::new();
436 tdes.encrypt_array_into_vec(iv, &message, &mut cipher);
437 print!("C =\t");
438 for c in cipher.clone()
439 { print!("{:02X} ", c); }
440 println!();
441 let mut txt = String::new();
442 for c in cipher.clone()
443 { write!(txt, "{:02X} ", c); }
444 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
445 println!("-------------------------------");
446}More examples
418fn bigcryptor128_encrypt_array_ofb_into_vec()
419{
420 println!("bigcryptor128_encrypt_array_ofb_into_vec()");
421 use std::io::Write;
422 use std::fmt::Write as _;
423 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
424
425 // TAES_128 case
426 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
427 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
428 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
429 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
430 println!("IV = {:#034X}", iv);
431 let mes = "In the beginning God created the heavens and the earth.";
432 println!("M =\t{}", mes);
433 let mut message = [0_u8; 55];
434 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
435 let mut cipher = Vec::<u8>::new();
436 taes.encrypt_array_into_vec(iv, &message, &mut cipher);
437 print!("C =\t");
438 for c in cipher.clone()
439 { print!("{:02X} ", c); }
440 println!();
441 let mut txt = String::new();
442 for c in cipher.clone()
443 { write!(txt, "{:02X} ", c); }
444 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
445 println!("-------------------------------");
446}1817fn aes_encrypt_array_ofb_into_vec()
1818{
1819 println!("aes_encrypt_array_ofb_into_vec()");
1820 use std::io::Write;
1821 use std::fmt::Write as _;
1822 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
1823
1824 // Normal case for AES-128
1825 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1826 println!("K =\t{:#016X}", key);
1827 let mut a_aes = AES_128::new_with_key_u128(key);
1828 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1829 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1830
1831 let mes = "In the beginning God created the heavens and the earth.";
1832 println!("M =\t{}", mes);
1833 let mut message = [0_u8; 55];
1834 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1835 let mut cipher = Vec::<u8>::new();
1836 a_aes.encrypt_array_into_vec(iv, &message, &mut cipher);
1837 print!("C =\t");
1838 for c in cipher.clone()
1839 { print!("{:02X} ", c); }
1840 println!();
1841 let mut txt = String::new();
1842 for c in cipher.clone()
1843 { write!(txt, "{:02X} ", c); }
1844 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1845 println!();
1846
1847 // Normal case for AES-192
1848 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1849 print!("K =\t");
1850 for i in 0..24
1851 { print!("{:02X}", key[i]); }
1852 println!();
1853 let mut a_aes = AES_192::new_with_key(&key);
1854 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1855 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1856
1857 let mes = "In the beginning God created the heavens and the earth.";
1858 println!("M =\t{}", mes);
1859 let mut message = [0_u8; 55];
1860 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1861 let mut cipher = Vec::<u8>::new();
1862 a_aes.encrypt_array_into_vec(iv, &message, &mut cipher);
1863 print!("C =\t");
1864 for c in cipher.clone()
1865 { print!("{:02X} ", c); }
1866 println!();
1867 let mut txt = String::new();
1868 for c in cipher.clone()
1869 { write!(txt, "{:02X} ", c); }
1870 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
1871 println!();
1872
1873 // Normal case for AES-256
1874 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1875 print!("K =\t");
1876 for i in 0..32
1877 { print!("{:02X}", key[i]); }
1878 println!();
1879 let mut a_aes = AES_256::new_with_key(&key);
1880 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1881 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1882
1883 let mes = "In the beginning God created the heavens and the earth.";
1884 println!("M =\t{}", mes);
1885 let mut message = [0_u8; 55];
1886 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1887 let mut cipher = Vec::<u8>::new();
1888 a_aes.encrypt_array_into_vec(iv, &message, &mut cipher);
1889 print!("C =\t");
1890 for c in cipher.clone()
1891 { print!("{:02X} ", c); }
1892 println!();
1893 let mut txt = String::new();
1894 for c in cipher.clone()
1895 { write!(txt, "{:02X} ", c); }
1896 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
1897 println!();
1898
1899 // Normal case for Rijndael-256-256
1900 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1901 print!("K =\t");
1902 for i in 0..32
1903 { print!("{:02X}", key[i]); }
1904 println!();
1905 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
1906 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1907 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
1908
1909 let mes = "In the beginning God created the heavens and the earth.";
1910 println!("M =\t{}", mes);
1911 let mut message = [0_u8; 55];
1912 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1913 let mut cipher = Vec::<u8>::new();
1914 a_rijndael.encrypt_array_into_vec(iv, &message, &mut cipher);
1915 print!("C =\t");
1916 for c in cipher.clone()
1917 { print!("{:02X} ", c); }
1918 println!();
1919 let mut txt = String::new();
1920 for c in cipher.clone()
1921 { write!(txt, "{:02X} ", c); }
1922 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
1923 println!();
1924
1925 // Normal case for Rijndael-512-512 for post-quantum
1926 use cryptocol::number::SharedArrays;
1927 use cryptocol::hash::SHA3_512;
1928 let mut sha3 = SHA3_512::new();
1929 sha3.absorb_str("Post-quantum");
1930 let key: [u8; 64] = sha3.get_hash_value_in_array();
1931 print!("K =\t");
1932 for i in 0..64
1933 { print!("{:02X}", key[i]); }
1934 println!();
1935 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
1936 sha3.absorb_str("Initialize");
1937 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
1938 iv.src = sha3.get_hash_value_in_array();
1939 let iv = unsafe { iv.des };
1940 print!("IV =\t");
1941 for i in 0..16
1942 { print!("{:08X}", iv[i].to_be()); }
1943 println!();
1944 let mes = "In the beginning God created the heavens and the earth.";
1945 println!("M =\t{}", mes);
1946 let mut message = [0_u8; 55];
1947 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1948 let mut cipher = Vec::<u8>::new();
1949 a_rijndael.encrypt_array_into_vec(iv, &message, &mut cipher);
1950 print!("C =\t");
1951 for c in cipher.clone()
1952 { print!("{:02X} ", c); }
1953 println!();
1954 let mut txt = String::new();
1955 for c in cipher.clone()
1956 { write!(txt, "{:02X} ", c); }
1957 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
1958 println!("-------------------------------");
1959}2544fn des_encrypt_array_ofb_into_vec()
2545{
2546 println!("des_encrypt_array_ofb_into_vec()");
2547 use std::io::Write;
2548 use std::fmt::Write as _;
2549 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
2550
2551 // Normal case
2552 let key = 0x_1234567890ABCDEF_u64;
2553 println!("K =\t{:#016X}", key);
2554 let mut a_des = DES::new_with_key_u64(key);
2555
2556 let mes = "In the beginning God created the heavens and the earth.";
2557 println!("M =\t{}", mes);
2558 let mut message = [0_u8; 55];
2559 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2560 let iv = 0x_FEDCBA0987654321_u64;
2561 println!("IV = {}", iv);
2562 let mut cipher = Vec::<u8>::new();
2563 a_des.encrypt_array_into_vec(iv, &message, &mut cipher);
2564 print!("C (16 rounds) =\t");
2565 for c in cipher.clone()
2566 { print!("{:02X} ", c); }
2567 println!();
2568 let mut txt = String::new();
2569 for c in cipher.clone()
2570 { write!(txt, "{:02X} ", c); }
2571 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
2572 println!();
2573
2574 // Expanded case for 128 rounds
2575 let key = 0x_1234567890ABCDEF_u64;
2576 println!("K =\t{:#016X}", key);
2577 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
2578
2579 let mes = "In the beginning God created the heavens and the earth.";
2580 println!("M =\t{}", mes);
2581 let mut message = [0_u8; 55];
2582 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2583 let iv = 0x_FEDCBA0987654321_u64;
2584 println!("IV = {}", iv);
2585 let mut cipher = Vec::<u8>::new();
2586 a_des.encrypt_array_into_vec(iv, &message, &mut cipher);
2587 print!("C (128 rounds) =\t");
2588 for c in cipher.clone()
2589 { print!("{:02X} ", c); }
2590 println!();
2591 let mut txt = String::new();
2592 for c in cipher.clone()
2593 { write!(txt, "{:02X} ", c); }
2594 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
2595 println!();
2596
2597 // Expanded case for 0 rounds which means that key is meaningless
2598 let key1 = 0x_1234567890ABCDEF_u64;
2599 let key2 = 0_u64;
2600 println!("K1 =\t{:#016X}", key1);
2601 println!("K2 =\t{:#016X}", key2);
2602 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
2603 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
2604
2605 let mes = "In the beginning God created the heavens and the earth.";
2606 println!("M =\t{}", mes);
2607 let mut message = [0_u8; 55];
2608 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2609
2610 let iv = 0x_FEDCBA0987654321_u64;
2611 println!("IV = {}", iv);
2612 let mut cipher1 = Vec::<u8>::new();
2613 let mut cipher2 = Vec::<u8>::new();
2614 c_des.encrypt_array_into_vec(iv, &message, &mut cipher1);
2615 d_des.encrypt_array_into_vec(iv, &message, &mut cipher2);
2616 print!("C (0 rounds) =\t");
2617 for c in cipher1.clone()
2618 { print!("{:02X} ", c); }
2619 println!();
2620 let mut txt = String::new();
2621 for c in cipher1.clone()
2622 { write!(txt, "{:02X} ", c); }
2623 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
2624 print!("D (0 rounds) =\t");
2625 for c in cipher2.clone()
2626 { print!("{:02X} ", c); }
2627 println!();
2628 let mut txt = String::new();
2629 for c in cipher2.clone()
2630 { write!(txt, "{:02X} ", c); }
2631 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
2632 println!();
2633
2634 // Normal case for the message of 0 bytes
2635 let key = 0x_1234567890ABCDEF_u64;
2636 println!("K =\t{:#016X}", key);
2637 let mut a_des = DES::new_with_key_u64(key);
2638
2639 let mes = "";
2640 println!("M =\t{}", mes);
2641 let mut message = [0_u8; 0];
2642 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2643 let iv = 0x_FEDCBA0987654321_u64;
2644 println!("IV = {}", iv);
2645 let mut cipher = Vec::<u8>::new();
2646 a_des.encrypt_array_into_vec(iv, &message, &mut cipher);
2647 print!("C =\t");
2648 for c in cipher.clone()
2649 { print!("{:02X} ", c); }
2650 println!();
2651 let mut txt = String::new();
2652 for c in cipher.clone()
2653 { write!(txt, "{:02X} ", c); }
2654 assert_eq!(txt, "");
2655 println!();
2656
2657 // Normal case for the message shorter than 8 bytes
2658 let key = 0x_1234567890ABCDEF_u64;
2659 println!("K =\t{:#016X}", key);
2660 let mut a_des = DES::new_with_key_u64(key);
2661
2662 let mes = "7 bytes";
2663 println!("M =\t{}", mes);
2664 let mut message = [0_u8; 7];
2665 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2666 let iv = 0x_FEDCBA0987654321_u64;
2667 println!("IV = {}", iv);
2668 let mut cipher = Vec::<u8>::new();
2669 a_des.encrypt_array_into_vec(iv, &message, &mut cipher);
2670 print!("C =\t");
2671 for c in cipher.clone()
2672 { print!("{:02X} ", c); }
2673 println!();
2674 let mut txt = String::new();
2675 for c in cipher.clone()
2676 { write!(txt, "{:02X} ", c); }
2677 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
2678 println!();
2679
2680 // Normal case for the message of 8 bytes
2681 let key = 0x_1234567890ABCDEF_u64;
2682 println!("K =\t{:#016X}", key);
2683 let mut a_des = DES::new_with_key_u64(key);
2684
2685 let mes = "I am OK.";
2686 println!("M =\t{}", mes);
2687 let mut message = [0_u8; 8];
2688 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2689 let iv = 0x_FEDCBA0987654321_u64;
2690 println!("IV = {}", iv);
2691 let mut cipher = Vec::<u8>::new();
2692 a_des.encrypt_array_into_vec(iv, &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, "2E 50 A0 48 B5 99 DB 07 ");
2701 println!();
2702
2703 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
2704 let key = 0x_1234567890ABCDEF_u64;
2705 println!("K =\t{:#016X}", key);
2706 let mut a_des = DES::new_with_key_u64(key);
2707
2708 let mes = "PARK Youngho";
2709 println!("M =\t{}", mes);
2710 let mut message = [0_u8; 12];
2711 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2712 let iv = 0x_FEDCBA0987654321_u64;
2713 println!("IV = {}", iv);
2714 let mut cipher = Vec::<u8>::new();
2715 a_des.encrypt_array_into_vec(iv, &message, &mut cipher);
2716 print!("C =\t");
2717 for c in cipher.clone()
2718 { print!("{:02X} ", c); }
2719 println!();
2720 let mut txt = String::new();
2721 for c in cipher.clone()
2722 { write!(txt, "{:02X} ", c); }
2723 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
2724 println!();
2725
2726 // Normal case for the message of 16 bytes
2727 let key = 0x_1234567890ABCDEF_u64;
2728 println!("K =\t{:#016X}", key);
2729 let mut a_des = DES::new_with_key_u64(key);
2730
2731 let mes = "고맙습니다.";
2732 println!("M =\t{}", mes);
2733 let mut message = [0_u8; 16];
2734 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2735 let iv = 0x_FEDCBA0987654321_u64;
2736 println!("IV = {}", iv);
2737 let mut cipher = Vec::<u8>::new();
2738 a_des.encrypt_array_into_vec(iv, &message, &mut cipher);
2739 print!("C =\t");
2740 for c in cipher.clone()
2741 { print!("{:02X} ", c); }
2742 println!();
2743 let mut txt = String::new();
2744 for c in cipher.clone()
2745 { write!(txt, "{:02X} ", c); }
2746 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
2747 println!("-------------------------------");
2748}Sourcefn encrypt_array_into_array<U, V, const N: usize, const M: usize>(
&mut self,
iv: T,
message: &[U; N],
cipher: &mut [V; M],
) -> u64
fn encrypt_array_into_array<U, V, const N: usize, const M: usize>( &mut self, iv: T, message: &[U; N], cipher: &mut [V; M], ) -> u64
Encrypts the data stored in an array [U; N] object without any padding
in OFB (Output FeedBack) mode, and stores the encrypted data
in array [V; M].
§Arguments
ivis an initialization vector for OFB mode.messageis an immutable reference to an array[U; N]object, and is the place where the plaintext to be encrypted is stored.cipheris a mutable reference to an array[V; M]object, and is the place where the encrypted data will be stored.
§Output
- This method returns the size of ciphertext.
- If this method failed in encryption or
message.len()is0, this method returnszero.
§Features
- If
messageis an empty array[U; 0]object, nothing will be encrypted, and stored in the array[V; M]objectcipher. - If
V::size_in_bytes()*Mis less thanU::size_in_bytes()*N, this method does not perform encryption and returnszero. - If
V::size_in_bytes()*Mis equal toU::size_in_bytes()*N, this method performs encryption, fills the arraycipherwith the encrypted ciphertext, and returns the size of the ciphertext in bytes. - If
V::size_in_bytes()*Mis greater thanU::size_in_bytes()*N, this method performs encryption, fills the arraycipherwith the encrypted ciphertext, and then fills the rest of the elements of the arraycipherwith zeros, and returns the size of the ciphertext in bytes. - The size of the area for ciphertext should be prepared to be
size_of::<U>()*message.len()at least. So, it is responsible for you to prepare thecipherarea big enough!
§For Rijndael or AES, and its variants
§Example 1 for AES-128
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ AES_128, CBC_ISO };
let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
println!("K =\t{:#016X}", key);
let mut a_aes = AES_128::new_with_key_u128(key);
let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
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; 55];
a_aes.encrypt_array_into_array(iv, &message, &mut cipher);
print!("C =\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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 07 AF 4E C8 EF 5D 0A 74 97 3F 90 9E 05 9E 9E 32 FB 55 54 45 7C ED A2 2B F8 07 66 C0 7B CB 98 F3 BF 93 15 BA 26 1C 47 ");§For more examples,
click here
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, OFB };
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 iv = 0x_FEDCBA0987654321_u64;
println!("IV = {}", iv);
let mut cipher = [0_u8; 55];
a_des.encrypt_array_into_array(iv, &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, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");§For more examples,
click here
§For BigCryptor128
§Example 1 for TAES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
+ AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
+ AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
println!("IV = {:#034X}", iv);
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; 55];
taes.encrypt_array_into_array(iv, &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, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");§For more examples,
click here
§For BigCryptor64
§Example 1 for TDES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
+ DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
+ DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
let iv = 0x_FEDCBA0987654321_u64;
println!("IV = {:#018X}", iv);
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; 55];
tdes.encrypt_array_into_array(iv, &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, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");§For more examples,
click here
Examples found in repository?
448fn bigcryptor64_encrypt_array_ofb_into_array()
449{
450 println!("bigcryptor64_encrypt_array_ofb_into_array()");
451 use std::io::Write;
452 use std::fmt::Write as _;
453 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
454
455 // TDES case
456 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
457 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
458 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
459 let iv = 0x_FEDCBA0987654321_u64;
460 println!("IV = {:#018X}", iv);
461 let mes = "In the beginning God created the heavens and the earth.";
462 println!("M =\t{}", mes);
463 let mut message = [0_u8; 55];
464 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
465 let mut cipher = [0_u8; 55];
466 tdes.encrypt_array_into_array(iv, &message, &mut cipher);
467 for c in cipher.clone()
468 { print!("{:02X} ", c); }
469 println!();
470 let mut txt = String::new();
471 for c in cipher.clone()
472 { write!(txt, "{:02X} ", c); }
473 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
474 println!("-------------------------------");
475}More examples
448fn bigcryptor128_encrypt_array_ofb_into_array()
449{
450 println!("bigcryptor128_encrypt_array_ofb_into_array()");
451 use std::io::Write;
452 use std::fmt::Write as _;
453 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
454
455 // TAES_128 case
456 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
457 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
458 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
459 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
460 println!("IV = {:#034X}", iv);
461 let mes = "In the beginning God created the heavens and the earth.";
462 println!("M =\t{}", mes);
463 let mut message = [0_u8; 55];
464 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
465 let mut cipher = [0_u8; 55];
466 taes.encrypt_array_into_array(iv, &message, &mut cipher);
467 for c in cipher.clone()
468 { print!("{:02X} ", c); }
469 println!();
470 let mut txt = String::new();
471 for c in cipher.clone()
472 { write!(txt, "{:02X} ", c); }
473 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
474 println!("-------------------------------");
475}1961fn aes_encrypt_array_ofb_into_array()
1962{
1963 println!("aes_encrypt_array_ofb_into_array()");
1964 use std::io::Write;
1965 use std::fmt::Write as _;
1966 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
1967
1968 // Normal case for AES-128
1969 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
1970 println!("K =\t{:#016X}", key);
1971 let mut a_aes = AES_128::new_with_key_u128(key);
1972 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1973 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
1974
1975 let mes = "In the beginning God created the heavens and the earth.";
1976 println!("M =\t{}", mes);
1977 let mut message = [0_u8; 55];
1978 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
1979 let mut cipher = [0_u8; 55];
1980 a_aes.encrypt_array_into_array(iv, &message, &mut cipher);
1981 print!("C =\t");
1982 for c in cipher.clone()
1983 { print!("{:02X} ", c); }
1984 println!();
1985 let mut txt = String::new();
1986 for c in cipher.clone()
1987 { write!(txt, "{:02X} ", c); }
1988 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
1989 println!();
1990
1991 // Normal case for AES-192
1992 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
1993 print!("K =\t");
1994 for i in 0..24
1995 { print!("{:02X}", key[i]); }
1996 println!();
1997 let mut a_aes = AES_192::new_with_key(&key);
1998 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
1999 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2000
2001 let mes = "In the beginning God created the heavens and the earth.";
2002 println!("M =\t{}", mes);
2003 let mut message = [0_u8; 55];
2004 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2005 let mut cipher = [0_u8; 55];
2006 a_aes.encrypt_array_into_array(iv, &message, &mut cipher);
2007 print!("C =\t");
2008 for c in cipher.clone()
2009 { print!("{:02X} ", c); }
2010 println!();
2011 let mut txt = String::new();
2012 for c in cipher.clone()
2013 { write!(txt, "{:02X} ", c); }
2014 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
2015 println!();
2016
2017 // Normal case for AES-256
2018 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2019 print!("K =\t");
2020 for i in 0..32
2021 { print!("{:02X}", key[i]); }
2022 println!();
2023 let mut a_aes = AES_256::new_with_key(&key);
2024 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2025 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2026
2027 let mes = "In the beginning God created the heavens and the earth.";
2028 println!("M =\t{}", mes);
2029 let mut message = [0_u8; 55];
2030 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2031 let mut cipher = [0_u8; 55];
2032 a_aes.encrypt_array_into_array(iv, &message, &mut cipher);
2033 print!("C =\t");
2034 for c in cipher.clone()
2035 { print!("{:02X} ", c); }
2036 println!();
2037 let mut txt = String::new();
2038 for c in cipher.clone()
2039 { write!(txt, "{:02X} ", c); }
2040 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
2041 println!();
2042
2043 // Normal case for Rijndael-256-256
2044 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2045 print!("K =\t");
2046 for i in 0..32
2047 { print!("{:02X}", key[i]); }
2048 println!();
2049 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
2050 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2051 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
2052
2053 let mes = "In the beginning God created the heavens and the earth.";
2054 println!("M =\t{}", mes);
2055 let mut message = [0_u8; 55];
2056 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2057 let mut cipher = [0_u8; 55];
2058 a_rijndael.encrypt_array_into_array(iv, &message, &mut cipher);
2059 print!("C =\t");
2060 for c in cipher.clone()
2061 { print!("{:02X} ", c); }
2062 println!();
2063 let mut txt = String::new();
2064 for c in cipher.clone()
2065 { write!(txt, "{:02X} ", c); }
2066 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
2067 println!();
2068
2069 // Normal case for Rijndael-512-512 for post-quantum
2070 use cryptocol::number::SharedArrays;
2071 use cryptocol::hash::SHA3_512;
2072 let mut sha3 = SHA3_512::new();
2073 sha3.absorb_str("Post-quantum");
2074 let key: [u8; 64] = sha3.get_hash_value_in_array();
2075 print!("K =\t");
2076 for i in 0..64
2077 { print!("{:02X}", key[i]); }
2078 println!();
2079 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
2080 sha3.absorb_str("Initialize");
2081 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
2082 iv.src = sha3.get_hash_value_in_array();
2083 let iv = unsafe { iv.des };
2084 print!("IV =\t");
2085 for i in 0..16
2086 { print!("{:08X}", iv[i].to_be()); }
2087 println!();
2088 let mes = "In the beginning God created the heavens and the earth.";
2089 println!("M =\t{}", mes);
2090 let mut message = [0_u8; 55];
2091 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2092 let mut cipher = [0_u8; 55];
2093 a_rijndael.encrypt_array_into_array(iv, &message, &mut cipher);
2094 print!("C =\t");
2095 for c in cipher.clone()
2096 { print!("{:02X} ", c); }
2097 println!();
2098 let mut txt = String::new();
2099 for c in cipher.clone()
2100 { write!(txt, "{:02X} ", c); }
2101 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
2102 println!("-------------------------------");
2103}2750fn des_encrypt_array_ofb_into_array()
2751{
2752 println!("des_encrypt_array_ofb_into_array()");
2753 use std::io::Write;
2754 use std::fmt::Write as _;
2755 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
2756
2757 // Normal case
2758 let key = 0x_1234567890ABCDEF_u64;
2759 println!("K =\t{:#016X}", key);
2760 let mut a_des = DES::new_with_key_u64(key);
2761
2762 let mes = "In the beginning God created the heavens and the earth.";
2763 println!("M =\t{}", mes);
2764 let mut message = [0_u8; 55];
2765 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2766 let iv = 0x_FEDCBA0987654321_u64;
2767 println!("IV = {}", iv);
2768 let mut cipher = [0_u8; 55];
2769 a_des.encrypt_array_into_array(iv, &message, &mut cipher);
2770 for c in cipher.clone()
2771 { print!("{:02X} ", c); }
2772 println!();
2773 let mut txt = String::new();
2774 for c in cipher.clone()
2775 { write!(txt, "{:02X} ", c); }
2776 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
2777 println!();
2778
2779 // Expanded case for 128 rounds
2780 let key = 0x_1234567890ABCDEF_u64;
2781 println!("K =\t{:#016X}", key);
2782 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
2783
2784 let mes = "In the beginning God created the heavens and the earth.";
2785 println!("M =\t{}", mes);
2786 let mut message = [0_u8; 55];
2787 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2788 let iv = 0x_FEDCBA0987654321_u64;
2789 println!("IV = {}", iv);
2790 let mut cipher = [0_u8; 55];
2791 a_des.encrypt_array_into_array(iv, &message, &mut cipher);
2792 print!("C (128 rounds) =\t");
2793 for c in cipher.clone()
2794 { print!("{:02X} ", c); }
2795 println!();
2796 let mut txt = String::new();
2797 for c in cipher.clone()
2798 { write!(txt, "{:02X} ", c); }
2799 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
2800 println!();
2801
2802 // Expanded case for 0 rounds which means that key is meaningless
2803 let key1 = 0x_1234567890ABCDEF_u64;
2804 let key2 = 0_u64;
2805 println!("K1 =\t{:#016X}", key1);
2806 println!("K2 =\t{:#016X}", key2);
2807 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
2808 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
2809
2810 let mes = "In the beginning God created the heavens and the earth.";
2811 println!("M =\t{}", mes);
2812 let mut message = [0_u8; 55];
2813 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2814 let iv = 0x_FEDCBA0987654321_u64;
2815 println!("IV = {}", iv);
2816 let mut cipher1 = [0_u8; 55];
2817 let mut cipher2 = [0_u8; 55];
2818 c_des.encrypt_array_into_array(iv, &message, &mut cipher1);
2819 d_des.encrypt_array_into_array(iv, &message, &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, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
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, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
2836 println!();
2837
2838 // Normal case for the message of 0 bytes
2839 let key = 0x_1234567890ABCDEF_u64;
2840 println!("K =\t{:#016X}", key);
2841 let mut a_des = DES::new_with_key_u64(key);
2842
2843 let mes = "";
2844 println!("M =\t{}", mes);
2845 let mut message = [0_u8; 0];
2846 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2847 let iv = 0x_FEDCBA0987654321_u64;
2848 println!("IV = {}", iv);
2849 let mut cipher = [0_u8; 0];
2850 a_des.encrypt_array_into_array(iv, &message, &mut cipher);
2851 print!("C =\t");
2852 for c in cipher.clone()
2853 { print!("{:02X} ", c); }
2854 println!();
2855 let mut txt = String::new();
2856 for c in cipher.clone()
2857 { write!(txt, "{:02X} ", c); }
2858 assert_eq!(txt, "");
2859 println!();
2860
2861 // Normal case for the message shorter than 8 bytes
2862 let key = 0x_1234567890ABCDEF_u64;
2863 println!("K =\t{:#016X}", key);
2864 let mut a_des = DES::new_with_key_u64(key);
2865
2866 let mes = "7 bytes";
2867 println!("M =\t{}", mes);
2868 let mut message = [0_u8; 7];
2869 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2870 let iv = 0x_FEDCBA0987654321_u64;
2871 println!("IV = {}", iv);
2872 let mut cipher = [0_u8; 7];
2873 a_des.encrypt_array_into_array(iv, &message, &mut cipher);
2874 print!("C =\t");
2875 for c in cipher.clone()
2876 { print!("{:02X} ", c); }
2877 println!();
2878 let mut txt = String::new();
2879 for c in cipher.clone()
2880 { write!(txt, "{:02X} ", c); }
2881 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
2882 println!();
2883
2884 // Normal case for the message of 8 bytes
2885 let key = 0x_1234567890ABCDEF_u64;
2886 println!("K =\t{:#016X}", key);
2887 let mut a_des = DES::new_with_key_u64(key);
2888
2889 let mes = "I am OK.";
2890 println!("M =\t{}", mes);
2891 let mut message = [0_u8; 8];
2892 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2893 let iv = 0x_FEDCBA0987654321_u64;
2894 println!("IV = {}", iv);
2895 let mut cipher = [0_u8; 8];
2896 a_des.encrypt_array_into_array(iv, &message, &mut cipher);
2897 print!("C =\t");
2898 for c in cipher.clone()
2899 { print!("{:02X} ", c); }
2900 println!();
2901 let mut txt = String::new();
2902 for c in cipher.clone()
2903 { write!(txt, "{:02X} ", c); }
2904 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
2905 println!();
2906
2907 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
2908 let key = 0x_1234567890ABCDEF_u64;
2909 println!("K =\t{:#016X}", key);
2910 let mut a_des = DES::new_with_key_u64(key);
2911
2912 let mes = "PARK Youngho";
2913 println!("M =\t{}", mes);
2914 let mut message = [0_u8; 12];
2915 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2916 let iv = 0x_FEDCBA0987654321_u64;
2917 println!("IV = {}", iv);
2918 let mut cipher = [0_u8; 12];
2919 a_des.encrypt_array_into_array(iv, &message, &mut cipher);
2920 print!("C =\t");
2921 for c in cipher.clone()
2922 { print!("{:02X} ", c); }
2923 println!();
2924 let mut txt = String::new();
2925 for c in cipher.clone()
2926 { write!(txt, "{:02X} ", c); }
2927 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
2928 println!();
2929
2930 // Normal case for the message of 16 bytes
2931 let key = 0x_1234567890ABCDEF_u64;
2932 println!("K =\t{:#016X}", key);
2933 let mut a_des = DES::new_with_key_u64(key);
2934
2935 let mes = "고맙습니다.";
2936 println!("M =\t{}", mes);
2937 let mut message = [0_u8; 16];
2938 message.copy_from_slice(unsafe { mes.to_string().as_mut_vec() });
2939 let iv = 0x_FEDCBA0987654321_u64;
2940 println!("IV = {}", iv);
2941 let mut cipher = [0_u8; 16];
2942 a_des.encrypt_array_into_array(iv, &message, &mut cipher);
2943 print!("C =\t");
2944 for c in cipher.clone()
2945 { print!("{:02X} ", c); }
2946 println!();
2947 let mut txt = String::new();
2948 for c in cipher.clone()
2949 { write!(txt, "{:02X} ", c); }
2950 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
2951 println!("-------------------------------");
2952}Sourcefn decrypt(
&mut self,
iv: T,
cipher: *const u8,
length_in_bytes: u64,
message: *mut u8,
) -> u64
fn decrypt( &mut self, iv: T, cipher: *const u8, length_in_bytes: u64, message: *mut u8, ) -> u64
Decrypts the data without any padding in OFB (Output FeedBack) mode.
§Arguments
ivis an initialization vector for OFB mode.cipheris an immutable pointer tou8which is*const u8, and is the place where the ciphertext to be decrypted is stored.length_in_bytesis ofu64-type, and is the length of the ciphertextcipherin bytes.messageis a mutable pointer tou8which is*mut u8, and is the place where the decrypted data will be stored.
§Output
- This method returns the size of plaintext in bytes.
- If this method failed in decryption, it always returns
zero. - Even if this method succeeded in decryption, it returns
zerowhen the original plaintext is zero-length empty data. Then, you will have to check whether or not it failed by using the methodis_successful()oris_failed(). - If
length_in_bytesis greater than0(which means that the original plaintext is surely not empty data) and it returnszero, you can interpret it that this method surely failed in decryption.
§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++.
- The size of the memory area which starts at
messageis assumed to be enough to store the plaintext. So, it is responsible for you to prepare themessagearea big enough! - The size of the area for plaintext does not have to be prepared more
than
length_in_bytes. - If the size of the area for plaintext is prepared more than
length_in_bytes, the rest of the area will be filled with0s.
§For Rijndael or AES, and its variants
§Example 1 for AES-128
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ AES_128, CBC_ISO };
let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
println!("K =\t{:#016X}", key);
let mut a_aes = AES_128::new_with_key_u128(key);
let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = [0_u8; 55];
a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
print!("C =\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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 07 AF 4E C8 EF 5D 0A 74 97 3F 90 9E 05 9E 9E 32 FB 55 54 45 7C ED A2 2B F8 07 66 C0 7B CB 98 F3 BF 93 15 BA 26 1C 47 ");
let mut recovered = vec![0; 55];
a_aes.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
print!("Ba =\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 =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");
assert_eq!(converted, message);§For more examples,
click here
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, OFB };
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 iv = 0x_FEDCBA0987654321_u64;
println!("IV = {}", iv);
let mut cipher = Vec::<u8>::new();
a_des.encrypt_str_into_vec(iv, &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, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
let mut recovered = vec![0; 55];
a_des.decrypt(iv, 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
§For BigCryptor128
§Example 1 for TAES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
+ AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
+ AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
println!("IV = {:#034X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
taes.encrypt_str_into_vec(iv, &message, &mut cipher);
print!("C =\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, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
let mut recovered = vec![0; 55];
taes.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
print!("Ba =\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 =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");
assert_eq!(converted, message);§For more examples,
click here
§For BigCryptor64
§Example 1 for TDES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
+ DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
+ DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
let iv = 0x_FEDCBA0987654321_u64;
println!("IV = {:#018X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
print!("C =\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, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
let mut recovered = vec![0; 55];
tdes.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
print!("Ba =\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 =\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?
477fn bigcryptor64_decrypt_ofb()
478{
479 println!("bigcryptor64_decrypt_ofb()");
480 use std::io::Write;
481 use std::fmt::Write as _;
482 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
483
484 // TDES case
485 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
486 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
487 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
488 let iv = 0x_FEDCBA0987654321_u64;
489 println!("IV = {:#018X}", iv);
490 let message = "In the beginning God created the heavens and the earth.";
491 println!("M =\t{}", message);
492 let mut cipher = Vec::<u8>::new();
493 tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
494 print!("C =\t");
495 for c in cipher.clone()
496 { print!("{:02X} ", c); }
497 println!();
498 let mut txt = String::new();
499 for c in cipher.clone()
500 { write!(txt, "{:02X} ", c); }
501 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
502
503 let mut recovered = vec![0; 55];
504 tdes.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
505 print!("Ba =\t");
506 for b in recovered.clone()
507 { print!("{:02X} ", b); }
508 println!();
509 let mut txt = String::new();
510 for c in recovered.clone()
511 { write!(txt, "{:02X} ", c); }
512 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 ");
513
514 let mut converted = String::new();
515 unsafe { converted.as_mut_vec() }.append(&mut recovered);
516
517 println!("Bb =\t{}", converted);
518 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
519 assert_eq!(converted, message);
520 println!("-------------------------------");
521}More examples
477fn bigcryptor128_decrypt_ofb()
478{
479 println!("bigcryptor128_decrypt_ofb()");
480 use std::io::Write;
481 use std::fmt::Write as _;
482 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
483
484 // TAES_128 case
485 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
486 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
487 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
488 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
489 println!("IV = {:#034X}", iv);
490 let message = "In the beginning God created the heavens and the earth.";
491 println!("M =\t{}", message);
492 let mut cipher = Vec::<u8>::new();
493 taes.encrypt_str_into_vec(iv, &message, &mut cipher);
494 print!("C =\t");
495 for c in cipher.clone()
496 { print!("{:02X} ", c); }
497 println!();
498 let mut txt = String::new();
499 for c in cipher.clone()
500 { write!(txt, "{:02X} ", c); }
501 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
502
503 let mut recovered = vec![0; 55];
504 taes.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
505 print!("Ba =\t");
506 for b in recovered.clone()
507 { print!("{:02X} ", b); }
508 println!();
509 let mut txt = String::new();
510 for c in recovered.clone()
511 { write!(txt, "{:02X} ", c); }
512 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 ");
513
514 let mut converted = String::new();
515 unsafe { converted.as_mut_vec() }.append(&mut recovered);
516
517 println!("Bb =\t{}", converted);
518 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
519 assert_eq!(converted, message);
520 println!("-------------------------------");
521}2106fn aes_decrypt_ofb()
2107{
2108 println!("aes_decrypt_ofb");
2109 use std::io::Write;
2110 use std::fmt::Write as _;
2111 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
2112
2113 // Normal case for AES-128
2114 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
2115 println!("K =\t{:#016X}", key);
2116 let mut a_aes = AES_128::new_with_key_u128(key);
2117 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2118 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0], iv[1], iv[2], iv[3]);
2119
2120 let message = "In the beginning God created the heavens and the earth.";
2121 println!("M =\t{}", message);
2122 let mut cipher = [0_u8; 55];
2123 a_aes.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
2132
2133 let mut recovered = vec![0; 55];
2134 a_aes.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2135 print!("Ba =\t");
2136 for b in recovered.clone()
2137 { print!("{:02X} ", b); }
2138 println!();
2139 let mut txt = String::new();
2140 for c in recovered.clone()
2141 { write!(txt, "{:02X} ", c); }
2142 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 ");
2143
2144 let mut converted = String::new();
2145 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2146
2147 println!("Bb =\t{}", converted);
2148 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2149 assert_eq!(converted, message);
2150 println!();
2151
2152 // Normal case for AES-192
2153 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2154 print!("K =\t");
2155 for i in 0..24
2156 { print!("{:02X}", key[i]); }
2157 println!();
2158 let mut a_aes = AES_192::new_with_key(&key);
2159 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2160 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2161
2162 let message = "In the beginning God created the heavens and the earth.";
2163 println!("M =\t{}", message);
2164 let mut cipher = [0_u8; 55];
2165 a_aes.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
2166 print!("C =\t");
2167 for c in cipher.clone()
2168 { print!("{:02X} ", c); }
2169 println!();
2170 let mut txt = String::new();
2171 for c in cipher.clone()
2172 { write!(txt, "{:02X} ", c); }
2173 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
2174
2175 let mut recovered = vec![0; 55];
2176 a_aes.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2177 print!("Ba =\t");
2178 for b in recovered.clone()
2179 { print!("{:02X} ", b); }
2180 println!();
2181 let mut txt = String::new();
2182 for c in recovered.clone()
2183 { write!(txt, "{:02X} ", c); }
2184 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 ");
2185
2186 let mut converted = String::new();
2187 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2188
2189 println!("Bb =\t{}", converted);
2190 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2191 assert_eq!(converted, message);
2192 println!();
2193
2194 // Normal case for AES-256
2195 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2196 print!("K =\t");
2197 for i in 0..32
2198 { print!("{:02X}", key[i]); }
2199 println!();
2200 let mut a_aes = AES_256::new_with_key(&key);
2201 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2202 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2203
2204 let message = "In the beginning God created the heavens and the earth.";
2205 println!("M =\t{}", message);
2206 let mut cipher = [0_u8; 55];
2207 a_aes.encrypt(iv, message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
2208 print!("C =\t");
2209 for c in cipher.clone()
2210 { print!("{:02X} ", c); }
2211 println!();
2212 let mut txt = String::new();
2213 for c in cipher.clone()
2214 { write!(txt, "{:02X} ", c); }
2215 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
2216
2217 let mut recovered = vec![0; 55];
2218 a_aes.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2219 print!("Ba =\t");
2220 for b in recovered.clone()
2221 { print!("{:02X} ", b); }
2222 println!();
2223 let mut txt = String::new();
2224 for c in recovered.clone()
2225 { write!(txt, "{:02X} ", c); }
2226 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 ");
2227
2228 let mut converted = String::new();
2229 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2230
2231 println!("Bb =\t{}", converted);
2232 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2233 assert_eq!(converted, message);
2234 println!();
2235
2236 // Normal case for Rijndael-256-256
2237 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2238 print!("K =\t");
2239 for i in 0..32
2240 { print!("{:02X}", key[i]); }
2241 println!();
2242 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
2243 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2244 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
2245
2246 let message = "In the beginning God created the heavens and the earth.";
2247 println!("M =\t{}", message);
2248 let mut cipher = [0_u8; 55];
2249 a_rijndael.encrypt(iv, message.as_ptr(), message.len() as u64, 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, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
2258
2259 let mut recovered = vec![0; 55];
2260 a_rijndael.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2261 print!("Ba =\t");
2262 for b in recovered.clone()
2263 { print!("{:02X} ", b); }
2264 println!();
2265 let mut txt = String::new();
2266 for c in recovered.clone()
2267 { write!(txt, "{:02X} ", c); }
2268 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 ");
2269
2270 let mut converted = String::new();
2271 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2272
2273 println!("Bb =\t{}", converted);
2274 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2275 assert_eq!(converted, message);
2276 println!();
2277
2278 // Normal case for Rijndael-512-512 for post-quantum
2279 use cryptocol::number::SharedArrays;
2280 use cryptocol::hash::SHA3_512;
2281 let mut sha3 = SHA3_512::new();
2282 sha3.absorb_str("Post-quantum");
2283 let key: [u8; 64] = sha3.get_hash_value_in_array();
2284 print!("K =\t");
2285 for i in 0..64
2286 { print!("{:02X}", key[i]); }
2287 println!();
2288 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
2289 sha3.absorb_str("Initialize");
2290 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
2291 iv.src = sha3.get_hash_value_in_array();
2292 let iv = unsafe { iv.des };
2293 print!("IV =\t");
2294 for i in 0..16
2295 { print!("{:08X}", iv[i].to_be()); }
2296 println!();
2297 let message = "In the beginning God created the heavens and the earth.";
2298 println!("M =\t{}", message);
2299 let mut cipher = [0_u8; 55];
2300 a_rijndael.encrypt(iv.clone(), message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
2301 print!("C =\t");
2302 for c in cipher.clone()
2303 { print!("{:02X} ", c); }
2304 println!();
2305 let mut txt = String::new();
2306 for c in cipher.clone()
2307 { write!(txt, "{:02X} ", c); }
2308 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
2309
2310 let mut recovered = vec![0; 55];
2311 a_rijndael.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2312 print!("Ba =\t");
2313 for b in recovered.clone()
2314 { print!("{:02X} ", b); }
2315 println!();
2316 let mut txt = String::new();
2317 for c in recovered.clone()
2318 { write!(txt, "{:02X} ", c); }
2319 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 ");
2320
2321 let mut converted = String::new();
2322 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2323
2324 println!("Bb =\t{}", converted);
2325 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2326 assert_eq!(converted, message);
2327 println!("-------------------------------");
2328}2954fn des_decrypt_ofb()
2955{
2956 println!("des_decrypt_ofb()");
2957 use std::io::Write;
2958 use std::fmt::Write as _;
2959 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
2960
2961 // Normal case
2962 let key = 0x_1234567890ABCDEF_u64;
2963 println!("K =\t{:#016X}", key);
2964 let mut a_des = DES::new_with_key_u64(key);
2965
2966 let message = "In the beginning God created the heavens and the earth.";
2967 println!("M =\t{}", message);
2968 let iv = 0x_FEDCBA0987654321_u64;
2969 println!("IV = {}", iv);
2970 let mut cipher = Vec::<u8>::new();
2971 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
2972 print!("C (16 rounds) =\t");
2973 for c in cipher.clone()
2974 { print!("{:02X} ", c); }
2975 println!();
2976 let mut txt = String::new();
2977 for c in cipher.clone()
2978 { write!(txt, "{:02X} ", c); }
2979 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
2980
2981 let mut recovered = vec![0; 55];
2982 a_des.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
2983 print!("Ba (16 rounds) =\t");
2984 for b in recovered.clone()
2985 { print!("{:02X} ", b); }
2986 println!();
2987 let mut txt = String::new();
2988 for c in recovered.clone()
2989 { write!(txt, "{:02X} ", c); }
2990 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 ");
2991
2992 let mut converted = String::new();
2993 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2994
2995 println!("Bb (16 rounds) =\t{}", converted);
2996 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2997 assert_eq!(converted, message);
2998 println!();
2999
3000 // Expanded case for 128 rounds
3001 let key = 0x_1234567890ABCDEF_u64;
3002 println!("K =\t{:#016X}", key);
3003 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
3004
3005 let message = "In the beginning God created the heavens and the earth.";
3006 println!("M =\t{}", message);
3007 let iv = 0x_FEDCBA0987654321_u64;
3008 println!("IV = {}", iv);
3009 let mut cipher = Vec::<u8>::new();
3010 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3011 print!("C (128 rounds) =\t");
3012 for c in cipher.clone()
3013 { print!("{:02X} ", c); }
3014 println!();
3015 let mut txt = String::new();
3016 for c in cipher.clone()
3017 { write!(txt, "{:02X} ", c); }
3018 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
3019
3020 let mut recovered = vec![0; 55];
3021 a_des.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
3022 print!("Ba (128 rounds) =\t");
3023 for b in recovered.clone()
3024 { print!("{:02X} ", b); }
3025 println!();
3026 let mut txt = String::new();
3027 for c in recovered.clone()
3028 { write!(txt, "{:02X} ", c); }
3029 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 ");
3030
3031 let mut converted = String::new();
3032 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3033
3034 println!("Bb (128 rounds) =\t{}", converted);
3035 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3036 assert_eq!(converted, message);
3037 println!();
3038
3039 // Expanded case for 0 rounds which means that key is meaningless
3040 let key1 = 0x_1234567890ABCDEF_u64;
3041 let key2 = 0_u64;
3042 println!("K =\t{:#016X}", key);
3043 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
3044 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
3045
3046 let message = "In the beginning God created the heavens and the earth.";
3047 println!("M =\t{}", message);
3048 let iv = 0x_FEDCBA0987654321_u64;
3049 println!("IV = {}", iv);
3050 let mut cipher1 = Vec::<u8>::new();
3051 let mut cipher2 = Vec::<u8>::new();
3052 c_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
3053 d_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
3054 print!("C (0 rounds) =\t");
3055 for c in cipher1.clone()
3056 { print!("{:02X} ", c); }
3057 println!();
3058 let mut txt = String::new();
3059 for c in cipher1.clone()
3060 { write!(txt, "{:02X} ", c); }
3061 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
3062 print!("D (0 rounds) =\t");
3063 for c in cipher2.clone()
3064 { print!("{:02X} ", c); }
3065 println!();
3066 let mut txt = String::new();
3067 for c in cipher2.clone()
3068 { write!(txt, "{:02X} ", c); }
3069 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
3070
3071 let mut recovered1 = vec![0; 55];
3072 let mut recovered2 = vec![0; 55];
3073 c_des.decrypt(iv, cipher1.as_ptr(), cipher1.len() as u64, recovered1.as_mut_ptr());
3074 d_des.decrypt(iv, cipher2.as_ptr(), cipher2.len() as u64, recovered2.as_mut_ptr());
3075 print!("B1a (0 rounds) =\t");
3076 for b in recovered1.clone()
3077 { print!("{:02X} ", b); }
3078 println!();
3079 let mut txt = String::new();
3080 for c in recovered1.clone()
3081 { write!(txt, "{:02X} ", c); }
3082 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 ");
3083 print!("B2a (0 rounds) =\t");
3084 for b in recovered2.clone()
3085 { print!("{:02X} ", b); }
3086 println!();
3087 let mut txt = String::new();
3088 for c in recovered2.clone()
3089 { write!(txt, "{:02X} ", c); }
3090 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 ");
3091
3092 let mut converted1 = String::new();
3093 let mut converted2 = String::new();
3094 unsafe { converted1.as_mut_vec() }.append(&mut recovered1);
3095 unsafe { converted2.as_mut_vec() }.append(&mut recovered2);
3096
3097 println!("B1b (0 rounds) =\t{}", converted1);
3098 assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
3099 assert_eq!(converted1, message);
3100 println!("B2b (0 rounds) =\t{}", converted2);
3101 assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
3102 assert_eq!(converted2, message);
3103 assert_eq!(converted1, converted1);
3104 println!();
3105
3106 // Normal case for the message of 0 bytes
3107 let key = 0x_1234567890ABCDEF_u64;
3108 println!("K =\t{:#016X}", key);
3109 let mut a_des = DES::new_with_key_u64(key);
3110
3111 let message = "";
3112 println!("M =\t{}", message);
3113 let iv = 0x_FEDCBA0987654321_u64;
3114 println!("IV = {}", iv);
3115 let mut cipher = Vec::<u8>::new();
3116 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3117 print!("C =\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, "");
3125
3126 let mut recovered = vec![0; 8];
3127 let len = a_des.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
3128 print!("Ba =\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, "00 00 00 00 00 00 00 00 ");
3136
3137 let mut converted = String::new();
3138 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3139 converted.truncate(len as usize);
3140
3141 println!("Bb =\t{}", converted);
3142 assert_eq!(converted, "");
3143 assert_eq!(converted, message);
3144 println!();
3145
3146 // Normal case for the message shorter than 8 bytes
3147 let key = 0x_1234567890ABCDEF_u64;
3148 println!("K =\t{:#016X}", key);
3149 let mut a_des = DES::new_with_key_u64(key);
3150
3151 let message = "7 bytes";
3152 println!("M =\t{}", message);
3153 let iv = 0x_FEDCBA0987654321_u64;
3154 println!("IV = {}", iv);
3155 let mut cipher = Vec::<u8>::new();
3156 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3157 print!("C =\t");
3158 for c in cipher.clone()
3159 { print!("{:02X} ", c); }
3160 println!();
3161 let mut txt = String::new();
3162 for c in cipher.clone()
3163 { write!(txt, "{:02X} ", c); }
3164 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
3165
3166 let mut recovered = vec![0; 8];
3167 let len = a_des.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
3168 print!("Ba =\t");
3169 for b in recovered.clone()
3170 { print!("{:02X} ", b); }
3171 println!();
3172 let mut txt = String::new();
3173 for c in recovered.clone()
3174 { write!(txt, "{:02X} ", c); }
3175 assert_eq!(txt, "37 20 62 79 74 65 73 00 ");
3176
3177 let mut converted = String::new();
3178 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3179 converted.truncate(len as usize);
3180
3181 println!("Bb =\t{}", converted);
3182 assert_eq!(converted, "7 bytes");
3183 assert_eq!(converted, message);
3184 println!();
3185
3186 // Normal case for the message of 8 bytes
3187 let key = 0x_1234567890ABCDEF_u64;
3188 println!("K =\t{:#016X}", key);
3189 let mut a_des = DES::new_with_key_u64(key);
3190
3191 let message = "I am OK.";
3192 println!("M =\t{}", message);
3193 let iv = 0x_FEDCBA0987654321_u64;
3194 println!("IV = {}", iv);
3195 let mut cipher = Vec::<u8>::new();
3196 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3197 print!("C =\t");
3198 for c in cipher.clone()
3199 { print!("{:02X} ", c); }
3200 println!();
3201 let mut txt = String::new();
3202 for c in cipher.clone()
3203 { write!(txt, "{:02X} ", c); }
3204 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
3205
3206 let mut recovered = vec![0; 16];
3207 let len = a_des.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
3208 print!("Ba =\t");
3209 for b in recovered.clone()
3210 { print!("{:02X} ", b); }
3211 println!();
3212 let mut txt = String::new();
3213 for c in recovered.clone()
3214 { write!(txt, "{:02X} ", c); }
3215 assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E 00 00 00 00 00 00 00 00 ");
3216
3217 let mut converted = String::new();
3218 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3219 converted.truncate(len as usize);
3220
3221 println!("Bb =\t{}", converted);
3222 assert_eq!(converted, "I am OK.");
3223 assert_eq!(converted, message);
3224 println!();
3225
3226 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
3227 let key = 0x_1234567890ABCDEF_u64;
3228 println!("K =\t{:#016X}", key);
3229 let mut a_des = DES::new_with_key_u64(key);
3230
3231 let message = "PARK Youngho";
3232 println!("M =\t{}", message);
3233 let iv = 0x_FEDCBA0987654321_u64;
3234 println!("IV = {}", iv);
3235 let mut cipher = Vec::<u8>::new();
3236 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3237 print!("C =\t");
3238 for c in cipher.clone()
3239 { print!("{:02X} ", c); }
3240 println!();
3241 let mut txt = String::new();
3242 for c in cipher.clone()
3243 { write!(txt, "{:02X} ", c); }
3244 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
3245
3246 let mut recovered = vec![0; 16];
3247 let len = a_des.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
3248 print!("Ba =\t");
3249 for b in recovered.clone()
3250 { print!("{:02X} ", b); }
3251 println!();
3252 let mut txt = String::new();
3253 for c in recovered.clone()
3254 { write!(txt, "{:02X} ", c); }
3255 assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F 00 00 00 00 ");
3256
3257 let mut converted = String::new();
3258 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3259 converted.truncate(len as usize);
3260
3261 println!("Bb =\t{}", converted);
3262 assert_eq!(converted, "PARK Youngho");
3263 assert_eq!(converted, message);
3264 println!();
3265
3266 // Normal case for the message of 16 bytes
3267 let key = 0x_1234567890ABCDEF_u64;
3268 println!("K =\t{:#016X}", key);
3269 let mut a_des = DES::new_with_key_u64(key);
3270
3271 let message = "고맙습니다.";
3272 println!("M =\t{}", message);
3273 let iv = 0x_FEDCBA0987654321_u64;
3274 println!("IV = {}", iv);
3275 let mut cipher = Vec::<u8>::new();
3276 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3277 print!("C =\t");
3278 for c in cipher.clone()
3279 { print!("{:02X} ", c); }
3280 println!();
3281 let mut txt = String::new();
3282 for c in cipher.clone()
3283 { write!(txt, "{:02X} ", c); }
3284 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
3285
3286 let mut recovered = vec![0; 24];
3287 let len = a_des.decrypt(iv, cipher.as_ptr(), cipher.len() as u64, recovered.as_mut_ptr());
3288 print!("Ba =\t");
3289 for b in recovered.clone()
3290 { print!("{:02X} ", b); }
3291 println!();
3292 let mut txt = String::new();
3293 for c in recovered.clone()
3294 { write!(txt, "{:02X} ", c); }
3295 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 ");
3296
3297 let mut converted = String::new();
3298 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3299 converted.truncate(len as usize);
3300
3301 println!("Bb =\t{}", converted);
3302 assert_eq!(converted, "고맙습니다.");
3303 assert_eq!(converted, message);
3304 println!("-------------------------------");
3305}Sourcefn decrypt_into_vec<U>(
&mut self,
iv: T,
cipher: *const u8,
length_in_bytes: u64,
message: &mut Vec<U>,
) -> u64
fn decrypt_into_vec<U>( &mut self, iv: T, cipher: *const u8, length_in_bytes: u64, message: &mut Vec<U>, ) -> u64
Decrypts the data without any padding in OFB (Output FeedBack) mode,
and stores the decrypted data in Vec<U>.
§Arguments
ivis an initialization vector for OFB mode.cipheris an immutable pointer tou8which is*const u8, and is the place where the ciphertext to be decrypted is stored.length_in_bytesis ofu64-type, and is the length of the ciphertextcipherin bytes.messageis a mutable reference toVec<U>object, and is the place where the decrypted data will be stored.
§Output
- This method returns the size of plaintext in bytes.
- If this method failed in decryption, it always returns
zero. - Even if this method succeeded in decryption, it returns
zerowhen the original plaintext is zero-length empty data. Then, you will have to check whether or not it failed by using the methodis_successful()oris_failed(). - If
length_in_bytesis greater than0(which means that the original plaintext is surely not empty data) and it returnszero, you can interpret it that this method surely failed in decryption.
§Features
- You are not encouraged to use this method in pure Rust programming. Instead, use other safer methods such as decrypt_*_into_vec().
- This method is useful to use in hybrid programming with C/C++.
- 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 Rijndael or AES, and its variants
§Example 1 for AES-128
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ AES_128, CBC_ISO };
let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
println!("K =\t{:#016X}", key);
let mut a_aes = AES_128::new_with_key_u128(key);
let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = [0_u8; 55];
a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
print!("C =\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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 07 AF 4E C8 EF 5D 0A 74 97 3F 90 9E 05 9E 9E 32 FB 55 54 45 7C ED A2 2B F8 07 66 C0 7B CB 98 F3 BF 93 15 BA 26 1C 47 ");
let mut recovered = Vec::<u8>::new();
a_aes.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
print!("Ba =\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 =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");
assert_eq!(converted, message);§For more examples,
click here
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, OFB };
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 iv = 0x_FEDCBA0987654321_u64;
println!("IV = {}", iv);
let mut cipher = Vec::<u8>::new();
a_des.encrypt_str_into_vec(iv, &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, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
let mut recovered = Vec::<u8>::new();
a_des.decrypt_into_vec(iv, 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
§For BigCryptor128
§Example 1 for TAES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
+ AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
+ AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
println!("IV = {:#034X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
taes.encrypt_str_into_vec(iv, &message, &mut cipher);
print!("C =\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, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
let mut recovered = Vec::<u8>::new();
taes.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
print!("Ba =\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 =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");
assert_eq!(converted, message);§For more examples,
click here
§For BigCryptor64
§Example 1 for TDES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
+ DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
+ DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
let iv = 0x_FEDCBA0987654321_u64;
println!("IV = {:#018X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
print!("C =\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, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
let mut recovered = Vec::<u8>::new();
tdes.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
print!("Ba =\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 =\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?
523fn bigcryptor64_decrypt_ofb_into_vec()
524{
525 println!("bigcryptor64_decrypt_ofb_into_vec()");
526 use std::io::Write;
527 use std::fmt::Write as _;
528 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
529
530 // TDES case
531 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
532 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
533 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
534 let iv = 0x_FEDCBA0987654321_u64;
535 println!("IV = {:#018X}", iv);
536 let message = "In the beginning God created the heavens and the earth.";
537 println!("M =\t{}", message);
538 let mut cipher = Vec::<u8>::new();
539 tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
540 print!("C =\t");
541 for c in cipher.clone()
542 { print!("{:02X} ", c); }
543 println!();
544 let mut txt = String::new();
545 for c in cipher.clone()
546 { write!(txt, "{:02X} ", c); }
547 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
548
549 let mut recovered = Vec::<u8>::new();
550 tdes.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
551 print!("Ba =\t");
552 for b in recovered.clone()
553 { print!("{:02X} ", b); }
554 println!();
555 let mut txt = String::new();
556 for c in recovered.clone()
557 { write!(txt, "{:02X} ", c); }
558 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 ");
559
560 let mut converted = String::new();
561 unsafe { converted.as_mut_vec() }.append(&mut recovered);
562
563 println!("Bb =\t{}", converted);
564 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
565 assert_eq!(converted, message);
566 println!("-------------------------------");
567}More examples
523fn bigcryptor128_decrypt_ofb_into_vec()
524{
525 println!("bigcryptor128_decrypt_ofb_into_vec()");
526 use std::io::Write;
527 use std::fmt::Write as _;
528 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
529
530 // TAES_128 case
531 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
532 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
533 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
534 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
535 println!("IV = {:#034X}", iv);
536 let message = "In the beginning God created the heavens and the earth.";
537 println!("M =\t{}", message);
538 let mut cipher = Vec::<u8>::new();
539 taes.encrypt_str_into_vec(iv, &message, &mut cipher);
540 print!("C =\t");
541 for c in cipher.clone()
542 { print!("{:02X} ", c); }
543 println!();
544 let mut txt = String::new();
545 for c in cipher.clone()
546 { write!(txt, "{:02X} ", c); }
547 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
548
549 let mut recovered = Vec::<u8>::new();
550 taes.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
551 print!("Ba =\t");
552 for b in recovered.clone()
553 { print!("{:02X} ", b); }
554 println!();
555 let mut txt = String::new();
556 for c in recovered.clone()
557 { write!(txt, "{:02X} ", c); }
558 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 ");
559
560 let mut converted = String::new();
561 unsafe { converted.as_mut_vec() }.append(&mut recovered);
562
563 println!("Bb =\t{}", converted);
564 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
565 assert_eq!(converted, message);
566 println!("-------------------------------");
567}2331fn aes_decrypt_ofb_into_vec()
2332{
2333 println!("aes_decrypt_ofb_into_vec()");
2334 use std::io::Write;
2335 use std::fmt::Write as _;
2336 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
2337
2338 // Normal case for AES-128
2339 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
2340 println!("K =\t{:#016X}", key);
2341 let mut a_aes = AES_128::new_with_key_u128(key);
2342 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2343 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2344
2345 let message = "In the beginning God created the heavens and the earth.";
2346 println!("M =\t{}", message);
2347 let mut cipher = [0_u8; 55];
2348 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2349 print!("C =\t");
2350 for c in cipher.clone()
2351 { print!("{:02X} ", c); }
2352 println!();
2353 let mut txt = String::new();
2354 for c in cipher.clone()
2355 { write!(txt, "{:02X} ", c); }
2356 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
2357 println!();
2358
2359 let mut recovered = Vec::<u8>::new();
2360 a_aes.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2361 print!("Ba =\t");
2362 for b in recovered.clone()
2363 { print!("{:02X} ", b); }
2364 println!();
2365 let mut txt = String::new();
2366 for c in recovered.clone()
2367 { write!(txt, "{:02X} ", c); }
2368 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 ");
2369
2370 let mut converted = String::new();
2371 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2372
2373 println!("Bb =\t{}", converted);
2374 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2375 assert_eq!(converted, message);
2376 println!();
2377
2378 // Normal case for AES-192
2379 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2380 print!("K =\t");
2381 for i in 0..24
2382 { print!("{:02X}", key[i]); }
2383 println!();
2384 let mut a_aes = AES_192::new_with_key(&key);
2385 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2386 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2387
2388 let message = "In the beginning God created the heavens and the earth.";
2389 println!("M =\t{}", message);
2390 let mut cipher = [0_u8; 55];
2391 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2392 print!("C =\t");
2393 for c in cipher.clone()
2394 { print!("{:02X} ", c); }
2395 println!();
2396 let mut txt = String::new();
2397 for c in cipher.clone()
2398 { write!(txt, "{:02X} ", c); }
2399 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
2400 println!();
2401
2402 let mut recovered = Vec::<u8>::new();
2403 a_aes.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2404 print!("Ba =\t");
2405 for b in recovered.clone()
2406 { print!("{:02X} ", b); }
2407 println!();
2408 let mut txt = String::new();
2409 for c in recovered.clone()
2410 { write!(txt, "{:02X} ", c); }
2411 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 ");
2412
2413 let mut converted = String::new();
2414 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2415
2416 println!("Bb =\t{}", converted);
2417 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2418 assert_eq!(converted, message);
2419 println!();
2420
2421 // Normal case for AES-256
2422 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2423 print!("K =\t");
2424 for i in 0..32
2425 { print!("{:02X}", key[i]); }
2426 println!();
2427 let mut a_aes = AES_256::new_with_key(&key);
2428 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2429 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2430
2431 let message = "In the beginning God created the heavens and the earth.";
2432 println!("M =\t{}", message);
2433 let mut cipher = [0_u8; 55];
2434 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2435 print!("C =\t");
2436 for c in cipher.clone()
2437 { print!("{:02X} ", c); }
2438 println!();
2439 let mut txt = String::new();
2440 for c in cipher.clone()
2441 { write!(txt, "{:02X} ", c); }
2442 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
2443 println!();
2444
2445 let mut recovered = Vec::<u8>::new();
2446 a_aes.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2447 print!("Ba =\t");
2448 for b in recovered.clone()
2449 { print!("{:02X} ", b); }
2450 println!();
2451 let mut txt = String::new();
2452 for c in recovered.clone()
2453 { write!(txt, "{:02X} ", c); }
2454 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 ");
2455
2456 let mut converted = String::new();
2457 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2458
2459 println!("Bb =\t{}", converted);
2460 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2461 assert_eq!(converted, message);
2462 println!();
2463
2464 // Normal case for Rijndael-256-256
2465 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2466 print!("K =\t");
2467 for i in 0..32
2468 { print!("{:02X}", key[i]); }
2469 println!();
2470 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
2471 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2472 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
2473
2474 let message = "In the beginning God created the heavens and the earth.";
2475 println!("M =\t{}", message);
2476 let mut cipher = [0_u8; 55];
2477 a_rijndael.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2478 print!("C =\t");
2479 for c in cipher.clone()
2480 { print!("{:02X} ", c); }
2481 println!();
2482 let mut txt = String::new();
2483 for c in cipher.clone()
2484 { write!(txt, "{:02X} ", c); }
2485 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
2486 println!();
2487
2488 let mut recovered = Vec::<u8>::new();
2489 a_rijndael.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2490 print!("Ba =\t");
2491 for b in recovered.clone()
2492 { print!("{:02X} ", b); }
2493 println!();
2494 let mut txt = String::new();
2495 for c in recovered.clone()
2496 { write!(txt, "{:02X} ", c); }
2497 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 ");
2498
2499 let mut converted = String::new();
2500 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2501
2502 println!("Bb =\t{}", converted);
2503 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2504 assert_eq!(converted, message);
2505 println!();
2506
2507 // Normal case for Rijndael-512-512 for post-quantum
2508 use cryptocol::number::SharedArrays;
2509 use cryptocol::hash::SHA3_512;
2510 let mut sha3 = SHA3_512::new();
2511 sha3.absorb_str("Post-quantum");
2512 let key: [u8; 64] = sha3.get_hash_value_in_array();
2513 print!("K =\t");
2514 for i in 0..64
2515 { print!("{:02X}", key[i]); }
2516 println!();
2517 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
2518 sha3.absorb_str("Initialize");
2519 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
2520 iv.src = sha3.get_hash_value_in_array();
2521 let iv = unsafe { iv.des };
2522 print!("IV =\t");
2523 for i in 0..16
2524 { print!("{:08X}", iv[i].to_be()); }
2525 println!();
2526 let message = "In the beginning God created the heavens and the earth.";
2527 println!("M =\t{}", message);
2528 let mut cipher = [0_u8; 55];
2529 a_rijndael.encrypt(iv.clone(), message.as_ptr(), message.len() as u64, cipher.as_mut_ptr());
2530 print!("C =\t");
2531 for c in cipher.clone()
2532 { print!("{:02X} ", c); }
2533 println!();
2534 let mut txt = String::new();
2535 for c in cipher.clone()
2536 { write!(txt, "{:02X} ", c); }
2537 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
2538
2539 let mut recovered = Vec::<u8>::new();
2540 a_rijndael.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
2541 print!("Ba =\t");
2542 for b in recovered.clone()
2543 { print!("{:02X} ", b); }
2544 println!();
2545 let mut txt = String::new();
2546 for c in recovered.clone()
2547 { write!(txt, "{:02X} ", c); }
2548 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 ");
2549
2550 let mut converted = String::new();
2551 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2552
2553 println!("Bb =\t{}", converted);
2554 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2555 assert_eq!(converted, message);
2556 println!("-------------------------------");
2557}3307fn des_decrypt_ofb_into_vec()
3308{
3309 println!("des_decrypt_ofb_into_vec()");
3310 use std::io::Write;
3311 use std::fmt::Write as _;
3312 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
3313
3314 // Normal case
3315 let key = 0x_1234567890ABCDEF_u64;
3316 println!("K =\t{:#016X}", key);
3317 let mut a_des = DES::new_with_key_u64(key);
3318
3319 let message = "In the beginning God created the heavens and the earth.";
3320 println!("M =\t{}", message);
3321 let iv = 0x_FEDCBA0987654321_u64;
3322 println!("IV = {}", iv);
3323 let mut cipher = Vec::<u8>::new();
3324 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3325 print!("C (16 rounds) =\t");
3326 for c in cipher.clone()
3327 { print!("{:02X} ", c); }
3328 println!();
3329 let mut txt = String::new();
3330 for c in cipher.clone()
3331 { write!(txt, "{:02X} ", c); }
3332 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
3333
3334 let mut recovered = Vec::<u8>::new();
3335 a_des.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3336 print!("Ba (16 rounds) =\t");
3337 for b in recovered.clone()
3338 { print!("{:02X} ", b); }
3339 println!();
3340 let mut txt = String::new();
3341 for c in recovered.clone()
3342 { write!(txt, "{:02X} ", c); }
3343 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 ");
3344
3345 let mut converted = String::new();
3346 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3347
3348 println!("Bb (16 rounds) =\t{}", converted);
3349 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3350 assert_eq!(converted, message);
3351 println!();
3352
3353 // Expanded case for 128 rounds
3354 let key = 0x_1234567890ABCDEF_u64;
3355 println!("K =\t{:#016X}", key);
3356 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
3357
3358 let message = "In the beginning God created the heavens and the earth.";
3359 println!("M =\t{}", message);
3360 let iv = 0x_FEDCBA0987654321_u64;
3361 println!("IV = {}", iv);
3362 let mut cipher = Vec::<u8>::new();
3363 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3364 print!("C (128 rounds) =\t");
3365 for c in cipher.clone()
3366 { print!("{:02X} ", c); }
3367 println!();
3368 let mut txt = String::new();
3369 for c in cipher.clone()
3370 { write!(txt, "{:02X} ", c); }
3371 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
3372
3373 let mut recovered = Vec::<u8>::new();
3374 a_des.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3375 print!("Ba (128 rounds) =\t");
3376 for b in recovered.clone()
3377 { print!("{:02X} ", b); }
3378 println!();
3379 let mut txt = String::new();
3380 for c in recovered.clone()
3381 { write!(txt, "{:02X} ", c); }
3382 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 ");
3383
3384 let mut converted = String::new();
3385 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3386
3387 println!("Bb (128 rounds) =\t{}", converted);
3388 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3389 assert_eq!(converted, message);
3390 println!();
3391
3392 // Expanded case for 0 rounds which means that key is meaningless
3393 let key1 = 0x_1234567890ABCDEF_u64;
3394 let key2 = 0_u64;
3395 println!("K =\t{:#016X}", key);
3396 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
3397 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
3398
3399 let message = "In the beginning God created the heavens and the earth.";
3400 println!("M =\t{}", message);
3401 let iv = 0x_FEDCBA0987654321_u64;
3402 println!("IV = {}", iv);
3403 let mut cipher1 = Vec::<u8>::new();
3404 let mut cipher2 = Vec::<u8>::new();
3405 c_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
3406 d_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
3407 print!("C (0 rounds) =\t");
3408 for c in cipher1.clone()
3409 { print!("{:02X} ", c); }
3410 println!();
3411 let mut txt = String::new();
3412 for c in cipher1.clone()
3413 { write!(txt, "{:02X} ", c); }
3414 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
3415 print!("D (0 rounds) =\t");
3416 for c in cipher2.clone()
3417 { print!("{:02X} ", c); }
3418 println!();
3419 let mut txt = String::new();
3420 for c in cipher2.clone()
3421 { write!(txt, "{:02X} ", c); }
3422 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
3423
3424 let mut recovered1 = Vec::<u8>::new();
3425 let mut recovered2 = Vec::<u8>::new();
3426 c_des.decrypt_into_vec(iv, cipher1.as_ptr(), cipher1.len() as u64, &mut recovered1);
3427 d_des.decrypt_into_vec(iv, cipher2.as_ptr(), cipher2.len() as u64, &mut recovered2);
3428 print!("B1a (0 rounds) =\t");
3429 for b in recovered1.clone()
3430 { print!("{:02X} ", b); }
3431 println!();
3432 let mut txt = String::new();
3433 for c in recovered1.clone()
3434 { write!(txt, "{:02X} ", c); }
3435 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 ");
3436 print!("B2a (0 rounds) =\t");
3437 for b in recovered2.clone()
3438 { print!("{:02X} ", b); }
3439 println!();
3440 let mut txt = String::new();
3441 for c in recovered2.clone()
3442 { write!(txt, "{:02X} ", c); }
3443 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 ");
3444
3445 let mut converted1 = String::new();
3446 let mut converted2 = String::new();
3447 unsafe { converted1.as_mut_vec() }.append(&mut recovered1);
3448 unsafe { converted2.as_mut_vec() }.append(&mut recovered2);
3449
3450 println!("B1b (0 rounds) =\t{}", converted1);
3451 assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
3452 assert_eq!(converted1, message);
3453 println!("B2b (0 rounds) =\t{}", converted2);
3454 assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
3455 assert_eq!(converted2, message);
3456 assert_eq!(converted1, converted1);
3457 println!();
3458
3459 // Normal case for the message of 0 bytes
3460 let key = 0x_1234567890ABCDEF_u64;
3461 println!("K =\t{:#016X}", key);
3462 let mut a_des = DES::new_with_key_u64(key);
3463
3464 let message = "";
3465 println!("M =\t{}", message);
3466 let iv = 0x_FEDCBA0987654321_u64;
3467 println!("IV = {}", iv);
3468 let mut cipher = Vec::<u8>::new();
3469 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3470 print!("C =\t");
3471 for c in cipher.clone()
3472 { print!("{:02X} ", c); }
3473 println!();
3474 let mut txt = String::new();
3475 for c in cipher.clone()
3476 { write!(txt, "{:02X} ", c); }
3477 assert_eq!(txt, "");
3478
3479 let mut recovered = Vec::<u8>::new();
3480 a_des.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3481 print!("Ba =\t");
3482 for b in recovered.clone()
3483 { print!("{:02X} ", b); }
3484 println!();
3485 let mut txt = String::new();
3486 for c in recovered.clone()
3487 { write!(txt, "{:02X} ", c); }
3488 assert_eq!(txt, "");
3489
3490 let mut converted = String::new();
3491 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3492
3493 println!("Bb =\t{}", converted);
3494 assert_eq!(converted, "");
3495 assert_eq!(converted, message);
3496 println!();
3497
3498 // Normal case for the message shorter than 8 bytes
3499 let key = 0x_1234567890ABCDEF_u64;
3500 println!("K =\t{:#016X}", key);
3501 let mut a_des = DES::new_with_key_u64(key);
3502
3503 let message = "7 bytes";
3504 println!("M =\t{}", message);
3505 let iv = 0x_FEDCBA0987654321_u64;
3506 println!("IV = {}", iv);
3507 let mut cipher = Vec::<u8>::new();
3508 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3509 print!("C =\t");
3510 for c in cipher.clone()
3511 { print!("{:02X} ", c); }
3512 println!();
3513 let mut txt = String::new();
3514 for c in cipher.clone()
3515 { write!(txt, "{:02X} ", c); }
3516 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
3517
3518 let mut recovered = Vec::<u8>::new();
3519 a_des.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3520 print!("Ba =\t");
3521 for b in recovered.clone()
3522 { print!("{:02X} ", b); }
3523 println!();
3524 let mut txt = String::new();
3525 for c in recovered.clone()
3526 { write!(txt, "{:02X} ", c); }
3527 assert_eq!(txt, "37 20 62 79 74 65 73 ");
3528
3529 let mut converted = String::new();
3530 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3531
3532 println!("Bb =\t{}", converted);
3533 assert_eq!(converted, "7 bytes");
3534 assert_eq!(converted, message);
3535 println!();
3536
3537 // Normal case for the message of 8 bytes
3538 let key = 0x_1234567890ABCDEF_u64;
3539 println!("K =\t{:#016X}", key);
3540 let mut a_des = DES::new_with_key_u64(key);
3541
3542 let message = "I am OK.";
3543 println!("M =\t{}", message);
3544 let iv = 0x_FEDCBA0987654321_u64;
3545 println!("IV = {}", iv);
3546 let mut cipher = Vec::<u8>::new();
3547 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3548 print!("C =\t");
3549 for c in cipher.clone()
3550 { print!("{:02X} ", c); }
3551 println!();
3552 let mut txt = String::new();
3553 for c in cipher.clone()
3554 { write!(txt, "{:02X} ", c); }
3555 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
3556
3557 let mut recovered = Vec::<u8>::new();
3558 a_des.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3559 print!("Ba =\t");
3560 for b in recovered.clone()
3561 { print!("{:02X} ", b); }
3562 println!();
3563 let mut txt = String::new();
3564 for c in recovered.clone()
3565 { write!(txt, "{:02X} ", c); }
3566 assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E ");
3567
3568 let mut converted = String::new();
3569 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3570
3571 println!("Bb =\t{}", converted);
3572 assert_eq!(converted, "I am OK.");
3573 assert_eq!(converted, message);
3574 println!();
3575
3576 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
3577 let key = 0x_1234567890ABCDEF_u64;
3578 println!("K =\t{:#016X}", key);
3579 let mut a_des = DES::new_with_key_u64(key);
3580
3581 let message = "PARK Youngho";
3582 println!("M =\t{}", message);
3583 let iv = 0x_FEDCBA0987654321_u64;
3584 println!("IV = {}", iv);
3585 let mut cipher = Vec::<u8>::new();
3586 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3587 print!("C =\t");
3588 for c in cipher.clone()
3589 { print!("{:02X} ", c); }
3590 println!();
3591 let mut txt = String::new();
3592 for c in cipher.clone()
3593 { write!(txt, "{:02X} ", c); }
3594 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
3595
3596 let mut recovered = Vec::<u8>::new();
3597 a_des.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3598 print!("Ba =\t");
3599 for b in recovered.clone()
3600 { print!("{:02X} ", b); }
3601 println!();
3602 let mut txt = String::new();
3603 for c in recovered.clone()
3604 { write!(txt, "{:02X} ", c); }
3605 assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F ");
3606
3607 let mut converted = String::new();
3608 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3609
3610 println!("Bb =\t{}", converted);
3611 assert_eq!(converted, "PARK Youngho");
3612 assert_eq!(converted, message);
3613 println!();
3614
3615 // Normal case for the message of 16 bytes
3616 let key = 0x_1234567890ABCDEF_u64;
3617 println!("K =\t{:#016X}", key);
3618 let mut a_des = DES::new_with_key_u64(key);
3619
3620 let message = "고맙습니다.";
3621 println!("M =\t{}", message);
3622 let iv = 0x_FEDCBA0987654321_u64;
3623 println!("IV = {}", iv);
3624 let mut cipher = Vec::<u8>::new();
3625 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
3626 print!("C =\t");
3627 for c in cipher.clone()
3628 { print!("{:02X} ", c); }
3629 println!();
3630 let mut txt = String::new();
3631 for c in cipher.clone()
3632 { write!(txt, "{:02X} ", c); }
3633 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
3634
3635 let mut recovered = Vec::<u8>::new();
3636 a_des.decrypt_into_vec(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
3637 print!("Ba =\t");
3638 for b in recovered.clone()
3639 { print!("{:02X} ", b); }
3640 println!();
3641 let mut txt = String::new();
3642 for c in recovered.clone()
3643 { write!(txt, "{:02X} ", c); }
3644 assert_eq!(txt, "EA B3 A0 EB A7 99 EC 8A B5 EB 8B 88 EB 8B A4 2E ");
3645
3646 let mut converted = String::new();
3647 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3648
3649 println!("Bb =\t{}", converted);
3650 assert_eq!(converted, "고맙습니다.");
3651 assert_eq!(converted, message);
3652 println!("-------------------------------");
3653}Sourcefn decrypt_into_string(
&mut self,
iv: T,
cipher: *const u8,
length_in_bytes: u64,
message: &mut String,
) -> u64
fn decrypt_into_string( &mut self, iv: T, cipher: *const u8, length_in_bytes: u64, message: &mut String, ) -> u64
Decrypts the data without any padding in OFB (Output FeedBack) mode,
and stores the decrypted data in a String.
§Arguments
ivis an initialization vector for OFB mode.cipheris an immutable pointer tou8which is*const u8, and is the place where the ciphertext to be decrypted is stored.length_in_bytesis ofu64-type, and is the length of the ciphertextcipherin bytes.messageis a mutable reference to aStringobject, and is the place where the decrypted data will be stored.
§Output
- This method returns the size of plaintext in bytes.
- If this method failed in decryption, it always returns
zero. - Even if this method succeeded in decryption, it returns
zerowhen the original plaintext is zero-length empty data. Then, you will have to check whether or not it failed by using the methodis_successful()oris_failed(). - If
length_in_bytesis greater than0(which means that the original plaintext is surely not empty data) and it returnszero, you can interpret it that this method surely failed in decryption.
§Features
- You are not encouraged to use this method in pure Rust programming. Instead, use other safer methods such as decrypt_*_into_string().
- This method is useful to use in hybrid programming with C/C++.
- 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 Rijndael or AES, and its variants
§Example 1 for AES-128
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ AES_128, CBC_ISO };
let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
println!("K =\t{:#016X}", key);
let mut a_aes = AES_128::new_with_key_u128(key);
let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = [0_u8; 55];
a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
print!("C =\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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 07 AF 4E C8 EF 5D 0A 74 97 3F 90 9E 05 9E 9E 32 FB 55 54 45 7C ED A2 2B F8 07 66 C0 7B CB 98 F3 BF 93 15 BA 26 1C 47 ");
println!();
let mut converted= String::new();
a_aes.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut converted);
println!("B =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");
assert_eq!(converted, message);§For more examples,
click here
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, OFB };
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 iv = 0x_FEDCBA0987654321_u64;
println!("IV = {}", iv);
let mut cipher = Vec::<u8>::new();
a_des.encrypt_str_into_vec(iv, &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, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
let mut recovered = String::new();
a_des.decrypt_into_string(iv, 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
§For BigCryptor128
§Example 1 for TAES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
+ AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
+ AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
println!("IV = {:#034X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
taes.encrypt_str_into_vec(iv, &message, &mut cipher);
print!("C =\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, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
let mut recovered = String::new();
taes.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
println!("B =\t{}", recovered);
assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
assert_eq!(recovered, message);§For more examples,
click here
§For BigCryptor64
§Example 1 for TDES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
+ DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
+ DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
let iv = 0x_FEDCBA0987654321_u64;
println!("IV = {:#018X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
print!("C =\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, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
let mut recovered = String::new();
tdes.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
println!("B =\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?
615fn bigcryptor64_decrypt_ofb_into_string()
616{
617 println!("bigcryptor64_decrypt_ofb_into_string()");
618 use std::io::Write;
619 use std::fmt::Write as _;
620 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
621
622 // TDES case
623 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
624 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
625 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
626 let iv = 0x_FEDCBA0987654321_u64;
627 println!("IV = {:#018X}", iv);
628 let message = "In the beginning God created the heavens and the earth.";
629 println!("M =\t{}", message);
630 let mut cipher = Vec::<u8>::new();
631 tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
632 print!("C =\t");
633 for c in cipher.clone()
634 { print!("{:02X} ", c); }
635 println!();
636 let mut txt = String::new();
637 for c in cipher.clone()
638 { write!(txt, "{:02X} ", c); }
639 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
640
641 let mut recovered = String::new();
642 tdes.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
643 println!("B =\t{}", recovered);
644 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
645 assert_eq!(recovered, message);
646 println!("-------------------------------");
647}More examples
615fn bigcryptor128_decrypt_ofb_into_string()
616{
617 println!("bigcryptor128_decrypt_ofb_into_string()");
618 use std::io::Write;
619 use std::fmt::Write as _;
620 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
621
622 // TAES_128 case
623 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
624 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
625 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
626 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
627 println!("IV = {:#034X}", iv);
628 let message = "In the beginning God created the heavens and the earth.";
629 println!("M =\t{}", message);
630 let mut cipher = Vec::<u8>::new();
631 taes.encrypt_str_into_vec(iv, &message, &mut cipher);
632 print!("C =\t");
633 for c in cipher.clone()
634 { print!("{:02X} ", c); }
635 println!();
636 let mut txt = String::new();
637 for c in cipher.clone()
638 { write!(txt, "{:02X} ", c); }
639 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
640
641 let mut recovered = String::new();
642 taes.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
643 println!("B =\t{}", recovered);
644 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
645 assert_eq!(recovered, message);
646 println!("-------------------------------");
647}2789fn aes_decrypt_ofb_into_string()
2790{
2791 println!("aes_decrypt_ofb_into_string()");
2792 use std::io::Write;
2793 use std::fmt::Write as _;
2794 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
2795
2796 // Normal case for AES-128
2797 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
2798 println!("K =\t{:#016X}", key);
2799 let mut a_aes = AES_128::new_with_key_u128(key);
2800 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2801 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2802
2803 let message = "In the beginning God created the heavens and the earth.";
2804 println!("M =\t{}", message);
2805 let mut cipher = [0_u8; 55];
2806 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2807 print!("C =\t");
2808 for c in cipher.clone()
2809 { print!("{:02X} ", c); }
2810 println!();
2811 let mut txt = String::new();
2812 for c in cipher.clone()
2813 { write!(txt, "{:02X} ", c); }
2814 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
2815
2816 let mut converted= String::new();
2817 a_aes.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut converted);
2818 println!("B =\t{}", converted);
2819 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2820 assert_eq!(converted, message);
2821 println!();
2822
2823 // Normal case for AES-192
2824 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2825 print!("K =\t");
2826 for i in 0..24
2827 { print!("{:02X}", key[i]); }
2828 println!();
2829 let mut a_aes = AES_192::new_with_key(&key);
2830 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2831 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2832
2833 let message = "In the beginning God created the heavens and the earth.";
2834 println!("M =\t{}", message);
2835 let mut cipher = [0_u8; 55];
2836 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2837 print!("C =\t");
2838 for c in cipher.clone()
2839 { print!("{:02X} ", c); }
2840 println!();
2841 let mut txt = String::new();
2842 for c in cipher.clone()
2843 { write!(txt, "{:02X} ", c); }
2844 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
2845
2846 let mut converted= String::new();
2847 a_aes.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut converted);
2848 println!("B =\t{}", converted);
2849 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2850 assert_eq!(converted, message);
2851 println!();
2852
2853 // Normal case for AES-256
2854 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2855 print!("K =\t");
2856 for i in 0..32
2857 { print!("{:02X}", key[i]); }
2858 println!();
2859 let mut a_aes = AES_256::new_with_key(&key);
2860 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2861 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2862
2863 let message = "In the beginning God created the heavens and the earth.";
2864 println!("M =\t{}", message);
2865 let mut cipher = [0_u8; 55];
2866 a_aes.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2867 print!("C =\t");
2868 for c in cipher.clone()
2869 { print!("{:02X} ", c); }
2870 println!();
2871 let mut txt = String::new();
2872 for c in cipher.clone()
2873 { write!(txt, "{:02X} ", c); }
2874 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
2875
2876 let mut converted= String::new();
2877 a_aes.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut converted);
2878 println!("B =\t{}", converted);
2879 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2880 assert_eq!(converted, message);
2881 println!();
2882
2883 // Normal case for Rijndael-256-256
2884 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
2885 print!("K =\t");
2886 for i in 0..32
2887 { print!("{:02X}", key[i]); }
2888 println!();
2889 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
2890 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2891 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
2892
2893 let message = "In the beginning God created the heavens and the earth.";
2894 println!("M =\t{}", message);
2895 let mut cipher = [0_u8; 55];
2896 a_rijndael.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2897 print!("C =\t");
2898 for c in cipher.clone()
2899 { print!("{:02X} ", c); }
2900 println!();
2901 let mut txt = String::new();
2902 for c in cipher.clone()
2903 { write!(txt, "{:02X} ", c); }
2904 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
2905
2906 let mut converted= String::new();
2907 a_rijndael.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut converted);
2908 println!("B =\t{}", converted);
2909 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2910 assert_eq!(converted, message);
2911 println!();
2912
2913 // Normal case for Rijndael-512-512 for post-quantum
2914 use cryptocol::number::SharedArrays;
2915 use cryptocol::hash::SHA3_512;
2916 let mut sha3 = SHA3_512::new();
2917 sha3.absorb_str("Post-quantum");
2918 let key: [u8; 64] = sha3.get_hash_value_in_array();
2919 print!("K =\t");
2920 for i in 0..64
2921 { print!("{:02X}", key[i]); }
2922 println!();
2923 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
2924 sha3.absorb_str("Initialize");
2925 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
2926 iv.src = sha3.get_hash_value_in_array();
2927 let iv = unsafe { iv.des };
2928 print!("IV =\t");
2929 for i in 0..16
2930 { print!("{:08X}", iv[i].to_be()); }
2931 println!();
2932 let message = "In the beginning God created the heavens and the earth.";
2933 println!("M =\t{}", message);
2934 let mut cipher = [0_u8; 55];
2935 a_rijndael.encrypt_str(iv.clone(), &message, cipher.as_mut_ptr());
2936 print!("C =\t");
2937 for c in cipher.clone()
2938 { print!("{:02X} ", c); }
2939 println!();
2940 let mut txt = String::new();
2941 for c in cipher.clone()
2942 { write!(txt, "{:02X} ", c); }
2943 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
2944
2945 let mut converted= String::new();
2946 a_rijndael.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut converted);
2947 println!("B =\t{}", converted);
2948 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2949 assert_eq!(converted, message);
2950 println!("-------------------------------");
2951}4009fn des_decrypt_ofb_into_string()
4010{
4011 println!("des_decrypt_ofb_into_string()");
4012 use std::io::Write;
4013 use std::fmt::Write as _;
4014 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
4015
4016 // Normal case
4017 let key = 0x_1234567890ABCDEF_u64;
4018 println!("K =\t{:#016X}", key);
4019 let mut a_des = DES::new_with_key_u64(key);
4020
4021 let message = "In the beginning God created the heavens and the earth.";
4022 println!("M =\t{}", message);
4023 let iv = 0x_FEDCBA0987654321_u64;
4024 println!("IV = {}", iv);
4025 let mut cipher = Vec::<u8>::new();
4026 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4027 print!("C (16 rounds) =\t");
4028 for c in cipher.clone()
4029 { print!("{:02X} ", c); }
4030 println!();
4031 let mut txt = String::new();
4032 for c in cipher.clone()
4033 { write!(txt, "{:02X} ", c); }
4034 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
4035
4036 let mut recovered = String::new();
4037 a_des.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
4038 println!("B (16 rounds) =\t{}", recovered);
4039 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
4040 assert_eq!(recovered, message);
4041 println!();
4042
4043 // Expanded case for 128 rounds
4044 let key = 0x_1234567890ABCDEF_u64;
4045 println!("K =\t{:#016X}", key);
4046 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
4047
4048 let message = "In the beginning God created the heavens and the earth.";
4049 println!("M =\t{}", message);
4050 let iv = 0x_FEDCBA0987654321_u64;
4051 println!("IV = {}", iv);
4052 let mut cipher = Vec::<u8>::new();
4053 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4054 print!("C (128 rounds) =\t");
4055 for c in cipher.clone()
4056 { print!("{:02X} ", c); }
4057 println!();
4058 let mut txt = String::new();
4059 for c in cipher.clone()
4060 { write!(txt, "{:02X} ", c); }
4061 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
4062
4063 let mut recovered = String::new();
4064 a_des.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
4065 println!("B (128 rounds) =\t{}", recovered);
4066 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
4067 assert_eq!(recovered, message);
4068 println!();
4069
4070 // Expanded case for 0 rounds which means that key is meaningless
4071 let key1 = 0x_1234567890ABCDEF_u64;
4072 let key2 = 0_u64;
4073 println!("K =\t{:#016X}", key);
4074 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
4075 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
4076
4077 let message = "In the beginning God created the heavens and the earth.";
4078 println!("M =\t{}", message);
4079 let iv = 0x_FEDCBA0987654321_u64;
4080 println!("IV = {}", iv);
4081 let mut cipher1 = Vec::<u8>::new();
4082 let mut cipher2 = Vec::<u8>::new();
4083 c_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
4084 d_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
4085 print!("C (0 rounds) =\t");
4086 for c in cipher1.clone()
4087 { print!("{:02X} ", c); }
4088 println!();
4089 let mut txt = String::new();
4090 for c in cipher1.clone()
4091 { write!(txt, "{:02X} ", c); }
4092 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
4093 print!("D (0 rounds) =\t");
4094 for c in cipher2.clone()
4095 { print!("{:02X} ", c); }
4096 println!();
4097 let mut txt = String::new();
4098 for c in cipher2.clone()
4099 { write!(txt, "{:02X} ", c); }
4100 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
4101
4102 let mut recovered1 = String::new();
4103 let mut recovered2 = String::new();
4104 c_des.decrypt_into_string(iv, cipher1.as_ptr(), cipher1.len() as u64, &mut recovered1);
4105 d_des.decrypt_into_string(iv, cipher2.as_ptr(), cipher2.len() as u64, &mut recovered2);
4106 println!("B1 (0 rounds) =\t{}", recovered1);
4107 println!("B2 (0 rounds) =\t{}", recovered2);
4108 assert_eq!(recovered1, "In the beginning God created the heavens and the earth.");
4109 assert_eq!(recovered2, "In the beginning God created the heavens and the earth.");
4110 assert_eq!(recovered1, message);
4111 assert_eq!(recovered2, message);
4112 assert_eq!(recovered1, recovered2);
4113 println!();
4114
4115 // Normal case for the message of 0 bytes
4116 let key = 0x_1234567890ABCDEF_u64;
4117 println!("K =\t{:#016X}", key);
4118 let mut a_des = DES::new_with_key_u64(key);
4119
4120 let message = "";
4121 println!("M =\t{}", message);
4122 let iv = 0x_FEDCBA0987654321_u64;
4123 println!("IV = {}", iv);
4124 let mut cipher = Vec::<u8>::new();
4125 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4126 print!("C =\t");
4127 for c in cipher.clone()
4128 { print!("{:02X} ", c); }
4129 println!();
4130 let mut txt = String::new();
4131 for c in cipher.clone()
4132 { write!(txt, "{:02X} ", c); }
4133 assert_eq!(txt, "");
4134
4135 let mut recovered = String::new();
4136 a_des.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
4137 println!("B =\t{}", recovered);
4138 assert_eq!(recovered, "");
4139 assert_eq!(recovered, message);
4140 println!();
4141
4142 // Normal case for the message shorter than 8 bytes
4143 let key = 0x_1234567890ABCDEF_u64;
4144 println!("K =\t{:#016X}", key);
4145 let mut a_des = DES::new_with_key_u64(key);
4146
4147 let message = "7 bytes";
4148 println!("M =\t{}", message);
4149 let iv = 0x_FEDCBA0987654321_u64;
4150 println!("IV = {}", iv);
4151 let mut cipher = Vec::<u8>::new();
4152 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4153 print!("C =\t");
4154 for c in cipher.clone()
4155 { print!("{:02X} ", c); }
4156 println!();
4157 let mut txt = String::new();
4158 for c in cipher.clone()
4159 { write!(txt, "{:02X} ", c); }
4160 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
4161
4162 let mut recovered = String::new();
4163 a_des.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
4164 println!("B =\t{}", recovered);
4165 assert_eq!(recovered, "7 bytes");
4166 assert_eq!(recovered, message);
4167 println!();
4168
4169 // Normal case for the message of 8 bytes
4170 let key = 0x_1234567890ABCDEF_u64;
4171 println!("K =\t{:#016X}", key);
4172 let mut a_des = DES::new_with_key_u64(key);
4173
4174 let message = "I am OK.";
4175 println!("M =\t{}", message);
4176 let iv = 0x_FEDCBA0987654321_u64;
4177 println!("IV = {}", iv);
4178 let mut cipher = Vec::<u8>::new();
4179 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4180 print!("C =\t");
4181 for c in cipher.clone()
4182 { print!("{:02X} ", c); }
4183 println!();
4184 let mut txt = String::new();
4185 for c in cipher.clone()
4186 { write!(txt, "{:02X} ", c); }
4187 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
4188
4189 let mut recovered = String::new();
4190 a_des.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
4191 println!("B =\t{}", recovered);
4192 assert_eq!(recovered, "I am OK.");
4193 assert_eq!(recovered, message);
4194 println!();
4195
4196 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
4197 let key = 0x_1234567890ABCDEF_u64;
4198 println!("K =\t{:#016X}", key);
4199 let mut a_des = DES::new_with_key_u64(key);
4200
4201 let message = "PARK Youngho";
4202 println!("M =\t{}", message);
4203 let iv = 0x_FEDCBA0987654321_u64;
4204 println!("IV = {}", iv);
4205 let mut cipher = Vec::<u8>::new();
4206 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4207 print!("C =\t");
4208 for c in cipher.clone()
4209 { print!("{:02X} ", c); }
4210 println!();
4211 let mut txt = String::new();
4212 for c in cipher.clone()
4213 { write!(txt, "{:02X} ", c); }
4214 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
4215
4216 let mut recovered = String::new();
4217 a_des.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
4218 println!("B =\t{}", recovered);
4219 assert_eq!(recovered, "PARK Youngho");
4220 assert_eq!(recovered, message);
4221 println!();
4222
4223 // Normal case for the message of 16 bytes
4224 let key = 0x_1234567890ABCDEF_u64;
4225 println!("K =\t{:#016X}", key);
4226 let mut a_des = DES::new_with_key_u64(key);
4227
4228 let message = "고맙습니다.";
4229 println!("M =\t{}", message);
4230 let iv = 0x_FEDCBA0987654321_u64;
4231 println!("IV = {}", iv);
4232 let mut cipher = Vec::<u8>::new();
4233 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4234 print!("C =\t");
4235 for c in cipher.clone()
4236 { print!("{:02X} ", c); }
4237 println!();
4238 let mut txt = String::new();
4239 for c in cipher.clone()
4240 { write!(txt, "{:02X} ", c); }
4241 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
4242
4243 let mut recovered = String::new();
4244 a_des.decrypt_into_string(iv, cipher.as_ptr(), cipher.len() as u64, &mut recovered);
4245 println!("B =\t{}", recovered);
4246 assert_eq!(recovered, "고맙습니다.");
4247 assert_eq!(recovered, message);
4248 println!("-------------------------------");
4249}Sourcefn decrypt_vec<U>(&mut self, iv: T, cipher: &Vec<U>, message: *mut u8) -> u64
fn decrypt_vec<U>(&mut self, iv: T, cipher: &Vec<U>, message: *mut u8) -> u64
Decrypts the data stored in a Vec<U> object without any padding
in OFB (Output FeedBack) mode.
§Arguments
ivis an initialization vector for OFB mode.cipheris an immutable reference toVec<U>object, and is the place where the plaintext to be decrypted is stored.messageis a mutable pointer tou8which is*mut u8, and is the place where the decrypted data will be stored.
§Output
- This method returns the size of plaintext in bytes.
- If this method failed in decryption, it always returns
zero. - Even if this method succeeded in decryption, it returns
zerowhen the original plaintext is zero-length empty data. Then, you will have to check whether or not it failed by using the methodis_successful()oris_failed(). - If
size_of::<U>()*cipher.len()is greater than0(which means that the original plaintext is surely not empty data) and it returnszero, you can interpret it that this method surely failed in decryption.
§Features
- You are not encouraged to use this method in pure Rust programming. Instead, use other safer methods such as decrypt_vec_into_*().
- This method is useful to use in hybrid programming with C/C++.
- The size of the memory area which starts at
messageis assumed to be enough to store the plaintext. So, it is responsible for you to prepare themessagearea big enough! - The size of the area for plaintext does not have to be prepared more
than
size_of::<U>()*cipher.len(). - If the size of the area for plaintext is prepared more than
size_of::<U>()*cipher.len(), the rest of the area will be filled with0s.
§For Rijndael or AES, and its variants
§Example 1 for AES-128
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ AES_128, CBC_ISO };
let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
println!("K =\t{:#016X}", key);
let mut a_aes = AES_128::new_with_key_u128(key);
let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
print!("C =\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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 07 AF 4E C8 EF 5D 0A 74 97 3F 90 9E 05 9E 9E 32 FB 55 54 45 7C ED A2 2B F8 07 66 C0 7B CB 98 F3 BF 93 15 BA 26 1C 47 ");
let mut recovered = vec![0; 55];
a_aes.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
print!("Ba =\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 =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");
assert_eq!(converted, message);§For more examples,
click here
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, OFB };
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 iv = 0x_FEDCBA0987654321_u64;
println!("IV = {}", iv);
let mut cipher = Vec::<u8>::new();
a_des.encrypt_str_into_vec(iv, &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, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
let mut recovered = vec![0; 55];
a_des.decrypt_vec(iv, &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
§For BigCryptor128
§Example 1 for TAES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
+ AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
+ AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
println!("IV = {:#034X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
taes.encrypt_str_into_vec(iv, &message, &mut cipher);
print!("C =\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, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
let mut recovered = vec![0; 55];
taes.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
print!("Ba =\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 =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");
assert_eq!(converted, message);§For more examples,
click here
§For BigCryptor64
§Example 1 for TDES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
+ DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
+ DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
let iv = 0x_FEDCBA0987654321_u64;
println!("IV = {:#018X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
print!("C =\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, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
let mut recovered = vec![0; 55];
tdes.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
print!("Ba =\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 =\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?
649fn bigcryptor64_decrypt_vec_ofb()
650{
651 println!("bigcryptor64_decrypt_vec_ofb()");
652 use std::io::Write;
653 use std::fmt::Write as _;
654 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
655
656 // TDES case
657 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
658 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
659 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
660 let iv = 0x_FEDCBA0987654321_u64;
661 println!("IV = {:#018X}", iv);
662 let message = "In the beginning God created the heavens and the earth.";
663 println!("M =\t{}", message);
664 let mut cipher = Vec::<u8>::new();
665 tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
666 print!("C =\t");
667 for c in cipher.clone()
668 { print!("{:02X} ", c); }
669 println!();
670 let mut txt = String::new();
671 for c in cipher.clone()
672 { write!(txt, "{:02X} ", c); }
673 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
674
675 let mut recovered = vec![0; 55];
676 tdes.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
677 print!("Ba =\t");
678 for b in recovered.clone()
679 { print!("{:02X} ", b); }
680 println!();
681 let mut txt = String::new();
682 for c in recovered.clone()
683 { write!(txt, "{:02X} ", c); }
684 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 ");
685
686 let mut converted = String::new();
687 unsafe { converted.as_mut_vec() }.append(&mut recovered);
688
689 println!("Bb =\t{}", converted);
690 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
691 assert_eq!(converted, message);
692 println!("-------------------------------");
693}More examples
649fn bigcryptor128_decrypt_vec_ofb()
650{
651 println!("bigcryptor128_decrypt_vec_ofb()");
652 use std::io::Write;
653 use std::fmt::Write as _;
654 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
655
656 // TAES_128 case
657 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
658 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
659 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
660 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
661 println!("IV = {:#034X}", iv);
662 let message = "In the beginning God created the heavens and the earth.";
663 println!("M =\t{}", message);
664 let mut cipher = Vec::<u8>::new();
665 taes.encrypt_str_into_vec(iv, &message, &mut cipher);
666 print!("C =\t");
667 for c in cipher.clone()
668 { print!("{:02X} ", c); }
669 println!();
670 let mut txt = String::new();
671 for c in cipher.clone()
672 { write!(txt, "{:02X} ", c); }
673 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
674
675 let mut recovered = vec![0; 55];
676 taes.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
677 print!("Ba =\t");
678 for b in recovered.clone()
679 { print!("{:02X} ", b); }
680 println!();
681 let mut txt = String::new();
682 for c in recovered.clone()
683 { write!(txt, "{:02X} ", c); }
684 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 ");
685
686 let mut converted = String::new();
687 unsafe { converted.as_mut_vec() }.append(&mut recovered);
688
689 println!("Bb =\t{}", converted);
690 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
691 assert_eq!(converted, message);
692 println!("-------------------------------");
693}2953fn aes_decrypt_vec_ofb()
2954{
2955 println!("aes_decrypt_vec_ofb()");
2956 use std::io::Write;
2957 use std::fmt::Write as _;
2958 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
2959
2960 // Normal case for AES-128
2961 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
2962 println!("K =\t{:#016X}", key);
2963 let mut a_aes = AES_128::new_with_key_u128(key);
2964 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
2965 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
2966
2967 let message = "In the beginning God created the heavens and the earth.";
2968 println!("M =\t{}", message);
2969 let mut cipher = Vec::<u8>::new();
2970 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
2971 print!("C =\t");
2972 for c in cipher.clone()
2973 { print!("{:02X} ", c); }
2974 println!();
2975 let mut txt = String::new();
2976 for c in cipher.clone()
2977 { write!(txt, "{:02X} ", c); }
2978 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
2979
2980 let mut recovered = vec![0; 55];
2981 a_aes.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
2982 print!("Ba =\t");
2983 for b in recovered.clone()
2984 { print!("{:02X} ", b); }
2985 println!();
2986 let mut txt = String::new();
2987 for c in recovered.clone()
2988 { write!(txt, "{:02X} ", c); }
2989 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 ");
2990
2991 let mut converted = String::new();
2992 unsafe { converted.as_mut_vec() }.append(&mut recovered);
2993
2994 println!("Bb =\t{}", converted);
2995 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
2996 assert_eq!(converted, message);
2997 println!();
2998
2999 // Normal case for AES-192
3000 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3001 print!("K =\t");
3002 for i in 0..24
3003 { print!("{:02X}", key[i]); }
3004 println!();
3005 let mut a_aes = AES_192::new_with_key(&key);
3006 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3007 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3008
3009 let message = "In the beginning God created the heavens and the earth.";
3010 println!("M =\t{}", message);
3011 let mut cipher = Vec::<u8>::new();
3012 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3013 print!("C =\t");
3014 for c in cipher.clone()
3015 { print!("{:02X} ", c); }
3016 println!();
3017 let mut txt = String::new();
3018 for c in cipher.clone()
3019 { write!(txt, "{:02X} ", c); }
3020 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
3021
3022 let mut recovered = vec![0; 55];
3023 a_aes.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
3024 print!("Ba =\t");
3025 for b in recovered.clone()
3026 { print!("{:02X} ", b); }
3027 println!();
3028 let mut txt = String::new();
3029 for c in recovered.clone()
3030 { write!(txt, "{:02X} ", c); }
3031 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 ");
3032
3033 let mut converted = String::new();
3034 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3035
3036 println!("Bb =\t{}", converted);
3037 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3038 assert_eq!(converted, message);
3039 println!();
3040
3041 // Normal case for AES-256
3042 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3043 print!("K =\t");
3044 for i in 0..32
3045 { print!("{:02X}", key[i]); }
3046 println!();
3047 let mut a_aes = AES_256::new_with_key(&key);
3048 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3049 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3050
3051 let message = "In the beginning God created the heavens and the earth.";
3052 println!("M =\t{}", message);
3053 let mut cipher = Vec::<u8>::new();
3054 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3055 print!("C =\t");
3056 for c in cipher.clone()
3057 { print!("{:02X} ", c); }
3058 println!();
3059 let mut txt = String::new();
3060 for c in cipher.clone()
3061 { write!(txt, "{:02X} ", c); }
3062 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
3063
3064 let mut recovered = vec![0; 55];
3065 a_aes.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
3066 print!("Ba =\t");
3067 for b in recovered.clone()
3068 { print!("{:02X} ", b); }
3069 println!();
3070 let mut txt = String::new();
3071 for c in recovered.clone()
3072 { write!(txt, "{:02X} ", c); }
3073 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 ");
3074
3075 let mut converted = String::new();
3076 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3077
3078 println!("Bb =\t{}", converted);
3079 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3080 assert_eq!(converted, message);
3081 println!();
3082
3083 // Normal case for Rijndael-256-256
3084 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3085 print!("K =\t");
3086 for i in 0..32
3087 { print!("{:02X}", key[i]); }
3088 println!();
3089 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
3090 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3091 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
3092
3093 let message = "In the beginning God created the heavens and the earth.";
3094 println!("M =\t{}", message);
3095 let mut cipher = Vec::<u8>::new();
3096 a_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
3097 print!("C =\t");
3098 for c in cipher.clone()
3099 { print!("{:02X} ", c); }
3100 println!();
3101 let mut txt = String::new();
3102 for c in cipher.clone()
3103 { write!(txt, "{:02X} ", c); }
3104 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
3105
3106 let mut recovered = vec![0; 55];
3107 a_rijndael.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
3108 print!("Ba =\t");
3109 for b in recovered.clone()
3110 { print!("{:02X} ", b); }
3111 println!();
3112 let mut txt = String::new();
3113 for c in recovered.clone()
3114 { write!(txt, "{:02X} ", c); }
3115 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 ");
3116
3117 let mut converted = String::new();
3118 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3119
3120 println!("Bb =\t{}", converted);
3121 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3122 assert_eq!(converted, message);
3123 println!();
3124
3125 // Normal case for Rijndael-512-512 for post-quantum
3126 use cryptocol::number::SharedArrays;
3127 use cryptocol::hash::SHA3_512;
3128 let mut sha3 = SHA3_512::new();
3129 sha3.absorb_str("Post-quantum");
3130 let key: [u8; 64] = sha3.get_hash_value_in_array();
3131 print!("K =\t");
3132 for i in 0..64
3133 { print!("{:02X}", key[i]); }
3134 println!();
3135 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
3136 sha3.absorb_str("Initialize");
3137 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
3138 iv.src = sha3.get_hash_value_in_array();
3139 let iv = unsafe { iv.des };
3140 print!("IV =\t");
3141 for i in 0..16
3142 { print!("{:08X}", iv[i].to_be()); }
3143 println!();
3144 let message = "In the beginning God created the heavens and the earth.";
3145 println!("M =\t{}", message);
3146 let mut cipher = Vec::<u8>::new();
3147 a_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
3148 print!("C =\t");
3149 for c in cipher.clone()
3150 { print!("{:02X} ", c); }
3151 println!();
3152 let mut txt = String::new();
3153 for c in cipher.clone()
3154 { write!(txt, "{:02X} ", c); }
3155 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
3156
3157 let mut recovered = vec![0; 55];
3158 a_rijndael.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
3159 print!("Ba =\t");
3160 for b in recovered.clone()
3161 { print!("{:02X} ", b); }
3162 println!();
3163 let mut txt = String::new();
3164 for c in recovered.clone()
3165 { write!(txt, "{:02X} ", c); }
3166 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 ");
3167
3168 let mut converted = String::new();
3169 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3170
3171 println!("Bb =\t{}", converted);
3172 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3173 assert_eq!(converted, message);
3174 println!("-------------------------------");
3175}4251fn des_decrypt_vec_ofb()
4252{
4253 println!("des_decrypt_vec_ofb()");
4254 use std::io::Write;
4255 use std::fmt::Write as _;
4256 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
4257
4258 // Normal case
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 = "In the beginning God created the heavens and the earth.";
4264 println!("M =\t{}", message);
4265 let iv = 0x_FEDCBA0987654321_u64;
4266 println!("IV = {}", iv);
4267 let mut cipher = Vec::<u8>::new();
4268 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4269 print!("C (16 rounds) =\t");
4270 for c in cipher.clone()
4271 { print!("{:02X} ", c); }
4272 println!();
4273 let mut txt = String::new();
4274 for c in cipher.clone()
4275 { write!(txt, "{:02X} ", c); }
4276 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
4277
4278 let mut recovered = vec![0; 55];
4279 a_des.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
4280 print!("Ba (16 rounds) =\t");
4281 for b in recovered.clone()
4282 { print!("{:02X} ", b); }
4283 println!();
4284 let mut txt = String::new();
4285 for c in recovered.clone()
4286 { write!(txt, "{:02X} ", c); }
4287 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 ");
4288
4289 let mut converted = String::new();
4290 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4291
4292 println!("Bb (16 rounds) =\t{}", converted);
4293 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4294 assert_eq!(converted, message);
4295 println!();
4296
4297 // Expanded case for 128 rounds
4298 let key = 0x_1234567890ABCDEF_u64;
4299 println!("K =\t{:#016X}", key);
4300 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
4301
4302 let message = "In the beginning God created the heavens and the earth.";
4303 println!("M =\t{}", message);
4304 let iv = 0x_FEDCBA0987654321_u64;
4305 println!("IV = {}", iv);
4306 let mut cipher = Vec::<u8>::new();
4307 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4308 print!("C (128 rounds) =\t");
4309 for c in cipher.clone()
4310 { print!("{:02X} ", c); }
4311 println!();
4312 let mut txt = String::new();
4313 for c in cipher.clone()
4314 { write!(txt, "{:02X} ", c); }
4315 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
4316
4317 let mut recovered = vec![0; 55];
4318 a_des.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
4319 print!("Ba (128 rounds) =\t");
4320 for b in recovered.clone()
4321 { print!("{:02X} ", b); }
4322 println!();
4323 let mut txt = String::new();
4324 for c in recovered.clone()
4325 { write!(txt, "{:02X} ", c); }
4326 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 ");
4327
4328 let mut converted = String::new();
4329 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4330
4331 println!("Bb (128 rounds) =\t{}", converted);
4332 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4333 assert_eq!(converted, message);
4334 println!();
4335
4336 // Expanded case for 0 rounds which means that key is meaningless
4337 let key1 = 0x_1234567890ABCDEF_u64;
4338 let key2 = 0_u64;
4339 println!("K =\t{:#016X}", key);
4340 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
4341 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
4342
4343 let message = "In the beginning God created the heavens and the earth.";
4344 println!("M =\t{}", message);
4345 let iv = 0x_FEDCBA0987654321_u64;
4346 println!("IV = {}", iv);
4347 let mut cipher1 = Vec::<u8>::new();
4348 let mut cipher2 = Vec::<u8>::new();
4349 c_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
4350 d_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
4351 print!("C (0 rounds) =\t");
4352 for c in cipher1.clone()
4353 { print!("{:02X} ", c); }
4354 println!();
4355 let mut txt = String::new();
4356 for c in cipher1.clone()
4357 { write!(txt, "{:02X} ", c); }
4358 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
4359 print!("D (0 rounds) =\t");
4360 for c in cipher2.clone()
4361 { print!("{:02X} ", c); }
4362 println!();
4363 let mut txt = String::new();
4364 for c in cipher2.clone()
4365 { write!(txt, "{:02X} ", c); }
4366 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
4367
4368 let mut recovered1 = vec![0; 55];
4369 let mut recovered2 = vec![0; 55];
4370 c_des.decrypt_vec(iv, &cipher1, recovered1.as_mut_ptr());
4371 d_des.decrypt_vec(iv, &cipher2, recovered2.as_mut_ptr());
4372 print!("B1a (0 rounds) =\t");
4373 for b in recovered1.clone()
4374 { print!("{:02X} ", b); }
4375 println!();
4376 let mut txt = String::new();
4377 for c in recovered1.clone()
4378 { write!(txt, "{:02X} ", c); }
4379 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 ");
4380 print!("B2a (0 rounds) =\t");
4381 for b in recovered2.clone()
4382 { print!("{:02X} ", b); }
4383 println!();
4384 let mut txt = String::new();
4385 for c in recovered2.clone()
4386 { write!(txt, "{:02X} ", c); }
4387 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 ");
4388
4389 let mut converted1 = String::new();
4390 let mut converted2 = String::new();
4391 unsafe { converted1.as_mut_vec() }.append(&mut recovered1);
4392 unsafe { converted2.as_mut_vec() }.append(&mut recovered2);
4393
4394 println!("B1b (0 rounds) =\t{}", converted1);
4395 assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
4396 assert_eq!(converted1, message);
4397 println!("B2b (0 rounds) =\t{}", converted2);
4398 assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
4399 assert_eq!(converted2, message);
4400 assert_eq!(converted1, converted1);
4401 println!();
4402
4403 // Normal case for the message of 0 bytes
4404 let key = 0x_1234567890ABCDEF_u64;
4405 println!("K =\t{:#016X}", key);
4406 let mut a_des = DES::new_with_key_u64(key);
4407
4408 let message = "";
4409 println!("M =\t{}", message);
4410 let iv = 0x_FEDCBA0987654321_u64;
4411 println!("IV = {}", iv);
4412 let mut cipher = Vec::<u8>::new();
4413 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4414 print!("C =\t");
4415 for c in cipher.clone()
4416 { print!("{:02X} ", c); }
4417 println!();
4418 let mut txt = String::new();
4419 for c in cipher.clone()
4420 { write!(txt, "{:02X} ", c); }
4421 assert_eq!(txt, "");
4422
4423 let mut recovered = vec![0; 8];
4424 let len = a_des.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
4425 print!("Ba =\t");
4426 for b in recovered.clone()
4427 { print!("{:02X} ", b); }
4428 println!();
4429 let mut txt = String::new();
4430 for c in recovered.clone()
4431 { write!(txt, "{:02X} ", c); }
4432 assert_eq!(txt, "00 00 00 00 00 00 00 00 ");
4433
4434 let mut converted = String::new();
4435 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4436 converted.truncate(len as usize);
4437
4438 println!("Bb =\t{}", converted);
4439 assert_eq!(converted, "");
4440 assert_eq!(converted, message);
4441 println!();
4442
4443 // Normal case for the message shorter than 8 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 = "7 bytes";
4449 println!("M =\t{}", message);
4450 let iv = 0x_FEDCBA0987654321_u64;
4451 println!("IV = {}", iv);
4452 let mut cipher = Vec::<u8>::new();
4453 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4454 print!("C =\t");
4455 for c in cipher.clone()
4456 { print!("{:02X} ", c); }
4457 println!();
4458 let mut txt = String::new();
4459 for c in cipher.clone()
4460 { write!(txt, "{:02X} ", c); }
4461 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
4462
4463 let mut recovered = vec![0; 8];
4464 let len = a_des.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
4465 print!("Ba =\t");
4466 for b in recovered.clone()
4467 { print!("{:02X} ", b); }
4468 println!();
4469 let mut txt = String::new();
4470 for c in recovered.clone()
4471 { write!(txt, "{:02X} ", c); }
4472 assert_eq!(txt, "37 20 62 79 74 65 73 00 ");
4473
4474 let mut converted = String::new();
4475 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4476 converted.truncate(len as usize);
4477
4478 println!("Bb =\t{}", converted);
4479 assert_eq!(converted, "7 bytes");
4480 assert_eq!(converted, message);
4481 println!();
4482
4483 // Normal case for the message of 8 bytes
4484 let key = 0x_1234567890ABCDEF_u64;
4485 println!("K =\t{:#016X}", key);
4486 let mut a_des = DES::new_with_key_u64(key);
4487
4488 let message = "I am OK.";
4489 println!("M =\t{}", message);
4490 let iv = 0x_FEDCBA0987654321_u64;
4491 println!("IV = {}", iv);
4492 let mut cipher = Vec::<u8>::new();
4493 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4494 print!("C =\t");
4495 for c in cipher.clone()
4496 { print!("{:02X} ", c); }
4497 println!();
4498 let mut txt = String::new();
4499 for c in cipher.clone()
4500 { write!(txt, "{:02X} ", c); }
4501 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
4502
4503 let mut recovered = vec![0; 16];
4504 let len = a_des.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
4505 print!("Ba =\t");
4506 for b in recovered.clone()
4507 { print!("{:02X} ", b); }
4508 println!();
4509 let mut txt = String::new();
4510 for c in recovered.clone()
4511 { write!(txt, "{:02X} ", c); }
4512 assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E 00 00 00 00 00 00 00 00 ");
4513
4514 let mut converted = String::new();
4515 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4516 converted.truncate(len as usize);
4517
4518 println!("Bb =\t{}", converted);
4519 assert_eq!(converted, "I am OK.");
4520 assert_eq!(converted, message);
4521 println!();
4522
4523 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
4524 let key = 0x_1234567890ABCDEF_u64;
4525 println!("K =\t{:#016X}", key);
4526 let mut a_des = DES::new_with_key_u64(key);
4527
4528 let message = "PARK Youngho";
4529 println!("M =\t{}", message);
4530 let iv = 0x_FEDCBA0987654321_u64;
4531 println!("IV = {}", iv);
4532 let mut cipher = Vec::<u8>::new();
4533 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4534 print!("C =\t");
4535 for c in cipher.clone()
4536 { print!("{:02X} ", c); }
4537 println!();
4538 let mut txt = String::new();
4539 for c in cipher.clone()
4540 { write!(txt, "{:02X} ", c); }
4541 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
4542
4543 let mut recovered = vec![0; 16];
4544 let len = a_des.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
4545 print!("Ba =\t");
4546 for b in recovered.clone()
4547 { print!("{:02X} ", b); }
4548 println!();
4549 let mut txt = String::new();
4550 for c in recovered.clone()
4551 { write!(txt, "{:02X} ", c); }
4552 assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F 00 00 00 00 ");
4553
4554 let mut converted = String::new();
4555 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4556 converted.truncate(len as usize);
4557
4558 println!("Bb =\t{}", converted);
4559 assert_eq!(converted, "PARK Youngho");
4560 assert_eq!(converted, message);
4561 println!();
4562
4563 // Normal case for the message of 16 bytes
4564 let key = 0x_1234567890ABCDEF_u64;
4565 println!("K =\t{:#016X}", key);
4566 let mut a_des = DES::new_with_key_u64(key);
4567
4568 let message = "고맙습니다.";
4569 println!("M =\t{}", message);
4570 let iv = 0x_FEDCBA0987654321_u64;
4571 println!("IV = {}", iv);
4572 let mut cipher = Vec::<u8>::new();
4573 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4574 print!("C =\t");
4575 for c in cipher.clone()
4576 { print!("{:02X} ", c); }
4577 println!();
4578 let mut txt = String::new();
4579 for c in cipher.clone()
4580 { write!(txt, "{:02X} ", c); }
4581 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
4582
4583 let mut recovered = vec![0; 24];
4584 let len = a_des.decrypt_vec(iv, &cipher, recovered.as_mut_ptr());
4585 print!("Ba =\t");
4586 for b in recovered.clone()
4587 { print!("{:02X} ", b); }
4588 println!();
4589 let mut txt = String::new();
4590 for c in recovered.clone()
4591 { write!(txt, "{:02X} ", c); }
4592 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 ");
4593
4594 let mut converted = String::new();
4595 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4596 converted.truncate(len as usize);
4597
4598 println!("Bb =\t{}", converted);
4599 assert_eq!(converted, "고맙습니다.");
4600 assert_eq!(converted, message);
4601 println!("-------------------------------");
4602}Sourcefn decrypt_vec_into_vec<U, V>(
&mut self,
iv: T,
cipher: &Vec<U>,
message: &mut Vec<V>,
) -> u64
fn decrypt_vec_into_vec<U, V>( &mut self, iv: T, cipher: &Vec<U>, message: &mut Vec<V>, ) -> u64
Decrypts the data stored in a Vec<U> object without any padding in
OFB (Output FeedBack) mode, and stores the decrypted data in Vec<V>.
§Arguments
ivis an initialization vector for OFB mode.cipheris an immutable reference toVec<U>object, and is the place where the ciphertext to be decrypted is stored.messageis a mutable reference toVec<U>object, and is the place where the decrypted data will be stored.
§Output
- This method returns the size of plaintext in bytes.
- If this method failed in decryption, it always returns
zero. - Even if this method succeeded in decryption, it returns
zerowhen the original plaintext is zero-length empty data. Then, you will have to check whether or not it failed by using the methodis_successful()oris_failed(). - If
size_of::<U>()*cipher.len()is greater than0(which means that the original plaintext is surely not empty data) and it returnszero, you can interpret it that this method surely failed in decryption.
§Features
- 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 Rijndael or AES, and its variants
§Example 1 for AES-128
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ AES_128, CBC_ISO };
let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
println!("K =\t{:#016X}", key);
let mut a_aes = AES_128::new_with_key_u128(key);
let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
print!("C =\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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 07 AF 4E C8 EF 5D 0A 74 97 3F 90 9E 05 9E 9E 32 FB 55 54 45 7C ED A2 2B F8 07 66 C0 7B CB 98 F3 BF 93 15 BA 26 1C 47 ");
let mut recovered = Vec::<u8>::new();
a_aes.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
print!("Ba =\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 =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");
assert_eq!(converted, message);§For more examples,
click here
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, OFB };
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 iv = 0x_FEDCBA0987654321_u64;
println!("IV = {}", iv);
let mut cipher = Vec::<u8>::new();
a_des.encrypt_str_into_vec(iv, &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, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
let mut recovered = Vec::<u8>::new();
a_des.decrypt_vec_into_vec(iv, &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
§For BigCryptor128
§Example 1 for TAES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
+ AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
+ AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
println!("IV = {:#034X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
taes.encrypt_str_into_vec(iv, &message, &mut cipher);
print!("C =\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, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
let mut recovered = Vec::<u8>::new();
taes.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
print!("Ba =\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 =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");
assert_eq!(converted, message);§For more examples,
click here
§For BigCryptor64
§Example 1 for TDES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
+ DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
+ DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
let iv = 0x_FEDCBA0987654321_u64;
println!("IV = {:#018X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
print!("C =\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, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
let mut recovered = Vec::<u8>::new();
tdes.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
print!("Ba =\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 =\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?
695fn bigcryptor64_decrypt_vec_ofb_into_vec()
696{
697 println!("bigcryptor64_decrypt_vec_ofb_into_vec()");
698 use std::io::Write;
699 use std::fmt::Write as _;
700 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
701
702 // TDES case
703 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
704 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
705 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
706 let iv = 0x_FEDCBA0987654321_u64;
707 println!("IV = {:#018X}", iv);
708 let message = "In the beginning God created the heavens and the earth.";
709 println!("M =\t{}", message);
710 let mut cipher = Vec::<u8>::new();
711 tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
712 print!("C =\t");
713 for c in cipher.clone()
714 { print!("{:02X} ", c); }
715 println!();
716 let mut txt = String::new();
717 for c in cipher.clone()
718 { write!(txt, "{:02X} ", c); }
719 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
720
721 let mut recovered = Vec::<u8>::new();
722 tdes.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
723 print!("Ba =\t");
724 for b in recovered.clone()
725 { print!("{:02X} ", b); }
726 println!();
727 let mut txt = String::new();
728 for c in recovered.clone()
729 { write!(txt, "{:02X} ", c); }
730 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 ");
731
732 let mut converted = String::new();
733 unsafe { converted.as_mut_vec() }.append(&mut recovered);
734
735 println!("Bb =\t{}", converted);
736 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
737 assert_eq!(converted, message);
738 println!("-------------------------------");
739}More examples
695fn bigcryptor128_decrypt_vec_ofb_into_vec()
696{
697 println!("bigcryptor128_decrypt_vec_ofb_into_vec()");
698 use std::io::Write;
699 use std::fmt::Write as _;
700 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
701
702 // TAES_128 case
703 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
704 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
705 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
706 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
707 println!("IV = {:#034X}", iv);
708 let message = "In the beginning God created the heavens and the earth.";
709 println!("M =\t{}", message);
710 let mut cipher = Vec::<u8>::new();
711 taes.encrypt_str_into_vec(iv, &message, &mut cipher);
712 print!("C =\t");
713 for c in cipher.clone()
714 { print!("{:02X} ", c); }
715 println!();
716 let mut txt = String::new();
717 for c in cipher.clone()
718 { write!(txt, "{:02X} ", c); }
719 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
720
721 let mut recovered = Vec::<u8>::new();
722 taes.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
723 print!("Ba =\t");
724 for b in recovered.clone()
725 { print!("{:02X} ", b); }
726 println!();
727 let mut txt = String::new();
728 for c in recovered.clone()
729 { write!(txt, "{:02X} ", c); }
730 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 ");
731
732 let mut converted = String::new();
733 unsafe { converted.as_mut_vec() }.append(&mut recovered);
734
735 println!("Bb =\t{}", converted);
736 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
737 assert_eq!(converted, message);
738 println!("-------------------------------");
739}3177fn aes_decrypt_vec_ofb_into_vec()
3178{
3179 println!("aes_decrypt_vec_ofb_into_vec()");
3180 use std::io::Write;
3181 use std::fmt::Write as _;
3182 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
3183
3184 // Normal case for AES-128
3185 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
3186 println!("K =\t{:#016X}", key);
3187 let mut a_aes = AES_128::new_with_key_u128(key);
3188 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3189 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3190
3191 let message = "In the beginning God created the heavens and the earth.";
3192 println!("M =\t{}", message);
3193 let mut cipher = Vec::<u8>::new();
3194 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3195 print!("C =\t");
3196 for c in cipher.clone()
3197 { print!("{:02X} ", c); }
3198 println!();
3199 let mut txt = String::new();
3200 for c in cipher.clone()
3201 { write!(txt, "{:02X} ", c); }
3202 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
3203
3204 let mut recovered = Vec::<u8>::new();
3205 a_aes.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
3206 print!("Ba =\t");
3207 for b in recovered.clone()
3208 { print!("{:02X} ", b); }
3209 println!();
3210 let mut txt = String::new();
3211 for c in recovered.clone()
3212 { write!(txt, "{:02X} ", c); }
3213 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 ");
3214
3215 let mut converted = String::new();
3216 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3217
3218 println!("Bb =\t{}", converted);
3219 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3220 assert_eq!(converted, message);
3221 println!();
3222
3223 // Normal case for AES-192
3224 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3225 print!("K =\t");
3226 for i in 0..24
3227 { print!("{:02X}", key[i]); }
3228 println!();
3229 let mut a_aes = AES_192::new_with_key(&key);
3230 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3231 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3232
3233 let message = "In the beginning God created the heavens and the earth.";
3234 println!("M =\t{}", message);
3235 let mut cipher = Vec::<u8>::new();
3236 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3237 print!("C =\t");
3238 for c in cipher.clone()
3239 { print!("{:02X} ", c); }
3240 println!();
3241 let mut txt = String::new();
3242 for c in cipher.clone()
3243 { write!(txt, "{:02X} ", c); }
3244 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
3245
3246 let mut recovered = Vec::<u8>::new();
3247 a_aes.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
3248 print!("Ba =\t");
3249 for b in recovered.clone()
3250 { print!("{:02X} ", b); }
3251 println!();
3252 let mut txt = String::new();
3253 for c in recovered.clone()
3254 { write!(txt, "{:02X} ", c); }
3255 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
3256
3257 let mut converted = String::new();
3258 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3259
3260 println!("Bb =\t{}", converted);
3261 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3262 assert_eq!(converted, message);
3263 println!();
3264
3265 // Normal case for AES-256
3266 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3267 print!("K =\t");
3268 for i in 0..32
3269 { print!("{:02X}", key[i]); }
3270 println!();
3271 let mut a_aes = AES_256::new_with_key(&key);
3272 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3273 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3274
3275 let message = "In the beginning God created the heavens and the earth.";
3276 println!("M =\t{}", message);
3277 let mut cipher = Vec::<u8>::new();
3278 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3279 print!("C =\t");
3280 for c in cipher.clone()
3281 { print!("{:02X} ", c); }
3282 println!();
3283 let mut txt = String::new();
3284 for c in cipher.clone()
3285 { write!(txt, "{:02X} ", c); }
3286 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
3287
3288 let mut recovered = Vec::<u8>::new();
3289 a_aes.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
3290 print!("Ba =\t");
3291 for b in recovered.clone()
3292 { print!("{:02X} ", b); }
3293 println!();
3294 let mut txt = String::new();
3295 for c in recovered.clone()
3296 { write!(txt, "{:02X} ", c); }
3297 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 ");
3298
3299 let mut converted = String::new();
3300 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3301
3302 println!("Bb =\t{}", converted);
3303 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3304 assert_eq!(converted, message);
3305 println!();
3306
3307 // Normal case for Rijndael-256-256
3308 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3309 print!("K =\t");
3310 for i in 0..32
3311 { print!("{:02X}", key[i]); }
3312 println!();
3313 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
3314 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3315 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
3316
3317 let message = "In the beginning God created the heavens and the earth.";
3318 println!("M =\t{}", message);
3319 let mut cipher = Vec::<u8>::new();
3320 a_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
3321 print!("C =\t");
3322 for c in cipher.clone()
3323 { print!("{:02X} ", c); }
3324 println!();
3325 let mut txt = String::new();
3326 for c in cipher.clone()
3327 { write!(txt, "{:02X} ", c); }
3328 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
3329
3330 let mut recovered = Vec::<u8>::new();
3331 a_rijndael.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
3332 print!("Ba =\t");
3333 for b in recovered.clone()
3334 { print!("{:02X} ", b); }
3335 println!();
3336 let mut txt = String::new();
3337 for c in recovered.clone()
3338 { write!(txt, "{:02X} ", c); }
3339 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 ");
3340
3341 let mut converted = String::new();
3342 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3343
3344 println!("Bb =\t{}", converted);
3345 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3346 assert_eq!(converted, message);
3347 println!();
3348
3349 // Normal case for Rijndael-512-512 for post-quantum
3350 use cryptocol::number::SharedArrays;
3351 use cryptocol::hash::SHA3_512;
3352 let mut sha3 = SHA3_512::new();
3353 sha3.absorb_str("Post-quantum");
3354 let key: [u8; 64] = sha3.get_hash_value_in_array();
3355 print!("K =\t");
3356 for i in 0..64
3357 { print!("{:02X}", key[i]); }
3358 println!();
3359 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
3360 sha3.absorb_str("Initialize");
3361 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
3362 iv.src = sha3.get_hash_value_in_array();
3363 let iv = unsafe { iv.des };
3364 print!("IV =\t");
3365 for i in 0..16
3366 { print!("{:08X}", iv[i].to_be()); }
3367 println!();
3368
3369 let message = "In the beginning God created the heavens and the earth.";
3370 println!("M =\t{}", message);
3371 let mut cipher = Vec::<u8>::new();
3372 a_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
3373 print!("C =\t");
3374 for c in cipher.clone()
3375 { print!("{:02X} ", c); }
3376 println!();
3377 let mut txt = String::new();
3378 for c in cipher.clone()
3379 { write!(txt, "{:02X} ", c); }
3380 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
3381
3382 let mut recovered = Vec::<u8>::new();
3383 a_rijndael.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
3384 print!("Ba =\t");
3385 for b in recovered.clone()
3386 { print!("{:02X} ", b); }
3387 println!();
3388 let mut txt = String::new();
3389 for c in recovered.clone()
3390 { write!(txt, "{:02X} ", c); }
3391 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E ");
3392
3393 let mut converted = String::new();
3394 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3395
3396 println!("Bb =\t{}", converted);
3397 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3398 assert_eq!(converted, message);
3399 println!("-------------------------------");
3400}4604fn des_decrypt_vec_ofb_into_vec()
4605{
4606 println!("des_decrypt_vec_ofb_into_vec()");
4607 use std::io::Write;
4608 use std::fmt::Write as _;
4609 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
4610
4611 // Normal case
4612 let key = 0x_1234567890ABCDEF_u64;
4613 println!("K =\t{:#016X}", key);
4614 let mut a_des = DES::new_with_key_u64(key);
4615
4616 let message = "In the beginning God created the heavens and the earth.";
4617 println!("M =\t{}", message);
4618 let iv = 0x_FEDCBA0987654321_u64;
4619 println!("IV = {}", iv);
4620 let mut cipher = Vec::<u8>::new();
4621 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4622 print!("C (16 rounds) =\t");
4623 for c in cipher.clone()
4624 { print!("{:02X} ", c); }
4625 println!();
4626 let mut txt = String::new();
4627 for c in cipher.clone()
4628 { write!(txt, "{:02X} ", c); }
4629 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
4630
4631 let mut recovered = Vec::<u8>::new();
4632 a_des.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
4633 print!("Ba (16 rounds) =\t");
4634 for b in recovered.clone()
4635 { print!("{:02X} ", b); }
4636 println!();
4637 let mut txt = String::new();
4638 for c in recovered.clone()
4639 { write!(txt, "{:02X} ", c); }
4640 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 ");
4641
4642 let mut converted = String::new();
4643 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4644
4645 println!("Bb (16 rounds) =\t{}", converted);
4646 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4647 assert_eq!(converted, message);
4648 println!();
4649
4650 // Expanded case for 128 rounds
4651 let key = 0x_1234567890ABCDEF_u64;
4652 println!("K =\t{:#016X}", key);
4653 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
4654
4655 let message = "In the beginning God created the heavens and the earth.";
4656 println!("M =\t{}", message);
4657 let iv = 0x_FEDCBA0987654321_u64;
4658 println!("IV = {}", iv);
4659 let mut cipher = Vec::<u8>::new();
4660 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4661 print!("C (128 rounds) =\t");
4662 for c in cipher.clone()
4663 { print!("{:02X} ", c); }
4664 println!();
4665 let mut txt = String::new();
4666 for c in cipher.clone()
4667 { write!(txt, "{:02X} ", c); }
4668 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
4669
4670 let mut recovered = Vec::<u8>::new();
4671 a_des.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
4672 print!("Ba (128 rounds) =\t");
4673 for b in recovered.clone()
4674 { print!("{:02X} ", b); }
4675 println!();
4676 let mut txt = String::new();
4677 for c in recovered.clone()
4678 { write!(txt, "{:02X} ", c); }
4679 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 ");
4680
4681 let mut converted = String::new();
4682 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4683
4684 println!("Bb (128 rounds) =\t{}", converted);
4685 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4686 assert_eq!(converted, message);
4687 println!();
4688
4689 // Expanded case for 0 rounds which means that key is meaningless
4690 let key1 = 0x_1234567890ABCDEF_u64;
4691 let key2 = 0_u64;
4692 println!("K =\t{:#016X}", key);
4693 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
4694 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
4695
4696 let message = "In the beginning God created the heavens and the earth.";
4697 println!("M =\t{}", message);
4698 let iv = 0x_FEDCBA0987654321_u64;
4699 println!("IV = {}", iv);
4700 let mut cipher1 = Vec::<u8>::new();
4701 let mut cipher2 = Vec::<u8>::new();
4702 c_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
4703 d_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
4704 print!("C (0 rounds) =\t");
4705 for c in cipher1.clone()
4706 { print!("{:02X} ", c); }
4707 println!();
4708 let mut txt = String::new();
4709 for c in cipher1.clone()
4710 { write!(txt, "{:02X} ", c); }
4711 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
4712 print!("D (0 rounds) =\t");
4713 for c in cipher2.clone()
4714 { print!("{:02X} ", c); }
4715 println!();
4716 let mut txt = String::new();
4717 for c in cipher2.clone()
4718 { write!(txt, "{:02X} ", c); }
4719 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
4720
4721 let mut recovered1 = Vec::<u8>::new();
4722 let mut recovered2 = Vec::<u8>::new();
4723 c_des.decrypt_vec_into_vec(iv, &cipher1, &mut recovered1);
4724 d_des.decrypt_vec_into_vec(iv, &cipher2, &mut recovered2);
4725 print!("B1a (0 rounds) =\t");
4726 for b in recovered1.clone()
4727 { print!("{:02X} ", b); }
4728 println!();
4729 let mut txt = String::new();
4730 for c in recovered1.clone()
4731 { write!(txt, "{:02X} ", c); }
4732 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 ");
4733 print!("B2a (0 rounds) =\t");
4734 for b in recovered2.clone()
4735 { print!("{:02X} ", b); }
4736 println!();
4737 let mut txt = String::new();
4738 for c in recovered2.clone()
4739 { write!(txt, "{:02X} ", c); }
4740 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 ");
4741
4742 let mut converted1 = String::new();
4743 let mut converted2 = String::new();
4744 unsafe { converted1.as_mut_vec() }.append(&mut recovered1);
4745 unsafe { converted2.as_mut_vec() }.append(&mut recovered2);
4746
4747 println!("B1b (0 rounds) =\t{}", converted1);
4748 assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
4749 assert_eq!(converted1, message);
4750 println!("B2b (0 rounds) =\t{}", converted2);
4751 assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
4752 assert_eq!(converted2, message);
4753 assert_eq!(converted1, converted1);
4754 println!();
4755
4756 // Normal case for the message of 0 bytes
4757 let key = 0x_1234567890ABCDEF_u64;
4758 println!("K =\t{:#016X}", key);
4759 let mut a_des = DES::new_with_key_u64(key);
4760
4761 let message = "";
4762 println!("M =\t{}", message);
4763 let iv = 0x_FEDCBA0987654321_u64;
4764 println!("IV = {}", iv);
4765 let mut cipher = Vec::<u8>::new();
4766 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4767 print!("C =\t");
4768 for c in cipher.clone()
4769 { print!("{:02X} ", c); }
4770 println!();
4771 let mut txt = String::new();
4772 for c in cipher.clone()
4773 { write!(txt, "{:02X} ", c); }
4774 assert_eq!(txt, "");
4775
4776 let mut recovered = Vec::<u8>::new();
4777 a_des.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
4778 print!("Ba =\t");
4779 for b in recovered.clone()
4780 { print!("{:02X} ", b); }
4781 println!();
4782 let mut txt = String::new();
4783 for c in recovered.clone()
4784 { write!(txt, "{:02X} ", c); }
4785 assert_eq!(txt, "");
4786
4787 let mut converted = String::new();
4788 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4789
4790 println!("Bb =\t{}", converted);
4791 assert_eq!(converted, "");
4792 assert_eq!(converted, message);
4793 println!();
4794
4795 // Normal case for the message shorter than 8 bytes
4796 let key = 0x_1234567890ABCDEF_u64;
4797 println!("K =\t{:#016X}", key);
4798 let mut a_des = DES::new_with_key_u64(key);
4799
4800 let message = "7 bytes";
4801 println!("M =\t{}", message);
4802 let iv = 0x_FEDCBA0987654321_u64;
4803 println!("IV = {}", iv);
4804 let mut cipher = Vec::<u8>::new();
4805 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4806 print!("C =\t");
4807 for c in cipher.clone()
4808 { print!("{:02X} ", c); }
4809 println!();
4810 let mut txt = String::new();
4811 for c in cipher.clone()
4812 { write!(txt, "{:02X} ", c); }
4813 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
4814
4815 let mut recovered = Vec::<u8>::new();
4816 a_des.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
4817 print!("Ba =\t");
4818 for b in recovered.clone()
4819 { print!("{:02X} ", b); }
4820 println!();
4821 let mut txt = String::new();
4822 for c in recovered.clone()
4823 { write!(txt, "{:02X} ", c); }
4824 assert_eq!(txt, "37 20 62 79 74 65 73 ");
4825
4826 let mut converted = String::new();
4827 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4828
4829 println!("Bb =\t{}", converted);
4830 assert_eq!(converted, "7 bytes");
4831 assert_eq!(converted, message);
4832 println!();
4833
4834 // Normal case for the message of 8 bytes
4835 let key = 0x_1234567890ABCDEF_u64;
4836 println!("K =\t{:#016X}", key);
4837 let mut a_des = DES::new_with_key_u64(key);
4838
4839 let message = "I am OK.";
4840 println!("M =\t{}", message);
4841 let iv = 0x_FEDCBA0987654321_u64;
4842 println!("IV = {}", iv);
4843 let mut cipher = Vec::<u8>::new();
4844 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4845 print!("C =\t");
4846 for c in cipher.clone()
4847 { print!("{:02X} ", c); }
4848 println!();
4849 let mut txt = String::new();
4850 for c in cipher.clone()
4851 { write!(txt, "{:02X} ", c); }
4852 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
4853
4854 let mut recovered = Vec::<u8>::new();
4855 a_des.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
4856 print!("Ba =\t");
4857 for b in recovered.clone()
4858 { print!("{:02X} ", b); }
4859 println!();
4860 let mut txt = String::new();
4861 for c in recovered.clone()
4862 { write!(txt, "{:02X} ", c); }
4863 assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E ");
4864
4865 let mut converted = String::new();
4866 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4867
4868 println!("Bb =\t{}", converted);
4869 assert_eq!(converted, "I am OK.");
4870 assert_eq!(converted, message);
4871 println!();
4872
4873 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
4874 let key = 0x_1234567890ABCDEF_u64;
4875 println!("K =\t{:#016X}", key);
4876 let mut a_des = DES::new_with_key_u64(key);
4877
4878 let message = "PARK Youngho";
4879 println!("M =\t{}", message);
4880 let iv = 0x_FEDCBA0987654321_u64;
4881 println!("IV = {}", iv);
4882 let mut cipher = Vec::<u8>::new();
4883 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4884 print!("C =\t");
4885 for c in cipher.clone()
4886 { print!("{:02X} ", c); }
4887 println!();
4888 let mut txt = String::new();
4889 for c in cipher.clone()
4890 { write!(txt, "{:02X} ", c); }
4891 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
4892
4893 let mut recovered = Vec::<u8>::new();
4894 a_des.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
4895 print!("Ba =\t");
4896 for b in recovered.clone()
4897 { print!("{:02X} ", b); }
4898 println!();
4899 let mut txt = String::new();
4900 for c in recovered.clone()
4901 { write!(txt, "{:02X} ", c); }
4902 assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F ");
4903
4904 let mut converted = String::new();
4905 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4906
4907 println!("Bb =\t{}", converted);
4908 assert_eq!(converted, "PARK Youngho");
4909 assert_eq!(converted, message);
4910 println!();
4911
4912 // Normal case for the message of 16 bytes
4913 let key = 0x_1234567890ABCDEF_u64;
4914 println!("K =\t{:#016X}", key);
4915 let mut a_des = DES::new_with_key_u64(key);
4916
4917 let message = "고맙습니다.";
4918 println!("M =\t{}", message);
4919 let iv = 0x_FEDCBA0987654321_u64;
4920 println!("IV = {}", iv);
4921 let mut cipher = Vec::<u8>::new();
4922 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4923 print!("C =\t");
4924 for c in cipher.clone()
4925 { print!("{:02X} ", c); }
4926 println!();
4927 let mut txt = String::new();
4928 for c in cipher.clone()
4929 { write!(txt, "{:02X} ", c); }
4930 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
4931
4932 let mut recovered = Vec::<u8>::new();
4933 a_des.decrypt_vec_into_vec(iv, &cipher, &mut recovered);
4934 print!("Ba =\t");
4935 for b in recovered.clone()
4936 { print!("{:02X} ", b); }
4937 println!();
4938 let mut txt = String::new();
4939 for c in recovered.clone()
4940 { write!(txt, "{:02X} ", c); }
4941 assert_eq!(txt, "EA B3 A0 EB A7 99 EC 8A B5 EB 8B 88 EB 8B A4 2E ");
4942
4943 let mut converted = String::new();
4944 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4945
4946 println!("Bb =\t{}", converted);
4947 assert_eq!(converted, "고맙습니다.");
4948 assert_eq!(converted, message);
4949 println!("-------------------------------");
4950}Sourcefn decrypt_vec_into_array<U, V, const N: usize>(
&mut self,
iv: T,
cipher: &Vec<U>,
message: &mut [V; N],
) -> u64
fn decrypt_vec_into_array<U, V, const N: usize>( &mut self, iv: T, cipher: &Vec<U>, message: &mut [V; N], ) -> u64
Decrypts the data stored in a Vec<U> object without any padding in OFB
(Output FeedBack) mode, and stores the decrypted data in array [V; N].
§Arguments
ivis an initialization vector for OFB mode.cipheris an immutable reference toVec<U>object, and is the place where the ciphertext to be decrypted is stored.messageis a mutable reference to an array[V; N]object, and is the place where the decrypted data will be stored.
§Output
- This method returns the size of plaintext in bytes.
- If this method failed in decryption, it always returns
zero. - Even if this method succeeded in decryption, it returns
zerowhen the original plaintext is zero-length empty data. Then, you will have to check whether or not it failed by using the methodis_successful()oris_failed(). - If
size_of::<U>()*cipher.len()is greater than0(which means that the original plaintext is surely not empty data) and it returnszero, you can interpret it that this method surely failed in decryption.
§Features
- If
size_of::<V>()*Nis less thansize_of::<U>()*cipher.len(), this method does not perform decryption but returnszero. - If
size_of::<V>()*Nis equal to or greater thansize_of::<U>()*cipher.len(), this method performs decryption, fills the arraymessagewith the decrypted data, and then fills the rest of the elements of the arraymessagewith zeros, and returns the size of the plaintext. - It is responsible for you to prepare the
messagearea big enough! - The size of the area for plaintext does not have to be prepared more
than
size_of::<U>()*cipher.len().
§For Rijndael or AES, and its variants
§Example 1 for AES-128
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ AES_128, CBC_ISO };
let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
println!("K =\t{:#016X}", key);
let mut a_aes = AES_128::new_with_key_u128(key);
let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
print!("C =\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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 07 AF 4E C8 EF 5D 0A 74 97 3F 90 9E 05 9E 9E 32 FB 55 54 45 7C ED A2 2B F8 07 66 C0 7B CB 98 F3 BF 93 15 BA 26 1C 47 ");
let mut recovered = [0; 64];
let len = a_aes.decrypt_vec_into_array(iv, &cipher, &mut recovered);
print!("Ba =\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 00 00 00 00 00 00 00 00 ");
let mut converted = String::new();
unsafe { converted.as_mut_vec() }.write(&recovered);
unsafe { converted.as_mut_vec() }.truncate(len as usize);
println!("Bb =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");
assert_eq!(converted, message);§For more examples,
click here
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, OFB };
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 iv = 0x_FEDCBA0987654321_u64;
println!("IV = {}", iv);
let mut cipher = Vec::<u8>::new();
a_des.encrypt_str_into_vec(iv, &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, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
let mut recovered = [0u8; 56];
let len = a_des.decrypt_vec_into_array(iv, &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
§For BigCryptor128
§Example 1 for TAES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
+ AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
+ AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
println!("IV = {:#034X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
taes.encrypt_str_into_vec(iv, &message, &mut cipher);
print!("C =\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, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
let mut recovered = [0u8; 56];
let len = taes.decrypt_vec_into_array(iv, &cipher, &mut recovered);
print!("Ba =\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 =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");§For more examples,
click here
§For BigCryptor64
§Example 1 for TDES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
+ DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
+ DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
let iv = 0x_FEDCBA0987654321_u64;
println!("IV = {:#018X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
print!("C =\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, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
let mut recovered = [0u8; 56];
let len = tdes.decrypt_vec_into_array(iv, &cipher, &mut recovered);
print!("Ba =\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 =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");§For more examples,
click here
Examples found in repository?
741fn bigcryptor64_decrypt_vec_ofb_into_array()
742{
743 println!("bigcryptor64_decrypt_vec_ofb_into_array()");
744 use std::io::Write;
745 use std::fmt::Write as _;
746 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
747
748 // TDES case
749 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
750 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
751 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
752 let iv = 0x_FEDCBA0987654321_u64;
753 println!("IV = {:#018X}", iv);
754 let message = "In the beginning God created the heavens and the earth.";
755 println!("M =\t{}", message);
756 let mut cipher = Vec::<u8>::new();
757 tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
758 print!("C =\t");
759 for c in cipher.clone()
760 { print!("{:02X} ", c); }
761 println!();
762 let mut txt = String::new();
763 for c in cipher.clone()
764 { write!(txt, "{:02X} ", c); }
765 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
766
767 let mut recovered = [0u8; 56];
768 let len = tdes.decrypt_vec_into_array(iv, &cipher, &mut recovered);
769 print!("Ba =\t");
770 for b in recovered.clone()
771 { print!("{:02X} ", b); }
772 println!();
773 let mut txt = String::new();
774 for c in recovered.clone()
775 { write!(txt, "{:02X} ", c); }
776 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 ");
777
778 let mut converted = String::new();
779 unsafe { converted.as_mut_vec() }.write(&recovered);
780 unsafe { converted.as_mut_vec() }.truncate(len as usize);
781 println!("Bb =\t{}", converted);
782 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
783 assert_eq!(converted, message);
784 println!("-------------------------------");
785}More examples
741fn bigcryptor128_decrypt_vec_ofb_into_array()
742{
743 println!("bigcryptor128_decrypt_vec_ofb_into_array()");
744 use std::io::Write;
745 use std::fmt::Write as _;
746 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
747
748 // TAES_128 case
749 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
750 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
751 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
752 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
753 println!("IV = {:#034X}", iv);
754 let message = "In the beginning God created the heavens and the earth.";
755 println!("M =\t{}", message);
756 let mut cipher = Vec::<u8>::new();
757 taes.encrypt_str_into_vec(iv, &message, &mut cipher);
758 print!("C =\t");
759 for c in cipher.clone()
760 { print!("{:02X} ", c); }
761 println!();
762 let mut txt = String::new();
763 for c in cipher.clone()
764 { write!(txt, "{:02X} ", c); }
765 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
766
767 let mut recovered = [0u8; 56];
768 let len = taes.decrypt_vec_into_array(iv, &cipher, &mut recovered);
769 print!("Ba =\t");
770 for b in recovered.clone()
771 { print!("{:02X} ", b); }
772 println!();
773 let mut txt = String::new();
774 for c in recovered.clone()
775 { write!(txt, "{:02X} ", c); }
776 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 ");
777
778 let mut converted = String::new();
779 unsafe { converted.as_mut_vec() }.write(&recovered);
780 unsafe { converted.as_mut_vec() }.truncate(len as usize);
781 println!("Bb =\t{}", converted);
782 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
783 assert_eq!(converted, message);
784 println!("-------------------------------");
785}3402fn aes_decrypt_vec_ofb_into_array()
3403{
3404 println!("aes_decrypt_vec_ofb_into_array()");
3405 use std::io::Write;
3406 use std::fmt::Write as _;
3407 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
3408
3409 // Normal case for AES-128
3410 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
3411 println!("K =\t{:#016X}", key);
3412 let mut a_aes = AES_128::new_with_key_u128(key);
3413 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3414 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3415
3416 let message = "In the beginning God created the heavens and the earth.";
3417 println!("M =\t{}", message);
3418 let mut cipher = Vec::<u8>::new();
3419 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3420 print!("C =\t");
3421 for c in cipher.clone()
3422 { print!("{:02X} ", c); }
3423 println!();
3424 let mut txt = String::new();
3425 for c in cipher.clone()
3426 { write!(txt, "{:02X} ", c); }
3427 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
3428
3429 let mut recovered = [0; 64];
3430 let len = a_aes.decrypt_vec_into_array(iv, &cipher, &mut recovered);
3431 print!("Ba =\t");
3432 for b in recovered.clone()
3433 { print!("{:02X} ", b); }
3434 println!();
3435 let mut txt = String::new();
3436 for c in recovered.clone()
3437 { write!(txt, "{:02X} ", c); }
3438 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
3439
3440 let mut converted = String::new();
3441 unsafe { converted.as_mut_vec() }.write(&recovered);
3442 unsafe { converted.as_mut_vec() }.truncate(len as usize);
3443 println!("Bb =\t{}", converted);
3444 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3445 assert_eq!(converted, message);
3446 println!();
3447
3448 // Normal case for AES-192
3449 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3450 print!("K =\t");
3451 for i in 0..24
3452 { print!("{:02X}", key[i]); }
3453 println!();
3454 let mut a_aes = AES_192::new_with_key(&key);
3455 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3456 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3457
3458 let message = "In the beginning God created the heavens and the earth.";
3459 println!("M =\t{}", message);
3460 let mut cipher = Vec::<u8>::new();
3461 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3462 print!("C =\t");
3463 for c in cipher.clone()
3464 { print!("{:02X} ", c); }
3465 println!();
3466 let mut txt = String::new();
3467 for c in cipher.clone()
3468 { write!(txt, "{:02X} ", c); }
3469 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
3470
3471 let mut recovered = [0; 64];
3472 a_aes.decrypt_vec_into_array(iv, &cipher, &mut recovered);
3473 print!("Ba =\t");
3474 for b in recovered.clone()
3475 { print!("{:02X} ", b); }
3476 println!();
3477 let mut txt = String::new();
3478 for c in recovered.clone()
3479 { write!(txt, "{:02X} ", c); }
3480 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
3481
3482 let mut converted = String::new();
3483 unsafe { converted.as_mut_vec() }.write(&recovered);
3484 unsafe { converted.as_mut_vec() }.truncate(len as usize);
3485 println!("Bb =\t{}", converted);
3486 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3487 assert_eq!(converted, message);
3488 println!();
3489
3490 // Normal case for AES-256
3491 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3492 print!("K =\t");
3493 for i in 0..32
3494 { print!("{:02X}", key[i]); }
3495 println!();
3496 let mut a_aes = AES_256::new_with_key(&key);
3497 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3498 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3499
3500 let message = "In the beginning God created the heavens and the earth.";
3501 println!("M =\t{}", message);
3502 let mut cipher = Vec::<u8>::new();
3503 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3504 print!("C =\t");
3505 for c in cipher.clone()
3506 { print!("{:02X} ", c); }
3507 println!();
3508 let mut txt = String::new();
3509 for c in cipher.clone()
3510 { write!(txt, "{:02X} ", c); }
3511 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
3512
3513 let mut recovered = [0; 64];
3514 a_aes.decrypt_vec_into_array(iv, &cipher, &mut recovered);
3515 print!("Ba =\t");
3516 for b in recovered.clone()
3517 { print!("{:02X} ", b); }
3518 println!();
3519 let mut txt = String::new();
3520 for c in recovered.clone()
3521 { write!(txt, "{:02X} ", c); }
3522 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
3523
3524 let mut converted = String::new();
3525 unsafe { converted.as_mut_vec() }.write(&recovered);
3526 unsafe { converted.as_mut_vec() }.truncate(len as usize);
3527 println!("Bb =\t{}", converted);
3528 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3529 assert_eq!(converted, message);
3530 println!();
3531
3532 // Normal case for Rijndael-256-256
3533 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3534 print!("K =\t");
3535 for i in 0..32
3536 { print!("{:02X}", key[i]); }
3537 println!();
3538 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
3539 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3540 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
3541
3542 let message = "In the beginning God created the heavens and the earth.";
3543 println!("M =\t{}", message);
3544 let mut cipher = Vec::<u8>::new();
3545 a_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
3546 print!("C =\t");
3547 for c in cipher.clone()
3548 { print!("{:02X} ", c); }
3549 println!();
3550 let mut txt = String::new();
3551 for c in cipher.clone()
3552 { write!(txt, "{:02X} ", c); }
3553 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
3554
3555 let mut recovered = [0; 64];
3556 a_rijndael.decrypt_vec_into_array(iv, &cipher, &mut recovered);
3557 print!("Ba =\t");
3558 for b in recovered.clone()
3559 { print!("{:02X} ", b); }
3560 println!();
3561 let mut txt = String::new();
3562 for c in recovered.clone()
3563 { write!(txt, "{:02X} ", c); }
3564 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
3565
3566 let mut converted = String::new();
3567 unsafe { converted.as_mut_vec() }.write(&recovered);
3568 unsafe { converted.as_mut_vec() }.truncate(len as usize);
3569 println!("Bb =\t{}", converted);
3570 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3571 assert_eq!(converted, message);
3572 println!();
3573
3574 // Normal case for Rijndael-512-512 for post-quantum
3575 use cryptocol::number::SharedArrays;
3576 use cryptocol::hash::SHA3_512;
3577 let mut sha3 = SHA3_512::new();
3578 sha3.absorb_str("Post-quantum");
3579 let key: [u8; 64] = sha3.get_hash_value_in_array();
3580 print!("K =\t");
3581 for i in 0..64
3582 { print!("{:02X}", key[i]); }
3583 println!();
3584 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
3585 sha3.absorb_str("Initialize");
3586 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
3587 iv.src = sha3.get_hash_value_in_array();
3588 let iv = unsafe { iv.des };
3589 print!("IV =\t");
3590 for i in 0..16
3591 { print!("{:08X}", iv[i].to_be()); }
3592 println!();
3593
3594 let message = "In the beginning God created the heavens and the earth.";
3595 println!("M =\t{}", message);
3596 let mut cipher = Vec::<u8>::new();
3597 a_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
3598 print!("C =\t");
3599 for c in cipher.clone()
3600 { print!("{:02X} ", c); }
3601 println!();
3602 let mut txt = String::new();
3603 for c in cipher.clone()
3604 { write!(txt, "{:02X} ", c); }
3605 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
3606
3607 let mut recovered = [0; 64];
3608 a_rijndael.decrypt_vec_into_array(iv, &cipher, &mut recovered);
3609 print!("Ba =\t");
3610 for b in recovered.clone()
3611 { print!("{:02X} ", b); }
3612 println!();
3613 let mut txt = String::new();
3614 for c in recovered.clone()
3615 { write!(txt, "{:02X} ", c); }
3616 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
3617
3618 let mut converted = String::new();
3619 unsafe { converted.as_mut_vec() }.write(&recovered);
3620 unsafe { converted.as_mut_vec() }.truncate(len as usize);
3621 println!("Bb =\t{}", converted);
3622 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3623 assert_eq!(converted, message);
3624 println!("-------------------------------");
3625}4952fn des_decrypt_vec_ofb_into_array()
4953{
4954 println!("des_decrypt_vec_ofb_into_array()");
4955 use std::io::Write;
4956 use std::fmt::Write as _;
4957 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
4958
4959 // Normal case
4960 let key = 0x_1234567890ABCDEF_u64;
4961 println!("K =\t{:#016X}", key);
4962 let mut a_des = DES::new_with_key_u64(key);
4963
4964 let message = "In the beginning God created the heavens and the earth.";
4965 println!("M =\t{}", message);
4966 let iv = 0x_FEDCBA0987654321_u64;
4967 println!("IV = {}", iv);
4968 let mut cipher = Vec::<u8>::new();
4969 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
4970 print!("C (16 rounds) =\t");
4971 for c in cipher.clone()
4972 { print!("{:02X} ", c); }
4973 println!();
4974 let mut txt = String::new();
4975 for c in cipher.clone()
4976 { write!(txt, "{:02X} ", c); }
4977 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
4978
4979 let mut recovered = [0u8; 56];
4980 let len = a_des.decrypt_vec_into_array(iv, &cipher, &mut recovered);
4981 print!("Ba (16 rounds) =\t");
4982 for b in recovered.clone()
4983 { print!("{:02X} ", b); }
4984 println!();
4985 let mut txt = String::new();
4986 for c in recovered.clone()
4987 { write!(txt, "{:02X} ", c); }
4988 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 ");
4989
4990 let mut converted = String::new();
4991 unsafe { converted.as_mut_vec() }.write(&recovered);
4992 unsafe { converted.as_mut_vec() }.truncate(len as usize);
4993 println!("Bb (16 rounds) =\t{}", converted);
4994 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4995 assert_eq!(converted, message);
4996 println!();
4997
4998 // Expanded case for 128 rounds
4999 let key = 0x_1234567890ABCDEF_u64;
5000 println!("K =\t{:#016X}", key);
5001 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
5002
5003 let message = "In the beginning God created the heavens and the earth.";
5004 println!("M =\t{}", message);
5005 let iv = 0x_FEDCBA0987654321_u64;
5006 println!("IV = {}", iv);
5007 let mut cipher = Vec::<u8>::new();
5008 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5009 print!("C (128 rounds) =\t");
5010 for c in cipher.clone()
5011 { print!("{:02X} ", c); }
5012 println!();
5013 let mut txt = String::new();
5014 for c in cipher.clone()
5015 { write!(txt, "{:02X} ", c); }
5016 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
5017
5018 let mut recovered = [0u8; 56];
5019 let len = a_des.decrypt_vec_into_array(iv, &cipher, &mut recovered);
5020 print!("Ba (16 rounds) =\t");
5021 for b in recovered.clone()
5022 { print!("{:02X} ", b); }
5023 println!();
5024 let mut txt = String::new();
5025 for c in recovered.clone()
5026 { write!(txt, "{:02X} ", c); }
5027 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 ");
5028
5029 let mut converted = String::new();
5030 unsafe { converted.as_mut_vec() }.write(&recovered);
5031 unsafe { converted.as_mut_vec() }.truncate(len as usize);
5032 println!("Bb (16 rounds) =\t{}", converted);
5033 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
5034 assert_eq!(converted, message);
5035 println!();
5036
5037 // Expanded case for 0 rounds which means that key is meaningless
5038 let key1 = 0x_1234567890ABCDEF_u64;
5039 let key2 = 0_u64;
5040 println!("K =\t{:#016X}", key);
5041 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
5042 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
5043
5044 let message = "In the beginning God created the heavens and the earth.";
5045 println!("M =\t{}", message);
5046 let iv = 0x_FEDCBA0987654321_u64;
5047 println!("IV = {}", iv);
5048 let mut cipher1 = Vec::<u8>::new();
5049 let mut cipher2 = Vec::<u8>::new();
5050 c_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
5051 d_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
5052 print!("C (0 rounds) =\t");
5053 for c in cipher1.clone()
5054 { print!("{:02X} ", c); }
5055 println!();
5056 let mut txt = String::new();
5057 for c in cipher1.clone()
5058 { write!(txt, "{:02X} ", c); }
5059 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
5060 print!("D (0 rounds) =\t");
5061 for c in cipher2.clone()
5062 { print!("{:02X} ", c); }
5063 println!();
5064 let mut txt = String::new();
5065 for c in cipher2.clone()
5066 { write!(txt, "{:02X} ", c); }
5067 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
5068
5069 let mut recovered1 = [0u8; 56];
5070 let mut recovered2 = [0u8; 56];
5071 let len1 = c_des.decrypt_vec_into_array(iv, &cipher1, &mut recovered1);
5072 let len2 = d_des.decrypt_vec_into_array(iv, &cipher2, &mut recovered2);
5073 print!("B1a (0 rounds) =\t");
5074 for b in recovered1.clone()
5075 { print!("{:02X} ", b); }
5076 println!();
5077 let mut txt = String::new();
5078 for c in recovered1.clone()
5079 { write!(txt, "{:02X} ", c); }
5080 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 ");
5081 print!("B2a (0 rounds) =\t");
5082 for b in recovered2.clone()
5083 { print!("{:02X} ", b); }
5084 println!();
5085 let mut txt = String::new();
5086 for c in recovered.clone()
5087 { write!(txt, "{:02X} ", c); }
5088 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 ");
5089
5090 let mut converted1 = String::new();
5091 let mut converted2 = String::new();
5092 unsafe { converted1.as_mut_vec() }.write(&recovered1);
5093 unsafe { converted2.as_mut_vec() }.write(&recovered2);
5094 unsafe { converted1.as_mut_vec() }.truncate(len1 as usize);
5095 unsafe { converted2.as_mut_vec() }.truncate(len2 as usize);
5096 println!("B1b (0 rounds) =\t{}", converted1);
5097 println!("B2b (0 rounds) =\t{}", converted2);
5098 assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
5099 assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
5100 assert_eq!(converted1, message);
5101 assert_eq!(converted2, message);
5102 assert_eq!(converted1, converted2);
5103 println!();
5104
5105 // Normal case for the message of 0 bytes
5106 let key = 0x_1234567890ABCDEF_u64;
5107 println!("K =\t{:#016X}", key);
5108 let mut a_des = DES::new_with_key_u64(key);
5109
5110 let message = "";
5111 println!("M =\t{}", message);
5112 let iv = 0x_FEDCBA0987654321_u64;
5113 println!("IV = {}", iv);
5114 let mut cipher = Vec::<u8>::new();
5115 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5116 print!("C =\t");
5117 for c in cipher.clone()
5118 { print!("{:02X} ", c); }
5119 println!();
5120 let mut txt = String::new();
5121 for c in cipher.clone()
5122 { write!(txt, "{:02X} ", c); }
5123 assert_eq!(txt, "");
5124
5125 let mut recovered = [0u8; 8];
5126 let len = a_des.decrypt_vec_into_array(iv, &cipher, &mut recovered);
5127
5128 print!("Ba =\t");
5129 for b in recovered.clone()
5130 { print!("{:02X} ", b); }
5131 println!();
5132 let mut txt = String::new();
5133 for c in recovered.clone()
5134 { write!(txt, "{:02X} ", c); }
5135 assert_eq!(txt, "00 00 00 00 00 00 00 00 ");
5136
5137 let mut converted = String::new();
5138 unsafe { converted.as_mut_vec() }.write(&recovered);
5139 unsafe { converted.as_mut_vec() }.truncate(len as usize);
5140 println!("Bb =\t{}", converted);
5141 assert_eq!(converted, "");
5142 assert_eq!(converted, message);
5143 println!();
5144
5145 // Normal case for the message shorter than 8 bytes
5146 let key = 0x_1234567890ABCDEF_u64;
5147 println!("K =\t{:#016X}", key);
5148 let mut a_des = DES::new_with_key_u64(key);
5149
5150 let message = "7 bytes";
5151 println!("M =\t{}", message);
5152 let iv = 0x_FEDCBA0987654321_u64;
5153 println!("IV = {}", iv);
5154 let mut cipher = Vec::<u8>::new();
5155 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5156 print!("C =\t");
5157 for c in cipher.clone()
5158 { print!("{:02X} ", c); }
5159 println!();
5160 let mut txt = String::new();
5161 for c in cipher.clone()
5162 { write!(txt, "{:02X} ", c); }
5163 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
5164
5165 let mut recovered = [0u8; 8];
5166 let len = a_des.decrypt_vec_into_array(iv, &cipher, &mut recovered);
5167
5168 print!("Ba =\t");
5169 for b in recovered.clone()
5170 { print!("{:02X} ", b); }
5171 println!();
5172 let mut txt = String::new();
5173 for c in recovered.clone()
5174 { write!(txt, "{:02X} ", c); }
5175 assert_eq!(txt, "37 20 62 79 74 65 73 00 ");
5176
5177 let mut converted = String::new();
5178 unsafe { converted.as_mut_vec() }.write(&recovered);
5179 unsafe { converted.as_mut_vec() }.truncate(len as usize);
5180 println!("Bb =\t{}", converted);
5181 assert_eq!(converted, "7 bytes");
5182 assert_eq!(converted, message);
5183 println!();
5184
5185 // Normal case for the message of 8 bytes
5186 let key = 0x_1234567890ABCDEF_u64;
5187 println!("K =\t{:#016X}", key);
5188 let mut a_des = DES::new_with_key_u64(key);
5189
5190 let message = "I am OK.";
5191 println!("M =\t{}", message);
5192 let iv = 0x_FEDCBA0987654321_u64;
5193 println!("IV = {}", iv);
5194 let mut cipher = Vec::<u8>::new();
5195 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5196 print!("C =\t");
5197 for c in cipher.clone()
5198 { print!("{:02X} ", c); }
5199 println!();
5200 let mut txt = String::new();
5201 for c in cipher.clone()
5202 { write!(txt, "{:02X} ", c); }
5203 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
5204
5205 let mut recovered = [0u8; 16];
5206 let len = a_des.decrypt_vec_into_array(iv, &cipher, &mut recovered);
5207
5208 print!("Ba =\t");
5209 for b in recovered.clone()
5210 { print!("{:02X} ", b); }
5211 println!();
5212 let mut txt = String::new();
5213 for c in recovered.clone()
5214 { write!(txt, "{:02X} ", c); }
5215 assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E 00 00 00 00 00 00 00 00 ");
5216
5217 let mut converted = String::new();
5218 unsafe { converted.as_mut_vec() }.write(&recovered);
5219 unsafe { converted.as_mut_vec() }.truncate(len as usize);
5220 println!("Bb =\t{}", converted);
5221 assert_eq!(converted, "I am OK.");
5222 assert_eq!(converted, message);
5223 println!();
5224
5225 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
5226 let key = 0x_1234567890ABCDEF_u64;
5227 println!("K =\t{:#016X}", key);
5228 let mut a_des = DES::new_with_key_u64(key);
5229
5230 let message = "PARK Youngho";
5231 println!("M =\t{}", message);
5232 let iv = 0x_FEDCBA0987654321_u64;
5233 println!("IV = {}", iv);
5234 let mut cipher = Vec::<u8>::new();
5235 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5236 print!("C =\t");
5237 for c in cipher.clone()
5238 { print!("{:02X} ", c); }
5239 println!();
5240 let mut txt = String::new();
5241 for c in cipher.clone()
5242 { write!(txt, "{:02X} ", c); }
5243 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
5244
5245 let mut recovered = [0u8; 16];
5246 let len = a_des.decrypt_vec_into_array(iv, &cipher, &mut recovered);
5247
5248 print!("Ba =\t");
5249 for b in recovered.clone()
5250 { print!("{:02X} ", b); }
5251 println!();
5252 let mut txt = String::new();
5253 for c in recovered.clone()
5254 { write!(txt, "{:02X} ", c); }
5255 assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F 00 00 00 00 ");
5256
5257 let mut converted = String::new();
5258 unsafe { converted.as_mut_vec() }.write(&recovered);
5259 unsafe { converted.as_mut_vec() }.truncate(len as usize);
5260 println!("Bb =\t{}", converted);
5261 assert_eq!(converted, "PARK Youngho");
5262 assert_eq!(converted, message);
5263 println!();
5264
5265 // Normal case for the message of 16 bytes
5266 let key = 0x_1234567890ABCDEF_u64;
5267 println!("K =\t{:#016X}", key);
5268 let mut a_des = DES::new_with_key_u64(key);
5269
5270 let message = "고맙습니다.";
5271 println!("M =\t{}", message);
5272 let iv = 0x_FEDCBA0987654321_u64;
5273 println!("IV = {}", iv);
5274 let mut cipher = Vec::<u8>::new();
5275 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5276 print!("C =\t");
5277 for c in cipher.clone()
5278 { print!("{:02X} ", c); }
5279 println!();
5280 let mut txt = String::new();
5281 for c in cipher.clone()
5282 { write!(txt, "{:02X} ", c); }
5283 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
5284
5285 let mut recovered = [0u8; 24];
5286 let len = a_des.decrypt_vec_into_array(iv, &cipher, &mut recovered);
5287
5288 print!("Ba =\t");
5289 for b in recovered.clone()
5290 { print!("{:02X} ", b); }
5291 println!();
5292 let mut txt = String::new();
5293 for c in recovered.clone()
5294 { write!(txt, "{:02X} ", c); }
5295 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 ");
5296
5297 let mut converted = String::new();
5298 unsafe { converted.as_mut_vec() }.write(&recovered);
5299 unsafe { converted.as_mut_vec() }.truncate(len as usize);
5300 println!("Bb =\t{}", converted);
5301 assert_eq!(converted, "고맙습니다.");
5302 assert_eq!(converted, message);
5303 println!("-------------------------------");
5304}Sourcefn decrypt_vec_into_string<U>(
&mut self,
iv: T,
cipher: &Vec<U>,
message: &mut String,
) -> u64
fn decrypt_vec_into_string<U>( &mut self, iv: T, cipher: &Vec<U>, message: &mut String, ) -> u64
Decrypts the data in str without any padding in OFB (Output FeedBack)
mode, and stores the decrypted data in String.
§Arguments
ivis an initialization vector for OFB mode.cipheris an immutable reference toVec<U>object, and is the place where the ciphertext to be decrypted is stored.messageis a mutable reference to aStringobject, and is the place where the decrypted data will be stored.
§Output
- This method returns the size of plaintext in bytes.
- If this method failed in decryption, it always returns
zero. - Even if this method succeeded in decryption, it returns
zerowhen the original plaintext is zero-length empty data. Then, you will have to check whether or not it failed by using the methodis_successful()oris_failed(). - If
size_of::<U>()*cipher.len()is greater than0(which means that the original plaintext is surely not empty data) and it returnszero, you can interpret it that this method surely failed in decryption.
§Features
- 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 Rijndael or AES, and its variants
§Example 1 for AES-128
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ AES_128, CBC_ISO };
let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
println!("K =\t{:#016X}", key);
let mut a_aes = AES_128::new_with_key_u128(key);
let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
print!("C =\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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 07 AF 4E C8 EF 5D 0A 74 97 3F 90 9E 05 9E 9E 32 FB 55 54 45 7C ED A2 2B F8 07 66 C0 7B CB 98 F3 BF 93 15 BA 26 1C 47 ");
let mut converted= String::new();
a_aes.decrypt_vec_into_string(iv, &cipher, &mut converted);
println!("B =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");
assert_eq!(converted, message);§For more examples,
click here
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, OFB };
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 iv = 0x_FEDCBA0987654321_u64;
println!("IV = {}", iv);
let mut cipher = Vec::<u8>::new();
a_des.encrypt_str_into_vec(iv, &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, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
let mut recovered = String::new();
a_des.decrypt_vec_into_string(iv, &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
§For BigCryptor128
§Example 1 for TAES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
+ AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
+ AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
println!("IV = {:#034X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
taes.encrypt_str_into_vec(iv, &message, &mut cipher);
print!("C =\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, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
let mut recovered = String::new();
taes.decrypt_vec_into_string(iv, &cipher, &mut recovered);
println!("B =\t{}", recovered);
assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
assert_eq!(recovered, message);§For more examples,
click here
§For BigCryptor64
§Example 1 for TDES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
+ DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
+ DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
let iv = 0x_FEDCBA0987654321_u64;
println!("IV = {:#018X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = Vec::<u8>::new();
tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
print!("C =\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, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
let mut recovered = String::new();
tdes.decrypt_vec_into_string(iv, &cipher, &mut recovered);
println!("B =\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?
787fn bigcryptor64_decrypt_vec_ofb_into_string()
788{
789 println!("bigcryptor64_decrypt_vec_ofb_into_string()");
790 use std::io::Write;
791 use std::fmt::Write as _;
792 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
793
794 // TDES case
795 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
796 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
797 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
798 let iv = 0x_FEDCBA0987654321_u64;
799 println!("IV = {:#018X}", iv);
800 let message = "In the beginning God created the heavens and the earth.";
801 println!("M =\t{}", message);
802 let mut cipher = Vec::<u8>::new();
803 tdes.encrypt_str_into_vec(iv, &message, &mut cipher);
804 print!("C =\t");
805 for c in cipher.clone()
806 { print!("{:02X} ", c); }
807 println!();
808 let mut txt = String::new();
809 for c in cipher.clone()
810 { write!(txt, "{:02X} ", c); }
811 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
812
813 let mut recovered = String::new();
814 tdes.decrypt_vec_into_string(iv, &cipher, &mut recovered);
815 println!("B =\t{}", recovered);
816 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
817 assert_eq!(recovered, message);
818 println!("-------------------------------");
819}More examples
787fn bigcryptor128_decrypt_vec_ofb_into_string()
788{
789 println!("bigcryptor128_decrypt_vec_ofb_into_string()");
790 use std::io::Write;
791 use std::fmt::Write as _;
792 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
793
794 // TAES_128 case
795 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
796 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
797 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
798 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
799 println!("IV = {:#034X}", iv);
800 let message = "In the beginning God created the heavens and the earth.";
801 println!("M =\t{}", message);
802 let mut cipher = Vec::<u8>::new();
803 taes.encrypt_str_into_vec(iv, &message, &mut cipher);
804 print!("C =\t");
805 for c in cipher.clone()
806 { print!("{:02X} ", c); }
807 println!();
808 let mut txt = String::new();
809 for c in cipher.clone()
810 { write!(txt, "{:02X} ", c); }
811 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
812
813 let mut recovered = String::new();
814 taes.decrypt_vec_into_string(iv, &cipher, &mut recovered);
815 println!("B =\t{}", recovered);
816 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
817 assert_eq!(recovered, message);
818 println!("-------------------------------");
819}3627fn aes_decrypt_vec_ofb_into_string()
3628{
3629 println!("aes_decrypt_vec_ofb_into_string()");
3630 use std::io::Write;
3631 use std::fmt::Write as _;
3632 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
3633
3634 // Normal case for AES-128
3635 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
3636 println!("K =\t{:#016X}", key);
3637 let mut a_aes = AES_128::new_with_key_u128(key);
3638 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3639 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3640
3641 let message = "In the beginning God created the heavens and the earth.";
3642 println!("M =\t{}", message);
3643 let mut cipher = Vec::<u8>::new();
3644 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3645 print!("C =\t");
3646 for c in cipher.clone()
3647 { print!("{:02X} ", c); }
3648 println!();
3649 let mut txt = String::new();
3650 for c in cipher.clone()
3651 { write!(txt, "{:02X} ", c); }
3652 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
3653
3654 let mut converted= String::new();
3655 a_aes.decrypt_vec_into_string(iv, &cipher, &mut converted);
3656 println!("B =\t{}", converted);
3657 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3658 assert_eq!(converted, message);
3659 println!();
3660
3661 // Normal case for AES-192
3662 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3663 print!("K =\t");
3664 for i in 0..24
3665 { print!("{:02X}", key[i]); }
3666 println!();
3667 let mut a_aes = AES_192::new_with_key(&key);
3668 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3669 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3670
3671 let message = "In the beginning God created the heavens and the earth.";
3672 println!("M =\t{}", message);
3673 let mut cipher = Vec::<u8>::new();
3674 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3675 print!("C =\t");
3676 for c in cipher.clone()
3677 { print!("{:02X} ", c); }
3678 println!();
3679 let mut txt = String::new();
3680 for c in cipher.clone()
3681 { write!(txt, "{:02X} ", c); }
3682 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
3683
3684 let mut converted= String::new();
3685 a_aes.decrypt_vec_into_string(iv, &cipher, &mut converted);
3686 println!("B =\t{}", converted);
3687 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3688 assert_eq!(converted, message);
3689 println!();
3690
3691 // Normal case for AES-256
3692 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3693 print!("K =\t");
3694 for i in 0..32
3695 { print!("{:02X}", key[i]); }
3696 println!();
3697 let mut a_aes = AES_256::new_with_key(&key);
3698 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3699 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3700
3701 let message = "In the beginning God created the heavens and the earth.";
3702 println!("M =\t{}", message);
3703 let mut cipher = Vec::<u8>::new();
3704 a_aes.encrypt_str_into_vec(iv, &message, &mut cipher);
3705 print!("C =\t");
3706 for c in cipher.clone()
3707 { print!("{:02X} ", c); }
3708 println!();
3709 let mut txt = String::new();
3710 for c in cipher.clone()
3711 { write!(txt, "{:02X} ", c); }
3712 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
3713
3714 let mut converted= String::new();
3715 a_aes.decrypt_vec_into_string(iv, &cipher, &mut converted);
3716 println!("B =\t{}", converted);
3717 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3718 assert_eq!(converted, message);
3719 println!();
3720
3721 // Normal case for Rijndael-256-256
3722 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3723 print!("K =\t");
3724 for i in 0..32
3725 { print!("{:02X}", key[i]); }
3726 println!();
3727 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
3728 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3729 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
3730
3731 let message = "In the beginning God created the heavens and the earth.";
3732 println!("M =\t{}", message);
3733 let mut cipher = Vec::<u8>::new();
3734 a_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
3735 print!("C =\t");
3736 for c in cipher.clone()
3737 { print!("{:02X} ", c); }
3738 println!();
3739 let mut txt = String::new();
3740 for c in cipher.clone()
3741 { write!(txt, "{:02X} ", c); }
3742 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
3743
3744 let mut converted= String::new();
3745 a_rijndael.decrypt_vec_into_string(iv, &cipher, &mut converted);
3746 println!("B =\t{}", converted);
3747 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3748 assert_eq!(converted, message);
3749 println!();
3750
3751 // Normal case for Rijndael-512-512 for post-quantum
3752 use cryptocol::number::SharedArrays;
3753 use cryptocol::hash::SHA3_512;
3754 let mut sha3 = SHA3_512::new();
3755 sha3.absorb_str("Post-quantum");
3756 let key: [u8; 64] = sha3.get_hash_value_in_array();
3757 print!("K =\t");
3758 for i in 0..64
3759 { print!("{:02X}", key[i]); }
3760 println!();
3761 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
3762 sha3.absorb_str("Initialize");
3763 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
3764 iv.src = sha3.get_hash_value_in_array();
3765 let iv = unsafe { iv.des };
3766 print!("IV =\t");
3767 for i in 0..16
3768 { print!("{:08X}", iv[i].to_be()); }
3769 println!();
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_rijndael.encrypt_str_into_vec(iv, &message, &mut cipher);
3775 print!("C =\t");
3776 for c in cipher.clone()
3777 { print!("{:02X} ", c); }
3778 println!();
3779 let mut txt = String::new();
3780 for c in cipher.clone()
3781 { write!(txt, "{:02X} ", c); }
3782 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
3783
3784 let mut converted= String::new();
3785 a_rijndael.decrypt_vec_into_string(iv, &cipher, &mut converted);
3786 println!("B =\t{}", converted);
3787 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3788 assert_eq!(converted, message);
3789 println!("-------------------------------");
3790}5306fn des_decrypt_vec_ofb_into_string()
5307{
5308 println!("des_decrypt_vec_ofb_into_string()");
5309 use std::io::Write;
5310 use std::fmt::Write as _;
5311 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
5312
5313 // Normal case
5314 let key = 0x_1234567890ABCDEF_u64;
5315 println!("K =\t{:#016X}", key);
5316 let mut a_des = DES::new_with_key_u64(key);
5317
5318 let message = "In the beginning God created the heavens and the earth.";
5319 println!("M =\t{}", message);
5320 let iv = 0x_FEDCBA0987654321_u64;
5321 println!("IV = {}", iv);
5322 let mut cipher = Vec::<u8>::new();
5323 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5324 print!("C (16 rounds) =\t");
5325 for c in cipher.clone()
5326 { print!("{:02X} ", c); }
5327 println!();
5328 let mut txt = String::new();
5329 for c in cipher.clone()
5330 { write!(txt, "{:02X} ", c); }
5331 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
5332
5333 let mut recovered = String::new();
5334 a_des.decrypt_vec_into_string(iv, &cipher, &mut recovered);
5335 println!("B (16 rounds) =\t{}", recovered);
5336 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
5337 assert_eq!(recovered, message);
5338 println!();
5339
5340 // Expanded case for 128 rounds
5341 let key = 0x_1234567890ABCDEF_u64;
5342 println!("K =\t{:#016X}", key);
5343 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
5344
5345 let message = "In the beginning God created the heavens and the earth.";
5346 println!("M =\t{}", message);
5347 let iv = 0x_FEDCBA0987654321_u64;
5348 println!("IV = {}", iv);
5349 let mut cipher = Vec::<u8>::new();
5350 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5351 print!("C (128 rounds) =\t");
5352 for c in cipher.clone()
5353 { print!("{:02X} ", c); }
5354 println!();
5355 let mut txt = String::new();
5356 for c in cipher.clone()
5357 { write!(txt, "{:02X} ", c); }
5358 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
5359
5360 let mut recovered = String::new();
5361 a_des.decrypt_vec_into_string(iv, &cipher, &mut recovered);
5362 println!("B (128 rounds) =\t{}", recovered);
5363 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
5364 assert_eq!(recovered, message);
5365 println!();
5366
5367 // Expanded case for 0 rounds which means that key is meaningless
5368 let key1 = 0x_1234567890ABCDEF_u64;
5369 let key2 = 0_u64;
5370 println!("K =\t{:#016X}", key);
5371 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
5372 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
5373
5374 let message = "In the beginning God created the heavens and the earth.";
5375 println!("M =\t{}", message);
5376 let iv = 0x_FEDCBA0987654321_u64;
5377 println!("IV = {}", iv);
5378 let mut cipher1 = Vec::<u8>::new();
5379 let mut cipher2 = Vec::<u8>::new();
5380 c_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
5381 d_des.encrypt_into_vec(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
5382 print!("C (0 rounds) =\t");
5383 for c in cipher1.clone()
5384 { print!("{:02X} ", c); }
5385 println!();
5386 let mut txt = String::new();
5387 for c in cipher1.clone()
5388 { write!(txt, "{:02X} ", c); }
5389 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
5390 print!("D (0 rounds) =\t");
5391 for c in cipher2.clone()
5392 { print!("{:02X} ", c); }
5393 println!();
5394 let mut txt = String::new();
5395 for c in cipher2.clone()
5396 { write!(txt, "{:02X} ", c); }
5397 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
5398
5399 let mut recovered1 = String::new();
5400 let mut recovered2 = String::new();
5401 c_des.decrypt_vec_into_string(iv, &cipher1, &mut recovered1);
5402 d_des.decrypt_vec_into_string(iv, &cipher2, &mut recovered2);
5403 println!("B1 (0 rounds) =\t{}", recovered1);
5404 println!("B2 (0 rounds) =\t{}", recovered2);
5405 assert_eq!(recovered1, "In the beginning God created the heavens and the earth.");
5406 assert_eq!(recovered2, "In the beginning God created the heavens and the earth.");
5407 assert_eq!(recovered1, message);
5408 assert_eq!(recovered2, message);
5409 assert_eq!(recovered1, recovered2);
5410 println!();
5411
5412 // Normal case for the message of 0 bytes
5413 let key = 0x_1234567890ABCDEF_u64;
5414 println!("K =\t{:#016X}", key);
5415 let mut a_des = DES::new_with_key_u64(key);
5416
5417 let message = "";
5418 println!("M =\t{}", message);
5419 let iv = 0x_FEDCBA0987654321_u64;
5420 println!("IV = {}", iv);
5421 let mut cipher = Vec::<u8>::new();
5422 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5423 print!("C =\t");
5424 for c in cipher.clone()
5425 { print!("{:02X} ", c); }
5426 println!();
5427 let mut txt = String::new();
5428 for c in cipher.clone()
5429 { write!(txt, "{:02X} ", c); }
5430 assert_eq!(txt, "");
5431
5432 let mut recovered = String::new();
5433 a_des.decrypt_vec_into_string(iv, &cipher, &mut recovered);
5434 println!("B =\t{}", recovered);
5435 assert_eq!(recovered, "");
5436 assert_eq!(recovered, message);
5437 println!();
5438
5439 // Normal case for the message shorter than 8 bytes
5440 let key = 0x_1234567890ABCDEF_u64;
5441 println!("K =\t{:#016X}", key);
5442 let mut a_des = DES::new_with_key_u64(key);
5443
5444 let message = "7 bytes";
5445 println!("M =\t{}", message);
5446 let iv = 0x_FEDCBA0987654321_u64;
5447 println!("IV = {}", iv);
5448 let mut cipher = Vec::<u8>::new();
5449 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5450 print!("C =\t");
5451 for c in cipher.clone()
5452 { print!("{:02X} ", c); }
5453 println!();
5454 let mut txt = String::new();
5455 for c in cipher.clone()
5456 { write!(txt, "{:02X} ", c); }
5457 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
5458
5459 let mut recovered = String::new();
5460 a_des.decrypt_vec_into_string(iv, &cipher, &mut recovered);
5461 println!("B =\t{}", recovered);
5462 assert_eq!(recovered, "7 bytes");
5463 assert_eq!(recovered, message);
5464 println!();
5465
5466 // Normal case for the message of 8 bytes
5467 let key = 0x_1234567890ABCDEF_u64;
5468 println!("K =\t{:#016X}", key);
5469 let mut a_des = DES::new_with_key_u64(key);
5470
5471 let message = "I am OK.";
5472 println!("M =\t{}", message);
5473 let iv = 0x_FEDCBA0987654321_u64;
5474 println!("IV = {}", iv);
5475 let mut cipher = Vec::<u8>::new();
5476 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5477 print!("C =\t");
5478 for c in cipher.clone()
5479 { print!("{:02X} ", c); }
5480 println!();
5481 let mut txt = String::new();
5482 for c in cipher.clone()
5483 { write!(txt, "{:02X} ", c); }
5484 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
5485
5486 let mut recovered = String::new();
5487 a_des.decrypt_vec_into_string(iv, &cipher, &mut recovered);
5488 println!("B =\t{}", recovered);
5489 assert_eq!(recovered, "I am OK.");
5490 assert_eq!(recovered, message);
5491 println!();
5492
5493 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
5494 let key = 0x_1234567890ABCDEF_u64;
5495 println!("K =\t{:#016X}", key);
5496 let mut a_des = DES::new_with_key_u64(key);
5497
5498 let message = "PARK Youngho";
5499 println!("M =\t{}", message);
5500 let iv = 0x_FEDCBA0987654321_u64;
5501 println!("IV = {}", iv);
5502 let mut cipher = Vec::<u8>::new();
5503 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5504 print!("C =\t");
5505 for c in cipher.clone()
5506 { print!("{:02X} ", c); }
5507 println!();
5508 let mut txt = String::new();
5509 for c in cipher.clone()
5510 { write!(txt, "{:02X} ", c); }
5511 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
5512
5513 let mut recovered = String::new();
5514 a_des.decrypt_vec_into_string(iv, &cipher, &mut recovered);
5515 println!("B =\t{}", recovered);
5516 assert_eq!(recovered, "PARK Youngho");
5517 assert_eq!(recovered, message);
5518 println!();
5519
5520 // Normal case for the message of 16 bytes
5521 let key = 0x_1234567890ABCDEF_u64;
5522 println!("K =\t{:#016X}", key);
5523 let mut a_des = DES::new_with_key_u64(key);
5524
5525 let message = "고맙습니다.";
5526 println!("M =\t{}", message);
5527 let iv = 0x_FEDCBA0987654321_u64;
5528 println!("IV = {}", iv);
5529 let mut cipher = Vec::<u8>::new();
5530 a_des.encrypt_str_into_vec(iv, &message, &mut cipher);
5531 print!("C =\t");
5532 for c in cipher.clone()
5533 { print!("{:02X} ", c); }
5534 println!();
5535 let mut txt = String::new();
5536 for c in cipher.clone()
5537 { write!(txt, "{:02X} ", c); }
5538 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
5539
5540 let mut recovered = String::new();
5541 a_des.decrypt_vec_into_string(iv, &cipher, &mut recovered);
5542 println!("B =\t{}", recovered);
5543 assert_eq!(recovered, "고맙습니다.");
5544 assert_eq!(recovered, message);
5545 println!("-------------------------------");
5546}Sourcefn decrypt_array<U, const N: usize>(
&mut self,
iv: T,
cipher: &[U; N],
message: *mut u8,
) -> u64
fn decrypt_array<U, const N: usize>( &mut self, iv: T, cipher: &[U; N], message: *mut u8, ) -> u64
Decrypts the data stored in an array [U; N] object without any padding
in OFB (Output FeedBack) mode.
§Arguments
ivis an initialization vector for OFB mode.cipheris an immutable reference to an array[U; N]object, and is the place where the plaintext to be decrypted is stored.messageis a mutable pointer tou8which is*mut u8, and is the place where the decrypted data will be stored.
§Output
- This method returns the size of plaintext in bytes.
- If this method failed in decryption, it always returns
zero. - Even if this method succeeded in decryption, it returns
zerowhen the original plaintext is zero-length empty data. Then, you will have to check whether or not it failed by using the methodis_successful()oris_failed(). - If
size_of::<U>()*Nis greater than0(which means that the original plaintext is surely not empty data) and it returnszero, you can interpret it that this method surely failed in decryption.
§Features
- You are not encouraged to use this method in pure Rust programming. Instead, use other safer methods such as decrypt_array_into_*().
- This method is useful to use in hybrid programming with C/C++.
- The size of the memory area which starts at
messageis assumed to be enough to store the plaintext. So, it is responsible for you to prepare themessagearea big enough! - The size of the area for plaintext does not have to be prepared more
than
size_of::<U>()*N. - If the size of the area for plaintext is prepared more than
size_of::<U>()*N, the rest of the area will be filled with0s.
§For Rijndael or AES, and its variants
§Example 1 for AES-128
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ AES_128, CBC_ISO };
let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
println!("K =\t{:#016X}", key);
let mut a_aes = AES_128::new_with_key_u128(key);
let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = [0_u8; 55];
a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
print!("C =\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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 07 AF 4E C8 EF 5D 0A 74 97 3F 90 9E 05 9E 9E 32 FB 55 54 45 7C ED A2 2B F8 07 66 C0 7B CB 98 F3 BF 93 15 BA 26 1C 47 ");
println!();
let mut recovered = vec![0; 55];
a_aes.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
print!("Ba =\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 =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");
assert_eq!(converted, message);§For more examples,
click here
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, OFB };
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 iv = 0x_FEDCBA0987654321_u64;
println!("IV = {}", iv);
let mut cipher = [0_u8; 55];
a_des.encrypt_into_array(iv, 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, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
let mut recovered = vec![0; 55];
let len = a_des.decrypt_array(iv, &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
§For BigCryptor128
§Example 1 for TAES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
+ AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
+ AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
println!("IV = {:#034X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message); let mut cipher = [0_u8; 55];
taes.encrypt_str_into_array(iv, &message, &mut cipher);
print!("C =\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, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
let mut recovered = vec![0; 55];
let len = taes.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
recovered.truncate(len as usize);
print!("Ba =\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 =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");
assert_eq!(converted, message);§For more examples,
click here
§For BigCryptor64
§Example 1 for TDES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
+ DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
+ DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
let iv = 0x_FEDCBA0987654321_u64;
println!("IV = {:#018X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message); let mut cipher = [0_u8; 55];
tdes.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
print!("C =\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, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
let mut recovered = vec![0; 55];
let len = tdes.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
recovered.truncate(len as usize);
print!("Ba =\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 =\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?
821fn bigcryptor64_decrypt_array_ofb()
822{
823 println!("bigcryptor64_decrypt_array_ofb()");
824 use std::io::Write;
825 use std::fmt::Write as _;
826 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
827
828 // TDES case
829 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
830 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
831 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
832 let iv = 0x_FEDCBA0987654321_u64;
833 println!("IV = {:#018X}", iv);
834 let message = "In the beginning God created the heavens and the earth.";
835 println!("M =\t{}", message); let mut cipher = [0_u8; 55];
836 tdes.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
837 print!("C =\t");
838 for c in cipher.clone()
839 { print!("{:02X} ", c); }
840 println!();
841 let mut txt = String::new();
842 for c in cipher.clone()
843 { write!(txt, "{:02X} ", c); }
844 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
845
846 let mut recovered = vec![0; 55];
847 let len = tdes.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
848 recovered.truncate(len as usize);
849 print!("Ba =\t");
850 for b in recovered.clone()
851 { print!("{:02X} ", b); }
852 println!();
853 let mut txt = String::new();
854 for c in recovered.clone()
855 { write!(txt, "{:02X} ", c); }
856 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 ");
857
858 let mut converted = String::new();
859 unsafe { converted.as_mut_vec() }.append(&mut recovered);
860
861 println!("Bb =\t{}", converted);
862 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
863 assert_eq!(converted, message);
864 println!("-------------------------------");
865}More examples
821fn bigcryptor128_decrypt_array_ofb()
822{
823 println!("bigcryptor128_decrypt_array_ofb()");
824 use std::io::Write;
825 use std::fmt::Write as _;
826 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
827
828 // TAES_128 case
829 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
830 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
831 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
832 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
833 println!("IV = {:#034X}", iv);
834 let message = "In the beginning God created the heavens and the earth.";
835 println!("M =\t{}", message); let mut cipher = [0_u8; 55];
836 taes.encrypt_str_into_array(iv, &message, &mut cipher);
837 print!("C =\t");
838 for c in cipher.clone()
839 { print!("{:02X} ", c); }
840 println!();
841 let mut txt = String::new();
842 for c in cipher.clone()
843 { write!(txt, "{:02X} ", c); }
844 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
845
846 let mut recovered = vec![0; 55];
847 let len = taes.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
848 recovered.truncate(len as usize);
849 print!("Ba =\t");
850 for b in recovered.clone()
851 { print!("{:02X} ", b); }
852 println!();
853 let mut txt = String::new();
854 for c in recovered.clone()
855 { write!(txt, "{:02X} ", c); }
856 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 ");
857
858 let mut converted = String::new();
859 unsafe { converted.as_mut_vec() }.append(&mut recovered);
860
861 println!("Bb =\t{}", converted);
862 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
863 assert_eq!(converted, message);
864 println!("-------------------------------");
865}3792fn aes_decrypt_array_ofb()
3793{
3794 println!("aes_decrypt_array_ofb()");
3795 use std::io::Write;
3796 use std::fmt::Write as _;
3797 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
3798
3799 // Normal case for AES-128
3800 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
3801 println!("K =\t{:#016X}", key);
3802 let mut a_aes = AES_128::new_with_key_u128(key);
3803 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3804 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3805
3806 let message = "In the beginning God created the heavens and the earth.";
3807 println!("M =\t{}", message);
3808 let mut cipher = [0_u8; 55];
3809 a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
3810 print!("C =\t");
3811 for c in cipher.clone()
3812 { print!("{:02X} ", c); }
3813 println!();
3814 let mut txt = String::new();
3815 for c in cipher.clone()
3816 { write!(txt, "{:02X} ", c); }
3817 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
3818
3819 let mut recovered = vec![0; 55];
3820 a_aes.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
3821 print!("Ba =\t");
3822 for b in recovered.clone()
3823 { print!("{:02X} ", b); }
3824 println!();
3825 let mut txt = String::new();
3826 for c in recovered.clone()
3827 { write!(txt, "{:02X} ", c); }
3828 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 ");
3829
3830 let mut converted = String::new();
3831 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3832
3833 println!("Bb =\t{}", converted);
3834 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3835 assert_eq!(converted, message);
3836 println!();
3837
3838 // Normal case for AES-192
3839 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3840 print!("K =\t");
3841 for i in 0..24
3842 { print!("{:02X}", key[i]); }
3843 println!();
3844 let mut a_aes = AES_192::new_with_key(&key);
3845 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3846 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3847
3848 let message = "In the beginning God created the heavens and the earth.";
3849 println!("M =\t{}", message);
3850 let mut cipher = [0_u8; 55];
3851 a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
3852 print!("C =\t");
3853 for c in cipher.clone()
3854 { print!("{:02X} ", c); }
3855 println!();
3856 let mut txt = String::new();
3857 for c in cipher.clone()
3858 { write!(txt, "{:02X} ", c); }
3859 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
3860
3861 let mut recovered = vec![0; 55];
3862 a_aes.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
3863 print!("Ba =\t");
3864 for b in recovered.clone()
3865 { print!("{:02X} ", b); }
3866 println!();
3867 let mut txt = String::new();
3868 for c in recovered.clone()
3869 { write!(txt, "{:02X} ", c); }
3870 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 ");
3871
3872 let mut converted = String::new();
3873 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3874
3875 println!("Bb =\t{}", converted);
3876 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3877 assert_eq!(converted, message);
3878 println!();
3879
3880 // Normal case for AES-256
3881 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3882 print!("K =\t");
3883 for i in 0..32
3884 { print!("{:02X}", key[i]); }
3885 println!();
3886 let mut a_aes = AES_256::new_with_key(&key);
3887 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3888 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
3889
3890 let message = "In the beginning God created the heavens and the earth.";
3891 println!("M =\t{}", message);
3892 let mut cipher = [0_u8; 55];
3893 a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
3894 print!("C =\t");
3895 for c in cipher.clone()
3896 { print!("{:02X} ", c); }
3897 println!();
3898 let mut txt = String::new();
3899 for c in cipher.clone()
3900 { write!(txt, "{:02X} ", c); }
3901 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
3902
3903 let mut recovered = vec![0; 55];
3904 a_aes.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
3905 print!("Ba =\t");
3906 for b in recovered.clone()
3907 { print!("{:02X} ", b); }
3908 println!();
3909 let mut txt = String::new();
3910 for c in recovered.clone()
3911 { write!(txt, "{:02X} ", c); }
3912 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 ");
3913
3914 let mut converted = String::new();
3915 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3916
3917 println!("Bb =\t{}", converted);
3918 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3919 assert_eq!(converted, message);
3920 println!();
3921
3922 // Normal case for Rijndael-256-256
3923 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
3924 print!("K =\t");
3925 for i in 0..32
3926 { print!("{:02X}", key[i]); }
3927 println!();
3928 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
3929 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
3930 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
3931
3932 let message = "In the beginning God created the heavens and the earth.";
3933 println!("M =\t{}", message);
3934 let mut cipher = [0_u8; 55];
3935 a_rijndael.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
3936 print!("C =\t");
3937 for c in cipher.clone()
3938 { print!("{:02X} ", c); }
3939 println!();
3940 let mut txt = String::new();
3941 for c in cipher.clone()
3942 { write!(txt, "{:02X} ", c); }
3943 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
3944
3945 let mut recovered = vec![0; 55];
3946 a_rijndael.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
3947 print!("Ba =\t");
3948 for b in recovered.clone()
3949 { print!("{:02X} ", b); }
3950 println!();
3951 let mut txt = String::new();
3952 for c in recovered.clone()
3953 { write!(txt, "{:02X} ", c); }
3954 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 ");
3955
3956 let mut converted = String::new();
3957 unsafe { converted.as_mut_vec() }.append(&mut recovered);
3958
3959 println!("Bb =\t{}", converted);
3960 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
3961 assert_eq!(converted, message);
3962 println!();
3963
3964 // Normal case for Rijndael-512-512 for post-quantum
3965 use cryptocol::number::SharedArrays;
3966 use cryptocol::hash::SHA3_512;
3967 let mut sha3 = SHA3_512::new();
3968 sha3.absorb_str("Post-quantum");
3969 let key: [u8; 64] = sha3.get_hash_value_in_array();
3970 print!("K =\t");
3971 for i in 0..64
3972 { print!("{:02X}", key[i]); }
3973 println!();
3974 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
3975 sha3.absorb_str("Initialize");
3976 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
3977 iv.src = sha3.get_hash_value_in_array();
3978 let iv = unsafe { iv.des };
3979 print!("IV =\t");
3980 for i in 0..16
3981 { print!("{:08X}", iv[i].to_be()); }
3982 println!();
3983 let message = "In the beginning God created the heavens and the earth.";
3984 println!("M =\t{}", message);
3985 let mut cipher = [0_u8; 55];
3986 a_rijndael.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
3987 print!("C =\t");
3988 for c in cipher.clone()
3989 { print!("{:02X} ", c); }
3990 println!();
3991 let mut txt = String::new();
3992 for c in cipher.clone()
3993 { write!(txt, "{:02X} ", c); }
3994 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
3995
3996 let mut recovered = vec![0; 55];
3997 a_rijndael.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
3998 print!("Ba =\t");
3999 for b in recovered.clone()
4000 { print!("{:02X} ", b); }
4001 println!();
4002 let mut txt = String::new();
4003 for c in recovered.clone()
4004 { write!(txt, "{:02X} ", c); }
4005 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 ");
4006
4007 let mut converted = String::new();
4008 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4009
4010 println!("Bb =\t{}", converted);
4011 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4012 assert_eq!(converted, message);
4013 println!("-------------------------------");
4014}5548fn des_decrypt_array_ofb()
5549{
5550 println!("des_decrypt_array_ofb()");
5551 use std::io::Write;
5552 use std::fmt::Write as _;
5553 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
5554
5555 // Normal case
5556 let key = 0x_1234567890ABCDEF_u64;
5557 println!("K =\t{:#016X}", key);
5558 let mut a_des = DES::new_with_key_u64(key);
5559
5560 let message = "In the beginning God created the heavens and the earth.";
5561 println!("M =\t{}", message);
5562 let iv = 0x_FEDCBA0987654321_u64;
5563 println!("IV = {}", iv);
5564 let mut cipher = [0_u8; 55];
5565 a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
5566 print!("C (16 rounds) =\t");
5567 for c in cipher.clone()
5568 { print!("{:02X} ", c); }
5569 println!();
5570 let mut txt = String::new();
5571 for c in cipher.clone()
5572 { write!(txt, "{:02X} ", c); }
5573 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
5574
5575 let mut recovered = vec![0; 55];
5576 let len = a_des.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
5577 recovered.truncate(len as usize);
5578 print!("Ba (16 rounds) =\t");
5579 for b in recovered.clone()
5580 { print!("{:02X} ", b); }
5581 println!();
5582 let mut txt = String::new();
5583 for c in recovered.clone()
5584 { write!(txt, "{:02X} ", c); }
5585 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 ");
5586
5587 let mut converted = String::new();
5588 unsafe { converted.as_mut_vec() }.append(&mut recovered);
5589
5590 println!("Bb (16 rounds) =\t{}", converted);
5591 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
5592 assert_eq!(converted, message);
5593 println!();
5594
5595 // Expanded case for 128 rounds
5596 let key = 0x_1234567890ABCDEF_u64;
5597 println!("K =\t{:#016X}", key);
5598 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
5599
5600 let message = "In the beginning God created the heavens and the earth.";
5601 println!("M =\t{}", message);
5602 let iv = 0x_FEDCBA0987654321_u64;
5603 println!("IV = {}", iv);
5604 let mut cipher = [0_u8; 55];
5605 a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
5606 print!("C (128 rounds) =\t");
5607 for c in cipher.clone()
5608 { print!("{:02X} ", c); }
5609 println!();
5610 let mut txt = String::new();
5611 for c in cipher.clone()
5612 { write!(txt, "{:02X} ", c); }
5613 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
5614
5615 let mut recovered = vec![0; 55];
5616 let len = a_des.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
5617 recovered.truncate(len as usize);
5618 print!("Ba (128 rounds) =\t");
5619 for b in recovered.clone()
5620 { print!("{:02X} ", b); }
5621 println!();
5622 let mut txt = String::new();
5623 for c in recovered.clone()
5624 { write!(txt, "{:02X} ", c); }
5625 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 ");
5626
5627 let mut converted = String::new();
5628 unsafe { converted.as_mut_vec() }.append(&mut recovered);
5629
5630 println!("Bb (128 rounds) =\t{}", converted);
5631 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
5632 assert_eq!(converted, message);
5633 println!();
5634
5635 // Expanded case for 0 rounds which means that key is meaningless
5636 let key1 = 0x_1234567890ABCDEF_u64;
5637 let key2 = 0_u64;
5638 println!("K =\t{:#016X}", key);
5639 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
5640 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
5641
5642 let message = "In the beginning God created the heavens and the earth.";
5643 println!("M =\t{}", message);
5644 let iv = 0x_FEDCBA0987654321_u64;
5645 println!("IV = {}", iv);
5646 let mut cipher1 = [0_u8; 55];
5647 let mut cipher2 = [0_u8; 55];
5648 c_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
5649 d_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
5650 print!("C (0 rounds) =\t");
5651 for c in cipher1.clone()
5652 { print!("{:02X} ", c); }
5653 println!();
5654 let mut txt = String::new();
5655 for c in cipher1.clone()
5656 { write!(txt, "{:02X} ", c); }
5657 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
5658 print!("D (0 rounds) =\t");
5659 for c in cipher2.clone()
5660 { print!("{:02X} ", c); }
5661 println!();
5662 let mut txt = String::new();
5663 for c in cipher2.clone()
5664 { write!(txt, "{:02X} ", c); }
5665 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
5666
5667 let mut recovered1 = vec![0; 55];
5668 let mut recovered2 = vec![0; 55];
5669 let len1 = c_des.decrypt_array(iv, &cipher1, recovered1.as_mut_ptr());
5670 let len2 = d_des.decrypt_array(iv, &cipher2, recovered2.as_mut_ptr());
5671 recovered1.truncate(len1 as usize);
5672 recovered2.truncate(len2 as usize);
5673
5674 print!("B1a (0 rounds) =\t");
5675 for b in recovered1.clone()
5676 { print!("{:02X} ", b); }
5677 println!();
5678 let mut txt = String::new();
5679 for c in recovered1.clone()
5680 { write!(txt, "{:02X} ", c); }
5681 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 ");
5682 print!("B2a (0 rounds) =\t");
5683 for b in recovered2.clone()
5684 { print!("{:02X} ", b); }
5685 println!();
5686 let mut txt = String::new();
5687 for c in recovered2.clone()
5688 { write!(txt, "{:02X} ", c); }
5689 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 ");
5690
5691 let mut converted1 = String::new();
5692 let mut converted2 = String::new();
5693 unsafe { converted1.as_mut_vec() }.append(&mut recovered1);
5694 unsafe { converted2.as_mut_vec() }.append(&mut recovered2);
5695
5696 println!("B1b (0 rounds) =\t{}", converted1);
5697 assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
5698 assert_eq!(converted1, message);
5699 println!("B2b (0 rounds) =\t{}", converted2);
5700 assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
5701 assert_eq!(converted2, message);
5702 assert_eq!(converted1, converted1);
5703 println!();
5704
5705 // Normal case for the message of 0 bytes
5706 let key = 0x_1234567890ABCDEF_u64;
5707 println!("K =\t{:#016X}", key);
5708 let mut a_des = DES::new_with_key_u64(key);
5709
5710 let message = "";
5711 println!("M =\t{}", message);
5712 let iv = 0x_FEDCBA0987654321_u64;
5713 println!("IV = {}", iv);
5714 let mut cipher = [0_u8; 0];
5715 a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
5716 print!("C =\t");
5717 for c in cipher.clone()
5718 { print!("{:02X} ", c); }
5719 println!();
5720 let mut txt = String::new();
5721 for c in cipher.clone()
5722 { write!(txt, "{:02X} ", c); }
5723 assert_eq!(txt, "");
5724
5725 let mut recovered = vec![0; 8];
5726 let len = a_des.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
5727 recovered.truncate(len as usize);
5728
5729 print!("Ba =\t");
5730 for b in recovered.clone()
5731 { print!("{:02X} ", b); }
5732 println!();
5733 let mut txt = String::new();
5734 for c in recovered.clone()
5735 { write!(txt, "{:02X} ", c); }
5736 assert_eq!(txt, "");
5737
5738 let mut converted = String::new();
5739 unsafe { converted.as_mut_vec() }.append(&mut recovered);
5740
5741 println!("Bb =\t{}", converted);
5742 assert_eq!(converted, "");
5743 assert_eq!(converted, message);
5744 println!();
5745
5746 // Normal case for the message shorter than 8 bytes
5747 let key = 0x_1234567890ABCDEF_u64;
5748 println!("K =\t{:#016X}", key);
5749 let mut a_des = DES::new_with_key_u64(key);
5750
5751 let message = "7 bytes";
5752 println!("M =\t{}", message);
5753 let iv = 0x_FEDCBA0987654321_u64;
5754 println!("IV = {}", iv);
5755 let mut cipher = [0_u8; 7];
5756 a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
5757 print!("C =\t");
5758 for c in cipher.clone()
5759 { print!("{:02X} ", c); }
5760 println!();
5761 let mut txt = String::new();
5762 for c in cipher.clone()
5763 { write!(txt, "{:02X} ", c); }
5764 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
5765
5766 let mut recovered = vec![0; 8];
5767 let len = a_des.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
5768 recovered.truncate(len as usize);
5769
5770 print!("Ba =\t");
5771 for b in recovered.clone()
5772 { print!("{:02X} ", b); }
5773 println!();
5774 let mut txt = String::new();
5775 for c in recovered.clone()
5776 { write!(txt, "{:02X} ", c); }
5777 assert_eq!(txt, "37 20 62 79 74 65 73 ");
5778
5779 let mut converted = String::new();
5780 unsafe { converted.as_mut_vec() }.append(&mut recovered);
5781
5782 println!("Bb =\t{}", converted);
5783 assert_eq!(converted, "7 bytes");
5784 assert_eq!(converted, message);
5785 println!();
5786
5787 // Normal case for the message of 8 bytes
5788 let key = 0x_1234567890ABCDEF_u64;
5789 println!("K =\t{:#016X}", key);
5790 let mut a_des = DES::new_with_key_u64(key);
5791
5792 let message = "I am OK.";
5793 println!("M =\t{}", message);
5794 let iv = 0x_FEDCBA0987654321_u64;
5795 println!("IV = {}", iv);
5796 let mut cipher = [0_u8; 8];
5797 a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
5798 print!("C =\t");
5799 for c in cipher.clone()
5800 { print!("{:02X} ", c); }
5801 println!();
5802 let mut txt = String::new();
5803 for c in cipher.clone()
5804 { write!(txt, "{:02X} ", c); }
5805 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
5806
5807 let mut recovered = vec![0; 16];
5808 let len = a_des.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
5809 recovered.truncate(len as usize);
5810
5811 print!("Ba =\t");
5812 for b in recovered.clone()
5813 { print!("{:02X} ", b); }
5814 println!();
5815 let mut txt = String::new();
5816 for c in recovered.clone()
5817 { write!(txt, "{:02X} ", c); }
5818 assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E ");
5819
5820 let mut converted = String::new();
5821 unsafe { converted.as_mut_vec() }.append(&mut recovered);
5822
5823 println!("Bb =\t{}", converted);
5824 assert_eq!(converted, "I am OK.");
5825 assert_eq!(converted, message);
5826 println!();
5827
5828 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
5829 let key = 0x_1234567890ABCDEF_u64;
5830 println!("K =\t{:#016X}", key);
5831 let mut a_des = DES::new_with_key_u64(key);
5832
5833 let message = "PARK Youngho";
5834 println!("M =\t{}", message);
5835 let iv = 0x_FEDCBA0987654321_u64;
5836 println!("IV = {}", iv);
5837 let mut cipher = [0_u8; 12];
5838 a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
5839 print!("C =\t");
5840 for c in cipher.clone()
5841 { print!("{:02X} ", c); }
5842 println!();
5843 let mut txt = String::new();
5844 for c in cipher.clone()
5845 { write!(txt, "{:02X} ", c); }
5846 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
5847
5848 let mut recovered = vec![0; 16];
5849 let len = a_des.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
5850 recovered.truncate(len as usize);
5851 print!("Ba =\t");
5852 for b in recovered.clone()
5853 { print!("{:02X} ", b); }
5854 println!();
5855 let mut txt = String::new();
5856 for c in recovered.clone()
5857 { write!(txt, "{:02X} ", c); }
5858 assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F ");
5859
5860 let mut converted = String::new();
5861 unsafe { converted.as_mut_vec() }.append(&mut recovered);
5862
5863 println!("Bb =\t{}", converted);
5864 assert_eq!(converted, "PARK Youngho");
5865 assert_eq!(converted, message);
5866 println!();
5867
5868 // Normal case for the message of 16 bytes
5869 let key = 0x_1234567890ABCDEF_u64;
5870 println!("K =\t{:#016X}", key);
5871 let mut a_des = DES::new_with_key_u64(key);
5872
5873 let message = "고맙습니다.";
5874 println!("M =\t{}", message);
5875 let iv = 0x_FEDCBA0987654321_u64;
5876 println!("IV = {}", iv);
5877 let mut cipher = [0_u8; 16];
5878 a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
5879 print!("C =\t");
5880 for c in cipher.clone()
5881 { print!("{:02X} ", c); }
5882 println!();
5883 let mut txt = String::new();
5884 for c in cipher.clone()
5885 { write!(txt, "{:02X} ", c); }
5886 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
5887
5888 let mut recovered = vec![0; 24];
5889 let len = a_des.decrypt_array(iv, &cipher, recovered.as_mut_ptr());
5890 recovered.truncate(len as usize);
5891
5892 print!("Ba =\t");
5893 for b in recovered.clone()
5894 { print!("{:02X} ", b); }
5895 println!();
5896 let mut txt = String::new();
5897 for c in recovered.clone()
5898 { write!(txt, "{:02X} ", c); }
5899 assert_eq!(txt, "EA B3 A0 EB A7 99 EC 8A B5 EB 8B 88 EB 8B A4 2E ");
5900
5901 let mut converted = String::new();
5902 unsafe { converted.as_mut_vec() }.append(&mut recovered);
5903
5904 println!("Bb =\t{}", converted);
5905 assert_eq!(converted, "고맙습니다.");
5906 assert_eq!(converted, message);
5907 println!("-------------------------------");
5908}Sourcefn decrypt_array_into_vec<U, V, const N: usize>(
&mut self,
iv: T,
cipher: &[U; N],
message: &mut Vec<V>,
) -> u64
fn decrypt_array_into_vec<U, V, const N: usize>( &mut self, iv: T, cipher: &[U; N], message: &mut Vec<V>, ) -> u64
Decrypts the data stored in an array [U; N] object without any padding
in OFB (Output FeedBack) mode, and stores the decrypted data in Vec<V>.
§Arguments
ivis an initialization vector for OFB mode.cipheris an immutable reference to an array[U; N]object, and is the place where the plaintext to be decrypted is stored.messageis a mutable reference toVec<U>object, and is the place where the decrypted data will be stored.
§Output
- This method returns the size of plaintext in bytes.
- If this method failed in decryption, it always returns
zero. - Even if this method succeeded in decryption, it returns
zerowhen the original plaintext is zero-length empty data. Then, you will have to check whether or not it failed by using the methodis_successful()oris_failed(). - If
size_of::<U>()*Nis greater than0(which means that the original plaintext is surely not empty data) and it returnszero, you can interpret it that this method surely failed in decryption.
§Features
- 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 Rijndael or AES, and its variants
§Example 1 for AES-128
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ AES_128, CBC_ISO };
let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
println!("K =\t{:#016X}", key);
let mut a_aes = AES_128::new_with_key_u128(key);
let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = [0_u8; 55];
a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
print!("C =\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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 07 AF 4E C8 EF 5D 0A 74 97 3F 90 9E 05 9E 9E 32 FB 55 54 45 7C ED A2 2B F8 07 66 C0 7B CB 98 F3 BF 93 15 BA 26 1C 47 ");
println!();
let mut recovered = vec![0; 55];
a_aes.decrypt_array_into_vec(iv, &cipher, &mut recovered);
print!("Ba =\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 =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");
assert_eq!(converted, message);§For more examples,
click here
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, OFB };
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 iv = 0x_FEDCBA0987654321_u64;
println!("IV = {}", iv);
let mut cipher = [0_u8; 55];
a_des.encrypt_into_array(iv, 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, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
let mut recovered = Vec::<u8>::new();
a_des.decrypt_array_into_vec(iv, &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
§For BigCryptor128
§Example 1 for TAES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
+ AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
+ AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
println!("IV = {:#034X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message); let mut cipher = [0_u8; 55];
taes.encrypt_str_into_array(iv, &message, &mut cipher);
print!("C =\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, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
let mut recovered = Vec::<u8>::new();
taes.decrypt_array_into_vec(iv, &cipher, &mut recovered);
print!("Ba =\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 =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");
assert_eq!(converted, message);§For more examples,
click here
§For BigCryptor64
§Example 1 for TDES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
+ DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
+ DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
let iv = 0x_FEDCBA0987654321_u64;
println!("IV = {:#018X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message); let mut cipher = [0_u8; 55];
tdes.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
print!("C =\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, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
let mut recovered = Vec::<u8>::new();
tdes.decrypt_array_into_vec(iv, &cipher, &mut recovered);
print!("Ba =\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 =\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?
867fn bigcryptor64_decrypt_array_ofb_into_vec()
868{
869 println!("bigcryptor64_decrypt_array_ofb_into_vec()");
870 use std::io::Write;
871 use std::fmt::Write as _;
872 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
873
874 // TDES case
875 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
876 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
877 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
878 let iv = 0x_FEDCBA0987654321_u64;
879 println!("IV = {:#018X}", iv);
880 let message = "In the beginning God created the heavens and the earth.";
881 println!("M =\t{}", message); let mut cipher = [0_u8; 55];
882 tdes.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
883 print!("C =\t");
884 for c in cipher.clone()
885 { print!("{:02X} ", c); }
886 println!();
887 let mut txt = String::new();
888 for c in cipher.clone()
889 { write!(txt, "{:02X} ", c); }
890 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
891
892 let mut recovered = Vec::<u8>::new();
893 tdes.decrypt_array_into_vec(iv, &cipher, &mut recovered);
894 print!("Ba =\t");
895 for b in recovered.clone()
896 { print!("{:02X} ", b); }
897 println!();
898 let mut txt = String::new();
899 for c in recovered.clone()
900 { write!(txt, "{:02X} ", c); }
901 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 ");
902
903 let mut converted = String::new();
904 unsafe { converted.as_mut_vec() }.append(&mut recovered);
905
906 println!("Bb =\t{}", converted);
907 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
908 assert_eq!(converted, message);
909 println!("-------------------------------");
910}More examples
867fn bigcryptor128_decrypt_array_ofb_into_vec()
868{
869 println!("bigcryptor128_decrypt_array_ofb_into_vec()");
870 use std::io::Write;
871 use std::fmt::Write as _;
872 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
873
874 // TAES_128 case
875 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
876 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
877 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
878 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
879 println!("IV = {:#034X}", iv);
880 let message = "In the beginning God created the heavens and the earth.";
881 println!("M =\t{}", message); let mut cipher = [0_u8; 55];
882 taes.encrypt_str_into_array(iv, &message, &mut cipher);
883 print!("C =\t");
884 for c in cipher.clone()
885 { print!("{:02X} ", c); }
886 println!();
887 let mut txt = String::new();
888 for c in cipher.clone()
889 { write!(txt, "{:02X} ", c); }
890 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
891
892 let mut recovered = Vec::<u8>::new();
893 taes.decrypt_array_into_vec(iv, &cipher, &mut recovered);
894 print!("Ba =\t");
895 for b in recovered.clone()
896 { print!("{:02X} ", b); }
897 println!();
898 let mut txt = String::new();
899 for c in recovered.clone()
900 { write!(txt, "{:02X} ", c); }
901 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 ");
902
903 let mut converted = String::new();
904 unsafe { converted.as_mut_vec() }.append(&mut recovered);
905
906 println!("Bb =\t{}", converted);
907 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
908 assert_eq!(converted, message);
909 println!("-------------------------------");
910}4016fn aes_decrypt_array_ofb_into_vec()
4017{
4018 println!("aes_decrypt_array_ofb_into_vec()");
4019 use std::io::Write;
4020 use std::fmt::Write as _;
4021 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
4022
4023 // Normal case for AES-128
4024 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
4025 println!("K =\t{:#016X}", key);
4026 let mut a_aes = AES_128::new_with_key_u128(key);
4027 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
4028 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
4029
4030 let message = "In the beginning God created the heavens and the earth.";
4031 println!("M =\t{}", message);
4032 let mut cipher = [0_u8; 55];
4033 a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4034 print!("C =\t");
4035 for c in cipher.clone()
4036 { print!("{:02X} ", c); }
4037 println!();
4038 let mut txt = String::new();
4039 for c in cipher.clone()
4040 { write!(txt, "{:02X} ", c); }
4041 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
4042
4043 let mut recovered = vec![0; 55];
4044 a_aes.decrypt_array_into_vec(iv, &cipher, &mut recovered);
4045 print!("Ba =\t");
4046 for b in recovered.clone()
4047 { print!("{:02X} ", b); }
4048 println!();
4049 let mut txt = String::new();
4050 for c in recovered.clone()
4051 { write!(txt, "{:02X} ", c); }
4052 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 ");
4053
4054 let mut converted = String::new();
4055 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4056
4057 println!("Bb =\t{}", converted);
4058 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4059 assert_eq!(converted, message);
4060 println!();
4061
4062 // Normal case for AES-192
4063 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
4064 print!("K =\t");
4065 for i in 0..24
4066 { print!("{:02X}", key[i]); }
4067 println!();
4068 let mut a_aes = AES_192::new_with_key(&key);
4069 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
4070 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
4071
4072 let message = "In the beginning God created the heavens and the earth.";
4073 println!("M =\t{}", message);
4074 let mut cipher = [0_u8; 55];
4075 a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4076 print!("C =\t");
4077 for c in cipher.clone()
4078 { print!("{:02X} ", c); }
4079 println!();
4080 let mut txt = String::new();
4081 for c in cipher.clone()
4082 { write!(txt, "{:02X} ", c); }
4083 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
4084
4085 let mut recovered = vec![0; 55];
4086 a_aes.decrypt_array_into_vec(iv, &cipher, &mut recovered);
4087 print!("Ba =\t");
4088 for b in recovered.clone()
4089 { print!("{:02X} ", b); }
4090 println!();
4091 let mut txt = String::new();
4092 for c in recovered.clone()
4093 { write!(txt, "{:02X} ", c); }
4094 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 ");
4095
4096 let mut converted = String::new();
4097 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4098
4099 println!("Bb =\t{}", converted);
4100 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4101 assert_eq!(converted, message);
4102 println!();
4103
4104 // Normal case for AES-256
4105 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
4106 print!("K =\t");
4107 for i in 0..32
4108 { print!("{:02X}", key[i]); }
4109 println!();
4110 let mut a_aes = AES_256::new_with_key(&key);
4111 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
4112 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
4113
4114 let message = "In the beginning God created the heavens and the earth.";
4115 println!("M =\t{}", message);
4116 let mut cipher = [0_u8; 55];
4117 a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4118 print!("C =\t");
4119 for c in cipher.clone()
4120 { print!("{:02X} ", c); }
4121 println!();
4122 let mut txt = String::new();
4123 for c in cipher.clone()
4124 { write!(txt, "{:02X} ", c); }
4125 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
4126
4127 let mut recovered = vec![0; 55];
4128 a_aes.decrypt_array_into_vec(iv, &cipher, &mut recovered);
4129 print!("Ba =\t");
4130 for b in recovered.clone()
4131 { print!("{:02X} ", b); }
4132 println!();
4133 let mut txt = String::new();
4134 for c in recovered.clone()
4135 { write!(txt, "{:02X} ", c); }
4136 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 ");
4137
4138 let mut converted = String::new();
4139 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4140
4141 println!("Bb =\t{}", converted);
4142 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4143 assert_eq!(converted, message);
4144 println!();
4145
4146 // Normal case for Rijndael-256-256
4147 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
4148 print!("K =\t");
4149 for i in 0..32
4150 { print!("{:02X}", key[i]); }
4151 println!();
4152 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
4153 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
4154 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
4155
4156 let message = "In the beginning God created the heavens and the earth.";
4157 println!("M =\t{}", message);
4158 let mut cipher = [0_u8; 55];
4159 a_rijndael.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4160 print!("C =\t");
4161 for c in cipher.clone()
4162 { print!("{:02X} ", c); }
4163 println!();
4164 let mut txt = String::new();
4165 for c in cipher.clone()
4166 { write!(txt, "{:02X} ", c); }
4167 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
4168
4169 let mut recovered = vec![0; 55];
4170 a_rijndael.decrypt_array_into_vec(iv, &cipher, &mut recovered);
4171 print!("Ba =\t");
4172 for b in recovered.clone()
4173 { print!("{:02X} ", b); }
4174 println!();
4175 let mut txt = String::new();
4176 for c in recovered.clone()
4177 { write!(txt, "{:02X} ", c); }
4178 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 ");
4179
4180 let mut converted = String::new();
4181 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4182
4183 println!("Bb =\t{}", converted);
4184 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4185 assert_eq!(converted, message);
4186 println!();
4187
4188 // Normal case for Rijndael-512-512 for post-quantum
4189 use cryptocol::number::SharedArrays;
4190 use cryptocol::hash::SHA3_512;
4191 let mut sha3 = SHA3_512::new();
4192 sha3.absorb_str("Post-quantum");
4193 let key: [u8; 64] = sha3.get_hash_value_in_array();
4194 print!("K =\t");
4195 for i in 0..64
4196 { print!("{:02X}", key[i]); }
4197 println!();
4198 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
4199 sha3.absorb_str("Initialize");
4200 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
4201 iv.src = sha3.get_hash_value_in_array();
4202 let iv = unsafe { iv.des };
4203 print!("IV =\t");
4204 for i in 0..16
4205 { print!("{:08X}", iv[i].to_be()); }
4206 println!();
4207 let message = "In the beginning God created the heavens and the earth.";
4208 println!("M =\t{}", message);
4209 let mut cipher = [0_u8; 55];
4210 a_rijndael.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4211 print!("C =\t");
4212 for c in cipher.clone()
4213 { print!("{:02X} ", c); }
4214 println!();
4215 let mut txt = String::new();
4216 for c in cipher.clone()
4217 { write!(txt, "{:02X} ", c); }
4218 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
4219
4220 let mut recovered = vec![0; 55];
4221 a_rijndael.decrypt_array_into_vec(iv, &cipher, &mut recovered);
4222 print!("Ba =\t");
4223 for b in recovered.clone()
4224 { print!("{:02X} ", b); }
4225 println!();
4226 let mut txt = String::new();
4227 for c in recovered.clone()
4228 { write!(txt, "{:02X} ", c); }
4229 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 ");
4230
4231 let mut converted = String::new();
4232 unsafe { converted.as_mut_vec() }.append(&mut recovered);
4233
4234 println!("Bb =\t{}", converted);
4235 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4236 assert_eq!(converted, message);
4237 println!("-------------------------------");
4238}5910fn des_decrypt_array_ofb_into_vec()
5911{
5912 println!("des_decrypt_array_ofb_into_vec()");
5913 use std::io::Write;
5914 use std::fmt::Write as _;
5915 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
5916
5917 // Normal case
5918 let key = 0x_1234567890ABCDEF_u64;
5919 println!("K =\t{:#016X}", key);
5920 let mut a_des = DES::new_with_key_u64(key);
5921
5922 let message = "In the beginning God created the heavens and the earth.";
5923 println!("M =\t{}", message);
5924 let iv = 0x_FEDCBA0987654321_u64;
5925 println!("IV = {}", iv);
5926 let mut cipher = [0_u8; 55];
5927 a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
5928 print!("C (16 rounds) =\t");
5929 for c in cipher.clone()
5930 { print!("{:02X} ", c); }
5931 println!();
5932 let mut txt = String::new();
5933 for c in cipher.clone()
5934 { write!(txt, "{:02X} ", c); }
5935 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
5936
5937 let mut recovered = Vec::<u8>::new();
5938 a_des.decrypt_array_into_vec(iv, &cipher, &mut recovered);
5939 print!("Ba (16 rounds) =\t");
5940 for b in recovered.clone()
5941 { print!("{:02X} ", b); }
5942 println!();
5943 let mut txt = String::new();
5944 for c in recovered.clone()
5945 { write!(txt, "{:02X} ", c); }
5946 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 ");
5947
5948 let mut converted = String::new();
5949 unsafe { converted.as_mut_vec() }.append(&mut recovered);
5950
5951 println!("Bb (16 rounds) =\t{}", converted);
5952 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
5953 assert_eq!(converted, message);
5954 println!();
5955
5956 // Expanded case for 128 rounds
5957 let key = 0x_1234567890ABCDEF_u64;
5958 println!("K =\t{:#016X}", key);
5959 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
5960
5961 let message = "In the beginning God created the heavens and the earth.";
5962 println!("M =\t{}", message);
5963 let iv = 0x_FEDCBA0987654321_u64;
5964 println!("IV = {}", iv);
5965 let mut cipher = [0_u8; 55];
5966 a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
5967 print!("C (128 rounds) =\t");
5968 for c in cipher.clone()
5969 { print!("{:02X} ", c); }
5970 println!();
5971 let mut txt = String::new();
5972 for c in cipher.clone()
5973 { write!(txt, "{:02X} ", c); }
5974 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
5975
5976 let mut recovered = Vec::<u8>::new();
5977 a_des.decrypt_array_into_vec(iv, &cipher, &mut recovered);
5978 print!("Ba (128 rounds) =\t");
5979 for b in recovered.clone()
5980 { print!("{:02X} ", b); }
5981 println!();
5982 let mut txt = String::new();
5983 for c in recovered.clone()
5984 { write!(txt, "{:02X} ", c); }
5985 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 ");
5986
5987 let mut converted = String::new();
5988 unsafe { converted.as_mut_vec() }.append(&mut recovered);
5989
5990 println!("Bb (128 rounds) =\t{}", converted);
5991 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
5992 assert_eq!(converted, message);
5993 println!();
5994
5995 // Expanded case for 0 rounds which means that key is meaningless
5996 let key1 = 0x_1234567890ABCDEF_u64;
5997 let key2 = 0_u64;
5998 println!("K =\t{:#016X}", key);
5999 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
6000 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
6001
6002 let message = "In the beginning God created the heavens and the earth.";
6003 println!("M =\t{}", message);
6004 let iv = 0x_FEDCBA0987654321_u64;
6005 println!("IV = {}", iv);
6006 let mut cipher1 = [0_u8; 55];
6007 let mut cipher2 = [0_u8; 55];
6008 c_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
6009 d_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
6010 print!("C (0 rounds) =\t");
6011 for c in cipher1.clone()
6012 { print!("{:02X} ", c); }
6013 println!();
6014 let mut txt = String::new();
6015 for c in cipher1.clone()
6016 { write!(txt, "{:02X} ", c); }
6017 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
6018 print!("D (0 rounds) =\t");
6019 for c in cipher2.clone()
6020 { print!("{:02X} ", c); }
6021 println!();
6022 let mut txt = String::new();
6023 for c in cipher2.clone()
6024 { write!(txt, "{:02X} ", c); }
6025 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
6026
6027 let mut recovered1 = Vec::<u8>::new();
6028 let mut recovered2 = Vec::<u8>::new();
6029 c_des.decrypt_array_into_vec(iv, &cipher1, &mut recovered1);
6030 d_des.decrypt_array_into_vec(iv, &cipher2, &mut recovered2);
6031 print!("B1a (0 rounds) =\t");
6032 for b in recovered1.clone()
6033 { print!("{:02X} ", b); }
6034 println!();
6035 let mut txt = String::new();
6036 for c in recovered1.clone()
6037 { write!(txt, "{:02X} ", c); }
6038 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 ");
6039 print!("B2a (0 rounds) =\t");
6040 for b in recovered2.clone()
6041 { print!("{:02X} ", b); }
6042 println!();
6043 let mut txt = String::new();
6044 for c in recovered2.clone()
6045 { write!(txt, "{:02X} ", c); }
6046 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 ");
6047
6048 let mut converted1 = String::new();
6049 let mut converted2 = String::new();
6050 unsafe { converted1.as_mut_vec() }.append(&mut recovered1);
6051 unsafe { converted2.as_mut_vec() }.append(&mut recovered2);
6052
6053 println!("B1b (0 rounds) =\t{}", converted1);
6054 assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
6055 assert_eq!(converted1, message);
6056 println!("B2b (0 rounds) =\t{}", converted2);
6057 assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
6058 assert_eq!(converted2, message);
6059 assert_eq!(converted1, converted1);
6060 println!();
6061
6062 // Normal case for the message of 0 bytes
6063 let key = 0x_1234567890ABCDEF_u64;
6064 println!("K =\t{:#016X}", key);
6065 let mut a_des = DES::new_with_key_u64(key);
6066
6067 let message = "";
6068 println!("M =\t{}", message);
6069 let iv = 0x_FEDCBA0987654321_u64;
6070 println!("IV = {}", iv);
6071 let mut cipher = [0_u8; 0];
6072 a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6073 print!("C =\t");
6074 for c in cipher.clone()
6075 { print!("{:02X} ", c); }
6076 println!();
6077 let mut txt = String::new();
6078 for c in cipher.clone()
6079 { write!(txt, "{:02X} ", c); }
6080 assert_eq!(txt, "");
6081
6082 let mut recovered = Vec::<u8>::new();
6083 a_des.decrypt_array_into_vec(iv, &cipher, &mut recovered);
6084 print!("Ba =\t");
6085 for b in recovered.clone()
6086 { print!("{:02X} ", b); }
6087 println!();
6088 let mut txt = String::new();
6089 for c in recovered.clone()
6090 { write!(txt, "{:02X} ", c); }
6091 assert_eq!(txt, "");
6092
6093 let mut converted = String::new();
6094 unsafe { converted.as_mut_vec() }.append(&mut recovered);
6095
6096 println!("Bb =\t{}", converted);
6097 assert_eq!(converted, "");
6098 assert_eq!(converted, message);
6099 println!();
6100
6101 // Normal case for the message shorter than 8 bytes
6102 let key = 0x_1234567890ABCDEF_u64;
6103 println!("K =\t{:#016X}", key);
6104 let mut a_des = DES::new_with_key_u64(key);
6105
6106 let message = "7 bytes";
6107 println!("M =\t{}", message);
6108 let iv = 0x_FEDCBA0987654321_u64;
6109 println!("IV = {}", iv);
6110 let mut cipher = [0_u8; 7];
6111 a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6112 print!("C =\t");
6113 for c in cipher.clone()
6114 { print!("{:02X} ", c); }
6115 println!();
6116 let mut txt = String::new();
6117 for c in cipher.clone()
6118 { write!(txt, "{:02X} ", c); }
6119 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
6120
6121 let mut recovered = Vec::<u8>::new();
6122 a_des.decrypt_array_into_vec(iv, &cipher, &mut recovered);
6123 print!("Ba =\t");
6124 for b in recovered.clone()
6125 { print!("{:02X} ", b); }
6126 println!();
6127 let mut txt = String::new();
6128 for c in recovered.clone()
6129 { write!(txt, "{:02X} ", c); }
6130 assert_eq!(txt, "37 20 62 79 74 65 73 ");
6131
6132 let mut converted = String::new();
6133 unsafe { converted.as_mut_vec() }.append(&mut recovered);
6134
6135 println!("Bb =\t{}", converted);
6136 assert_eq!(converted, "7 bytes");
6137 assert_eq!(converted, message);
6138 println!();
6139
6140 // Normal case for the message of 8 bytes
6141 let key = 0x_1234567890ABCDEF_u64;
6142 println!("K =\t{:#016X}", key);
6143 let mut a_des = DES::new_with_key_u64(key);
6144
6145 let message = "I am OK.";
6146 println!("M =\t{}", message);
6147 let iv = 0x_FEDCBA0987654321_u64;
6148 println!("IV = {}", iv);
6149 let mut cipher = [0_u8; 8];
6150 a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6151 print!("C =\t");
6152 for c in cipher.clone()
6153 { print!("{:02X} ", c); }
6154 println!();
6155 let mut txt = String::new();
6156 for c in cipher.clone()
6157 { write!(txt, "{:02X} ", c); }
6158 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
6159
6160 let mut recovered = Vec::<u8>::new();
6161 a_des.decrypt_array_into_vec(iv, &cipher, &mut recovered);
6162 print!("Ba =\t");
6163 for b in recovered.clone()
6164 { print!("{:02X} ", b); }
6165 println!();
6166 let mut txt = String::new();
6167 for c in recovered.clone()
6168 { write!(txt, "{:02X} ", c); }
6169 assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E ");
6170
6171 let mut converted = String::new();
6172 unsafe { converted.as_mut_vec() }.append(&mut recovered);
6173
6174 println!("Bb =\t{}", converted);
6175 assert_eq!(converted, "I am OK.");
6176 assert_eq!(converted, message);
6177 println!();
6178
6179 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
6180 let key = 0x_1234567890ABCDEF_u64;
6181 println!("K =\t{:#016X}", key);
6182 let mut a_des = DES::new_with_key_u64(key);
6183
6184 let message = "PARK Youngho";
6185 println!("M =\t{}", message);
6186 let iv = 0x_FEDCBA0987654321_u64;
6187 println!("IV = {}", iv);
6188 let mut cipher = [0_u8; 12];
6189 a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6190 print!("C =\t");
6191 for c in cipher.clone()
6192 { print!("{:02X} ", c); }
6193 println!();
6194 let mut txt = String::new();
6195 for c in cipher.clone()
6196 { write!(txt, "{:02X} ", c); }
6197 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
6198
6199 let mut recovered = Vec::<u8>::new();
6200 a_des.decrypt_array_into_vec(iv, &cipher, &mut recovered);
6201 print!("Ba =\t");
6202 for b in recovered.clone()
6203 { print!("{:02X} ", b); }
6204 println!();
6205 let mut txt = String::new();
6206 for c in recovered.clone()
6207 { write!(txt, "{:02X} ", c); }
6208 assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F ");
6209
6210 let mut converted = String::new();
6211 unsafe { converted.as_mut_vec() }.append(&mut recovered);
6212
6213 println!("Bb =\t{}", converted);
6214 assert_eq!(converted, "PARK Youngho");
6215 assert_eq!(converted, message);
6216 println!();
6217
6218 // Normal case for the message of 16 bytes
6219 let key = 0x_1234567890ABCDEF_u64;
6220 println!("K =\t{:#016X}", key);
6221 let mut a_des = DES::new_with_key_u64(key);
6222
6223 let message = "고맙습니다.";
6224 println!("M =\t{}", message);
6225 let iv = 0x_FEDCBA0987654321_u64;
6226 println!("IV = {}", iv);
6227 let mut cipher = [0_u8; 16];
6228 a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6229 print!("C =\t");
6230 for c in cipher.clone()
6231 { print!("{:02X} ", c); }
6232 println!();
6233 let mut txt = String::new();
6234 for c in cipher.clone()
6235 { write!(txt, "{:02X} ", c); }
6236 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
6237
6238 let mut recovered = Vec::<u8>::new();
6239 a_des.decrypt_array_into_vec(iv, &cipher, &mut recovered);
6240 print!("Ba =\t");
6241 for b in recovered.clone()
6242 { print!("{:02X} ", b); }
6243 println!();
6244 let mut txt = String::new();
6245 for c in recovered.clone()
6246 { write!(txt, "{:02X} ", c); }
6247 assert_eq!(txt, "EA B3 A0 EB A7 99 EC 8A B5 EB 8B 88 EB 8B A4 2E ");
6248
6249 let mut converted = String::new();
6250 unsafe { converted.as_mut_vec() }.append(&mut recovered);
6251
6252 println!("Bb =\t{}", converted);
6253 assert_eq!(converted, "고맙습니다.");
6254 assert_eq!(converted, message);
6255 println!("-------------------------------");
6256}Sourcefn decrypt_array_into_array<U, V, const N: usize, const M: usize>(
&mut self,
iv: T,
cipher: &[U; N],
message: &mut [V; M],
) -> u64
fn decrypt_array_into_array<U, V, const N: usize, const M: usize>( &mut self, iv: T, cipher: &[U; N], message: &mut [V; M], ) -> u64
Decrypts the data stored in an array [U; N] object without any padding
in OFB (Output FeedBack) mode, and stores the decrypted data
in array [V; M].
§Arguments
ivis an initialization vector for OFB mode.cipheris an immutable reference to an array[U; N]object, and is the place where the plaintext to be decrypted is stored.messageis a mutable reference to an array[U; N]object, and is the place where the decrypted data will be stored.
§Output
- This method returns the size of plaintext in bytes.
- If this method failed in decryption, it always returns
zero. - Even if this method succeeded in decryption, it returns
zerowhen the original plaintext is zero-length empty data. Then, you will have to check whether or not it failed by using the methodis_successful()oris_failed(). - If
size_of::<U>()*Nis greater than0(which means that the original plaintext is surely not empty data) and it returnszero, you can interpret it that this method surely failed in decryption.
§Features
- If
size_of::<V>()*Mis less thansize_of::<U>()*N, this method does not perform decryption but returnszero. - If
size_of::<V>()*Mis equal to or greater thansize_of::<U>()*N, this method performs decryption, fills the arraymessagewith the decrypted data, and then fills the rest of the elements of the arraymessagewith zeros, and returns the size of the plaintext. - It is responsible for you to prepare the
messagearea big enough! - The size of the area for plaintext does not have to be prepared more
than
size_of::<U>()*N.
§For Rijndael or AES, and its variants
§Example 1 for AES-128
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ AES_128, CBC_ISO };
let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
println!("K =\t{:#016X}", key);
let mut a_aes = AES_128::new_with_key_u128(key);
let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = [0_u8; 55];
a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
print!("C =\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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 07 AF 4E C8 EF 5D 0A 74 97 3F 90 9E 05 9E 9E 32 FB 55 54 45 7C ED A2 2B F8 07 66 C0 7B CB 98 F3 BF 93 15 BA 26 1C 47 ");
println!();
let mut recovered = [0; 64];
let len = a_aes.decrypt_array_into_array(iv, &cipher, &mut recovered);
print!("Ba =\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 00 00 00 00 00 00 00 00 ");
let mut converted = String::new();
unsafe { converted.as_mut_vec() }.write(&recovered);
unsafe { converted.as_mut_vec() }.truncate(len as usize);
println!("Bb =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");
assert_eq!(converted, message);§For more examples,
click here
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, OFB };
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 iv = 0x_FEDCBA0987654321_u64;
println!("IV = {}", iv);
let mut cipher = [0_u8; 55];
a_des.encrypt_into_array(iv, 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, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
let mut recovered = [0u8; 56];
let len = a_des.decrypt_array_into_array(iv, &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
§For BigCryptor128
§Example 1 for TAES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
+ AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
+ AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
println!("IV = {:#034X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message); let mut cipher = [0_u8; 55];
taes.encrypt_str_into_array(iv, &message, &mut cipher);
print!("C =\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, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
let mut recovered = [0u8; 56];
let len = taes.decrypt_array_into_array(iv, &cipher, &mut recovered);
print!("Ba =\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 =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");
assert_eq!(converted, message);§For more examples,
click here
§For BigCryptor64
§Example 1 for TDES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
+ DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
+ DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
let iv = 0x_FEDCBA0987654321_u64;
println!("IV = {:#018X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message); let mut cipher = [0_u8; 55];
tdes.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
print!("C =\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, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
let mut recovered = [0u8; 56];
let len = tdes.decrypt_array_into_array(iv, &cipher, &mut recovered);
print!("Ba =\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 =\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?
912fn bigcryptor64_decrypt_array_ofb_into_array()
913{
914 println!("bigcryptor64_decrypt_array_ofb_into_array()");
915 use std::io::Write;
916 use std::fmt::Write as _;
917 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
918
919 // TDES case
920 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
921 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
922 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
923 let iv = 0x_FEDCBA0987654321_u64;
924 println!("IV = {:#018X}", iv);
925 let message = "In the beginning God created the heavens and the earth.";
926 println!("M =\t{}", message); let mut cipher = [0_u8; 55];
927 tdes.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
928 print!("C =\t");
929 for c in cipher.clone()
930 { print!("{:02X} ", c); }
931 println!();
932 let mut txt = String::new();
933 for c in cipher.clone()
934 { write!(txt, "{:02X} ", c); }
935 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
936
937 let mut recovered = [0u8; 56];
938 let len = tdes.decrypt_array_into_array(iv, &cipher, &mut recovered);
939 print!("Ba =\t");
940 for b in recovered.clone()
941 { print!("{:02X} ", b); }
942 println!();
943 let mut txt = String::new();
944 for c in recovered.clone()
945 { write!(txt, "{:02X} ", c); }
946 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 ");
947
948 let mut converted = String::new();
949 unsafe { converted.as_mut_vec() }.write(&recovered);
950 unsafe { converted.as_mut_vec() }.truncate(len as usize);
951 println!("Bb =\t{}", converted);
952 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
953 assert_eq!(converted, message);
954 println!("-------------------------------");
955}More examples
912fn bigcryptor128_decrypt_array_ofb_into_array()
913{
914 println!("bigcryptor128_decrypt_array_ofb_into_array()");
915 use std::io::Write;
916 use std::fmt::Write as _;
917 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
918
919 // TAES_128 case
920 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
921 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
922 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
923 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
924 println!("IV = {:#034X}", iv);
925 let message = "In the beginning God created the heavens and the earth.";
926 println!("M =\t{}", message); let mut cipher = [0_u8; 55];
927 taes.encrypt_str_into_array(iv, &message, &mut cipher);
928 print!("C =\t");
929 for c in cipher.clone()
930 { print!("{:02X} ", c); }
931 println!();
932 let mut txt = String::new();
933 for c in cipher.clone()
934 { write!(txt, "{:02X} ", c); }
935 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
936
937 let mut recovered = [0u8; 56];
938 let len = taes.decrypt_array_into_array(iv, &cipher, &mut recovered);
939 print!("Ba =\t");
940 for b in recovered.clone()
941 { print!("{:02X} ", b); }
942 println!();
943 let mut txt = String::new();
944 for c in recovered.clone()
945 { write!(txt, "{:02X} ", c); }
946 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 ");
947
948 let mut converted = String::new();
949 unsafe { converted.as_mut_vec() }.write(&recovered);
950 unsafe { converted.as_mut_vec() }.truncate(len as usize);
951 println!("Bb =\t{}", converted);
952 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
953 assert_eq!(converted, message);
954 println!("-------------------------------");
955}4240fn aes_decrypt_array_ofb_into_array()
4241{
4242 println!("aes_decrypt_array_ofb_into_array()");
4243 use std::io::Write;
4244 use std::fmt::Write as _;
4245 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
4246
4247 // Normal case for AES-128
4248 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
4249 println!("K =\t{:#016X}", key);
4250 let mut a_aes = AES_128::new_with_key_u128(key);
4251 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
4252 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
4253
4254 let message = "In the beginning God created the heavens and the earth.";
4255 println!("M =\t{}", message);
4256 let mut cipher = [0_u8; 55];
4257 a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4258 print!("C =\t");
4259 for c in cipher.clone()
4260 { print!("{:02X} ", c); }
4261 println!();
4262 let mut txt = String::new();
4263 for c in cipher.clone()
4264 { write!(txt, "{:02X} ", c); }
4265 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
4266
4267 let mut recovered = [0; 64];
4268 let len = a_aes.decrypt_array_into_array(iv, &cipher, &mut recovered);
4269 print!("Ba =\t");
4270 for b in recovered.clone()
4271 { print!("{:02X} ", b); }
4272 println!();
4273 let mut txt = String::new();
4274 for c in recovered.clone()
4275 { write!(txt, "{:02X} ", c); }
4276 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
4277
4278 let mut converted = String::new();
4279 unsafe { converted.as_mut_vec() }.write(&recovered);
4280 unsafe { converted.as_mut_vec() }.truncate(len as usize);
4281 println!("Bb =\t{}", converted);
4282 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4283 assert_eq!(converted, message);
4284 println!();
4285
4286 // Normal case for AES-192
4287 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
4288 print!("K =\t");
4289 for i in 0..24
4290 { print!("{:02X}", key[i]); }
4291 println!();
4292 let mut a_aes = AES_192::new_with_key(&key);
4293 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
4294 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
4295
4296 let message = "In the beginning God created the heavens and the earth.";
4297 println!("M =\t{}", message);
4298 let mut cipher = [0_u8; 55];
4299 a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4300 print!("C =\t");
4301 for c in cipher.clone()
4302 { print!("{:02X} ", c); }
4303 println!();
4304 let mut txt = String::new();
4305 for c in cipher.clone()
4306 { write!(txt, "{:02X} ", c); }
4307 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
4308
4309 let mut recovered = [0; 64];
4310 let len = a_aes.decrypt_array_into_array(iv, &cipher, &mut recovered);
4311 print!("Ba =\t");
4312 for b in recovered.clone()
4313 { print!("{:02X} ", b); }
4314 println!();
4315 let mut txt = String::new();
4316 for c in recovered.clone()
4317 { write!(txt, "{:02X} ", c); }
4318 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
4319
4320 let mut converted = String::new();
4321 unsafe { converted.as_mut_vec() }.write(&recovered);
4322 unsafe { converted.as_mut_vec() }.truncate(len as usize);
4323 println!("Bb =\t{}", converted);
4324 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4325 assert_eq!(converted, message);
4326 println!();
4327
4328 // Normal case for AES-256
4329 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
4330 print!("K =\t");
4331 for i in 0..32
4332 { print!("{:02X}", key[i]); }
4333 println!();
4334 let mut a_aes = AES_256::new_with_key(&key);
4335 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
4336 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
4337
4338 let message = "In the beginning God created the heavens and the earth.";
4339 println!("M =\t{}", message);
4340 let mut cipher = [0_u8; 55];
4341 a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4342 print!("C =\t");
4343 for c in cipher.clone()
4344 { print!("{:02X} ", c); }
4345 println!();
4346 let mut txt = String::new();
4347 for c in cipher.clone()
4348 { write!(txt, "{:02X} ", c); }
4349 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
4350
4351 let mut recovered = [0; 64];
4352 let len = a_aes.decrypt_array_into_array(iv, &cipher, &mut recovered);
4353 print!("Ba =\t");
4354 for b in recovered.clone()
4355 { print!("{:02X} ", b); }
4356 println!();
4357 let mut txt = String::new();
4358 for c in recovered.clone()
4359 { write!(txt, "{:02X} ", c); }
4360 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
4361
4362 let mut converted = String::new();
4363 unsafe { converted.as_mut_vec() }.write(&recovered);
4364 unsafe { converted.as_mut_vec() }.truncate(len as usize);
4365 println!("Bb =\t{}", converted);
4366 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4367 assert_eq!(converted, message);
4368 println!();
4369
4370 // Normal case for Rijndael-256-256
4371 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
4372 print!("K =\t");
4373 for i in 0..32
4374 { print!("{:02X}", key[i]); }
4375 println!();
4376 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
4377 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
4378 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
4379
4380 let message = "In the beginning God created the heavens and the earth.";
4381 println!("M =\t{}", message);
4382 let mut cipher = [0_u8; 55];
4383 a_rijndael.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4384 print!("C =\t");
4385 for c in cipher.clone()
4386 { print!("{:02X} ", c); }
4387 println!();
4388 let mut txt = String::new();
4389 for c in cipher.clone()
4390 { write!(txt, "{:02X} ", c); }
4391 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
4392
4393 let mut recovered = [0; 64];
4394 let len = a_rijndael.decrypt_array_into_array(iv, &cipher, &mut recovered);
4395 print!("Ba =\t");
4396 for b in recovered.clone()
4397 { print!("{:02X} ", b); }
4398 println!();
4399 let mut txt = String::new();
4400 for c in recovered.clone()
4401 { write!(txt, "{:02X} ", c); }
4402 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
4403
4404 let mut converted = String::new();
4405 unsafe { converted.as_mut_vec() }.write(&recovered);
4406 unsafe { converted.as_mut_vec() }.truncate(len as usize);
4407 println!("Bb =\t{}", converted);
4408 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4409 assert_eq!(converted, message);
4410 println!();
4411
4412 // Normal case for Rijndael-512-512 for post-quantum
4413 use cryptocol::number::SharedArrays;
4414 use cryptocol::hash::SHA3_512;
4415 let mut sha3 = SHA3_512::new();
4416 sha3.absorb_str("Post-quantum");
4417 let key: [u8; 64] = sha3.get_hash_value_in_array();
4418 print!("K =\t");
4419 for i in 0..64
4420 { print!("{:02X}", key[i]); }
4421 println!();
4422 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
4423 sha3.absorb_str("Initialize");
4424 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
4425 iv.src = sha3.get_hash_value_in_array();
4426 let iv = unsafe { iv.des };
4427 print!("IV =\t");
4428 for i in 0..16
4429 { print!("{:08X}", iv[i].to_be()); }
4430 println!();
4431 let message = "In the beginning God created the heavens and the earth.";
4432 println!("M =\t{}", message);
4433 let mut cipher = [0_u8; 55];
4434 a_rijndael.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4435 print!("C =\t");
4436 for c in cipher.clone()
4437 { print!("{:02X} ", c); }
4438 println!();
4439 let mut txt = String::new();
4440 for c in cipher.clone()
4441 { write!(txt, "{:02X} ", c); }
4442 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
4443
4444 let mut recovered = [0; 64];
4445 let len = a_rijndael.decrypt_array_into_array(iv, &cipher, &mut recovered);
4446 print!("Ba =\t");
4447 for b in recovered.clone()
4448 { print!("{:02X} ", b); }
4449 println!();
4450 let mut txt = String::new();
4451 for c in recovered.clone()
4452 { write!(txt, "{:02X} ", c); }
4453 assert_eq!(txt, "49 6E 20 74 68 65 20 62 65 67 69 6E 6E 69 6E 67 20 47 6F 64 20 63 72 65 61 74 65 64 20 74 68 65 20 68 65 61 76 65 6E 73 20 61 6E 64 20 74 68 65 20 65 61 72 74 68 2E 00 00 00 00 00 00 00 00 00 ");
4454
4455 let mut converted = String::new();
4456 unsafe { converted.as_mut_vec() }.write(&recovered);
4457 unsafe { converted.as_mut_vec() }.truncate(len as usize);
4458 println!("Bb =\t{}", converted);
4459 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4460 assert_eq!(converted, message);
4461 println!("-------------------------------");
4462}6258fn des_decrypt_array_ofb_into_array()
6259{
6260 println!("des_decrypt_array_ofb_into_array()");
6261 use std::io::Write;
6262 use std::fmt::Write as _;
6263 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
6264
6265 // Normal case
6266 let key = 0x_1234567890ABCDEF_u64;
6267 println!("K =\t{:#016X}", key);
6268 let mut a_des = DES::new_with_key_u64(key);
6269
6270 let message = "In the beginning God created the heavens and the earth.";
6271 println!("M =\t{}", message);
6272 let iv = 0x_FEDCBA0987654321_u64;
6273 println!("IV = {}", iv);
6274 let mut cipher = [0_u8; 55];
6275 a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6276 print!("C (16 rounds) =\t");
6277 for c in cipher.clone()
6278 { print!("{:02X} ", c); }
6279 println!();
6280 let mut txt = String::new();
6281 for c in cipher.clone()
6282 { write!(txt, "{:02X} ", c); }
6283 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
6284
6285 let mut recovered = [0u8; 56];
6286 let len = a_des.decrypt_array_into_array(iv, &cipher, &mut recovered);
6287 print!("Ba (16 rounds) =\t");
6288 for b in recovered.clone()
6289 { print!("{:02X} ", b); }
6290 println!();
6291 let mut txt = String::new();
6292 for c in recovered.clone()
6293 { write!(txt, "{:02X} ", c); }
6294 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 ");
6295
6296 let mut converted = String::new();
6297 unsafe { converted.as_mut_vec() }.write(&recovered);
6298 unsafe { converted.as_mut_vec() }.truncate(len as usize);
6299 println!("Bb (16 rounds) =\t{}", converted);
6300 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
6301 assert_eq!(converted, message);
6302 println!();
6303
6304 // Expanded case for 128 rounds
6305 let key = 0x_1234567890ABCDEF_u64;
6306 println!("K =\t{:#016X}", key);
6307 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
6308
6309 let message = "In the beginning God created the heavens and the earth.";
6310 println!("M =\t{}", message);
6311 let iv = 0x_FEDCBA0987654321_u64;
6312 println!("IV = {}", iv);
6313 let mut cipher = [0_u8; 55];
6314 a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6315 print!("C (128 rounds) =\t");
6316 for c in cipher.clone()
6317 { print!("{:02X} ", c); }
6318 println!();
6319 let mut txt = String::new();
6320 for c in cipher.clone()
6321 { write!(txt, "{:02X} ", c); }
6322 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
6323
6324 let mut recovered = [0u8; 56];
6325 let len = a_des.decrypt_array_into_array(iv, &cipher, &mut recovered);
6326 print!("Ba (16 rounds) =\t");
6327 for b in recovered.clone()
6328 { print!("{:02X} ", b); }
6329 println!();
6330 let mut txt = String::new();
6331 for c in recovered.clone()
6332 { write!(txt, "{:02X} ", c); }
6333 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 ");
6334
6335 let mut converted = String::new();
6336 unsafe { converted.as_mut_vec() }.write(&recovered);
6337 unsafe { converted.as_mut_vec() }.truncate(len as usize);
6338 println!("Bb (16 rounds) =\t{}", converted);
6339 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
6340 assert_eq!(converted, message);
6341 println!();
6342
6343 // Expanded case for 0 rounds which means that key is meaningless
6344 let key1 = 0x_1234567890ABCDEF_u64;
6345 let key2 = 0_u64;
6346 println!("K =\t{:#016X}", key);
6347 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
6348 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
6349
6350 let message = "In the beginning God created the heavens and the earth.";
6351 println!("M =\t{}", message);
6352 let iv = 0x_FEDCBA0987654321_u64;
6353 println!("IV = {}", iv);
6354 let mut cipher1 = [0_u8; 55];
6355 let mut cipher2 = [0_u8; 55];
6356 c_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
6357 d_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
6358 print!("C (0 rounds) =\t");
6359 for c in cipher1.clone()
6360 { print!("{:02X} ", c); }
6361 println!();
6362 let mut txt = String::new();
6363 for c in cipher1.clone()
6364 { write!(txt, "{:02X} ", c); }
6365 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
6366 print!("D (0 rounds) =\t");
6367 for c in cipher2.clone()
6368 { print!("{:02X} ", c); }
6369 println!();
6370 let mut txt = String::new();
6371 for c in cipher2.clone()
6372 { write!(txt, "{:02X} ", c); }
6373 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
6374
6375 let mut recovered1 = [0u8; 56];
6376 let mut recovered2 = [0u8; 56];
6377 let len1 = c_des.decrypt_array_into_array(iv, &cipher1, &mut recovered1);
6378 let len2 = d_des.decrypt_array_into_array(iv, &cipher2, &mut recovered2);
6379 print!("B1a (0 rounds) =\t");
6380 for b in recovered1.clone()
6381 { print!("{:02X} ", b); }
6382 println!();
6383 let mut txt = String::new();
6384 for c in recovered1.clone()
6385 { write!(txt, "{:02X} ", c); }
6386 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 ");
6387 print!("B2a (0 rounds) =\t");
6388 for b in recovered2.clone()
6389 { print!("{:02X} ", b); }
6390 println!();
6391 let mut txt = String::new();
6392 for c in recovered.clone()
6393 { write!(txt, "{:02X} ", c); }
6394 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 ");
6395
6396 let mut converted1 = String::new();
6397 let mut converted2 = String::new();
6398 unsafe { converted1.as_mut_vec() }.write(&recovered1);
6399 unsafe { converted2.as_mut_vec() }.write(&recovered2);
6400 unsafe { converted1.as_mut_vec() }.truncate(len1 as usize);
6401 unsafe { converted2.as_mut_vec() }.truncate(len2 as usize);
6402 println!("B1b (0 rounds) =\t{}", converted1);
6403 println!("B2b (0 rounds) =\t{}", converted2);
6404 assert_eq!(converted1, "In the beginning God created the heavens and the earth.");
6405 assert_eq!(converted2, "In the beginning God created the heavens and the earth.");
6406 assert_eq!(converted1, message);
6407 assert_eq!(converted2, message);
6408 assert_eq!(converted1, converted2);
6409 println!();
6410
6411 // Normal case for the message of 0 bytes
6412 let key = 0x_1234567890ABCDEF_u64;
6413 println!("K =\t{:#016X}", key);
6414 let mut a_des = DES::new_with_key_u64(key);
6415
6416 let message = "";
6417 println!("M =\t{}", message);
6418 let iv = 0x_FEDCBA0987654321_u64;
6419 println!("IV = {}", iv);
6420 let mut cipher = [0_u8; 0];
6421 a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6422 print!("C =\t");
6423 for c in cipher.clone()
6424 { print!("{:02X} ", c); }
6425 println!();
6426 let mut txt = String::new();
6427 for c in cipher.clone()
6428 { write!(txt, "{:02X} ", c); }
6429 assert_eq!(txt, "");
6430
6431 let mut recovered = [0u8; 8];
6432 let len = a_des.decrypt_array_into_array(iv, &cipher, &mut recovered);
6433
6434 print!("Ba =\t");
6435 for b in recovered.clone()
6436 { print!("{:02X} ", b); }
6437 println!();
6438 let mut txt = String::new();
6439 for c in recovered.clone()
6440 { write!(txt, "{:02X} ", c); }
6441 assert_eq!(txt, "00 00 00 00 00 00 00 00 ");
6442
6443 let mut converted = String::new();
6444 unsafe { converted.as_mut_vec() }.write(&recovered);
6445 unsafe { converted.as_mut_vec() }.truncate(len as usize);
6446 println!("Bb =\t{}", converted);
6447 assert_eq!(converted, "");
6448 assert_eq!(converted, message);
6449 println!();
6450
6451 // Normal case for the message shorter than 8 bytes
6452 let key = 0x_1234567890ABCDEF_u64;
6453 println!("K =\t{:#016X}", key);
6454 let mut a_des = DES::new_with_key_u64(key);
6455
6456 let message = "7 bytes";
6457 println!("M =\t{}", message);
6458 let iv = 0x_FEDCBA0987654321_u64;
6459 println!("IV = {}", iv);
6460 let mut cipher = [0_u8; 7];
6461 a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6462 print!("C =\t");
6463 for c in cipher.clone()
6464 { print!("{:02X} ", c); }
6465 println!();
6466 let mut txt = String::new();
6467 for c in cipher.clone()
6468 { write!(txt, "{:02X} ", c); }
6469 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
6470
6471 let mut recovered = [0u8; 8];
6472 let len = a_des.decrypt_array_into_array(iv, &cipher, &mut recovered);
6473
6474 print!("Ba =\t");
6475 for b in recovered.clone()
6476 { print!("{:02X} ", b); }
6477 println!();
6478 let mut txt = String::new();
6479 for c in recovered.clone()
6480 { write!(txt, "{:02X} ", c); }
6481 assert_eq!(txt, "37 20 62 79 74 65 73 00 ");
6482
6483 let mut converted = String::new();
6484 unsafe { converted.as_mut_vec() }.write(&recovered);
6485 unsafe { converted.as_mut_vec() }.truncate(len as usize);
6486 println!("Bb =\t{}", converted);
6487 assert_eq!(converted, "7 bytes");
6488 assert_eq!(converted, message);
6489 println!();
6490
6491 // Normal case for the message of 8 bytes
6492 let key = 0x_1234567890ABCDEF_u64;
6493 println!("K =\t{:#016X}", key);
6494 let mut a_des = DES::new_with_key_u64(key);
6495
6496 let message = "I am OK.";
6497 println!("M =\t{}", message);
6498 let iv = 0x_FEDCBA0987654321_u64;
6499 println!("IV = {}", iv);
6500 let mut cipher = [0_u8; 8];
6501 a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6502 print!("C =\t");
6503 for c in cipher.clone()
6504 { print!("{:02X} ", c); }
6505 println!();
6506 let mut txt = String::new();
6507 for c in cipher.clone()
6508 { write!(txt, "{:02X} ", c); }
6509 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
6510
6511 let mut recovered = [0u8; 16];
6512 let len = a_des.decrypt_array_into_array(iv, &cipher, &mut recovered);
6513
6514 print!("Ba =\t");
6515 for b in recovered.clone()
6516 { print!("{:02X} ", b); }
6517 println!();
6518 let mut txt = String::new();
6519 for c in recovered.clone()
6520 { write!(txt, "{:02X} ", c); }
6521 assert_eq!(txt, "49 20 61 6D 20 4F 4B 2E 00 00 00 00 00 00 00 00 ");
6522
6523 let mut converted = String::new();
6524 unsafe { converted.as_mut_vec() }.write(&recovered);
6525 unsafe { converted.as_mut_vec() }.truncate(len as usize);
6526 println!("Bb =\t{}", converted);
6527 assert_eq!(converted, "I am OK.");
6528 assert_eq!(converted, message);
6529 println!();
6530
6531 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
6532 let key = 0x_1234567890ABCDEF_u64;
6533 println!("K =\t{:#016X}", key);
6534 let mut a_des = DES::new_with_key_u64(key);
6535
6536 let message = "PARK Youngho";
6537 println!("M =\t{}", message);
6538 let iv = 0x_FEDCBA0987654321_u64;
6539 println!("IV = {}", iv);
6540 let mut cipher = [0_u8; 12];
6541 a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6542 print!("C =\t");
6543 for c in cipher.clone()
6544 { print!("{:02X} ", c); }
6545 println!();
6546 let mut txt = String::new();
6547 for c in cipher.clone()
6548 { write!(txt, "{:02X} ", c); }
6549 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
6550
6551 let mut recovered = [0u8; 16];
6552 let len = a_des.decrypt_array_into_array(iv, &cipher, &mut recovered);
6553
6554 print!("Ba =\t");
6555 for b in recovered.clone()
6556 { print!("{:02X} ", b); }
6557 println!();
6558 let mut txt = String::new();
6559 for c in recovered.clone()
6560 { write!(txt, "{:02X} ", c); }
6561 assert_eq!(txt, "50 41 52 4B 20 59 6F 75 6E 67 68 6F 00 00 00 00 ");
6562
6563 let mut converted = String::new();
6564 unsafe { converted.as_mut_vec() }.write(&recovered);
6565 unsafe { converted.as_mut_vec() }.truncate(len as usize);
6566 println!("Bb =\t{}", converted);
6567 assert_eq!(converted, "PARK Youngho");
6568 assert_eq!(converted, message);
6569 println!();
6570
6571 // Normal case for the message of 16 bytes
6572 let key = 0x_1234567890ABCDEF_u64;
6573 println!("K =\t{:#016X}", key);
6574 let mut a_des = DES::new_with_key_u64(key);
6575
6576 let message = "고맙습니다.";
6577 println!("M =\t{}", message);
6578 let iv = 0x_FEDCBA0987654321_u64;
6579 println!("IV = {}", iv);
6580 let mut cipher = [0_u8; 16];
6581 a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6582 print!("C =\t");
6583 for c in cipher.clone()
6584 { print!("{:02X} ", c); }
6585 println!();
6586 let mut txt = String::new();
6587 for c in cipher.clone()
6588 { write!(txt, "{:02X} ", c); }
6589 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
6590
6591 let mut recovered = [0u8; 24];
6592 let len = a_des.decrypt_array_into_array(iv, &cipher, &mut recovered);
6593
6594 print!("Ba =\t");
6595 for b in recovered.clone()
6596 { print!("{:02X} ", b); }
6597 println!();
6598 let mut txt = String::new();
6599 for c in recovered.clone()
6600 { write!(txt, "{:02X} ", c); }
6601 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 ");
6602
6603 let mut converted = String::new();
6604 unsafe { converted.as_mut_vec() }.write(&recovered);
6605 unsafe { converted.as_mut_vec() }.truncate(len as usize);
6606 println!("Bb =\t{}", converted);
6607 assert_eq!(converted, "고맙습니다.");
6608 assert_eq!(converted, message);
6609 println!("-------------------------------");
6610}Sourcefn decrypt_array_into_string<U, const N: usize>(
&mut self,
iv: T,
cipher: &[U; N],
message: &mut String,
) -> u64
fn decrypt_array_into_string<U, const N: usize>( &mut self, iv: T, cipher: &[U; N], message: &mut String, ) -> u64
Decrypts the data stored in an array [U; N] object without any padding
in OFB (Output FeedBack) mode, and stores the decrypted data in String.
§Arguments
ivis an initialization vector for OFB mode.cipheris an immutable reference to an array[U; N]object, and is the place where the plaintext to be decrypted is stored.messageis a mutable reference to aStringobject, and is the place where the decrypted data will be stored.
§Output
- This method returns the size of plaintext in bytes.
- If this method failed in decryption, it always returns
zero. - Even if this method succeeded in decryption, it returns
zerowhen the original plaintext is zero-length empty data. Then, you will have to check whether or not it failed by using the methodis_successful()oris_failed(). - If
size_of::<U>()*Nis greater than0(which means that the original plaintext is surely not empty data) and it returnszero, you can interpret it that this method surely failed in decryption.
§Features
- 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 Rijndael or AES, and its variants
§Example 1 for AES-128
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ AES_128, CBC_ISO };
let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
println!("K =\t{:#016X}", key);
let mut a_aes = AES_128::new_with_key_u128(key);
let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message);
let mut cipher = [0_u8; 55];
a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
print!("C =\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, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 07 AF 4E C8 EF 5D 0A 74 97 3F 90 9E 05 9E 9E 32 FB 55 54 45 7C ED A2 2B F8 07 66 C0 7B CB 98 F3 BF 93 15 BA 26 1C 47 ");
println!();
let mut converted= String::new();
a_aes.decrypt_array_into_string(iv, &cipher, &mut converted);
println!("B =\t{}", converted);
assert_eq!(converted, "In the beginning God created the heavens and the earth.");
assert_eq!(converted, message);§For more examples,
click here
§For DES and its variants
§Example 1 for Normal case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ DES, OFB };
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 iv = 0x_FEDCBA0987654321_u64;
println!("IV = {}", iv);
let mut cipher = [0_u8; 55];
a_des.encrypt_into_array(iv, 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, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
let mut recovered = String::new();
a_des.decrypt_array_into_string(iv, &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
§For BigCryptor128
§Example 1 for TAES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
+ AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
+ AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
println!("IV = {:#034X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message); let mut cipher = [0_u8; 55];
taes.encrypt_str_into_array(iv, &message, &mut cipher);
print!("C =\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, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
let mut recovered = String::new();
taes.decrypt_array_into_string(iv, &cipher, &mut recovered);
println!("B =\t{}", recovered);
assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
assert_eq!(recovered, message);§For more examples,
click here
§For BigCryptor64
§Example 1 for TDES case
use std::io::Write;
use std::fmt::Write as _;
use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
+ DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
+ DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
let iv = 0x_FEDCBA0987654321_u64;
println!("IV = {:#018X}", iv);
let message = "In the beginning God created the heavens and the earth.";
println!("M =\t{}", message); let mut cipher = [0_u8; 55];
tdes.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
print!("C =\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, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
let mut recovered = String::new();
tdes.decrypt_array_into_string(iv, &cipher, &mut recovered);
println!("B =\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?
957fn bigcryptor64_decrypt_array_ofb_into_string()
958{
959 println!("bigcryptor64_decrypt_array_ofb_into_string()");
960 use std::io::Write;
961 use std::fmt::Write as _;
962 use cryptocol::symmetric::{ BigCryptor64, DES, OFB };
963
964 // TDES case
965 let mut tdes = DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64)
966 + DES::decryptor_with_key_u64(0x_FEDCBA0987654321_u64)
967 + DES::encryptor_with_key_u64(0x_1234567890ABCDEF_u64);
968 let iv = 0x_FEDCBA0987654321_u64;
969 println!("IV = {:#018X}", iv);
970 let message = "In the beginning God created the heavens and the earth.";
971 println!("M =\t{}", message); let mut cipher = [0_u8; 55];
972 tdes.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
973 print!("C =\t");
974 for c in cipher.clone()
975 { print!("{:02X} ", c); }
976 println!();
977 let mut txt = String::new();
978 for c in cipher.clone()
979 { write!(txt, "{:02X} ", c); }
980 assert_eq!(txt, "A8 E8 A8 5C 2E 0C B6 68 B2 9A 04 35 11 DD F7 C6 8E 4E 9E EB D0 9B 97 1C F0 19 3F 0D 24 57 78 0F 84 2B F8 8C 22 26 B8 D3 AF D4 9C 69 86 1E 6D 2B 31 B4 10 49 29 0A 7A ");
981
982 let mut recovered = String::new();
983 tdes.decrypt_array_into_string(iv, &cipher, &mut recovered);
984 println!("B =\t{}", recovered);
985 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
986 assert_eq!(recovered, message);
987 println!("-------------------------------");
988}More examples
957fn bigcryptor128_decrypt_array_ofb_into_string()
958{
959 println!("bigcryptor128_decrypt_array_ofb_into_string()");
960 use std::io::Write;
961 use std::fmt::Write as _;
962 use cryptocol::symmetric::{ BigCryptor128, AES_128, OFB };
963
964 // TAES_128 case
965 let mut taes = AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128)
966 + AES_128::decryptor_with_key_u128(0x_FEDCBA09876543211234567890ABCDEF_u128)
967 + AES_128::encryptor_with_key_u128(0x_1234567890ABCDEFFEDCBA0987654321_u128);
968 let iv = 0x_FEDCBA09876543211234567890ABCDEF_u128;
969 println!("IV = {:#034X}", iv);
970 let message = "In the beginning God created the heavens and the earth.";
971 println!("M =\t{}", message); let mut cipher = [0_u8; 55];
972 taes.encrypt_str_into_array(iv, &message, &mut cipher);
973 print!("C =\t");
974 for c in cipher.clone()
975 { print!("{:02X} ", c); }
976 println!();
977 let mut txt = String::new();
978 for c in cipher.clone()
979 { write!(txt, "{:02X} ", c); }
980 assert_eq!(txt, "17 89 CA DE E0 32 FA BE EB 2C 22 5C 43 00 12 ED B0 53 37 D6 64 AB 13 40 05 7E CC 2B 68 2F EA D7 12 C4 D5 A9 A3 E0 3E 62 A5 F1 95 B9 2E B1 73 16 1F DC 15 35 7C 9F 0D ");
981
982 let mut recovered = String::new();
983 taes.decrypt_array_into_string(iv, &cipher, &mut recovered);
984 println!("B =\t{}", recovered);
985 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
986 assert_eq!(recovered, message);
987 println!("-------------------------------");
988}4464fn aes_decrypt_array_ofb_into_string()
4465{
4466 println!("aes_decrypt_array_ofb_into_string()");
4467 use std::io::Write;
4468 use std::fmt::Write as _;
4469 use cryptocol::symmetric::{ AES_128, AES_192, AES_256, Rijndael_256_256, Rijndael_512_512, OFB };
4470
4471 // Normal case for AES-128
4472 let key = 0x_1234567890ABCDEF1234567890ABCDEF_u128;
4473 println!("K =\t{:#016X}", key);
4474 let mut a_aes = AES_128::new_with_key_u128(key);
4475 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
4476 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
4477
4478 let message = "In the beginning God created the heavens and the earth.";
4479 println!("M =\t{}", message);
4480 let mut cipher = [0_u8; 55];
4481 a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4482 print!("C =\t");
4483 for c in cipher.clone()
4484 { print!("{:02X} ", c); }
4485 println!();
4486 let mut txt = String::new();
4487 for c in cipher.clone()
4488 { write!(txt, "{:02X} ", c); }
4489 assert_eq!(txt, "B9 AD 7F CB 45 2A B9 31 89 15 16 47 4C A9 F3 D1 37 0B 09 FA 85 70 29 5C 99 3A DB F3 A9 A2 B8 C6 5B 39 CF A6 07 29 23 FF 8B 1E B6 26 29 D6 C8 19 41 A5 4C A0 49 82 F6 ");
4490
4491 let mut converted= String::new();
4492 a_aes.decrypt_array_into_string(iv, &cipher, &mut converted);
4493 println!("B =\t{}", converted);
4494 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4495 assert_eq!(converted, message);
4496 println!();
4497
4498 // Normal case for AES-192
4499 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
4500 print!("K =\t");
4501 for i in 0..24
4502 { print!("{:02X}", key[i]); }
4503 println!();
4504 let mut a_aes = AES_192::new_with_key(&key);
4505 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
4506 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
4507
4508 let message = "In the beginning God created the heavens and the earth.";
4509 println!("M =\t{}", message);
4510 let mut cipher = [0_u8; 55];
4511 a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4512 print!("C =\t");
4513 for c in cipher.clone()
4514 { print!("{:02X} ", c); }
4515 println!();
4516 let mut txt = String::new();
4517 for c in cipher.clone()
4518 { write!(txt, "{:02X} ", c); }
4519 assert_eq!(txt, "AF 45 AD 45 36 8B 38 2A 69 F1 50 31 1D F6 90 88 31 2D 9D 2C FD E3 21 61 10 F7 18 81 AB 72 7F 21 37 D0 34 87 EE 62 70 BC 45 89 B2 F7 F3 9F 1D 27 D1 2D 8F FC 73 0D 68 ");
4520
4521 let mut converted= String::new();
4522 a_aes.decrypt_array_into_string(iv, &cipher, &mut converted);
4523 println!("B =\t{}", converted);
4524 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4525 assert_eq!(converted, message);
4526 println!();
4527
4528 // Normal case for AES-256
4529 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
4530 print!("K =\t");
4531 for i in 0..32
4532 { print!("{:02X}", key[i]); }
4533 println!();
4534 let mut a_aes = AES_256::new_with_key(&key);
4535 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
4536 println!("IV =\t{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be());
4537
4538 let message = "In the beginning God created the heavens and the earth.";
4539 println!("M =\t{}", message);
4540 let mut cipher = [0_u8; 55];
4541 a_aes.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4542 print!("C =\t");
4543 for c in cipher.clone()
4544 { print!("{:02X} ", c); }
4545 println!();
4546 let mut txt = String::new();
4547 for c in cipher.clone()
4548 { write!(txt, "{:02X} ", c); }
4549 assert_eq!(txt, "9B 2F 4A C9 65 B3 7C 61 E7 DE 9B 97 F1 4C A2 B6 41 17 10 CE 43 B6 36 4A 7C 4C DD E9 73 E5 8F 5E C5 AC B0 0A 00 02 4F 85 A9 37 2C 2D D3 C7 29 F7 45 F1 5C C7 7A D3 B8 ");
4550
4551 let mut converted= String::new();
4552 a_aes.decrypt_array_into_string(iv, &cipher, &mut converted);
4553 println!("B =\t{}", converted);
4554 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4555 assert_eq!(converted, message);
4556 println!();
4557
4558 // Normal case for Rijndael-256-256
4559 let key = [0x12_u8, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF];
4560 print!("K =\t");
4561 for i in 0..32
4562 { print!("{:02X}", key[i]); }
4563 println!();
4564 let mut a_rijndael = Rijndael_256_256::new_with_key(&key);
4565 let iv = [0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32, 0x87654321_u32, 0xFEDCBA09_u32];
4566 println!("IV =\t{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", iv[0].to_be(), iv[1].to_be(), iv[2].to_be(), iv[3].to_be(), iv[4].to_be(), iv[5].to_be(), iv[6].to_be(), iv[7].to_be());
4567
4568 let message = "In the beginning God created the heavens and the earth.";
4569 println!("M =\t{}", message);
4570 let mut cipher = [0_u8; 55];
4571 a_rijndael.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4572 print!("C =\t");
4573 for c in cipher.clone()
4574 { print!("{:02X} ", c); }
4575 println!();
4576 let mut txt = String::new();
4577 for c in cipher.clone()
4578 { write!(txt, "{:02X} ", c); }
4579 assert_eq!(txt, "A0 84 D5 64 D1 8C FD B4 67 7F 5F 01 DA FF 1F A3 39 F8 F4 66 5C D4 54 87 2C CA 6C 2B AB C3 FD CC A5 C8 17 32 0C 84 F6 A6 71 D4 DA C4 DB 82 8C C7 67 BF CD 28 7C 52 DA ");
4580
4581 let mut converted= String::new();
4582 a_rijndael.decrypt_array_into_string(iv, &cipher, &mut converted);
4583 println!("B =\t{}", converted);
4584 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4585 assert_eq!(converted, message);
4586 println!();
4587
4588 // Normal case for Rijndael-512-512 for post-quantum
4589 use cryptocol::number::SharedArrays;
4590 use cryptocol::hash::SHA3_512;
4591 let mut sha3 = SHA3_512::new();
4592 sha3.absorb_str("Post-quantum");
4593 let key: [u8; 64] = sha3.get_hash_value_in_array();
4594 print!("K =\t");
4595 for i in 0..64
4596 { print!("{:02X}", key[i]); }
4597 println!();
4598 let mut a_rijndael = Rijndael_512_512::new_with_key(&key);
4599 sha3.absorb_str("Initialize");
4600 let mut iv = SharedArrays::<u32, 16, u8, 64>::new();
4601 iv.src = sha3.get_hash_value_in_array();
4602 let iv = unsafe { iv.des };
4603 print!("IV =\t");
4604 for i in 0..16
4605 { print!("{:08X}", iv[i].to_be()); }
4606 println!();
4607 let message = "In the beginning God created the heavens and the earth.";
4608 println!("M =\t{}", message);
4609 let mut cipher = [0_u8; 55];
4610 a_rijndael.encrypt_str_into_array(iv.clone(), &message, &mut cipher);
4611 print!("C =\t");
4612 for c in cipher.clone()
4613 { print!("{:02X} ", c); }
4614 println!();
4615 let mut txt = String::new();
4616 for c in cipher.clone()
4617 { write!(txt, "{:02X} ", c); }
4618 assert_eq!(txt, "B7 50 B4 3D 3C 4A C7 58 C2 1E 11 33 FD A8 BE AC 3A 56 2F DF 89 F4 57 4F A6 E8 98 61 A6 30 EF 7C 2B 09 2A 18 59 AF AC 5E 31 22 9A E1 8C 4A 63 6F 06 C5 F2 41 23 60 54 ");
4619
4620 let mut converted= String::new();
4621 a_rijndael.decrypt_array_into_string(iv, &cipher, &mut converted);
4622 println!("B =\t{}", converted);
4623 assert_eq!(converted, "In the beginning God created the heavens and the earth.");
4624 assert_eq!(converted, message);
4625 println!("-------------------------------");
4626}6612fn des_decrypt_array_ofb_into_string()
6613{
6614 println!("des_decrypt_array_ofb_into_string()");
6615 use std::io::Write;
6616 use std::fmt::Write as _;
6617 use cryptocol::symmetric::{ DES, DES_Expanded, OFB };
6618
6619 // Normal case
6620 let key = 0x_1234567890ABCDEF_u64;
6621 println!("K =\t{:#016X}", key);
6622 let mut a_des = DES::new_with_key_u64(key);
6623
6624 let message = "In the beginning God created the heavens and the earth.";
6625 println!("M =\t{}", message);
6626 let iv = 0x_FEDCBA0987654321_u64;
6627 println!("IV = {}", iv);
6628 let mut cipher = [0_u8; 55];
6629 a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6630 print!("C (16 rounds) =\t");
6631 for c in cipher.clone()
6632 { print!("{:02X} ", c); }
6633 println!();
6634 let mut txt = String::new();
6635 for c in cipher.clone()
6636 { write!(txt, "{:02X} ", c); }
6637 assert_eq!(txt, "2E 1E E1 51 FD B3 B0 4B 2A EF BC 49 21 FA C0 27 FB 9F DD BB 17 8D 21 3B 49 66 A2 94 AB 4D 08 8E B9 8D D6 7F 9F 8B 8D 0E E3 E7 5D F4 57 BB 96 2D 63 C3 2F 9E 71 8C 72 ");
6638
6639 let mut recovered = String::new();
6640 a_des.decrypt_array_into_string(iv, &cipher, &mut recovered);
6641 println!("B (16 rounds) =\t{}", recovered);
6642 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
6643 assert_eq!(recovered, message);
6644 println!();
6645
6646 // Expanded case for 128 rounds
6647 let key = 0x_1234567890ABCDEF_u64;
6648 println!("K =\t{:#016X}", key);
6649 let mut a_des = DES_Expanded::<128, 0x_8103_8103_8103_8103_8103_8103_8103_8103_u128>::new_with_key_u64(key);
6650
6651 let message = "In the beginning God created the heavens and the earth.";
6652 println!("M =\t{}", message);
6653 let iv = 0x_FEDCBA0987654321_u64;
6654 println!("IV = {}", iv);
6655 let mut cipher = [0_u8; 55];
6656 a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6657 print!("C (128 rounds) =\t");
6658 for c in cipher.clone()
6659 { print!("{:02X} ", c); }
6660 println!();
6661 let mut txt = String::new();
6662 for c in cipher.clone()
6663 { write!(txt, "{:02X} ", c); }
6664 assert_eq!(txt, "19 B0 8F 23 01 31 B3 95 5F 0F 7C D5 96 55 DB 98 76 6C C7 23 49 6D 35 9B FD DE B7 1A 6B B2 8A EF B6 63 DB A5 50 F6 07 30 8C 75 28 32 25 32 33 77 B0 46 FE 96 1C F7 6B ");
6665
6666 let mut recovered = String::new();
6667 a_des.decrypt_array_into_string(iv, &cipher, &mut recovered);
6668 println!("B (128 rounds) =\t{}", recovered);
6669 assert_eq!(recovered, "In the beginning God created the heavens and the earth.");
6670 assert_eq!(recovered, message);
6671 println!();
6672
6673 // Expanded case for 0 rounds which means that key is meaningless
6674 let key1 = 0x_1234567890ABCDEF_u64;
6675 let key2 = 0_u64;
6676 println!("K =\t{:#016X}", key);
6677 let mut c_des = DES_Expanded::<0, 0>::new_with_key_u64(key1);
6678 let mut d_des = DES_Expanded::<0, 0>::new_with_key_u64(key2);
6679
6680 let message = "In the beginning God created the heavens and the earth.";
6681 println!("M =\t{}", message);
6682 let iv = 0x_FEDCBA0987654321_u64;
6683 println!("IV = {}", iv);
6684 let mut cipher1 = [0_u8; 55];
6685 let mut cipher2 = [0_u8; 55];
6686 c_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher1);
6687 d_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher2);
6688 print!("C (0 rounds) =\t");
6689 for c in cipher1.clone()
6690 { print!("{:02X} ", c); }
6691 println!();
6692 let mut txt = String::new();
6693 for c in cipher1.clone()
6694 { write!(txt, "{:02X} ", c); }
6695 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
6696 print!("D (0 rounds) =\t");
6697 for c in cipher2.clone()
6698 { print!("{:02X} ", c); }
6699 println!();
6700 let mut txt = String::new();
6701 for c in cipher2.clone()
6702 { write!(txt, "{:02X} ", c); }
6703 assert_eq!(txt, "5B ED BA 3F 6E 10 CC 9F 44 24 0C E9 67 D3 B2 99 32 C4 F5 2F 26 16 9E 98 40 37 00 E3 29 CE B4 9B 32 EB FF 2A 70 10 82 8E 01 22 0B E3 29 CE B4 9B 32 E6 FB 39 72 1D C2 ");
6704
6705 let mut recovered1 = String::new();
6706 let mut recovered2 = String::new();
6707 c_des.decrypt_array_into_string(iv, &cipher1, &mut recovered1);
6708 d_des.decrypt_array_into_string(iv, &cipher2, &mut recovered2);
6709 println!("B1 (0 rounds) =\t{}", recovered1);
6710 println!("B2 (0 rounds) =\t{}", recovered2);
6711 assert_eq!(recovered1, "In the beginning God created the heavens and the earth.");
6712 assert_eq!(recovered2, "In the beginning God created the heavens and the earth.");
6713 assert_eq!(recovered1, message);
6714 assert_eq!(recovered2, message);
6715 assert_eq!(recovered1, recovered2);
6716 println!();
6717
6718 // Normal case for the message of 0 bytes
6719 let key = 0x_1234567890ABCDEF_u64;
6720 println!("K =\t{:#016X}", key);
6721 let mut a_des = DES::new_with_key_u64(key);
6722
6723 let message = "";
6724 println!("M =\t{}", message);
6725 let iv = 0x_FEDCBA0987654321_u64;
6726 println!("IV = {}", iv);
6727 let mut cipher = [0_u8; 0];
6728 a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6729 print!("C =\t");
6730 for c in cipher.clone()
6731 { print!("{:02X} ", c); }
6732 println!();
6733 let mut txt = String::new();
6734 for c in cipher.clone()
6735 { write!(txt, "{:02X} ", c); }
6736 assert_eq!(txt, "");
6737
6738 let mut recovered = String::new();
6739 a_des.decrypt_array_into_string(iv, &cipher, &mut recovered);
6740 println!("B =\t{}", recovered);
6741 assert_eq!(recovered, "");
6742 assert_eq!(recovered, message);
6743 println!();
6744
6745 // Normal case for the message shorter than 8 bytes
6746 let key = 0x_1234567890ABCDEF_u64;
6747 println!("K =\t{:#016X}", key);
6748 let mut a_des = DES::new_with_key_u64(key);
6749
6750 let message = "7 bytes";
6751 println!("M =\t{}", message);
6752 let iv = 0x_FEDCBA0987654321_u64;
6753 println!("IV = {}", iv);
6754 let mut cipher = [0_u8; 7];
6755 a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6756 print!("C =\t");
6757 for c in cipher.clone()
6758 { print!("{:02X} ", c); }
6759 println!();
6760 let mut txt = String::new();
6761 for c in cipher.clone()
6762 { write!(txt, "{:02X} ", c); }
6763 assert_eq!(txt, "50 50 A3 5C E1 B3 E3 ");
6764
6765 let mut recovered = String::new();
6766 a_des.decrypt_array_into_string(iv, &cipher, &mut recovered);
6767 println!("B =\t{}", recovered);
6768 assert_eq!(recovered, "7 bytes");
6769 assert_eq!(recovered, message);
6770 println!();
6771
6772 // Normal case for the message of 8 bytes
6773 let key = 0x_1234567890ABCDEF_u64;
6774 println!("K =\t{:#016X}", key);
6775 let mut a_des = DES::new_with_key_u64(key);
6776
6777 let message = "I am OK.";
6778 println!("M =\t{}", message);
6779 let iv = 0x_FEDCBA0987654321_u64;
6780 println!("IV = {}", iv);
6781 let mut cipher = [0_u8; 8];
6782 a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6783 print!("C =\t");
6784 for c in cipher.clone()
6785 { print!("{:02X} ", c); }
6786 println!();
6787 let mut txt = String::new();
6788 for c in cipher.clone()
6789 { write!(txt, "{:02X} ", c); }
6790 assert_eq!(txt, "2E 50 A0 48 B5 99 DB 07 ");
6791
6792 let mut recovered = String::new();
6793 a_des.decrypt_array_into_string(iv, &cipher, &mut recovered);
6794 println!("B =\t{}", recovered);
6795 assert_eq!(recovered, "I am OK.");
6796 assert_eq!(recovered, message);
6797 println!();
6798
6799 // Normal case for the message longer than 8 bytes and shorter than 16 bytes
6800 let key = 0x_1234567890ABCDEF_u64;
6801 println!("K =\t{:#016X}", key);
6802 let mut a_des = DES::new_with_key_u64(key);
6803
6804 let message = "PARK Youngho";
6805 println!("M =\t{}", message);
6806 let iv = 0x_FEDCBA0987654321_u64;
6807 println!("IV = {}", iv);
6808 let mut cipher = [0_u8; 12];
6809 a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6810 print!("C =\t");
6811 for c in cipher.clone()
6812 { print!("{:02X} ", c); }
6813 println!();
6814 let mut txt = String::new();
6815 for c in cipher.clone()
6816 { write!(txt, "{:02X} ", c); }
6817 assert_eq!(txt, "37 31 93 6E B5 8F FF 5C 21 EF BD 48 ");
6818
6819 let mut recovered = String::new();
6820 a_des.decrypt_array_into_string(iv, &cipher, &mut recovered);
6821 println!("B =\t{}", recovered);
6822 assert_eq!(recovered, "PARK Youngho");
6823 assert_eq!(recovered, message);
6824 println!();
6825
6826 // Normal case for the message of 16 bytes
6827 let key = 0x_1234567890ABCDEF_u64;
6828 println!("K =\t{:#016X}", key);
6829 let mut a_des = DES::new_with_key_u64(key);
6830
6831 let message = "고맙습니다.";
6832 println!("M =\t{}", message);
6833 let iv = 0x_FEDCBA0987654321_u64;
6834 println!("IV = {}", iv);
6835 let mut cipher = [0_u8; 16];
6836 a_des.encrypt_into_array(iv, message.as_ptr(), message.len() as u64, &mut cipher);
6837 print!("C =\t");
6838 for c in cipher.clone()
6839 { print!("{:02X} ", c); }
6840 println!();
6841 let mut txt = String::new();
6842 for c in cipher.clone()
6843 { write!(txt, "{:02X} ", c); }
6844 assert_eq!(txt, "8D C3 61 CE 32 4F 7C A3 FA 63 5E AF A4 18 0A 6E ");
6845
6846 let mut recovered = String::new();
6847 a_des.decrypt_array_into_string(iv, &cipher, &mut recovered);
6848 println!("B =\t{}", recovered);
6849 assert_eq!(recovered, "고맙습니다.");
6850 assert_eq!(recovered, message);
6851 println!("-------------------------------");
6852}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.