CBCEncrypter

Struct CBCEncrypter 

Source
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>

Source

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}
Source

pub fn set_iv(&mut self, iv: &[u8])

Trait Implementations§

Source§

impl<'a, B: Block> BlockMode for CBCEncrypter<'a, B>

Source§

fn block_size(&self) -> usize

block_size returns the mode’s block size.
Source§

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])

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.