This project implements the Rail Fence Cipher encryption and decryption in Rust. It demonstrates the correctness of the implementation through unit tests and provides a command-line interface for encrypting and decrypting text.
- Encryption: Convert plaintext to ciphertext using the zigzag Rail Fence pattern.
- Decryption: Reconstruct plaintext from ciphertext using the Rail Fence pattern.
- Command-line Interface: Specify options like depth of the fence, input text, and whether to encrypt or decrypt.
- Unit Tests: Validate the correctness of the implementation with
cargo test.
To build and run this project, make sure you have the following installed:
To compile the project, navigate to the project directory and run:
cargo buildThis will create the compiled binary in the target/debug directory.
git clone https://github.com/martian58/rail_fence.git
cd rail_fence
cargo run -- --helpYou can use the cargo run command to execute the program. The program requires several arguments:
cargo run -- [OPTIONS]Or
./target/debug/rail_fence [OPTIONS]cargo run -- --helpOr
./target/debug/rail_fence --help-
Encrypt a Message:
cargo run -- -d 3 -i "HELLO"Output:
Encrypted Text: HOELL -
Decrypt a Message:
cargo run -- -d 3 -i "HOELL" --decryptOutput:
Decrypted Text: HELLO
| Option | Description |
|---|---|
-d, --depth |
Depth of the Rail Fence cipher (number of rails). Required. |
-i, --input |
The text to encrypt or decrypt. Required. |
-x, --decrypt |
Decrypt the input text instead of encrypting it. Optional (default is encryption). |
To validate the correctness of the implementation, run the unit tests using:
cargo testrunning 4 tests
test tests::test_encrypt ... ok
test tests::test_decrypt ... ok
test tests::test_encrypt_with_depth_4 ... ok
test tests::test_decrypt_with_depth_4 ... ok
test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
To remove build artifacts and clean the project directory, run:
cargo cleansrc/main.rs: Contains the implementation of the Rail Fence Cipher and the command-line interface.Cargo.toml: Manages dependencies and project metadata.README.md: Documentation for the project (this file).
-
Encrypt a Longer Message:
cargo run -- -d 4 -i "RAILFENCEISTHEBEST"Output:
Encrypted Text: RNHAECTETIFESBSLIE -
Decrypt the Same Message:
cargo run -- -d 4 -i "RNHAECTETIFESBSLIE" --decryptOutput:
Decrypted Text: RAILFENCEISTHEBEST
- Ensure the depth of the Rail Fence cipher (
--depth) is at least 2. - The input text can contain uppercase or lowercase characters, but the program will treat all characters as uppercase.
- Non-alphabetic characters are ignored during encryption/decryption.