From c9e5bef814bee76647cc0cb6058978d5bee91215 Mon Sep 17 00:00:00 2001 From: jacekpoz Date: Tue, 14 May 2024 14:53:21 +0200 Subject: [PATCH] print all keys in hex --- l4/z1/rsa-rs/src/main.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/l4/z1/rsa-rs/src/main.rs b/l4/z1/rsa-rs/src/main.rs index 86aa91b..6094071 100644 --- a/l4/z1/rsa-rs/src/main.rs +++ b/l4/z1/rsa-rs/src/main.rs @@ -1,4 +1,4 @@ -use std::env; +use std::{env, fmt::Display}; use rand::thread_rng; use num_bigint::{BigInt, BigUint, RandBigInt, ToBigInt}; @@ -11,6 +11,12 @@ struct Key { 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)] struct KeyPair { private: Key, @@ -157,13 +163,13 @@ fn main() { let pair_b = generate_key_pair(&p1, &p2) .expect("failed to generate key pair b"); - println!("private key A: {:?}", pair_a.private); - println!("public key A: {:?}", pair_a.public); - println!("private key B: {:?}", pair_b.private); - println!("public key B: {:?}", pair_b.public); + println!("private key A: {}", pair_a.private); + println!("public key A: {}", pair_a.public); + println!("private key B: {}", pair_b.private); + 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); - 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) || (&found_secret.0 == &p2 && &found_secret.1 == &p1) { println!("cracked B)");