Online RSA encrypt & decrypt

RSA key generation, public-key encryption, and private-key decryption share one modulus length. Exports use PEM. Public keys accept SPKI or PKCS#1. Private keys accept PKCS#8 or PKCS#1, and an optional password wraps the private PEM in AES-256. Encryption offers PKCS#1 v1.5 or OAEP with SHA-256. Plaintext is UTF-8. Ciphertext is Base64. The same padding must be chosen on encrypt and decrypt. A 2048-bit block yields 256 ciphertext bytes, about 344 Base64 characters. PKCS#1 v1.5 fits at most 245 bytes of plaintext at that size; OAEP with SHA-256 fits about 190. RSA is for short secrets, not whole files.

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

  1. The modulus is 2048 bits, so one RSA output block is 256 bytes no matter how short the plaintext was.
  2. 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.
  3. 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

  1. PKCS#1 v1.5 embeds a legacy padding block inside the modulus. OAEP mixes a label hash, a seed, and MGF1 with SHA-256.
  2. 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.
  3. 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

  1. BEGIN PUBLIC KEY is SubjectPublicKeyInfo. It includes an AlgorithmIdentifier before the RSA modulus and exponent.
  2. BEGIN RSA PUBLIC KEY is raw PKCS#1 RSAPublicKey without that wrapper. The bytes differ even when n and e are the same numbers.
  3. 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

  1. Plain RSA only carries a few hundred bytes at 2048 bits. A file or JSON document is far larger.
  2. Encrypt the payload with AES, generate a random AES key, and RSA-encrypt only that key with the recipient public key.
  3. 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.

Common questions

How long is RSA ciphertext for a 2048-bit key?
The output is always 256 bytes because the modulus is 2048 bits. That is one block regardless of whether the plaintext was hello with five bytes or a longer string under the padding limit. In Base64 the same 256 bytes usually become about 344 characters, including padding equals signs at the end. Hex would be 512 characters. The length does not reveal the plaintext size; padding fills the block up to the modulus.
What is the maximum plaintext size for PKCS#1 v1.5 with a 2048-bit key?
PKCS#1 v1.5 needs at least eleven bytes of padding structure inside the modulus, so the payload limit is slightly below 256 bytes. For 2048-bit RSA the practical maximum plaintext is about 245 bytes. A message longer than that cannot be encrypted in one RSA step without hybrid encryption. OAEP SHA-256 reserves more room for the encoding and lands near 190 bytes for the same key size.
What is the maximum plaintext for OAEP with SHA-256 on a 2048-bit key?
OAEP with SHA-256 uses a longer encoding inside the same 256-byte block. The hash length, label handling, and MGF1 output consume space that PKCS#1 v1.5 does not need in the same way. For 2048-bit keys the safe one-shot limit is about 190 bytes of message. Encrypting hello is fine. A medium JSON or cookie header may need hybrid AES encryption instead of a single RSA call.
Must encrypt and decrypt use the same RSA padding?
Yes. PKCS#1 v1.5 and OAEP SHA-256 are different algorithms on the same modulus. The ciphertext from one cannot be decrypted with the other. The hash in OAEP must also match; OAEP SHA-256 is not OAEP SHA-1. Parameters are not inside the 256-byte block in a way a receiver can guess. Both ends must agree on padding before testing whether a Base64 string opens.
What is the difference between BEGIN PUBLIC KEY and BEGIN RSA PUBLIC KEY?
BEGIN PUBLIC KEY is SPKI, SubjectPublicKeyInfo, with an algorithm wrapper around the key. BEGIN RSA PUBLIC KEY is PKCS#1 RSAPublicKey with modulus and exponent only. Tools convert between them when import allows. The mathematical key can be the same while the PEM bytes differ. Pick the header your importer documents, or export the form the other party expects.
What is the difference between BEGIN RSA PRIVATE KEY and BEGIN PRIVATE KEY?
BEGIN RSA PRIVATE KEY is PKCS#1 RSAPrivateKey, including CRT factors. BEGIN PRIVATE KEY is PKCS#8 PrivateKeyInfo, which wraps the same material with an algorithm id. OpenSSL can output either. Some APIs require PKCS#8. Decryption works when the underlying primes match regardless of wrapper, but paste errors happen when one side only accepts PKCS#8.
Why does an ENCRYPTED PRIVATE KEY PEM need a password?
BEGIN ENCRYPTED PRIVATE KEY means the PKCS#8 body is encrypted, commonly with AES-256 and a key derived from the password. RSA operations need the decrypted private key bytes in memory first. Without the password the PEM parses but decrypt of ciphertext fails because no private exponent is available. The password protects key storage on disk, not the RSA message padding choice.
When should RSA be combined with AES for encryption?
Whenever the data is larger than the RSA plaintext limit or performance matters. Generate a random AES key, encrypt the file or message with AES in a documented mode, then RSA-encrypt only the AES key with the recipient public key. The receiver uses the private key to recover the AES key and then decrypts the bulk. RSA padding and PEM format apply only to the small RSA step; the AES step has its own mode and IV rules.
How does RSA relate to JWT RS256?
RS256 signs a JWT with RSA over SHA-256 of the signing input bytes. That is a signature, not PKCS#1 encryption of the payload. The private key signs; verifiers use the public key. The same SPKI public PEM often serves verification while a PKCS#8 private PEM signs. Hashing details live in JWT encoder and hash digest; padding for encryption is a separate setting from signature schemes.