print all keys in hex

This commit is contained in:
jacekpoz 2024-05-14 14:53:21 +02:00
parent 37e3bbb70d
commit c9e5bef814
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8

View file

@ -1,4 +1,4 @@
use std::env; use std::{env, fmt::Display};
use rand::thread_rng; use rand::thread_rng;
use num_bigint::{BigInt, BigUint, RandBigInt, ToBigInt}; use num_bigint::{BigInt, BigUint, RandBigInt, ToBigInt};
@ -11,6 +11,12 @@ struct Key {
key: BigUint, key: BigUint,
} }
impl Display for Key {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Key {{ n: 0x{:X}, key: 0x{:X} }}", self.n, self.key)
}
}
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
struct KeyPair { struct KeyPair {
private: Key, private: Key,
@ -157,13 +163,13 @@ fn main() {
let pair_b = generate_key_pair(&p1, &p2) let pair_b = generate_key_pair(&p1, &p2)
.expect("failed to generate key pair b"); .expect("failed to generate key pair b");
println!("private key A: {:?}", pair_a.private); println!("private key A: {}", pair_a.private);
println!("public key A: {:?}", pair_a.public); println!("public key A: {}", pair_a.public);
println!("private key B: {:?}", pair_b.private); println!("private key B: {}", pair_b.private);
println!("public key B: {:?}", pair_b.public); println!("public key B: {}", pair_b.public);
let found_secret = find_private_key(&pair_a.public.n, &pair_a.public.key, &pair_a.private.key); let found_secret = find_private_key(&pair_a.public.n, &pair_a.public.key, &pair_a.private.key);
println!("found_b: {:?}", found_secret); println!("found_b: (0x{:X}, 0x{:X})", found_secret.0, found_secret.1);
if (&found_secret.0 == &p1 && &found_secret.1 == &p2) if (&found_secret.0 == &p1 && &found_secret.1 == &p2)
|| (&found_secret.0 == &p2 && &found_secret.1 == &p1) { || (&found_secret.0 == &p2 && &found_secret.1 == &p1) {
println!("cracked B)"); println!("cracked B)");