Implement challenge 24 and finish set 3.
This commit is contained in:
26
src/mtcipher.rs
Normal file
26
src/mtcipher.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
use crate::{bytes::Bytes, mt19937};
|
||||
|
||||
pub fn decrypt(key: u16, data: &Bytes) -> Bytes {
|
||||
encrypt(key, data)
|
||||
}
|
||||
|
||||
pub fn encrypt(key: u16, Bytes(data): &Bytes) -> Bytes {
|
||||
// You can create a trivial stream cipher out of any PRNG; use it to
|
||||
// generate a sequence of 8 bit outputs and call those outputs a keystream.
|
||||
// XOR each byte of plaintext with each successive byte of keystream.
|
||||
// Write the function that does this for MT19937 using a 16-bit seed.
|
||||
|
||||
let mut mt = mt19937::MT19937::new();
|
||||
mt.seed(key as u32);
|
||||
let mut result: Vec<u8> = vec![];
|
||||
|
||||
for chunk in data.chunks(4) {
|
||||
let key = mt.extract_bytes();
|
||||
for i in 0..chunk.len() {
|
||||
let cipher_char = chunk[i] ^ key[i];
|
||||
result.push(cipher_char);
|
||||
}
|
||||
}
|
||||
|
||||
Bytes(result)
|
||||
}
|
||||
Reference in New Issue
Block a user