3
$\begingroup$

(I apologize for obviously wrong tag. I could not find appropriate one.)

All mathematical description of encryption functions assume, despites the use of the word plaintext, that the message to be encrypted is a sequence of numbers. For example RSA with $n=pq$ assume 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 upto 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.

$\endgroup$
  • $\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 '16 at 10:29
  • $\begingroup$ For RSA this is specified as part of the padding. Typically OAEP. $\endgroup$ – CodesInChaos Dec 16 '16 at 11:29
4
$\begingroup$

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.

$\endgroup$
  • $\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$ – P Vanchinathan Dec 17 '16 at 2:47
  • $\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 '16 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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