Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

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.pem

Bob 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 -noout

OpenSSL 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 d mod (p−1)d \bmod (p - 1), d mod (q−1)d \bmod (q - 1), and q−1 mod pq^{-1} \bmod p. They let the owner of the key decrypt with two small exponentiations, one mod pp and one mod qq, instead of one big exponentiation mod nn, and then combine the two answers. (The method for combining them is called the Chinese Remainder Theorem.) This is several times faster than computing cd mod nc^d \bmod n directly. Here it is with the small key from the RSA section, p=1171p = 1171 and q=2099q = 2099:

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:

Eve sees 1365535
The message was no

It’s malleable. Multiplying ciphertexts multiplies the messages inside them, because m1e⋅m2e=(m1m2)em_1^e \cdot m_2^e = (m_1 m_2)^e. An attacker who sees the ciphertext of mm can multiply it by the ciphertext of 2 and get a valid ciphertext of 2m2m, without ever decrypting anything:

2000

Real 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 nn.

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 nn, 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.enc

The 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 nn. The ciphertext is a number less than nn, so with a 3072-bit key it’s always 3072/8=3843072 / 8 = 384 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, c=me mod nc = m^e \bmod n, 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.txt

Signing 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.txt

Bob checks the signature with Alice’s public key:

openssl dgst -sha256 -verify alice_pub.pem -signature report.sig report.txt

It 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.