I'm aware of the way that the original plain text is converted into a number by taking the ascii numbers of the byte array of the plain text, but doing things the other way around doesn't seem to work.

The encrypted message is pretty much a series of random digits(let's say it's in hexadecimal), which means that some of it will go above the 7F, or 127, limit of ascii. What happens then?

Edit: Ah, my error was that I somehow forgot that ascii was just 7 bits. Anyway, it was useful seeing how the ciphertext can be represented in different ways.

  • 1
    $\begingroup$ RSA is generally not used to encrypt text due to a limitation that the data to be encrypted must be shorter than the keysize-padding length and that it is very slow compared to a symmetric cipher such as AES. $\endgroup$ – zaph Jun 30 '18 at 10:42

I don't know what language you're using, but here are methods for bytes_to_integer and integer_to_bytes in python.

If you're using a language other than python, it should be relatively simple to translate as it uses fairly generic language constructs. You'll have to take into account the need for the integer to be a BigInt rather than an unsigned int/long/etc if necessary.

bytes_to_integer converts an array of bytes (8-bit octets) to an integer. Don't concern yourself with viewing the bytes as ascii encoded, doing the conversion with 7-bit chunks will probably be messy. Simply loop over the byte array and OR each byte into successively higher locations in the output integer.

integer_to_bytes converts a BigInt to an array of bytes. You simply apply the mask 255 to the integer to obtain the lowest 8 bits, append the obtained byte to your array, then shift the BigInt right by 8 bits, and repeat as necessary. You need to specify how long the resultant byte array should be.

def bytes_to_integer(data):
    output = 0    
    size = len(data)
    for index in range(size):
        output |= data[index] << (8 * (size - 1 - index))
    return output

def integer_to_bytes(integer, _bytes):
    output = bytearray()
    for byte in range(_bytes):        
        output.append((integer >> (8 * (_bytes - 1 - byte))) & 255)
    return output

From there you can encode the byte array to a compatible and readable format if/as necessary

  • $\begingroup$ Fine until the last, an arbitrary byte array can not be represented as ASCII or UTF-8, there are values that do not have a valid character representation. If a character representation an encoding such as hexadecimal, Base64, Base58 or another that handles the full range of binary values. $\endgroup$ – zaph Jun 30 '18 at 10:33
  • $\begingroup$ @zaph I changed it to be less specific $\endgroup$ – Ella Rose Jun 30 '18 at 14:29

You don't encode the encrypted result of an RSA encryption to text; it's just a sequence of bytes. It makes no sense as "text". In network traffic such bytes can often just be sent as is, but if you're in a text based protocol (mail , HTTP etc.) one could encode the RSA-bytes from the encryption as base64 encoded data, and this is sometimes done. In either case it's not meant for "human consumption", but used in a program. RSA is not a hand cipher...


What happens then?

These are just data. They have no particular meaning as a text or as a picture or as anything else.

Not every data is text. Look at binary representation of picture, audio or video file. Such files are not representable as text.

But there are cases when representing data as a text is an advantage. For instance, you want to provide your public RSA key to some web site. One way is to upload it as a file. But for many people it may be much more convenient, if they just copy it to clipboard and paste it in the needed field on the web page. Then it is common to use Base64 representation of binary data. In other cases hexadecimal representation may help.


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.