Skip to content

Instantly share code, notes, and snippets.

@Jviejo
Created August 16, 2024 11:05
Show Gist options
  • Save Jviejo/e0e49d3db2194bf296fa7e74bb2980eb to your computer and use it in GitHub Desktop.
Save Jviejo/e0e49d3db2194bf296fa7e74bb2980eb to your computer and use it in GitHub Desktop.
fn main() {
use num_bigint::BigInt;
// This section performs RSA encryption and decryption using BigInt for large integers.
let p = BigInt::from(1489); // First prime number
let q = BigInt::from(1493); // Second prime number
let n = &p * &q; // Calculate n as the product of p and q
let phi = (&p - 1) * (&q - 1); // Calculate phi(n) = (p-1)(q-1)
println!("n {} phi {}", n, phi); // Output n and phi
let e = BigInt::from(13); // Public exponent
let d = e.modinv(&phi).unwrap(); // Calculate private exponent d using modular inverse
println!("e {} {} d {} {}", e, n, d, n); // Output e, n, and d
let m = BigInt::from(8889); // Message to be encrypted
let c = m.modpow(&e, &n); // Encrypt the message using c = m^e mod n
println!("c {}", c); // Output the ciphertext
let m2 = c.modpow(&d, &n); // Decrypt the ciphertext using m2 = c^d mod n
println!("original {} m2 {}", m, m2); // Output the original message and }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment