pub fn cbc_bitflipping_attack_recover_key(k: &[u8]) -> Result<Vec<u8>, crate::Error> {
    let block_size = 16;
    let A = vec![b'A'; block_size];
    let B = vec![b'B'; block_size];
    let C = vec![b'C'; block_size];

    let mut secret_msg = Vec::new();
    secret_msg.extend_from_slice(&A);
    secret_msg.extend_from_slice(&B);
    secret_msg.extend_from_slice(&C);

    let ciphertext = &crate::set2::encrypt_user_data(&secret_msg, k, k).unwrap();
    let prefix_length = "comment1=cooking%20MCs;userdata=".len();

    let mut mutated_ciphertext = Vec::new();
    mutated_ciphertext.extend_from_slice(&ciphertext[prefix_length..prefix_length + block_size]);
    mutated_ciphertext.extend_from_slice(&vec![0; block_size]);
    mutated_ciphertext.extend_from_slice(&ciphertext[prefix_length..prefix_length + block_size]);

    let result = decrypt_user_data_with_error(&mutated_ciphertext, k);
    if let Err(err) = result {
        if let crate::Error::InvalidASCII(msg) = err {
            let p1 = &msg[..block_size];
            let p3 = &msg[2 * block_size..];

            return Ok(crate::set1::xor_buffers(
                hex::encode(p1).as_bytes(),
                hex::encode(p3).as_bytes(),
            )
            .unwrap());
        }

        Err(err)
    } else {
        Err(crate::Error::FailedToCrackKey)
    }
}
/// CBC bitflipping attack recover key
///
/// Solution for problem 3 from set 4: <https://cryptopals.com/sets/4/challenges/27>
CBC bitflipping attack recover key
Solution for problem 3 from set 4.
CBC bitflipping attack recover key Solution for problem 3 from set 4 .
pub fn cbc_bitflipping_attack_recover_key(k: &[u8]) -> Result<Vec<u8>, crate::Error> {
    let block_size = 16;
    let A = vec![b'A'; block_size];
    let B = vec![b'B'; block_size];
    let C = vec![b'C'; block_size];
    let mut secret_msg = Vec::new();
    secret_msg.extend_from_slice(&A);
    secret_msg.extend_from_slice(&B);
    secret_msg.extend_from_slice(&C);
    let ciphertext = &crate::set2::encrypt_user_data(&secret_msg, k, k).unwrap();
    let prefix_length = "comment1=cooking%20MCs;userdata=".len();
    let mut mutated_ciphertext = Vec::new();
    mutated_ciphertext.extend_from_slice(&ciphertext[prefix_length..prefix_length + block_size]);
    mutated_ciphertext.extend_from_slice(&vec![0; block_size]);
    mutated_ciphertext.extend_from_slice(&ciphertext[prefix_length..prefix_length + block_size]);
    let result = decrypt_user_data_with_error(&mutated_ciphertext, k);
    if let Err(err) = result {
        if let crate::Error::InvalidASCII(msg) = err {
            let p1 = &msg[..block_size];
            let p3 = &msg[2 * block_size..];
            return Ok(crate::set1::xor_buffers(
                hex::encode(p1).as_bytes(),
                hex::encode(p3).as_bytes(),
            )
            .unwrap());
        }
        Err(err)
    } else {
        Err(crate::Error::FailedToCrackKey)
    }
}
pub fn cbc_bitflipping_attack_recover_key ( k : & [ u8 ] ) -> Result < Vec < u8 > , crate :: Error > { let block_size = 16 ; let A = vec ! [ b'A' block_size ] ; let B = vec ! [ b'B' block_size ] ; let C = vec ! [ b'C' block_size ] ; let mut secret_msg = Vec :: new ( ) ; secret_msg . extend_from_slice ( & A ) ; secret_msg . extend_from_slice ( & B ) ; secret_msg . extend_from_slice ( & C ) ; let ciphertext = & crate :: set2 :: encrypt_user_data ( & secret_msg , k , k ) . unwrap ( ) ; let prefix_length = " " . len ( ) ; let mut mutated_ciphertext = Vec :: new ( ) ; mutated_ciphertext . extend_from_slice ( & ciphertext [ prefix_length .. prefix_length + block_size ] ) ; mutated_ciphertext . extend_from_slice ( & vec ! [ 0 block_size ] ) ; mutated_ciphertext . extend_from_slice ( & ciphertext [ prefix_length .. prefix_length + block_size ] ) ; let result = decrypt_user_data_with_error ( & mutated_ciphertext , k ) ; if let Err ( err ) = result { if let crate :: Error :: InvalidASCII ( msg ) = err { let p1 = & msg [ .. block_size ] ; let p3 = & msg [ 2 * block_size .. ] ; return Ok ( crate :: set1 :: xor_buffers ( hex :: encode ( p1 ) . as_bytes ( ) , hex :: encode ( p3 ) . as_bytes ( ) , ) . unwrap ( ) ) ; } Err ( err ) } else { Err ( crate :: Error :: FailedToCrackKey ) } }
CBC bitflipping attack recover key
Solution for problem 3 from set 4: <https://cryptopals.com/sets/4/challenges/27>
CBC bitflipping attack recover key Solution for problem 3 from set 4 : <https : // cryptopals . com / sets / 4 / challenges / 27 >
k
&[u8]