pub fn decrypt_unknown_string_ecb_oracle(k: &[u8]) -> Vec<u8> {
    let block_size = {
        let unknown_output = ecb_oracle(&Vec::new(), k).unwrap();
        let mut current_length = unknown_output.len();

        let mut m = Vec::new();
        while current_length == unknown_output.len() {
            m.push(b'A');
            current_length = ecb_oracle(&m, k).unwrap().len();
        }

        current_length - unknown_output.len()
    };

    let is_ecb = {
        let mut block = Vec::with_capacity(block_size * 2);
        for _ in 0..block.capacity() {
            block.push(b'A');
        }

        let is_ecb = crate::set1::detect_aes_128_ecb_mode(
            hex::encode(ecb_oracle(&block, k).unwrap()).as_bytes(),
        )
        .unwrap();

        is_ecb
    };

    if !is_ecb {
        panic!("the cipher is not using ecb mode");
    }

    let end = ecb_oracle(&Vec::new(), k).unwrap().len();
    let mut cracked_text = Vec::new();

    while cracked_text.len() != end {
        cracked_text.push({
            let mut prefix = block_size - ((1 + cracked_text.len()) % block_size);
            if prefix == block_size {
                prefix -= block_size;
            }

            let mut base = Vec::new();

            for _ in 0..prefix {
                base.push(b'A');
            }

            let expanded_length: usize = base.len() + 1 + cracked_text.len();
            let ciphertext = ecb_oracle(&base, k).unwrap();

            let mut dict = HashMap::new();
            for i in 0..256 {
                let mut b = base.clone();
                b.extend_from_slice(&cracked_text);
                b.push(i as u8);

                let mut c = ecb_oracle(&b, k).unwrap();
                c.truncate(expanded_length);

                dict.insert(c, b);
            }

            match dict.get(&ciphertext[..expanded_length]) {
                Some(v) => *v.get(v.len() - 1).unwrap(),
                None => b'\0',
            }
        })
    }

    cracked_text
}
/// Decrypts the unknown string for the ecb oracle
/// described in problem: <https://cryptopals.com/sets/2/challenges/12>
Decrypts the unknown string for the ecb oracle
described in problem.
Decrypts the unknown string for the ecb oracle described in problem .
pub fn decrypt_unknown_string_ecb_oracle(k: &[u8]) -> Vec<u8> {
    let block_size = {
        let unknown_output = ecb_oracle(&Vec::new(), k).unwrap();
        let mut current_length = unknown_output.len();
        let mut m = Vec::new();
        while current_length == unknown_output.len() {
            m.push(b'A');
            current_length = ecb_oracle(&m, k).unwrap().len();
        }
        current_length - unknown_output.len()
    };
    let is_ecb = {
        let mut block = Vec::with_capacity(block_size * 2);
        for _ in 0..block.capacity() {
            block.push(b'A');
        }
        let is_ecb = crate::set1::detect_aes_128_ecb_mode(
            hex::encode(ecb_oracle(&block, k).unwrap()).as_bytes(),
        )
        .unwrap();
        is_ecb
    };
    if !is_ecb {
        panic!("the cipher is not using ecb mode");
    }
    let end = ecb_oracle(&Vec::new(), k).unwrap().len();
    let mut cracked_text = Vec::new();
    while cracked_text.len() != end {
        cracked_text.push({
            let mut prefix = block_size - ((1 + cracked_text.len()) % block_size);
            if prefix == block_size {
                prefix -= block_size;
            }
            let mut base = Vec::new();
            for _ in 0..prefix {
                base.push(b'A');
            }
            let expanded_length: usize = base.len() + 1 + cracked_text.len();
            let ciphertext = ecb_oracle(&base, k).unwrap();
            let mut dict = HashMap::new();
            for i in 0..256 {
                let mut b = base.clone();
                b.extend_from_slice(&cracked_text);
                b.push(i as u8);
                let mut c = ecb_oracle(&b, k).unwrap();
                c.truncate(expanded_length);
                dict.insert(c, b);
            }
            match dict.get(&ciphertext[..expanded_length]) {
                Some(v) => *v.get(v.len() - 1).unwrap(),
                None => b'\0',
            }
        })
    }
    cracked_text
}
pub fn decrypt_unknown_string_ecb_oracle ( k : & [ u8 ] ) -> Vec < u8 > { let block_size = { let unknown_output = ecb_oracle ( & Vec :: new ( ) , k ) . unwrap ( ) ; let mut current_length = unknown_output . len ( ) ; let mut m = Vec :: new ( ) ; while current_length == unknown_output . len ( ) { m . push ( b'A' ) ; current_length = ecb_oracle ( & m , k ) . unwrap ( ) . len ( ) ; } current_length - unknown_output . len ( ) } ; let is_ecb = { let mut block = Vec :: with_capacity ( block_size * 2 ) ; for _ in 0 .. block . capacity ( ) { block . push ( b'A' ) ; } let is_ecb = crate :: set1 :: detect_aes_128_ecb_mode ( hex :: encode ( ecb_oracle ( & block , k ) . unwrap ( ) ) . as_bytes ( ) , ) . unwrap ( ) ; is_ecb } ; if ! is_ecb { panic ! ( " " ) ; } let end = ecb_oracle ( & Vec :: new ( ) , k ) . unwrap ( ) . len ( ) ; let mut cracked_text = Vec :: new ( ) ; while cracked_text . len ( ) != end { cracked_text . push ( { let mut prefix = block_size - ( ( 1 + cracked_text . len ( ) ) % block_size ) ; if prefix == block_size { prefix -= block_size ; } let mut base = Vec :: new ( ) ; for _ in 0 .. prefix { base . push ( b'A' ) ; } let expanded_length : usize = base . len ( ) + 1 + cracked_text . len ( ) ; let ciphertext = ecb_oracle ( & base , k ) . unwrap ( ) ; let mut dict = HashMap :: new ( ) ; for i in 0 .. 256 { let mut b = base . clone ( ) ; b . extend_from_slice ( & cracked_text ) ; b . push ( i as u8 ) ; let mut c = ecb_oracle ( & b , k ) . unwrap ( ) ; c . truncate ( expanded_length ) ; dict . insert ( c , b ) ; } match dict . get ( & ciphertext [ .. expanded_length ] ) { Some ( v ) => * v . get ( v . len ( ) - 1 ) . unwrap ( ) , None => b'\0' , } } ) } cracked_text }
Decrypts the unknown string for the ecb oracle
described in problem: <https://cryptopals.com/sets/2/challenges/12>
Decrypts the unknown string for the ecb oracle described in problem : <https : // cryptopals . com / sets / 2 / challenges / 12 >
k
&[u8]