RSA in Practice¶
In the RSA section you made keys by hand with small primes. Real software does exactly the same mathematics with numbers that are 2048 bits or longer, but it adds a few steps that textbook RSA leaves out. This section looks at those steps using OpenSSL, a free command-line tool (and library) behind a large share of the internet’s encryption.
Making a Key Pair¶
Suppose Bob wants a key pair. These two commands make a 3072-bit private key (the size NIST recommends for 128-bit security, as in the RSA section), then save the public half in its own file:
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:3072 -out bob_key.pem
openssl pkey -in bob_key.pem -pubout -out bob_pub.pemBob keeps bob_key.pem to himself and can give bob_pub.pem to anyone. To see what’s inside the private key, print it as text:
openssl pkey -in bob_key.pem -text -nooutOpenSSL writes the numbers in hexadecimal (base 16), split across many lines. The private key file holds every number from the RSA section: the modulus, both exponents and the two primes, plus three extra helper numbers. The public key file holds only the modulus and the public exponent, which is almost always 65537.
The Helper Numbers¶
The three helper numbers are , , and . They let the owner of the key decrypt with two small exponentiations, one mod and one mod , instead of one big exponentiation mod , and then combine the two answers. (The method for combining them is called the Chinese Remainder Theorem.) This is several times faster than computing directly. Here it is with the small key from the RSA section, and :
p, q = 1171, 2099
n = p * q
d = 1687553
c = 1016955 # the ciphertext of "hi" from the RSA section
exponent1 = d % (p - 1)
exponent2 = d % (q - 1)
coefficient = pow(q, -1, p)
print("exponent1 =", exponent1, " exponent2 =", exponent2, " coefficient =", coefficient)
m_p = pow(c, exponent1, p) # the message mod p
m_q = pow(c, exponent2, q) # the message mod q
m = m_q + q * ((coefficient * (m_p - m_q)) % p)
print("decrypted with the helpers:", m, integer_to_text(m))
print("decrypted directly: ", pow(c, d, n))exponent1 = 413 exponent2 = 761 coefficient = 612
decrypted with the helpers: 26729 hi
decrypted directly: 26729
Why Real RSA Adds Padding¶
Textbook RSA has two weaknesses that come straight from its mathematics.
It’s deterministic. The same message always encrypts to the same ciphertext. Suppose Eve knows that Alice’s answer is either go or no. She doesn’t need the private key: she encrypts both guesses with Alice’s public key and sees which one matches. Here’s that attack on the small key from the RSA section:
public_key, private_key = rsa_keygen(1171, 2099)
ciphertext = rsa(text_to_integer("no"), public_key) # what Eve sees
print("Eve sees", ciphertext)
for guess in ["go", "no"]:
if rsa(text_to_integer(guess), public_key) == ciphertext:
print("The message was", guess)Eve sees 1365535
The message was no
It’s malleable. Multiplying ciphertexts multiplies the messages inside them, because . An attacker who sees the ciphertext of can multiply it by the ciphertext of 2 and get a valid ciphertext of , without ever decrypting anything:
n = public_key[1]
c = rsa(1000, public_key) # the ciphertext of 1000
c_changed = (c * rsa(2, public_key)) % n # the attacker's change
rsa(c_changed, private_key) # it decrypts to 2 * 10002000Real RSA fixes both problems by adding padding before encrypting: the message is surrounded by random bytes and a fixed structure, then the whole padded block is encrypted. Because the random bytes change every time, the same message gives a different ciphertext every time, and multiplying ciphertexts almost always produces a block with the wrong structure, which the receiver rejects. OpenSSL uses an older padding scheme called PKCS #1 v1.5 unless you ask for another; new systems should use the newer OAEP padding. Padding takes up some room, so the message has to be a little shorter than .
Signatures get padding too: the hash is padded in a fixed format (PKCS #1 v1.5) or with random padding (PSS) before it’s signed.
Encrypting a File: Hybrid Encryption¶
RSA can’t encrypt a long file directly. The message has to fit, with its padding, in a number less than , so OpenSSL refuses anything longer than a few hundred bytes. Even if RSA could handle long messages, it would be far too slow, since every block would need a huge modular exponentiation.
Real systems use hybrid encryption instead. They make a random key for a fast symmetric cipher, encrypt the long message with that cipher, and use RSA to encrypt only the short key. Here Alice sends Bob the file report.txt:
openssl rand -hex 32 > session.key
openssl enc -aes-256-cbc -pbkdf2 -pass file:session.key -in report.txt -out report.txt.enc
openssl pkeyutl -encrypt -pubin -inkey bob_pub.pem -pkeyopt rsa_padding_mode:oaep -in session.key -out session.key.encThe first command makes a random 256-bit AES key, the second encrypts the report with AES, and the third encrypts the AES key with Bob’s public key, using OAEP padding. Alice sends Bob report.txt.enc and session.key.enc, and deletes her copy of session.key.
Two things about session.key.enc are worth noticing.
Its size is the size of . The ciphertext is a number less than , so with a 3072-bit key it’s always bytes, even though the key inside is much shorter.
It changes every time. Run the third command again and you get a different file, because the padding is random. Textbook RSA, , would give the same ciphertext every time.
Bob reverses the steps. His private key recovers the AES key, and the AES key recovers the report:
openssl pkeyutl -decrypt -inkey bob_key.pem -pkeyopt rsa_padding_mode:oaep -in session.key.enc -out session.key
openssl enc -d -aes-256-cbc -pbkdf2 -pass file:session.key -in report.txt.enc -out report.txtSigning a File¶
Encryption hides the report, but it doesn’t tell Bob who sent it, since anyone can use his public key. For that, Alice signs the report with her own private key, alice_key.pem, made the same way as Bob’s. Signing with OpenSSL follows the hash, then sign recipe, and the -sha256 option says which hash function to use:
openssl dgst -sha256 -sign alice_key.pem -out report.sig report.txtBob checks the signature with Alice’s public key:
openssl dgst -sha256 -verify alice_pub.pem -signature report.sig report.txtIt prints Verified OK. If anyone changes even one character of report.txt, its hash changes, and the check prints Verification failure instead.
RSA on the Web¶
HTTPS uses the same hybrid idea: public key cryptography sets up a short key, and AES (or a similar fast cipher) encrypts everything else. Today the key is usually agreed with elliptic curve Diffie–Hellman, and RSA’s main job on the web is the signature on the website’s certificate.