added some fixes

This commit is contained in:
Andrea Moro 2024-11-22 08:51:35 +01:00
parent 35ef48b721
commit b436e25188
2 changed files with 20 additions and 17 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
/target /target
.idea

View file

@ -1,5 +1,5 @@
use colog; use colog;
use log::{info, error}; use log::{info, error, warn};
use num::BigUint; use num::BigUint;
use num_primes::Generator; use num_primes::Generator;
use num_traits::cast::ToPrimitive; use num_traits::cast::ToPrimitive;
@ -19,13 +19,13 @@ impl RSA {
let q = Generator::new_prime(key_size); let q = Generator::new_prime(key_size);
// Convert num-primes::BigUint to num-bigint::BigUint // Convert num-primes::BigUint to num-bigint::BigUint
let p_bigint = num::BigUint::parse_bytes(p.to_string().as_bytes(), 10).unwrap(); let p_bigint = BigUint::parse_bytes(p.to_string().as_bytes(), 10).unwrap();
let q_bigint = num::BigUint::parse_bytes(q.to_string().as_bytes(), 10).unwrap(); let q_bigint = BigUint::parse_bytes(q.to_string().as_bytes(), 10).unwrap();
let n = &p_bigint * &q_bigint; let n = &p_bigint * &q_bigint;
let phi = let phi =
(&p_bigint - num::BigUint::one()) * (&q_bigint - num::BigUint::one()); (&p_bigint - BigUint::one()) * (&q_bigint - BigUint::one());
let e = num::BigUint::from(65537u32); let e = num::BigUint::from(65537u32);
@ -67,11 +67,7 @@ impl RSA {
} }
} }
fn main() { fn get_input() -> String {
colog::init();
info!("RSA Encryption/Decryption");
info!("Enter a message to encrypt:");
let mut message: String = "".to_string(); let mut message: String = "".to_string();
match io::stdin().read_line(&mut message) { match io::stdin().read_line(&mut message) {
Ok(n) => match n { Ok(n) => match n {
@ -81,18 +77,24 @@ fn main() {
Err(error) => error!("error: {error}"), Err(error) => error!("error: {error}"),
} }
let mut rsa_size: String = "".to_string(); message
}
fn main() {
colog::init();
info!("RSA Encryption/Decryption");
info!("Enter a message to encrypt:");
let message: String = get_input();
info!("Enter the size of the RSA key pair (in bits):"); info!("Enter the size of the RSA key pair (in bits):");
match io::stdin().read_line(&mut rsa_size) { let mut rsa_size: usize = get_input().trim().parse::<usize>().unwrap();
Ok(n) => match n {
0 => info!("No input provided"), if rsa_size < 1024 {
_ => info!("{} bytes read", n), warn!("Invalid RSA Size, Defaulting to 1024");
}, rsa_size = 1024;
Err(error) => error!("error: {error}"),
} }
let rsa = RSA::new(rsa_size.trim().parse().unwrap()); let rsa = RSA::new(rsa_size);
info!("Original: {}", message.trim()); info!("Original: {}", message.trim());