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

    if b.len() % BLOCK_SIZE != 0 {
        panic!("unsuported bytes length");
    }

    let blocks = b.len() / BLOCK_SIZE;

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

    loop {
        let current = &b[iteration * BLOCK_SIZE..(iteration + 1) * BLOCK_SIZE];
        let plaintext = crate::aes_ecb::decrypt_aes_128_ecb(current, k)?;

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

        result.extend_from_slice(&un_xored);

        prev = current;

        iteration += 1;
        if iteration == blocks {
            break;
        }
    }

    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 decrypt_aes_128_cbc(
    b: &[u8],
    k: &[u8],
    iv: &[u8],
) -> Result<Vec<u8>, symmetriccipher::SymmetricCipherError> {
    const BLOCK_SIZE: usize = 16; // 128 bits
    if b.len() % BLOCK_SIZE != 0 {
        panic!("unsuported bytes length");
    }
    let blocks = b.len() / BLOCK_SIZE;
    let mut result = Vec::new();
    let mut iteration = 0;
    let mut prev = iv;
    loop {
        let current = &b[iteration * BLOCK_SIZE..(iteration + 1) * BLOCK_SIZE];
        let plaintext = crate::aes_ecb::decrypt_aes_128_ecb(current, k)?;
        let un_xored = crate::set1::xor_buffers(
            hex::encode(plaintext).as_bytes(),
            hex::encode(prev).as_bytes(),
        )
        .unwrap();
        result.extend_from_slice(&un_xored);
        prev = current;
        iteration += 1;
        if iteration == blocks {
            break;
        }
    }
    Ok(result)
}
pub fn decrypt_aes_128_cbc ( b : & [ u8 ] , k : & [ u8 ] , iv : & [ u8 ] , ) -> Result < Vec < u8 > , symmetriccipher :: SymmetricCipherError > { const BLOCK_SIZE : usize = 16 ; // 128 bits if b . len ( ) % BLOCK_SIZE != 0 { panic ! ( " " ) ; } let blocks = b . len ( ) / BLOCK_SIZE ; let mut result = Vec :: new ( ) ; let mut iteration = 0 ; let mut prev = iv ; loop { let current = & b [ iteration * BLOCK_SIZE .. ( iteration + 1 ) * BLOCK_SIZE ] ; let plaintext = crate :: aes_ecb :: decrypt_aes_128_ecb ( current , k ) ? ; let un_xored = crate :: set1 :: xor_buffers ( hex :: encode ( plaintext ) . as_bytes ( ) , hex :: encode ( prev ) . as_bytes ( ) , ) . unwrap ( ) ; result . extend_from_slice ( & un_xored ) ; prev = current ; iteration += 1 ; if iteration == blocks { break ; } } 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]