27 lines
808 B
Rust
27 lines
808 B
Rust
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.into());
|
|
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)
|
|
}
|