-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup
More file actions
123 lines (108 loc) · 4.33 KB
/
Copy pathbackup
File metadata and controls
123 lines (108 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
fn get_input(message_to_user: String)-> String{
println!("{:?}", message_to_user);
let mut input = String::new();
io::stdin().read_line(&mut input).expect("error: unable to read user input");
return input;
}
#![allow(unused)]
use std::array;
use rand::seq::SliceRandom;
// Globals are declared outside all other scopes.
static ALPHABET: &str = "abcdefghijklmnopqrstuvwxyz";
fn enigma(key: [String;3], plaintext: &str) -> String{
let mut rotor_1 : String = key[0].clone();
let mut rotor_2 : String = key[1].clone();
let mut rotor_3 : String = key[2].clone();
/* let mut rotor_1 : String = String::from("sfnzuyjblohtqawdirpgvkxcme");
let mut rotor_2 : String = String::from("sqinotpjfhbzleadmwygvxurck");
let mut rotor_3 : String = String::from("qmjypuzcwlnixoeksvrfbthdga");
let message = String::from("hihiqmjypuzcwlnpjfhbzleixoeksvrfbthdga");
*/
let mut status : usize = 0;
let mut ciphertext: String= String::from("");
for ch in plaintext.chars(){
let mut ch_1 = rotor_1.chars().nth(index_in_string(ch, ALPHABET)).unwrap();
let mut ch_2 = rotor_2.chars().nth(index_in_string(ch_1, ALPHABET)).unwrap();
let mut ch_3 = rotor_3.chars().nth(index_in_string(ch_2, ALPHABET)).unwrap();
let reflected_char =reflector(ch_3);
ch_3 = ALPHABET.chars().nth(index_in_string(reflected_char, &rotor_3)).unwrap();
ch_2 = ALPHABET.chars().nth(index_in_string(ch_3, &rotor_2)).unwrap();
ch_1 = ALPHABET.chars().nth(index_in_string(ch_2, &rotor_1)).unwrap();
ciphertext.push(ch_1);
status += 1;
rotor_1 = rotate(&rotor_1);
if status%26 ==0 {
rotor_2 = rotate(&rotor_2);
}
if status%(26*26) ==0 {
rotor_3 = rotate(&rotor_3);
}
}
println!(" ciphertext is {}", ciphertext);
return ciphertext;
}
fn main() {
let enigma_key = enigma_key_generation();
/* let mut rotor_1 : String = enigma_key[0].clone();
let mut rotor_2 : String = enigma_key[1].clone();
let mut rotor_3 : String = enigma_key[2].clone();
*/
let mut rotor_1 : String = String::from("sfnzuyjblohtqawdirpgvkxcme");
let mut rotor_2 : String = String::from("sqinotpjfhbzleadmwygvxurck");
let mut rotor_3 : String = String::from("qmjypuzcwlnixoeksvrfbthdga");
let message = String::from("hihiqmjypuzcwlnpjfhbzleixoeksvrfbthdga");
let mut status : usize = 0;
let mut ciphertext: String= String::from("");
for ch in message.chars(){
let mut ch_1 = rotor_1.chars().nth(index_in_string(ch, ALPHABET)).unwrap();
let mut ch_2 = rotor_2.chars().nth(index_in_string(ch_1, ALPHABET)).unwrap();
let mut ch_3 = rotor_3.chars().nth(index_in_string(ch_2, ALPHABET)).unwrap();
let reflected_char =reflector(ch_3);
ch_3 = ALPHABET.chars().nth(index_in_string(reflected_char, &rotor_3)).unwrap();
ch_2 = ALPHABET.chars().nth(index_in_string(ch_3, &rotor_2)).unwrap();
ch_1 = ALPHABET.chars().nth(index_in_string(ch_2, &rotor_1)).unwrap();
ciphertext.push(ch_1);
status += 1;
rotor_1 = rotate(&rotor_1);
if status%26 ==0 {
rotor_2 = rotate(&rotor_2);
}
if status%(26*26) ==0 {
rotor_3 = rotate(&rotor_3);
}
}
println!(" ciphertext is {}", ciphertext);
}
fn rotate(temp_str: &str) -> String {
let mut str_vec: Vec<char> = temp_str.chars().collect();
str_vec.rotate_left(1);
str_vec.iter().collect()
}
fn enigma_key_generation() -> [String;3] {
println!(" this is my key");
let mut key : [String;3] = Default::default();
for i in 0..3{
let mut njm_rng = rand::thread_rng();
let mut bytes = ALPHABET.to_string().into_bytes();
bytes.shuffle(&mut njm_rng);
let r = String::from_utf8(bytes).unwrap();
key[i] = r;
}
return key;
}
fn reflector(ch: char) -> char{
let mut reflected_char : char = ' ';
let a = index_in_string(ch, ALPHABET );
reflected_char = ALPHABET.chars().nth(26 - a - 1).unwrap();
return reflected_char;
}
fn index_in_string(ch: char, my_string: &str ) -> usize
{
let mut result_char : char = ' ';
let a = match my_string.find(ch) {
Some(i) => i,
None => 27
};
let index = usize::try_from(a).unwrap();
return index;
}