pub fn aes_128_ctr(
    b: &[u8],
    k: &[u8],
    nonce: &[u8],
) -> Result<Vec<u8>, symmetriccipher::SymmetricCipherError> {
    if nonce.len() != 8 {
        panic!("invalid nonce length, must be 8 bytes");
    }

    let mut keystream = Vec::new();
    let mut counter: u64 = 0x0;

    for chunk in b.chunks(16) {
        let mut v = Vec::from(nonce);
        v.extend_from_slice(&counter.to_le_bytes());

        let mut k = crate::aes_ecb::encrypt_aes_128_ecb(&v, k)?;

        if chunk.len() < k.len() {
            k.truncate(chunk.len());
        }

        keystream.extend_from_slice(
            &crate::set1::xor_buffers(hex::encode(&chunk).as_bytes(), hex::encode(&k).as_bytes())
                .unwrap(),
        );

        counter += 1;
    }

    Ok(keystream)
}
/// Implement CTR mode
///
/// Solution for problem 2 from set3: <https://cryptopals.com/sets/3/challenges/18>
Implement CTR mode
Solution for problem 2 from set3.
Implement CTR mode Solution for problem 2 from set3 .
pub fn aes_128_ctr(
    b: &[u8],
    k: &[u8],
    nonce: &[u8],
) -> Result<Vec<u8>, symmetriccipher::SymmetricCipherError> {
    if nonce.len() != 8 {
        panic!("invalid nonce length, must be 8 bytes");
    }
    let mut keystream = Vec::new();
    let mut counter: u64 = 0x0;
    for chunk in b.chunks(16) {
        let mut v = Vec::from(nonce);
        v.extend_from_slice(&counter.to_le_bytes());
        let mut k = crate::aes_ecb::encrypt_aes_128_ecb(&v, k)?;
        if chunk.len() < k.len() {
            k.truncate(chunk.len());
        }
        keystream.extend_from_slice(
            &crate::set1::xor_buffers(hex::encode(&chunk).as_bytes(), hex::encode(&k).as_bytes())
                .unwrap(),
        );
        counter += 1;
    }
    Ok(keystream)
}
pub fn aes_128_ctr ( b : & [ u8 ] , k : & [ u8 ] , nonce : & [ u8 ] , ) -> Result < Vec < u8 > , symmetriccipher :: SymmetricCipherError > { if nonce . len ( ) != 8 { panic ! ( " " ) ; } let mut keystream = Vec :: new ( ) ; let mut counter : u64 = 0x0 ; for chunk in b . chunks ( 16 ) { let mut v = Vec :: from ( nonce ) ; v . extend_from_slice ( & counter . to_le_bytes ( ) ) ; let mut k = crate :: aes_ecb :: encrypt_aes_128_ecb ( & v , k ) ? ; if chunk . len ( ) < k . len ( ) { k . truncate ( chunk . len ( ) ) ; } keystream . extend_from_slice ( & crate :: set1 :: xor_buffers ( hex :: encode ( & chunk ) . as_bytes ( ) , hex :: encode ( & k ) . as_bytes ( ) ) . unwrap ( ) , ) ; counter += 1 ; } Ok ( keystream ) }
Implement CTR mode
Solution for problem 2 from set3: <https://cryptopals.com/sets/3/challenges/18>
Implement CTR mode Solution for problem 2 from set3 : <https : // cryptopals . com / sets / 3 / challenges / 18 >
b
&[u8]
k
&[u8]
nonce
&[u8]