Start with challenge 18.
This commit is contained in:
37
src/ctr.rs
Normal file
37
src/ctr.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use crate::bytes::Bytes;
|
||||
// use crate::cbc;
|
||||
// use openssl::symm;
|
||||
|
||||
fn _xor(a: &[u8], b: &[u8]) -> Vec<u8> {
|
||||
Iterator::zip(a.iter(), b.iter())
|
||||
.map(|z| *(z.0) ^ *(z.1))
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn _encrypt(Bytes(_key): &Bytes, Bytes(_data): &Bytes) -> Bytes {
|
||||
// let block_size = 16;
|
||||
let result: Vec<u8> = vec![];
|
||||
// let mut prev_cipher: Vec<u8> = iv.to_vec(); // first xor input is IV
|
||||
// for data in data.chunks(block_size) {
|
||||
// let xored = xor(&prev_cipher, data);
|
||||
// let mut cipher = cbc::enrypt_aes_128_ecb_block(key, &xored);
|
||||
// prev_cipher = cipher.to_vec(); // cipher is input for next xor
|
||||
// result.append(&mut cipher);
|
||||
// }
|
||||
Bytes(result)
|
||||
}
|
||||
|
||||
pub fn decrypt(Bytes(_key): &Bytes, Bytes(_data): &Bytes) -> Bytes {
|
||||
// XXX: does not handle padding for last block
|
||||
// let cipher_type = symm::Cipher::aes_128_ecb();
|
||||
// let block_size = cipher_type.block_size();
|
||||
let result: Vec<u8> = vec![];
|
||||
// let mut prev_cipher: Vec<u8> = iv.to_vec(); // first xor input is IV
|
||||
// for cipher in data.chunks(block_size) {
|
||||
// let xored = cbc::decrypt_aes_128_ecb_block(key, &cipher);
|
||||
// let data = xor(&xored, &prev_cipher);
|
||||
// result.extend(data.to_vec());
|
||||
// prev_cipher = cipher.to_vec();
|
||||
// }
|
||||
Bytes(result)
|
||||
}
|
||||
Reference in New Issue
Block a user