pub fn encrypt_aes_128_cbc(
    b: &[u8],
    k: &[u8],
    iv: &[u8],
) -> Result<Vec<u8>, symmetriccipher::SymmetricCipherError> {
    const BLOCK_SIZE: usize = 16; // 128 bits

    let mut result = Vec::new();
    let mut iteration = 0;
    let mut prev = Vec::from(iv);

    loop {
        let beg = iteration * BLOCK_SIZE;
        let mut end = (iteration + 1) * BLOCK_SIZE;

        let done = end >= b.len();

        if done && end > b.len() {
            end = beg + (b.len() - beg);
        }

        let current = crate::padding::pkcs7(&b[beg..end], BLOCK_SIZE);

        let xored = crate::set1::xor_buffers(
            hex::encode(current).as_bytes(),
            hex::encode(prev).as_bytes(),
        )
        .unwrap();

        let ciphertext = crate::aes_ecb::encrypt_aes_128_ecb(&xored, k)?;

        result.extend_from_slice(&ciphertext);
        prev = ciphertext;

        if done {
            break;
        }

        iteration += 1;
    }

    Ok(result)
}
/// Implement CBC mode
///
/// Solution for problem 2 from set2: <https://cryptopals.com/sets/2/challenges/10>
Implement CBC mode
Solution for problem 2 from set2.
Implement CBC mode Solution for problem 2 from set2 .
pub fn encrypt_aes_128_cbc(
    b: &[u8],
    k: &[u8],
    iv: &[u8],
) -> Result<Vec<u8>, symmetriccipher::SymmetricCipherError> {
    const BLOCK_SIZE: usize = 16; // 128 bits
    let mut result = Vec::new();
    let mut iteration = 0;
    let mut prev = Vec::from(iv);
    loop {
        let beg = iteration * BLOCK_SIZE;
        let mut end = (iteration + 1) * BLOCK_SIZE;
        let done = end >= b.len();
        if done && end > b.len() {
            end = beg + (b.len() - beg);
        }
        let current = crate::padding::pkcs7(&b[beg..end], BLOCK_SIZE);
        let xored = crate::set1::xor_buffers(
            hex::encode(current).as_bytes(),
            hex::encode(prev).as_bytes(),
        )
        .unwrap();
        let ciphertext = crate::aes_ecb::encrypt_aes_128_ecb(&xored, k)?;
        result.extend_from_slice(&ciphertext);
        prev = ciphertext;
        if done {
            break;
        }
        iteration += 1;
    }
    Ok(result)
}
pub fn encrypt_aes_128_cbc ( b : & [ u8 ] , k : & [ u8 ] , iv : & [ u8 ] , ) -> Result < Vec < u8 > , symmetriccipher :: SymmetricCipherError > { const BLOCK_SIZE : usize = 16 ; // 128 bits let mut result = Vec :: new ( ) ; let mut iteration = 0 ; let mut prev = Vec :: from ( iv ) ; loop { let beg = iteration * BLOCK_SIZE ; let mut end = ( iteration + 1 ) * BLOCK_SIZE ; let done = end >= b . len ( ) ; if done && end > b . len ( ) { end = beg + ( b . len ( ) - beg ) ; } let current = crate :: padding :: pkcs7 ( & b [ beg .. end ] , BLOCK_SIZE ) ; let xored = crate :: set1 :: xor_buffers ( hex :: encode ( current ) . as_bytes ( ) , hex :: encode ( prev ) . as_bytes ( ) , ) . unwrap ( ) ; let ciphertext = crate :: aes_ecb :: encrypt_aes_128_ecb ( & xored , k ) ? ; result . extend_from_slice ( & ciphertext ) ; prev = ciphertext ; if done { break ; } iteration += 1 ; } Ok ( result ) }
Implement CBC mode
Solution for problem 2 from set2: <https://cryptopals.com/sets/2/challenges/10>
Implement CBC mode Solution for problem 2 from set2 : <https : // cryptopals . com / sets / 2 / challenges / 10 >
b
&[u8]
k
&[u8]
iv
&[u8]