Skip to main content

encrypt

Function encrypt 

Source
pub fn encrypt(text: &str, rails: usize) -> Result<String>
Expand description

Encrypts text using the Rail Fence cipher algorithm.

Arranges the input text in a zigzag pattern across the specified number of rails, then reads the text row by row to create the ciphertext.

§Arguments

  • text - The plaintext to encrypt
  • rails - Number of rails (rows) to use (must be ≥ 2)

§Returns

Returns the encrypted text as a Result<String>.

§Algorithm

  1. Create N empty strings for each rail
  2. Traverse the text, placing each character on the appropriate rail
  3. Use a direction indicator to create the zigzag pattern
  4. Concatenate all rails to form the ciphertext

§Zigzag Pattern

  • Start at rail 0, move down
  • When reaching the last rail, change direction to move up
  • When reaching the first rail, change direction to move down
  • Continue until all characters are placed

§Examples

use ruscrypt::classical::rail_fence;

// With 3 rails: "HELLO" becomes "HOELL"
let result = rail_fence::encrypt("HELLO", 3).unwrap();

// With 2 rails: "HELLO" becomes "HLOEL"  
let result2 = rail_fence::encrypt("HELLO", 2).unwrap();

§Edge Cases

  • If rails < 2, returns the original text unchanged
  • Preserves all characters including spaces and punctuation
  • Empty text returns empty string
Examples found in repository?
examples/demo.rs (line 81)
53fn demo_classical_ciphers() -> Result<()> {
54    let sample_text = "HELLO WORLD";
55    
56    // Caesar Cipher Demo
57    println!("{}", "📜 Caesar Cipher:".yellow());
58    let shift = 3;
59    let caesar_encrypted = caesar::encrypt(sample_text, shift)?;
60    let caesar_decrypted = caesar::decrypt(&caesar_encrypted, shift)?;
61    println!("   Original: {}", sample_text.white());
62    println!("   Shift: {}", shift.to_string().cyan());
63    println!("   Encrypted: {}", caesar_encrypted.green());
64    println!("   Decrypted: {}", caesar_decrypted.blue());
65    println!("   ⚠️  Security: Educational only - easily broken");
66    
67    // Vigenère Cipher Demo
68    println!("\n{}", "🔤 Vigenère Cipher:".yellow());
69    let keyword = "KEY";
70    let vigenere_encrypted = vigenere::encrypt(sample_text, keyword)?;
71    let vigenere_decrypted = vigenere::decrypt(&vigenere_encrypted, keyword)?;
72    println!("   Original: {}", sample_text.white());
73    println!("   Keyword: {}", keyword.cyan());
74    println!("   Encrypted: {}", vigenere_encrypted.green());
75    println!("   Decrypted: {}", vigenere_decrypted.blue());
76    println!("   ⚠️  Security: Educational only - vulnerable to frequency analysis");
77    
78    // Rail Fence Cipher Demo
79    println!("\n{}", "🚂 Rail Fence Cipher:".yellow());
80    let rails = 3;
81    let railfence_encrypted = rail_fence::encrypt(sample_text, rails)?;
82    let railfence_decrypted = rail_fence::decrypt(&railfence_encrypted, rails)?;
83    println!("   Original: {}", sample_text.white());
84    println!("   Rails: {}", rails.to_string().cyan());
85    println!("   Encrypted: {}", railfence_encrypted.green());
86    println!("   Decrypted: {}", railfence_decrypted.blue());
87    println!("   ⚠️  Security: Educational only - simple transposition");
88    
89    // Playfair Cipher Demo
90    println!("\n{}", "🎯 Playfair Cipher:".yellow());
91    let playfair_key = "MONARCHY";
92    let playfair_text = "HELLO";
93    let playfair_encrypted = playfair::encrypt(playfair_text, playfair_key)?;
94    let playfair_decrypted = playfair::decrypt(&playfair_encrypted, playfair_key)?;
95    println!("   Original: {}", playfair_text.white());
96    println!("   Key: {}", playfair_key.cyan());
97    println!("   Encrypted: {}", playfair_encrypted.green());
98    println!("   Decrypted: {}", playfair_decrypted.blue());
99    println!("   ⚠️  Security: Educational only - digraph substitution");
100    
101    Ok(())
102}