pub fn edit(b: &[u8], k: &[u8], offset: usize, nb: &[u8]) -> Vec<u8> {
    if offset < 0 || offset > b.len() {
        panic!("offset out of bounds")
    }

    const block_size: usize = 16;

    let start = offset / block_size;
    let end = ((offset + nb.len() - 1) / block_size) + 1;

    let mut keystream = Vec::new();
    for b in start..end {
        let mut v = Vec::from([0 as u8; 8]);
        v.extend_from_slice(&(b as u64).to_le_bytes());

        keystream.extend_from_slice(&crate::aes_ecb::encrypt_aes_128_ecb(&v, k).unwrap());
    }

    // the precise address is offet % 16: (offset % 16)+nb.len()
    let ciphertext = crate::set1::xor_buffers(
        hex::encode(nb).as_bytes(),
        hex::encode(&keystream[(offset % block_size)..(offset % block_size) + nb.len()]).as_bytes(),
    )
    .unwrap();

    let mut result = Vec::new();
    result.extend_from_slice(&b[..offset]);
    result.extend_from_slice(&ciphertext);
    result.extend_from_slice(&b[offset + nb.len()..]);

    result
}
/// edit replaces the old text at a given offset with new text
///
///  Solution for problem 1 from set 4: <https://cryptopals.com/sets/4/challenges/25>
edit replaces the old text at a given offset with new text
Solution for problem 1 from set 4.
edit replaces the old text at a given offset with new text Solution for problem 1 from set 4 .
pub fn edit(b: &[u8], k: &[u8], offset: usize, nb: &[u8]) -> Vec<u8> {
    if offset < 0 || offset > b.len() {
        panic!("offset out of bounds")
    }
    const block_size: usize = 16;
    let start = offset / block_size;
    let end = ((offset + nb.len() - 1) / block_size) + 1;
    let mut keystream = Vec::new();
    for b in start..end {
        let mut v = Vec::from([0 as u8; 8]);
        v.extend_from_slice(&(b as u64).to_le_bytes());
        keystream.extend_from_slice(&crate::aes_ecb::encrypt_aes_128_ecb(&v, k).unwrap());
    }
    // the precise address is offet % 16: (offset % 16)+nb.len()
    let ciphertext = crate::set1::xor_buffers(
        hex::encode(nb).as_bytes(),
        hex::encode(&keystream[(offset % block_size)..(offset % block_size) + nb.len()]).as_bytes(),
    )
    .unwrap();
    let mut result = Vec::new();
    result.extend_from_slice(&b[..offset]);
    result.extend_from_slice(&ciphertext);
    result.extend_from_slice(&b[offset + nb.len()..]);
    result
}
pub fn edit ( b : & [ u8 ] , k : & [ u8 ] , offset : usize , nb : & [ u8 ] ) -> Vec < u8 > { if offset < 0 || offset > b . len ( ) { panic ! ( " " ) } const block_size : usize = 16 ; let start = offset / block_size ; let end = ( ( offset + nb . len ( ) - 1 ) / block_size ) + 1 ; let mut keystream = Vec :: new ( ) ; for b in start .. end { let mut v = Vec :: from ( [ 0 as u8 ; 8 ] ) ; v . extend_from_slice ( & ( b as u64 ) . to_le_bytes ( ) ) ; keystream . extend_from_slice ( & crate :: aes_ecb :: encrypt_aes_128_ecb ( & v , k ) . unwrap ( ) ) ; } // the precise address is offet % 16: (offset % 16)+nb.len() let ciphertext = crate :: set1 :: xor_buffers ( hex :: encode ( nb ) . as_bytes ( ) , hex :: encode ( & keystream [ ( offset % block_size ) .. ( offset % block_size ) + nb . len ( ) ] ) . as_bytes ( ) , ) . unwrap ( ) ; let mut result = Vec :: new ( ) ; result . extend_from_slice ( & b [ .. offset ] ) ; result . extend_from_slice ( & ciphertext ) ; result . extend_from_slice ( & b [ offset + nb . len ( ) .. ] ) ; result }
edit replaces the old text at a given offset with new text
Solution for problem 1 from set 4: <https://cryptopals.com/sets/4/challenges/25>
edit replaces the old text at a given offset with new text Solution for problem 1 from set 4 : <https : // cryptopals . com / sets / 4 / challenges / 25 >
b
&[u8]
k
&[u8]
offset
usize
nb
&[u8]