Skip to content

Commit 3983f1d

Browse files
authored
Specialized decoders added
Specialized decoders for various ciphers - Version 1.2.0
1 parent 7916f23 commit 3983f1d

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed

decoders/caesar.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
fn caesar_decrypt(ciphertext: &str, shift: u8) -> String {
2+
let ciphertext = ciphertext.to_uppercase();
3+
let mut decrypted = String::new();
4+
5+
for c in ciphertext.chars() {
6+
if c.is_alphabetic() {
7+
let decrypted_char = ((c as u8 - 'A' as u8 + 26 - (shift % 26)) % 26 + 'A' as u8) as char;
8+
decrypted.push(decrypted_char);
9+
} else {
10+
decrypted.push(c);
11+
}
12+
}
13+
14+
decrypted
15+
}
16+
17+
//fn main() {
18+
//let ciphertext = "EBIIL TLOIA";
19+
//let shift = 1;
20+
21+
//let decrypted = caesar_decrypt(ciphertext, shift);
22+
//println!("Decrypted text: {}", decrypted);
23+
//}

decoders/substitution.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
fn substitution_decrypt(ciphertext: &str, substitution_key: &str) -> String {
2+
let mut decrypted = String::new();
3+
4+
for c in ciphertext.chars() {
5+
if c.is_alphabetic() {
6+
let index = substitution_key.chars().position(|x| x == c.to_uppercase().next().unwrap());
7+
if let Some(i) = index {
8+
let decrypted_char = ('A' as u8 + i as u8) as char;
9+
decrypted.push(decrypted_char);
10+
} else {
11+
decrypted.push(c);
12+
}
13+
} else {
14+
decrypted.push(c);
15+
}
16+
}
17+
18+
decrypted
19+
}
20+
21+
//fn main() {
22+
//let ciphertext = "WKH TXLFN EURZQ IRA MXPSV RYHU WKH ODCB GRJ";
23+
//let substitution_key = "ZYXWVUTSRQPONMLKJIHGFEDCBA";
24+
25+
//let decrypted = substitution_decrypt(ciphertext, substitution_key);
26+
//println!("Decrypted text: {}", decrypted);
27+
//}

decoders/transposition.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//fn main() {
2+
//let ciphertext = "HNLOO ELOLD WRLOL DLWOE";
3+
//let key = vec![3, 1, 4, 2]; // Key representing the column order
4+
5+
//let plaintext = decrypt_transposition(&ciphertext, &key);
6+
//println!("Decrypted plaintext: {}", plaintext);
7+
//}
8+
9+
fn decrypt_transposition(ciphertext: &str, key: &[usize]) -> String {
10+
let column_count = key.len();
11+
let row_count = (ciphertext.len() as f64 / column_count as f64).ceil() as usize;
12+
let mut matrix = vec![vec![' '; row_count]; column_count];
13+
14+
// Fill the matrix column by column
15+
for (i, c) in ciphertext.chars().enumerate() {
16+
let col = i % column_count;
17+
let row = i / column_count;
18+
matrix[col][row] = c;
19+
}
20+
21+
// Reconstruct plaintext row by row
22+
let mut plaintext = String::new();
23+
for row in 0..row_count {
24+
for &col in key {
25+
if matrix[col - 1][row] != ' ' {
26+
plaintext.push(matrix[col - 1][row]);
27+
}
28+
}
29+
}
30+
31+
plaintext
32+
}

decoders/vigenere.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
fn vigenere_decrypt(ciphertext: &str, key: &str) -> String {
2+
let ciphertext = ciphertext.to_uppercase();
3+
let key = key.to_uppercase();
4+
let mut decrypted = String::new();
5+
6+
for (i, c) in ciphertext.chars().enumerate() {
7+
if c.is_alphabetic() {
8+
let key_char = key.chars().nth(i % key.len()).unwrap();
9+
let shift = (key_char as u8 - 'A' as u8) % 26;
10+
11+
let decrypted_char = ((c as u8 - 'A' as u8 + 26 - shift) % 26 + 'A' as u8) as char;
12+
decrypted.push(decrypted_char);
13+
} else {
14+
decrypted.push(c);
15+
}
16+
}
17+
18+
decrypted
19+
}
20+
21+
//fn main() {
22+
//let ciphertext = "LXFOPVEFRNHR";
23+
// let key = "LEMON";
24+
25+
//let decrypted = vigenere_decrypt(ciphertext, key);
26+
//println!("Decrypted text: {}", decrypted);
27+
//}

0 commit comments

Comments
 (0)