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.

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

1 Answer 1


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. If the numbers need to be (even) smaller then it is possible to define an alphabet and then convert the characters to indices within the alphabet.

  • 1
    $\begingroup$ 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? $\endgroup$ Dec 17, 2016 at 2:47
  • 1
    $\begingroup$ 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. $\endgroup$
    – Maarten Bodewes
    Dec 17, 2016 at 13:33
  • $\begingroup$ @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? $\endgroup$
    – Aravind A
    Mar 4, 2020 at 15:19
  • 3
    $\begingroup$ 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. $\endgroup$
    – Maarten Bodewes
    Mar 4, 2020 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.