You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This code defines two functions: encrypt and decrypt.
7
+
The encrypt function takes a plaintext string and a shift value, and returns the ciphertext string. The decrypt function takes a ciphertext string and a shift value,
8
+
and returns the plaintext string.
9
+
10
+
*/
11
+
12
+
pubfnencrypt(text:&str,shift:u8) -> String{
13
+
letmut result = String::new();
14
+
for c in text.chars(){
15
+
if c.is_ascii_alphabetic(){
16
+
let base = if c.is_ascii_lowercase(){b'a'}else{b'A'};
17
+
let offset = (c asu8 - base + shift) % 26;
18
+
result.push((base + offset)aschar);
19
+
}else{
20
+
result.push(c);
21
+
}
22
+
}
23
+
result
24
+
}
25
+
26
+
pubfndecrypt(text:&str,shift:u8) -> String{
27
+
encrypt(text,26 - shift)
28
+
}
29
+
30
+
/*
31
+
This code defines two functions: get_homophones and homophonic_cipher.
32
+
The get_homophones function generates a random number of random lowercase characters.
33
+
The homophonic_cipher function takes a plaintext string and generates a homophonic cipher based
34
+
on the homophones mapping.
35
+
36
+
* Generates a list of random homophones for each lowercase letter in the
37
+
* English alphabet. Maps each character in the plaintext to one of its
38
+
* random homophones to create the cipher text. Prints the plaintext,
39
+
* cipher text, and homophonic mapping. Returns the cipher text and
40
+
* homophonic mapping.
41
+
*
42
+
*
43
+
* Here is an example:
44
+
* Plaintext: the quick brown fox jumps over the lazy dog
0 commit comments