pub struct CBCEncrypter<'a, B: Block> { /* private fields */ }Expand description
CBCEncrypter implements encryption in cipher block chaining mode, using the given Block.
Implementations§
Source§impl<'a, B: Block> CBCEncrypter<'a, B>
impl<'a, B: Block> CBCEncrypter<'a, B>
Sourcepub fn new(b: &'a B, iv: &[u8]) -> Self
pub fn new(b: &'a B, iv: &[u8]) -> Self
new returns a BlockMode which encrypts in cipher block chaining mode, using the given Block. The length of iv must be the same as the Block’s block size.
Examples found in repository?
examples/aes.rs (line 70)
59fn aes_cbc(key: &[u8], plaintext: &[u8]) {
60 let block = aes::Cipher::new(key).unwrap();
61 assert!(
62 plaintext.len() % aes::BLOCK_SIZE == 0,
63 "Plaintext length must be a multiple of the block size"
64 );
65
66 // Using the initialization vector of all zeroes for demo purposes.
67 // In practice the IV should be unique for each encryption operation and upredictable,
68 // for example, generated using a secure random generator.
69 let iv = vec![0; aes::BLOCK_SIZE];
70 let mut mode = cipher::CBCEncrypter::new(&block, &iv);
71
72 let mut encrypted = vec![0; plaintext.len()];
73 mode.crypt_blocks(&mut encrypted, plaintext);
74
75 // let mut mode = cipher::CBCDecrypter::new(&block, &iv);
76 // let mut decrypted = vec![0; plaintext.len()];
77 // mode.crypt_blocks(&mut decrypted, &encrypted);
78
79 // println!("plaintext: {}", String::from_utf8_lossy(plaintext));
80 println!("CBC encrypted: {}", hex::encode_to_string(&encrypted));
81 // println!("decrypted: {}", String::from_utf8_lossy(&decrypted));
82 // println!();
83}pub fn set_iv(&mut self, iv: &[u8])
Trait Implementations§
Source§impl<'a, B: Block> BlockMode for CBCEncrypter<'a, B>
impl<'a, B: Block> BlockMode for CBCEncrypter<'a, B>
Source§fn block_size(&self) -> usize
fn block_size(&self) -> usize
block_size returns the mode’s block size.
Source§fn crypt_blocks(&mut self, dst: &mut [u8], src: &[u8])
fn crypt_blocks(&mut self, dst: &mut [u8], src: &[u8])
crypt_blocks encrypts or decrypts a number of blocks. The length of
src must be a multiple of the block size.
If dst.len() < src.len(), crypt_blocks should panic. It is acceptable
to pass a dst bigger than src, and in that case, crypt_blocks will
only update dst[..src.len()] and will not touch the rest of dst. Read more
Source§fn crypt_blocks_inplace(&mut self, data: &mut [u8])
fn crypt_blocks_inplace(&mut self, data: &mut [u8])
crypt_blocks_inplace is similar to crypt_blocks, but encrypts or decrypts
a number of blocks in the same buffer.
Auto Trait Implementations§
impl<'a, B> Freeze for CBCEncrypter<'a, B>
impl<'a, B> RefUnwindSafe for CBCEncrypter<'a, B>where
B: RefUnwindSafe,
impl<'a, B> Send for CBCEncrypter<'a, B>where
B: Sync,
impl<'a, B> Sync for CBCEncrypter<'a, B>where
B: Sync,
impl<'a, B> Unpin for CBCEncrypter<'a, B>
impl<'a, B> UnwindSafe for CBCEncrypter<'a, B>where
B: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more