pub fn break_single_xor_threaded(ciphertext: Vec<u8>) {

    let ciphertext = Arc::new(ciphertext);
    let (tx, rx) = mpsc::channel();
    
    // spawn threads
    for i in 0..2 {

        let (ciphertext, tx) = (ciphertext.clone(), tx.clone());
        
        thread::spawn(move || {
            let min = 128*i;
            let max = min + 127;
            let mut best_score = 0;
            let mut key_guess: u8 = 0;
            for c in min..max {
                let decoded_text = single_key_xor(&ciphertext, c);
                let score_guess = score_text(decoded_text);
                if score_guess > best_score {
                    best_score = score_guess;
                    key_guess = c;
                }
            }
            tx.send((best_score, key_guess)).unwrap();
        });
    }    

    // collect results, determine best score and probable key
    let mut best_score = 0;
    let mut key: u8 = 0;
    for _ in 0..2 {
        let (score_guess, key_guess) = rx.recv().unwrap();
        if score_guess > best_score {
            best_score = score_guess;
            key = key_guess;
        }
    }
    println!("Final key: 0x{} ({})", key, key as char);
}
/// Uses multiple threads to decrypt ciphertext XORed against single byte
Uses multiple threads to decrypt ciphertext XORed against single byte
Uses multiple threads to decrypt ciphertext XORed against single byte
pub fn break_single_xor_threaded(ciphertext: Vec<u8>) {
    let ciphertext = Arc::new(ciphertext);
    let (tx, rx) = mpsc::channel();
    // spawn threads
    for i in 0..2 {
        let (ciphertext, tx) = (ciphertext.clone(), tx.clone());
        thread::spawn(move || {
            let min = 128*i;
            let max = min + 127;
            let mut best_score = 0;
            let mut key_guess: u8 = 0;
            for c in min..max {
                let decoded_text = single_key_xor(&ciphertext, c);
                let score_guess = score_text(decoded_text);
                if score_guess > best_score {
                    best_score = score_guess;
                    key_guess = c;
                }
            }
            tx.send((best_score, key_guess)).unwrap();
        });
    }    
    // collect results, determine best score and probable key
    let mut best_score = 0;
    let mut key: u8 = 0;
    for _ in 0..2 {
        let (score_guess, key_guess) = rx.recv().unwrap();
        if score_guess > best_score {
            best_score = score_guess;
            key = key_guess;
        }
    }
    println!("Final key: 0x{} ({})", key, key as char);
}
pub fn break_single_xor_threaded ( ciphertext : Vec < u8 > ) { let ciphertext = Arc :: new ( ciphertext ) ; let ( tx , rx ) = mpsc :: channel ( ) ; // spawn threads for i in 0 .. 2 { let ( ciphertext , tx ) = ( ciphertext . clone ( ) , tx . clone ( ) ) ; thread :: spawn ( move | | { let min = 128 * i ; let max = min + 127 ; let mut best_score = 0 ; let mut key_guess : u8 = 0 ; for c in min .. max { let decoded_text = single_key_xor ( & ciphertext , c ) ; let score_guess = score_text ( decoded_text ) ; if score_guess > best_score { best_score = score_guess ; key_guess = c ; } } tx . send ( ( best_score , key_guess ) ) . unwrap ( ) ; } ) ; } // collect results, determine best score and probable key let mut best_score = 0 ; let mut key : u8 = 0 ; for _ in 0 .. 2 { let ( score_guess , key_guess ) = rx . recv ( ) . unwrap ( ) ; if score_guess > best_score { best_score = score_guess ; key = key_guess ; } } println ! ( " " key key as char ) ; }
Uses multiple threads to decrypt ciphertext XORed against single byte
Uses multiple threads to decrypt ciphertext XORed against single byte
ciphertext
Vec<u8>
ciphertext
Vec<u8>