Finish challenge 39 without invmod

This commit is contained in:
2022-10-14 18:23:05 -04:00
parent 6b17c66175
commit 3176f23662
3 changed files with 128 additions and 5 deletions

View File

@@ -5,6 +5,7 @@ use crate::srp;
use num_bigint::BigUint;
use num_bigint::RandBigInt;
use num_bigint::ToBigUint;
use openssl::bn::BigNum;
use openssl::sha::sha256;
use rand::Rng;
@@ -509,6 +510,33 @@ pub fn challenge38() -> Option<()> {
Some(())
}
pub fn challenge39() {
println!("[xxxx] Challenge 39: Implement RSA");
pub fn challenge39() -> Option<()> {
// I recommend you not bother with primegen,
// but do take the time to get your own EGCD and
// invmod algorithm working.
let a = BigNum::from_u32(17).ok()?;
let n = BigNum::from_u32(3120).ok()?;
let r = BigNum::from_u32(2753).ok()?;
assert_eq!(rsa::invmod(&a, &n).ok()?, r, "invmod does not work");
let (public_key, private_key) = rsa::rsa_gen_keys().ok()?;
// Test this out with a number, like "42".
let i = BigNum::from_u32(1337).ok()?;
let c = rsa::rsa_encrypt(&i, &public_key).ok()?;
let m = rsa::rsa_decrypt(&c, &private_key).ok()?;
assert_eq!(i, m, "rsa is broken");
let i = "Party, party, party, all night long. Yuah!";
let c = rsa::rsa_encrypt_str(&i, &public_key).ok()?;
let m = rsa::rsa_decrypt_str(&c, &private_key).ok()?;
assert_eq!(i, &m, "string rsa is broken");
println!("[okay] Challenge 39: implement RSA");
Some(())
}
pub fn challenge40() -> Option<()> {
// println!("[xxxx] Challenge 40: implement an E=3 RSA Broadcast attack");
None
}