pub struct SymmetricAlgorithmKey { /* private fields */ }
Expand description
Symmetric algorithm key
Implementations§
Source§impl SymmetricAlgorithmKey
impl SymmetricAlgorithmKey
Sourcepub fn key_size(&self) -> Result<usize>
pub fn key_size(&self) -> Result<usize>
Returns the key value size in bits.
§Examples
let key = algo.new_key("0123456789ABCDEF".as_bytes()).unwrap();
let key_size = key.key_size().unwrap();
assert_eq!(128, key_size);
Sourcepub fn block_size(&self) -> Result<usize>
pub fn block_size(&self) -> Result<usize>
Returns the block size in bytes.
This can be useful to find what size the IV should be.
§Examples
let key = algo.new_key("0123456789ABCDEF".as_bytes()).unwrap();
let key_size = key.block_size().unwrap();
assert_eq!(16, key_size);
Sourcepub fn set_msg_block_len(&mut self, len: usize) -> Result<()>
pub fn set_msg_block_len(&mut self, len: usize) -> Result<()>
Sets the message block length.
This can be set on any key handle that has the CFB chaining mode set. By default, it is set to 1 for 8-bit CFB. Setting it to the block size in bytes causes full-block CFB to be used. For XTS keys it is used to set the size, in bytes, of the XTS Data Unit (commonly 512 or 4096).
See here for more info.
Sourcepub fn encrypt(
&self,
iv: Option<&mut [u8]>,
data: &[u8],
padding: Option<Padding>,
) -> Result<Buffer>
pub fn encrypt( &self, iv: Option<&mut [u8]>, data: &[u8], padding: Option<Padding>, ) -> Result<Buffer>
Encrypts data using the symmetric key
The IV is not needed for ChainingMode::Ecb
, so None
should be used
in this case.
For chaining modes needing an IV, None
can be used and a default IV will be used.
This is not documented on Microsoft’s website and is therefore not recommended.
The data is padded with zeroes to a multiple of the block size of the cipher. If the data length equals the block size of the cipher, one additional block of padding is appended to the data.
§Examples
let key = algo.new_key("0123456789ABCDEF".as_bytes()).unwrap();
let mut iv = b"_THIS_IS_THE_IV_".to_vec();
let plaintext = "THIS_IS_THE_DATA".as_bytes();
let ciphertext = key.encrypt(Some(&mut iv), plaintext, Some(Padding::Block)).unwrap();
assert_eq!(ciphertext.as_slice(), [
0xE4, 0xD9, 0x90, 0x64, 0xA6, 0xA6, 0x5F, 0x7E,
0x70, 0xDB, 0xF9, 0xDD, 0xE7, 0x0D, 0x6F, 0x6A,
0x0C, 0xEC, 0xDB, 0xAD, 0x01, 0xB4, 0xB1, 0xDE,
0xB4, 0x4A, 0xB8, 0xA0, 0xEA, 0x0E, 0x8F, 0x31]);
Sourcepub fn decrypt(
&self,
iv: Option<&mut [u8]>,
data: &[u8],
padding: Option<Padding>,
) -> Result<Buffer>
pub fn decrypt( &self, iv: Option<&mut [u8]>, data: &[u8], padding: Option<Padding>, ) -> Result<Buffer>
Decrypts data using the symmetric key
The IV is not needed for ChainingMode::Ecb
, so None
should be used
in this case.
For chaining modes needing an IV, None
can be used and a default IV will be used.
This is not documented on Microsoft’s website and is therefore not recommended.
§Examples
let key = algo.new_key("0123456789ABCDEF".as_bytes()).unwrap();
let mut iv = b"_THIS_IS_THE_IV_".to_vec();
let ciphertext = [
0xE4, 0xD9, 0x90, 0x64, 0xA6, 0xA6, 0x5F, 0x7E,
0x70, 0xDB, 0xF9, 0xDD, 0xE7, 0x0D, 0x6F, 0x6A,
0x0C, 0xEC, 0xDB, 0xAD, 0x01, 0xB4, 0xB1, 0xDE,
0xB4, 0x4A, 0xB8, 0xA0, 0xEA, 0x0E, 0x8F, 0x31
];
let plaintext = key.decrypt(Some(&mut iv), &ciphertext, Some(Padding::Block)).unwrap();
assert_eq!(&plaintext.as_slice()[..16], "THIS_IS_THE_DATA".as_bytes());