SHARE
TWEET

Untitled

a guest Oct 20th, 2019 108 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fn main() {
  2.    println!("{:?}", encrypt("Hello, world!".to_string(), "password".to_string()));
  3. }
  4.  
  5. struct KeyIter {
  6.    key: Vec<u8>,
  7.    index: usize,
  8. }
  9.  
  10. impl KeyIter {
  11.    pub fn new(key: Vec<u8>) -> KeyIter {
  12.       KeyIter {
  13.          key,
  14.          index: 0,
  15.      }
  16.   }
  17.    pub fn next(&mut self) -> u8 {
  18.       let k = self.key[self.index];
  19.       self.index += 1;
  20.       if self.index == self.key.len() {
  21.          self.index = 0;
  22.       }
  23.       k
  24.   }
  25. }
  26.  
  27. fn encrypt(msg: String, key: String) -> Vec<u8> {
  28.    let msg: Vec<u8> = msg.into_bytes();
  29.    let key: Vec<u8> = key.into_bytes();
  30.    let mut encrypted: Vec<u8> = vec!();
  31.  
  32.    let mut key_iter: KeyIter = KeyIter::new(key);
  33.  
  34.    for b in msg.iter() {
  35.       let k = key_iter.next();
  36.       if k > *b {
  37.          encrypted.push(*b^k); // b xor k
  38.       } else {
  39.          encrypted.push(b-k);
  40.       }
  41.    }
  42.    
  43.    encrypted
  44. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top