From b436e25188df648c2c1f0bcda3bbb56003bc1202 Mon Sep 17 00:00:00 2001 From: Andrea Date: Fri, 22 Nov 2024 08:51:35 +0100 Subject: [PATCH] added some fixes --- .gitignore | 1 + src/main.rs | 36 +++++++++++++++++++----------------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index ea8c4bf..2a0038a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +.idea \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index f95b370..e7cc0a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use colog; -use log::{info, error}; +use log::{info, error, warn}; use num::BigUint; use num_primes::Generator; use num_traits::cast::ToPrimitive; @@ -19,13 +19,13 @@ impl RSA { let q = Generator::new_prime(key_size); // Convert num-primes::BigUint to num-bigint::BigUint - let p_bigint = num::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 p_bigint = BigUint::parse_bytes(p.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 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); @@ -67,11 +67,7 @@ impl RSA { } } -fn main() { - colog::init(); - info!("RSA Encryption/Decryption"); - info!("Enter a message to encrypt:"); - +fn get_input() -> String { let mut message: String = "".to_string(); match io::stdin().read_line(&mut message) { Ok(n) => match n { @@ -81,18 +77,24 @@ fn main() { 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):"); - match io::stdin().read_line(&mut rsa_size) { - Ok(n) => match n { - 0 => info!("No input provided"), - _ => info!("{} bytes read", n), - }, - Err(error) => error!("error: {error}"), + let mut rsa_size: usize = get_input().trim().parse::().unwrap(); + + if rsa_size < 1024 { + warn!("Invalid RSA Size, Defaulting to 1024"); + rsa_size = 1024; } - let rsa = RSA::new(rsa_size.trim().parse().unwrap()); + let rsa = RSA::new(rsa_size); info!("Original: {}", message.trim());