Skip to content

Commit

Permalink
Code cleanup done, minor issues fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
man2k committed Sep 15, 2023
1 parent 1814ec1 commit 9129a9e
Show file tree
Hide file tree
Showing 15 changed files with 313 additions and 512 deletions.
274 changes: 102 additions & 172 deletions src-tauri/Cargo.lock

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ license = ""
repository = "https://github.com/man2k/passdomNative"
edition = "2021"

[env]
RUST_BACKTRACE = "1"

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

Expand All @@ -28,7 +26,6 @@ dirs = "4.0.0"
steganography = "*"
argon2 = "0.5.1"
chrono = "0.4.27"
ccm = "0.5.0"
thiserror = "1.0.48"
anyhow = "1.0.75"

Expand Down
63 changes: 18 additions & 45 deletions src-tauri/src/commands/decryptfile/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::commands::lib::{Aes128Cbc, Aes192Cbc, Aes256Cbc};
use crate::utilities::keygen::keygenargon;
use super::super::utilities::keygenargon;
use super::lib::{Aes128Cbc, Aes192Cbc, Aes256Cbc, Algorithms};
use block_modes::BlockMode;
use dirs;
use std::fs::File;
Expand All @@ -24,48 +24,21 @@ pub fn decryptfile(
let (content, iv) = contents.split_at(contents.len() - 16);
let fkey = keygenargon(password, algo / 8, iv.try_into().unwrap()).unwrap();
let mut buffer = content.to_vec();
let downloads = dirs::download_dir().expect("Could not find downloads directory");
let finalpath = downloads.join(file_name);
let mut fil = File::create(finalpath).expect("Error Creating Encrypted File");

// println!("IV: {}", encode(&iv));
// println!("Key: {}", encode(&fkey));

if algo == 128 {
let cipher = Aes128Cbc::new_from_slices(&fkey, &iv).unwrap();
// let decrypted_ciphertext = cipher.decrypt(&mut buffer)?;
let decrypted_ciphertext = match cipher.decrypt(&mut buffer) {
Err(_) => return Err("decryption failed".to_string()),
Ok(decrypted_ciphertext) => decrypted_ciphertext,
};
let downloads = dirs::download_dir().expect("Could not find downloads directory");
let finalpath = downloads.join(file_name);
let mut fil = File::create(finalpath).expect("Error Creating Encrypted File");
fil.write_all(&decrypted_ciphertext)
.expect("Error Saving Encrypted File");
Ok("ok".to_string())
} else if algo == 192 {
let cipher = Aes192Cbc::new_from_slices(&fkey, &iv).unwrap();
let decrypted_ciphertext = match cipher.decrypt(&mut buffer) {
Err(_) => return Err("decryption failed".to_string()),
Ok(decrypted_ciphertext) => decrypted_ciphertext,
};
let downloads = dirs::download_dir().expect("Could not find downloads directory");
let finalpath = downloads.join(file_name);
let mut fil = File::create(finalpath).expect("Error Creating Encrypted File");
fil.write_all(&decrypted_ciphertext)
.expect("Error Saving Encrypted File");
Ok("ok".to_string())
} else if algo == 256 {
let cipher = Aes256Cbc::new_from_slices(&fkey, &iv).unwrap();
let decrypted_ciphertext = match cipher.decrypt(&mut buffer) {
Err(_) => return Err("decryption failed".to_string()),
Ok(decrypted_ciphertext) => decrypted_ciphertext,
};
let downloads = dirs::download_dir().expect("Could not find downloads directory");
let finalpath = downloads.join(file_name);
let mut fil = File::create(finalpath).expect("Error Creating Encrypted File");
fil.write_all(&decrypted_ciphertext)
.expect("Error Saving Encrypted File");
Ok("ok".to_string())
} else {
Ok("Some Error has occured".to_string())
}
let cipher = match algo {
128 => Algorithms::AES128(Aes128Cbc::new_from_slices(&fkey, &iv).unwrap()),
192 => Algorithms::AES192(Aes192Cbc::new_from_slices(&fkey, &iv).unwrap()),
256 => Algorithms::AES256(Aes256Cbc::new_from_slices(&fkey, &iv).unwrap()),
_ => todo!(),
};
let decrypted_ciphertext = match cipher.decrypt(&mut buffer) {
Err(_) => return Err("decryption failed".to_string()),
Ok(decrypted_ciphertext) => decrypted_ciphertext,
};
fil.write_all(&decrypted_ciphertext)
.expect("Error Saving Encrypted File");
Ok("ok".to_string())
}
47 changes: 15 additions & 32 deletions src-tauri/src/commands/decrypttext/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::commands::lib::{Aes128Cbc, Aes192Cbc, Aes256Cbc};
use crate::utilities::keygen::keygenargon;
use crate::commands::lib::{Aes128Cbc, Aes192Cbc, Aes256Cbc, Algorithms};
use crate::utilities::keygenargon;
use block_modes::BlockMode;
use std::str::from_utf8;

#[tauri::command(async)]
pub fn decrypttext(text: String, password: String, algo: usize) -> Result<String, String> {
Expand All @@ -10,35 +11,17 @@ pub fn decrypttext(text: String, password: String, algo: usize) -> Result<String
};
let (content, iv) = plaintext.split_at(plaintext.len() - 16);
let fkey = keygenargon(password, algo / 8, iv.try_into().unwrap()).unwrap();

let mut buffer = content.to_vec();
// println!("key : {}", encode(&fkey));
// println!("iv : {}", encode(&iv));
if algo == 128 {
let cipher = Aes128Cbc::new_from_slices(&fkey, &iv).unwrap();
let decrypted_ciphertext = match cipher.decrypt(&mut buffer) {
Err(_) => return Err("decryption failed".to_string()),
Ok(decrypted_ciphertext) => decrypted_ciphertext,
};
let s = std::str::from_utf8(&decrypted_ciphertext).unwrap();
Ok(s.to_string())
} else if algo == 192 {
let cipher = Aes192Cbc::new_from_slices(&fkey, &iv).unwrap();
let decrypted_ciphertext = match cipher.decrypt(&mut buffer) {
Err(_) => return Err("decryption failed".to_string()),
Ok(decrypted_ciphertext) => decrypted_ciphertext,
};
let s = std::str::from_utf8(&decrypted_ciphertext).unwrap();
Ok(s.to_string())
} else if algo == 256 {
let cipher = Aes256Cbc::new_from_slices(&fkey, &iv).unwrap();
let decrypted_ciphertext = match cipher.decrypt(&mut buffer) {
Err(_) => return Err("decryption failed".to_string()),
Ok(decrypted_ciphertext) => decrypted_ciphertext,
};
let s = std::str::from_utf8(&decrypted_ciphertext).unwrap();
Ok(s.to_string())
} else {
Ok("failed".into())
}
let cipher = match algo {
128 => Algorithms::AES128(Aes128Cbc::new_from_slices(&fkey, &iv).unwrap()),
192 => Algorithms::AES192(Aes192Cbc::new_from_slices(&fkey, &iv).unwrap()),
256 => Algorithms::AES256(Aes256Cbc::new_from_slices(&fkey, &iv).unwrap()),
_ => todo!(),
};
let decrypted_ciphertext = match cipher.decrypt(&mut buffer) {
Err(_) => return Err("decryption failed".to_string()),
Ok(decrypted_ciphertext) => decrypted_ciphertext,
};
let s = from_utf8(&decrypted_ciphertext).unwrap();
Ok(s.to_string())
}
56 changes: 4 additions & 52 deletions src-tauri/src/commands/desteganograph/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::commands::lib::Aes256Cbc;
use crate::utilities::keygen::keygenargon;
use block_modes::BlockMode;
use std::panic;
use super::decrypttext::decrypttext;
use steganography::decoder::*;
use steganography::util::*;

Expand All @@ -14,55 +11,10 @@ pub fn desteganograph(
) -> Result<String, String> {
// if !fileortext {
let encoded_image = file_as_image_buffer(img_path);

let dec = Decoder::new(encoded_image);
let out_buffer = dec.decode_alpha();
let clean_buffer: Vec<u8> = out_buffer.into_iter().filter(|b| *b != 0xff_u8).collect();
// println!("clean_buffer : {:?}", clean_buffer);

// DECRYPTION
let plaintext2 = clean_buffer;
let (content, ivv) = match panic::catch_unwind(|| plaintext2.split_at(plaintext2.len() - 16)) {
Err(_e) => {
return Err("invalid image".to_string());
}
Ok((content, ivv)) => (content, ivv),
};

let mut buffer = content.to_vec();
let fkey = keygenargon(password, 32, ivv.try_into().unwrap()).unwrap();
let cipher = Aes256Cbc::new_from_slices(&fkey, &ivv).unwrap();
let decrypted_ciphertext = match cipher.decrypt(&mut buffer) {
Err(_e) => return Err("decryption failed".to_string()),
Ok(e) => e,
};
let s = std::str::from_utf8(&decrypted_ciphertext).unwrap();
// println!("Dec : {}", s);
return Ok(s.to_string());
// }
// } else {
// let encoded_image = file_as_image_buffer(img_path);

// let dec = Decoder::new(encoded_image);
// let out_buffer = dec.decode_alpha();
// let clean_buffer: Vec<u8> = out_buffer.into_iter().filter(|b| *b != 0xff_u8).collect();
// // println!("clean_buffer : {:?}", clean_buffer);

// // DECRYPTION
// let plaintext2 = clean_buffer;
// let (content, ivv) = plaintext2.split_at(plaintext2.len() - 16);
// let mut buffer = content.to_vec();
// let fkey = keygenargon(password, 32, ivv.try_into().unwrap()).unwrap();
// let cipher = Aes256Cbc::new_from_slices(&fkey, &ivv).unwrap();
// let decrypted_ciphertext = cipher.decrypt(&mut buffer).unwrap();
// // let downloads = dirs::download_dir().expect("Could not find downloads directory");
// println!("{}", finalpath);
// let mut fil = File::create(finalpath).expect("Error Creating Encrypted File");
// fil.write_all(&decrypted_ciphertext)
// .expect("Error Saving Encrypted File");
// "This worked!".into()
// let s = std::str::from_utf8(&decrypted_ciphertext).unwrap();
// println!("Dec : {}", s);
// return s.to_string();
// }
let stbuffer = hex::encode(clean_buffer);
let decrypted_ciphertext = decrypttext(stbuffer, password, 256).unwrap();
return Ok(decrypted_ciphertext);
}
74 changes: 7 additions & 67 deletions src-tauri/src/commands/encryptfile/mod.rs
Original file line number Diff line number Diff line change
@@ -1,70 +1,10 @@
use crate::commands::lib::{Aes128Cbc, Aes192Cbc, Aes256Cbc};
use crate::utilities::passargon::passargon;
use super::super::utilities::passargon;
use super::lib::{Aes128Cbc, Aes192Cbc, Aes256Cbc, Algorithms, Error};
use block_modes::BlockMode;
use dirs;
use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;
use std::vec;
use thiserror;

const AES_128: usize = 128;
const AES_192: usize = 192;
const AES_256: usize = 256;

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error(transparent)]
Io(#[from] std::io::Error),
}

// we must manually implement serde::Serialize
impl serde::Serialize for Error {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::ser::Serializer,
{
serializer.serialize_str(self.to_string().as_ref())
}
}

enum Algorithms {
AES128(Aes128Cbc),
AES192(Aes192Cbc),
AES256(Aes256Cbc),
}

impl Algorithms {
fn encrypt(&self, buffer: &mut Vec<u8>, pos: usize) -> Result<Vec<u8>, Error> {
let ciphertext = match self {
Algorithms::AES128(cipher) => {
cipher
.clone()
.encrypt(buffer, pos)
.map_err(|e| e.to_string())
.unwrap() // Propagate errors
.to_vec()
}
Algorithms::AES192(cipher) => {
cipher
.clone()
.encrypt(buffer, pos)
.map_err(|e| e.to_string())
.unwrap() // Propagate errors
.to_vec()
}
Algorithms::AES256(cipher) => {
cipher
.clone()
.encrypt(buffer, pos)
.map_err(|e| e.to_string())
.unwrap() // Propagate errors
.to_vec()
} // _ => return Err("Error encrypting".into()), // Return an error
};
Ok(ciphertext)
}
}

#[tauri::command(async)]
pub fn encryptfile(
Expand All @@ -81,7 +21,7 @@ pub fn encryptfile(
Ok(file) => file,
};
let mut contents = Vec::new();
let _ = file.read_to_end(&mut contents);
file.read_to_end(&mut contents)?;
let pos = contents.len();
// println!("pos {}", pos);
let mut buffer: Vec<u8> = vec![0u8; pos + 100];
Expand All @@ -93,13 +33,13 @@ pub fn encryptfile(
// println!("iv : {:?} \nkey : {:?}", iv, key);

let cipher: Algorithms = match algo {
AES_128 => Algorithms::AES128(Aes128Cbc::new_from_slices(&key, &iv).unwrap()),
AES_192 => Algorithms::AES192(Aes192Cbc::new_from_slices(&key, &iv).unwrap()),
AES_256 => Algorithms::AES256(Aes256Cbc::new_from_slices(&key, &iv).unwrap()),
128 => Algorithms::AES128(Aes128Cbc::new_from_slices(&key, &iv).unwrap()),
192 => Algorithms::AES192(Aes192Cbc::new_from_slices(&key, &iv).unwrap()),
256 => Algorithms::AES256(Aes256Cbc::new_from_slices(&key, &iv).unwrap()),
_ => todo!(),
};

let ciphertext = cipher.encrypt(&mut buffer, pos).unwrap();
let ciphertext = cipher.encrypt(&mut buffer, pos)?;
let finalchipher = [ciphertext, iv.to_vec()].concat();
fil.write_all(&finalchipher)
.expect("Error Saving Encrypted File");
Expand Down
45 changes: 14 additions & 31 deletions src-tauri/src/commands/encrypttext/mod.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,26 @@
use crate::commands::lib::{Aes128Cbc, Aes192Cbc, Aes256Cbc};
use crate::utilities::passargon::passargon;
use super::super::utilities::passargon;
use super::lib::{Aes128Cbc, Aes192Cbc, Aes256Cbc, Algorithms, Error};
use block_modes::BlockMode;
use hex::encode;
use std::vec;

#[tauri::command(async)]
pub fn encrypttext(text_str: String, password: String, algo: usize) -> Result<String, String> {
pub fn encrypttext(text_str: String, password: String, algo: usize) -> Result<String, Error> {
let plaintext = text_str.as_bytes();
let pos = plaintext.len();
let mut buffer: Vec<u8> = vec![0u8; pos + 100];
buffer[..pos].copy_from_slice(&plaintext);
let (iv, key) = passargon(password, algo / 8).unwrap();
// println!("iv : {:?} \nkey : {:?}", iv, key);
if algo == 128 {
let cipher = Aes128Cbc::new_from_slices(&key, &iv).unwrap();

let ciphertext = match cipher.encrypt(&mut buffer, pos) {
Err(_) => return Err("encryption failed".to_string()),
Ok(cipt) => cipt,
};
let finalchipher = [ciphertext, &iv].concat();
Ok(encode(finalchipher).into())
} else if algo == 192 {
let cipher = Aes192Cbc::new_from_slices(&key, &iv).unwrap();
let ciphertext = match cipher.encrypt(&mut buffer, pos) {
Err(_) => return Err("encryption failed".to_string()),
Ok(cipt) => cipt,
};
let finalchipher = [ciphertext, &iv].concat();
Ok(encode(finalchipher).into())
} else if algo == 256 {
let cipher = Aes256Cbc::new_from_slices(&key, &iv).unwrap();
let ciphertext = match cipher.encrypt(&mut buffer, pos) {
Err(_) => return Err("encryption failed".to_string()),
Ok(cipt) => cipt,
};
let finalchipher = [ciphertext, &iv].concat();
let cipher: Algorithms = match algo {
128 => Algorithms::AES128(Aes128Cbc::new_from_slices(&key, &iv).unwrap()),
192 => Algorithms::AES192(Aes192Cbc::new_from_slices(&key, &iv).unwrap()),
256 => Algorithms::AES256(Aes256Cbc::new_from_slices(&key, &iv).unwrap()),
_ => todo!(),
};

Ok(encode(finalchipher).into())
} else {
Ok("failed".into())
}
let ciphertext = cipher.encrypt(&mut buffer, pos)?;
let finalchipher = [ciphertext, iv.to_vec()].concat();

Ok(encode(finalchipher))
}
Loading

0 comments on commit 9129a9e

Please sign in to comment.