Short answer: OAEP encodes a short message before the RSA public exponent. With SHA-256, a seed of 32 bytes is masked with MGF1, and a data block is masked with MGF1 of the masked seed. The data block is the hash of the empty label, a run of zero bytes, a 0x01 byte, and the message. The encoded string starts with a zero byte. The fragment uses a fixed seed of 32 bytes of 0x11 and two primes larger than 2^280, so the block is long enough for SHA-256 and the arithmetic stays checkable. A deployment draws the seed at random and uses a much larger modulus.
What the problem actually asks
Raw RSA maps an integer to an integer. Equal integers stay equal, and a message longer than the modulus does not fit. OAEP is the padding that turns a short message into the integer the exponent sees. MGF1 is a counter fed to SHA-256. The empty label is hashed into the data block so a different label would change the encoding. The fragment recovers hi. It does not encrypt a long message. A long message is encrypted with a symmetric key, and OAEP is a way to encode that short key.
What the algorithm guarantees
For this seed, this message, and this modulus, the public exponent and the private inverse return an encoding that ends in hi. The seed is fixed so the example can be repeated. A fresh seed makes two encodings of the same message differ. The modulus is only required to be long enough for two SHA-256 hashes, the message, and the marker bytes. Secrecy of a 280-bit factor is not the claim. The claim is the padding layout.
The working steps
- Build a data block from the label hash, zeros,
0x01, and the message. - Mask the data block with MGF1 of the seed, then mask the seed with MGF1 of the masked data block.
- Prefix a zero byte and interpret the string as an integer.
- Apply the RSA public exponent. On decryption, apply the private exponent and remove the masks.
Approaches that look simpler and fail
| Approach | Why it fails as a format for a long message |
|---|---|
| Split a long message into OAEP blocks | Each block is a padded RSA exponentiation. A long message uses a symmetric cipher, and OAEP carries the short key. |
| Reuse one seed for every message | The seed is the randomness in the encoding. A fixed seed makes the encoding deterministic. |
| Drop the label hash because the label is empty | The hash of the empty label still occupies the first 32 bytes of the data block. |
| Use a modulus shorter than the two hashes and the message | SHA-256 OAEP needs room for both masks, the marker, and the message. A shorter modulus does not fit the layout. |
RSA is the public exponent. OAEP is the encoding that exponent is applied to.
A working example
This Python fragment encodes hi with SHA-256 OAEP under a fixed seed and a modulus larger than 2^280, then applies the RSA exponent and inverts it. It prints hi.
import hashlib
import math
p = 1942668892225729070919461906823518906642406839052139521251812409738904285205208510537
q = 1942668892225729070919461906823518906642406839392421888172750873202278892636976722589
n = p * q
e = 65537
lam = (p - 1) * (q - 1) // math.gcd(p - 1, q - 1)
private = pow(e, -1, lam)
k = (n.bit_length() + 7) // 8
hlen = 32
message = b"hi"
seed = b"\x11" * hlen
def mgf(seed_bytes, length):
out = b""
counter = 0
while len(out) < length:
out += hashlib.sha256(seed_bytes + counter.to_bytes(4, "big")).digest()
counter += 1
return out[:length]
data = hashlib.sha256(b"").digest()
data += b"\x00" * (k - len(message) - 2 * hlen - 2) + b"\x01" + message
masked_data = bytes(a ^ b for a, b in zip(data, mgf(seed, k - hlen - 1)))
masked_seed = bytes(a ^ b for a, b in zip(seed, mgf(masked_data, hlen)))
encoded = b"\x00" + masked_seed + masked_data
cipher = pow(int.from_bytes(encoded, "big"), e, n)
decoded = pow(cipher, private, n).to_bytes(k, "big")
got_seed = bytes(a ^ b for a, b in zip(decoded[1:1 + hlen], mgf(decoded[1 + hlen:], hlen)))
got_data = bytes(a ^ b for a, b in zip(decoded[1 + hlen:], mgf(got_seed, k - hlen - 1)))
assert got_data.endswith(message)
print(message.decode())
Where it is used
RSAES-OAEP is the padding in PKCS #1 and in CMS when a short key is encrypted with RSA. The seed makes two encryptions of the same key differ. A long message is encrypted with AES or ChaCha20, and only the short key is padded this way.
When you can drop this algorithm
Drop raw residues, and drop this modulus, for a deployed encryption. Keep the OAEP layout where a short string is encoded with a random seed and a full-size RSA modulus, usually as the wrapper around a symmetric key.
Why this stays published
We publish this so a search for OAEP returns the two MGF1 masks and the data block that ends with the message. OAEP masks a seed and a data block with MGF1, prefixes a zero byte, and only then applies the RSA exponent.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
