What RSA does with a PEM key, a padding name, and a byte string
RSA encrypts or decrypts a short byte string with an asymmetric key pair. A 2048-bit modulus turns every successful encryption into exactly 256 bytes of ciphertext. Those bytes are often spelled in Base64, which is about 344 characters for one block because three bytes become four characters and padding adds one or two equals signs. The public key encrypts. Only the matching private key decrypts. The padding name must match on both sides. PKCS#1 v1.5 and OAEP with SHA-256 are different constructions. Swapping them does not open the message.
Public keys arrive as PEM text. A line that says BEGIN PUBLIC KEY wraps an SPKI structure. A line that says BEGIN RSA PUBLIC KEY wraps PKCS#1 RSAPublicKey bytes. Private keys say BEGIN RSA PRIVATE KEY for PKCS#1 or BEGIN PRIVATE KEY for PKCS#8. An encrypted PEM says BEGIN ENCRYPTED PRIVATE KEY and needs a password to unwrap AES-256 before RSA can run. The word hello as UTF-8 is five bytes. Under PKCS#1 v1.5 on a 2048-bit key the tool can hold on the order of 245 bytes. Under OAEP SHA-256 the safe limit is nearer 190 bytes for the same modulus.
RSA is slow and length-limited, so real systems use hybrid encryption. A random AES key wraps the bulk data in AES cipher, and RSA only encrypts that short AES key. Signatures hash first: the message goes through hash digest and the digest is signed with the private key. JWT encoder RS256 follows that pattern with SHA-256 over the signing input.
How modulus size, padding, PEM shape, and hybrid layout line up
Why a 2048-bit RSA ciphertext is always 256 bytes and about 344 Base64 characters
- The modulus is 2048 bits, so one RSA output block is 256 bytes no matter how short the plaintext was.
- Base64 writes three bytes as four characters. Two hundred fifty-six bytes become 342 payload characters plus padding, often 344 characters total with two equals signs.
- A five-byte hello still becomes 256 bytes out. The fixed width is a property of the key size, not of the message length.
Why PKCS#1 v1.5 and OAEP SHA-256 must be chosen together on encrypt and decrypt
- PKCS#1 v1.5 embeds a legacy padding block inside the modulus. OAEP mixes a label hash, a seed, and MGF1 with SHA-256.
- The decrypt side must use the same name and the same hash. OAEP SHA-256 does not unwrap a PKCS#1 v1.5 ciphertext and the reverse also fails.
- Libraries expose the padding as a parameter. A silent default of v1.5 on one host and OAEP on another yields valid-looking Base64 that never opens.
Why SPKI and PKCS#1 public PEM headers are not interchangeable labels
- BEGIN PUBLIC KEY is SubjectPublicKeyInfo. It includes an AlgorithmIdentifier before the RSA modulus and exponent.
- BEGIN RSA PUBLIC KEY is raw PKCS#1 RSAPublicKey without that wrapper. The bytes differ even when n and e are the same numbers.
- A parser that expects SPKI will reject PKCS#1 PEM unless it converts or the tool accepts both import forms explicitly.
Why hybrid AES plus RSA is the usual answer for data larger than the RSA limit
- Plain RSA only carries a few hundred bytes at 2048 bits. A file or JSON document is far larger.
- Encrypt the payload with AES, generate a random AES key, and RSA-encrypt only that key with the recipient public key.
- Decrypt reverses the order: private key yields the AES key, then AES opens the bulk ciphertext. Padding and PEM on the RSA step still must match the sender.
Where the ciphertext bytes look right but the message never opens
hello encrypted with OAEP SHA-256 and decrypted as PKCS#1 v1.5
The plaintext hello is five UTF-8 bytes. Encryption with a 2048-bit public key and OAEP SHA-256 still returns 256 bytes, which spell to roughly 344 Base64 characters. Decryption with PKCS#1 v1.5 uses a different padding parser on the same modulus. The bytes parse as a block but the padding check fails or garbage appears. The public and private PEM matched. Only the padding name did not. Both sides must select OAEP SHA-256 or both must select PKCS#1 v1.5 before comparing outputs.
A BEGIN RSA PUBLIC KEY pasted into a tool that only imports SPKI
Two PEM files can describe the same RSA modulus and exponent. BEGIN PUBLIC KEY carries SPKI bytes. BEGIN RSA PUBLIC KEY carries PKCS#1 RSAPublicKey bytes. Import that only accepts SPKI will refuse the PKCS#1 file or ask for conversion. Encryption with one imported shape and decryption with a converted shape still works when n and e match, but copy-paste between systems that normalize headers differently causes confusion before any ciphertext is produced.
A 300-byte JSON body forced through raw RSA without a hybrid envelope
PKCS#1 v1.5 on a 2048-bit key tolerates on the order of 245 bytes of plaintext. OAEP SHA-256 tolerates nearer 190 bytes. A three-hundred-byte UTF-8 JSON object exceeds both limits in one RSA operation. A working design generates an AES key, encrypts the JSON with AES, and RSA-encrypts only the AES key bytes. Skipping the AES step either errors at encrypt time or tempts a weaker split that is easy to mis-decrypt on the other side.