Skip to content

Commit

Permalink
💻 Better CLI interface
Browse files Browse the repository at this point in the history
  • Loading branch information
zawie committed Oct 14, 2024
1 parent 201834d commit 6f612dc
Show file tree
Hide file tree
Showing 5 changed files with 309 additions and 29 deletions.
230 changes: 230 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
[dependencies]
clap = { version = "4.0", features = ["derive"] }
31 changes: 26 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
# Encryption
I created an encryption algorithm that can encrypt an arbitrary amount of data with a 64 bit key.
# Feistel Cipher

## How it works.
It uses a feinstel cipher where a PRNG is used as the round function. However, the feinstel cipher can only encrypt 64 bit blocks of data.
I use the counter mode of operation to encrypt arbitrary amount of data while encrypting identical blocks differently.
## Usage

```cli
Usage: feistel [COMMAND]
Commands:
encrypt Encrypt data using Feistel cipher
decrypt Decrypt data using Feistel cipher
help Print this message or the help of the given subcommand(s)
Options:
-h, --help Print help
-V, --version Print version
```

## Example

```cli
cat sensitive.png | feistel encrypt --key 123 > encrypted.dat
cat encrypted.dat | feistel decrypt --key 123 > sensitive_decrypted.png
```

## About

This Feistel cipher uses a PRNG as the round function. However, the feistel cipher can only encrypt 64 bit blocks of data. I use the counter mode of operation to encrypt arbitrary amount of data while encrypting identical blocks differently.
51 changes: 51 additions & 0 deletions src/bin/feistel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use crypto::countermode::encrypt_stream;
use crypto::countermode::decrypt_stream;

use clap::{Arg, Command};
use std::io::{self, Read, Write};

fn main() {
// Define the CLI structure
let matches = Command::new("feistel")
.version("1.0")
.author("Adam Zawierucha")
.about("Feistel Cipher CLI using stdin and stdout")
.subcommand(
Command::new("encrypt")
.about("Encrypt data using Feistel cipher")
.arg(Arg::new("key")
.short('k')
.long("key")
.value_name("NUMBER")
.help("Encryption key (optional, default is 12345)")
.default_value("1234567890")
.value_parser(clap::value_parser!(u64))
)
)
.subcommand(
Command::new("decrypt")
.about("Decrypt data using Feistel cipher")
.arg(Arg::new("key")
.short('k')
.long("key")
.value_name("NUMBER")
.help("Decryption key (optional, default is 12345)")
.default_value("1234567890")
.value_parser(clap::value_parser!(u64))
)
)
.get_matches();

let mut stdin: Box<dyn Read> = Box::new(io::stdin());
let mut stdout: Box<dyn Write> = Box::new(io::stdout());

if let Some(sub_matches) = matches.subcommand_matches("encrypt") {
let key = sub_matches.get_one::<u64>("key").unwrap(); // Since default_value guarantees presence

let _ = encrypt_stream(&mut stdin, &mut stdout, *key);
} else if let Some(sub_matches) = matches.subcommand_matches("decrypt") {
let key = sub_matches.get_one::<u64>("key").unwrap(); // Since default_value guarantees presence

let _ = decrypt_stream(&mut stdin, &mut stdout, *key);
}
}
23 changes: 0 additions & 23 deletions src/main.rs

This file was deleted.

0 comments on commit 6f612dc

Please sign in to comment.