Samples of RSA (Rivest–Shamir–Adleman) public-key cryptosystem implementations for learning purposes
- src/rsa_gmp.rs - uses a rug, a high-level interface to the wrapper over GNU MP / GMP, a well known arbitrary precision arithmetic library
- src/rsa_openssl_bn.rs - uses an openssl, a safe interface to the popular OpenSSL library
- src/rsa_num.rs - uses a num, a collection of numeric types and traits in pure Rust
-
Choose two distinct primes
pandqFIPS.186-4, Section: B.3.1 Criteria for IFC Key Pairs
sqrt(2)*2^((nlen/2)-1) <= p <= 2^(nlen/2)-1 sqrt(2)*2^((nlen/2)-1) <= q <= 2^(nlen/2)-1 |p - q| > 2^((nlen/2)-100)where:
^is an exponentiation (power) arithmetic operationnlenis the appropriate length for the desired security strength -
Compute the modulus,
nn = p * q -
Compute the totient,
t
-
Euler's totient functionis used in the original RSAφ(n) = (p − 1) * (q − 1)which outputs the amount of numbers that are coprime to
n -
Carmichael function is recommended for modern RSA-based cryptosystems, also known as
reduced totient functionorleast universal exponent functionλ(n) = lcm(p − 1, q − 1)where
lcm()is the least common multiple
-
Choose a public key exponent, integer
e(usually65537in decimal, or0x010001in hex)1 < e < t gcd(t, e) = 1 -
Compute the modular multiplicative inverse,
dd = (e ^ (−1)) mod t 1 = (d * e) mod t -
Public key
(e, n) -
Private key
(d, n)
The numbers p, q, and d must be kept secret
The encryption of the plaintext message, m
c = (m ^ e) mod n
The decryption of the ciphertext, c
D = (c ^ d) mod n
This project was created for research purposes and is not intended for use in production systems.