-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Specialized decoders for various ciphers - Version 1.2.0
1 parent
7916f23
commit 3983f1d
Showing
4 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
fn caesar_decrypt(ciphertext: &str, shift: u8) -> String { | ||
let ciphertext = ciphertext.to_uppercase(); | ||
let mut decrypted = String::new(); | ||
|
||
for c in ciphertext.chars() { | ||
if c.is_alphabetic() { | ||
let decrypted_char = ((c as u8 - 'A' as u8 + 26 - (shift % 26)) % 26 + 'A' as u8) as char; | ||
decrypted.push(decrypted_char); | ||
} else { | ||
decrypted.push(c); | ||
} | ||
} | ||
|
||
decrypted | ||
} | ||
|
||
//fn main() { | ||
//let ciphertext = "EBIIL TLOIA"; | ||
//let shift = 1; | ||
|
||
//let decrypted = caesar_decrypt(ciphertext, shift); | ||
//println!("Decrypted text: {}", decrypted); | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
fn substitution_decrypt(ciphertext: &str, substitution_key: &str) -> String { | ||
let mut decrypted = String::new(); | ||
|
||
for c in ciphertext.chars() { | ||
if c.is_alphabetic() { | ||
let index = substitution_key.chars().position(|x| x == c.to_uppercase().next().unwrap()); | ||
if let Some(i) = index { | ||
let decrypted_char = ('A' as u8 + i as u8) as char; | ||
decrypted.push(decrypted_char); | ||
} else { | ||
decrypted.push(c); | ||
} | ||
} else { | ||
decrypted.push(c); | ||
} | ||
} | ||
|
||
decrypted | ||
} | ||
|
||
//fn main() { | ||
//let ciphertext = "WKH TXLFN EURZQ IRA MXPSV RYHU WKH ODCB GRJ"; | ||
//let substitution_key = "ZYXWVUTSRQPONMLKJIHGFEDCBA"; | ||
|
||
//let decrypted = substitution_decrypt(ciphertext, substitution_key); | ||
//println!("Decrypted text: {}", decrypted); | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//fn main() { | ||
//let ciphertext = "HNLOO ELOLD WRLOL DLWOE"; | ||
//let key = vec![3, 1, 4, 2]; // Key representing the column order | ||
|
||
//let plaintext = decrypt_transposition(&ciphertext, &key); | ||
//println!("Decrypted plaintext: {}", plaintext); | ||
//} | ||
|
||
fn decrypt_transposition(ciphertext: &str, key: &[usize]) -> String { | ||
let column_count = key.len(); | ||
let row_count = (ciphertext.len() as f64 / column_count as f64).ceil() as usize; | ||
let mut matrix = vec![vec![' '; row_count]; column_count]; | ||
|
||
// Fill the matrix column by column | ||
for (i, c) in ciphertext.chars().enumerate() { | ||
let col = i % column_count; | ||
let row = i / column_count; | ||
matrix[col][row] = c; | ||
} | ||
|
||
// Reconstruct plaintext row by row | ||
let mut plaintext = String::new(); | ||
for row in 0..row_count { | ||
for &col in key { | ||
if matrix[col - 1][row] != ' ' { | ||
plaintext.push(matrix[col - 1][row]); | ||
} | ||
} | ||
} | ||
|
||
plaintext | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
fn vigenere_decrypt(ciphertext: &str, key: &str) -> String { | ||
let ciphertext = ciphertext.to_uppercase(); | ||
let key = key.to_uppercase(); | ||
let mut decrypted = String::new(); | ||
|
||
for (i, c) in ciphertext.chars().enumerate() { | ||
if c.is_alphabetic() { | ||
let key_char = key.chars().nth(i % key.len()).unwrap(); | ||
let shift = (key_char as u8 - 'A' as u8) % 26; | ||
|
||
let decrypted_char = ((c as u8 - 'A' as u8 + 26 - shift) % 26 + 'A' as u8) as char; | ||
decrypted.push(decrypted_char); | ||
} else { | ||
decrypted.push(c); | ||
} | ||
} | ||
|
||
decrypted | ||
} | ||
|
||
//fn main() { | ||
//let ciphertext = "LXFOPVEFRNHR"; | ||
// let key = "LEMON"; | ||
|
||
//let decrypted = vigenere_decrypt(ciphertext, key); | ||
//println!("Decrypted text: {}", decrypted); | ||
//} |