use crate::Solve;
impl Solve for OtpCipher {
fn solve(&self) -> String {
let mut plaintext: Vec<u8> = vec![];
for byte in self.ciphertext.to_string().into_bytes() {
let mut encrypted_bytes = 0;
for padding in &self.pad {
encrypted_bytes = byte ^ padding;
}
plaintext.push(encrypted_bytes);
}
String::from_utf8(plaintext).unwrap()
}
}
impl ToString for OtpCipher {
fn to_string(&self) -> String {
self.ciphertext.to_string()
}
}
pub struct OtpCipher {
ciphertext: String,
pad: Vec<u8>,
}
pub fn encrypt(pad: Vec<u8>, plaintext: &str) -> OtpCipher {
let mut ciphertext: Vec<u8> = vec![];
for byte in plaintext.to_string().into_bytes() {
let mut encrypted_bytes = 0;
for padding in &pad {
encrypted_bytes = byte ^ padding;
}
ciphertext.push(encrypted_bytes);
}
let ciphertext = String::from_utf8(ciphertext).unwrap();
OtpCipher { ciphertext, pad }
}
pub fn decrypt(pad: Vec<u8>, ciphertext: &str) -> String {
let mut plaintext: Vec<u8> = vec![];
for byte in ciphertext.to_string().into_bytes() {
let mut encrypted_bytes = 0;
for padding in &pad {
encrypted_bytes = byte ^ padding;
}
plaintext.push(encrypted_bytes);
}
String::from_utf8(plaintext).unwrap()
}