How RSA Converts Plaintext to Large Integers
RSA encryption relies on modular arithmetic, which requires inputs to be numbers rather than strings of characters. Before the mathematical operations of RSA can take place, raw plaintext—such as text, documents, or keys—must undergo a standardized transformation pipeline. This process converts characters into raw bytes, applies cryptographic padding for security, and interprets the resulting byte sequence as a single large integer in the binary number system.
1. Character Encoding to Bytes
The first step is translating human-readable characters into machine-readable bytes using a character encoding standard such as ASCII or UTF-8.
- Each character in the message is mapped to a specific numerical byte value (0 to 255).
- For example, the ASCII text
"RSA"corresponds to three hexadecimal bytes:0x52,0x53, and0x41(decimal values: 82, 83, 65). - In binary, these three bytes are represented as
01010010,01010011, and01000001.
2. Cryptographic Padding (OS2IP and OAEP)
Raw plaintext cannot simply be converted directly into a number without security risks. If the message is short or predictable, attackers can exploit mathematical relationships (such as the multiplicativity of RSA) or use dictionary attacks.
To prevent this, the byte sequence undergoes a cryptographic padding process, most commonly Optimal Asymmetric Encryption Padding (OAEP) or PKCS#1 v1.5: * Length Adjustment: Padding extends the byte array to match the size of the RSA key modulus (e.g., 256 bytes for a 2048-bit key). * Randomization: OAEP adds randomized data and applies cryptographic hash functions to ensure that encrypting the same plaintext twice produces completely different integer outputs. * Octet-String-to-Integer-Primitive (OS2IP): Standards such as PKCS#1 define the formal algorithm (OS2IP) to map the padded octet sequence (byte array) directly into a numerical representation.
3. Base-256 to Integer Conversion
Once padded, the message exists as a continuous array of bytes \((B_0, B_1, B_2, \dots, B_{k-1})\), where each \(B\) is a value between \(0\) and \(255\).
This array is treated as a single number represented in base-256 (radix-256) using big-endian (network) byte order:
\[\text{Integer } m = \sum_{i=0}^{k-1} B_i \times 256^{(k - 1 - i)}\]
Alternatively, in the binary number system (base-2), the bytes are
concatenated together into a single bitstring. For instance, the
sequence 0x52 0x53 0x41 becomes:
\[\text{Binary: } 010100100101001101000001_2\] \[\text{Decimal: } 5,395,265_{10}\]
4. Verification Against the Modulus
For RSA encryption to function properly, the resulting integer \(m\) must satisfy one strict mathematical condition:
\[0 \le m < n\]
where \(n\) is the RSA public modulus (the product of primes \(p\) and \(q\)). Because the padding process sizes the byte array to be slightly smaller than the bit-length of \(n\), the converted integer \(m\) is guaranteed to fall safely within this range.
5. Final Mathematical Processing
Once \(m\) is represented as a large binary integer, the computer performs standard modular exponentiation using the public key exponent \(e\):
\[c = m^e \pmod n\]
The calculated ciphertext \(c\) is also a large integer, which can then be converted back into a byte sequence (using the Integer-to-Octet-String-Primitive, or I2OSP) for transmission over a network.