# Convert plain text as numbers to encrypt using RSA

All mathematical description of encryption functions assume, despite the use of the word plaintext, that the message to be encrypted is a sequence of numbers. For example RSA with $$n=pq$$ assumes that one wants to encrypt a number $$x$$ less than $$n$$ than a message consisting alphabets. It says encrypted form of $$x$$ is $$x^e\pmod m$$. Is there any (open) standard for converting text to numbers before encrypting?

As ASCII values go only up to 256. Does one take plain text, say 10 characters at a time, and read it as a 10-digit number in base 256? This is my own guess. I would actually want to know the exact standard followed in the industry.

• Text is a sequence of bytes which you can interpret as a big-endian integer which is basically what is done usually – SEJPM Dec 16 '16 at 10:29
• For RSA this is specified as part of the padding. Typically OAEP. – CodesInChaos Dec 16 '16 at 11:29

Is there any (open) standard for converting text to numbers before encrypting?

Well, yes and no.

There is the Unicode standard which converts each character as a single code point. But that's not what you are after, you want to encode a complete text into a number.

To do this the standard way to operate is to use two conversions:

1. convert the text to bytes using a character-encoding;
2. convert the bytes to a (large) number.

Now RSA certainly operates on numbers, but the RSA PKCS#1 standard is defined to take bytes as input. So step 2 is an integral part of PKCS#1 called OS2IP. OS2IP means Octet String (bytes) to (two) Integer Primitive. OS2IP simply interprets the bytes as an unsigned, big endian number.

OS2IP however happens after padding, which is performed on the input bytes. Padding is required to make RSA secure.

So that leaves us with the first step: converting the text to bytes. Text is stored inside computers as a string of characters. Character encoding is what is used to convert a set of characters (an alphabet) to bytes. The most logical character encoding is no doubt UTF-8, which encodes most of the most common Unicode characters to a single byte, ASCII compatible representation.

Unicode with UTF-8 is definitely as close as you can get to a commonly accepted standard.

If you want to experiment with raw/textbook/unpadded RSA, you could first encode using ASCII or UTF-8, and convert the resulting bytes to a number using OS2IP - but keep in mind that this is insecure.

• Thanks for the information. As UTF-8 uses maximum 32-bits, with RSA or any Public Key system it is possible for the adversary to pre-compute the encryptions for all the $2^{32}$ unicode characters and break the system by table lookup. How is this tackled? – P Vanchinathan Dec 17 '16 at 2:47
• By the aforementioned padding. Padding for encryption adds at least $255^8$ bits of randomness to input (represented as bytes). Only after that is the result converted into a number. – Maarten Bodewes Dec 17 '16 at 13:33
• @Maarten Bodewes♦ Can you please explain how is the padding removed after decryption in RSA? Is the length of the original message transmitted to the recepient? – Vivekanand V Mar 4 '20 at 15:19
• The size of the message is encoded in the padding, for instance for OAEP padding see step 3g of the standard, which you probably should read in it's entirety. It's not that gruesome as you might expect. – Maarten Bodewes Mar 4 '20 at 15:23