Skip to content

Commit

Permalink
Specialized decoders added
Browse files Browse the repository at this point in the history
Specialized decoders for various ciphers - Version 1.2.0
enginestein authored Aug 13, 2023
1 parent 7916f23 commit 3983f1d
Showing 4 changed files with 109 additions and 0 deletions.
23 changes: 23 additions & 0 deletions decoders/caesar.rs
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);
//}
27 changes: 27 additions & 0 deletions decoders/substitution.rs
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);
//}
32 changes: 32 additions & 0 deletions decoders/transposition.rs
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
}
27 changes: 27 additions & 0 deletions decoders/vigenere.rs
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);
//}

0 comments on commit 3983f1d

Please sign in to comment.