{"id":499,"date":"2026-11-13T13:39:44","date_gmt":"2026-11-13T05:39:44","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-oaep-encodes-a-short-message-before-the-rsa-exponent\/"},"modified":"2026-11-13T21:59:10","modified_gmt":"2026-11-13T13:59:10","slug":"how-oaep-encodes-a-short-message-before-the-rsa-exponent","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-oaep-encodes-a-short-message-before-the-rsa-exponent\/","title":{"rendered":"How OAEP encodes a short message before the RSA exponent"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>OAEP<\/strong> 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 <code>0x01<\/code> byte, and the message. The encoded string starts with a zero byte. The fragment uses a fixed seed of 32 bytes of <code>0x11<\/code> and two primes larger than <code>2^280<\/code>, 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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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 <code>hi<\/code>. 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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">For this seed, this message, and this modulus, the public exponent and the private inverse return an encoding that ends in <code>hi<\/code>. 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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Build a data block from the label hash, zeros, <code>0x01<\/code>, and the message.<\/li>\n<li>Mask the data block with MGF1 of the seed, then mask the seed with MGF1 of the masked data block.<\/li>\n<li>Prefix a zero byte and interpret the string as an integer.<\/li>\n<li>Apply the RSA public exponent. On decryption, apply the private exponent and remove the masks.<\/li>\n<\/ol>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails as a format for a long message<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Split a long message into OAEP blocks<\/td>\n<td>Each block is a padded RSA exponentiation. A long message uses a symmetric cipher, and OAEP carries the short key.<\/td>\n<\/tr>\n<tr>\n<td>Reuse one seed for every message<\/td>\n<td>The seed is the randomness in the encoding. A fixed seed makes the encoding deterministic.<\/td>\n<\/tr>\n<tr>\n<td>Drop the label hash because the label is empty<\/td>\n<td>The hash of the empty label still occupies the first 32 bytes of the data block.<\/td>\n<\/tr>\n<tr>\n<td>Use a modulus shorter than the two hashes and the message<\/td>\n<td>SHA-256 OAEP needs room for both masks, the marker, and the message. A shorter modulus does not fit the layout.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">RSA is the public exponent. OAEP is the encoding that exponent is applied to.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment encodes <code>hi<\/code> with SHA-256 OAEP under a fixed seed and a modulus larger than <code>2^280<\/code>, then applies the RSA exponent and inverts it. It prints <code>hi<\/code>.<\/p>\n\n<pre><code class=\"language-python\">\nimport hashlib\nimport math\n\np = 1942668892225729070919461906823518906642406839052139521251812409738904285205208510537\nq = 1942668892225729070919461906823518906642406839392421888172750873202278892636976722589\nn = p * q\ne = 65537\nlam = (p - 1) * (q - 1) \/\/ math.gcd(p - 1, q - 1)\nprivate = pow(e, -1, lam)\nk = (n.bit_length() + 7) \/\/ 8\nhlen = 32\nmessage = b&quot;hi&quot;\nseed = b&quot;\\x11&quot; * hlen\n\ndef mgf(seed_bytes, length):\n    out = b&quot;&quot;\n    counter = 0\n    while len(out) &lt; length:\n        out += hashlib.sha256(seed_bytes + counter.to_bytes(4, &quot;big&quot;)).digest()\n        counter += 1\n    return out[:length]\n\ndata = hashlib.sha256(b&quot;&quot;).digest()\ndata += b&quot;\\x00&quot; * (k - len(message) - 2 * hlen - 2) + b&quot;\\x01&quot; + message\nmasked_data = bytes(a ^ b for a, b in zip(data, mgf(seed, k - hlen - 1)))\nmasked_seed = bytes(a ^ b for a, b in zip(seed, mgf(masked_data, hlen)))\nencoded = b&quot;\\x00&quot; + masked_seed + masked_data\ncipher = pow(int.from_bytes(encoded, &quot;big&quot;), e, n)\ndecoded = pow(cipher, private, n).to_bytes(k, &quot;big&quot;)\ngot_seed = bytes(a ^ b for a, b in zip(decoded[1:1 + hlen], mgf(decoded[1 + hlen:], hlen)))\ngot_data = bytes(a ^ b for a, b in zip(decoded[1 + hlen:], mgf(got_seed, k - hlen - 1)))\nassert got_data.endswith(message)\nprint(message.decode())\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">RSAES-OAEP is the padding in <strong>PKCS #1<\/strong> 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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for OAEP returns the two MGF1 masks and the data block that ends with the message. <strong>OAEP masks a seed and a data block with MGF1, prefixes a zero byte, and only then applies the RSA exponent.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,49],"tags":[],"class_list":["post-499","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-cryptography"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/499","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=499"}],"version-history":[{"count":1,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/499\/revisions"}],"predecessor-version":[{"id":711,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/499\/revisions\/711"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}