-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaesar_cipher_ascii.rs
75 lines (50 loc) · 1.94 KB
/
caesar_cipher_ascii.rs
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
use menu;
use std;
// Encrypt the message using the Caesar Cipher's Algorithm
// and return the encrypted message as a string.
pub fn encrypt_ascii(message:String) -> String {
let encryption_key:i32;
let mut encrypted_message = String::new();
// Ask user to provide the encryption key.
println!("Enter the encryption key (a positive integer):" );
encryption_key = menu::read_integer() % 96;
// Shift each character or the plaintext message to the right
// as many positions as are specified by the encryption key.
for character in message.chars() {
//this will hold the character we are adding to the ciphered message
let new_char;
let mut my_ch = character as u32;
if (my_ch >= 32) & (my_ch < 127) {
my_ch += encryption_key as u32;
if my_ch > '~' as u32 {
my_ch -= 96;
}
}
new_char = std::char::from_u32(my_ch).unwrap();
//push each encrypted character to the encrypted_message
encrypted_message.push(new_char);
}
// return the encrypted message.
encrypted_message
}
// Decrypt the message using the encryption key
// and return the plaintext message as a string.
pub fn decrypt_ascii( encrypted_message: String, encryption_key:i32 ) -> String {
let mut plaintext_message = String::new();
for character in encrypted_message.chars() {
//this will hold the character we are adding to the ciphered message
let new_char;
let mut my_ch = character as u32;
if (my_ch >= 32) & (my_ch < 127) {
my_ch -= encryption_key as u32;
if my_ch < ' ' as u32 {
my_ch += 96;
}
}
new_char = std::char::from_u32(my_ch).unwrap();
//push each decrypted character to the plaintext_message
plaintext_message.push(new_char);
}
// return the plaintext message
plaintext_message
}