1

I implemented a RSA algorithm on python. But I have a problem with the fact that you need to present any message in numerical form (a set of digits) in order to raise to a power. The difficulty is that if you do this with the ascii, how do you know how many digits are in the ascii code of the character 1, 2 or 3, for the unambiguous decode. Are there other options?

def decodeMessage(self, encodedMessage):
        decodedBlocks = []
        for block in encodedMessage:
            decoded = self.mod_exp(block, self.e, self.N)
            decodedBlocks.append(decoded)
        return decodedBlocks
  • How is the number of digits important for that calculation? – Klaus D. Apr 04 '20 at 05:49
  • what do you mean by how many digits are in the ascii code? – deadshot Apr 04 '20 at 05:51
  • public and private keys are big numbers. Subscriber A presents his message in digital form (using, for example, one of the character encodings) and breaks it into blocks. And I don’t know how, after encoding, I can uniquely decode encodings back to text, because an encoding can consist of either one digit or three – Victor Sakhno Apr 04 '20 at 06:31
  • For example, https://stackoverflow.com/questions/24600091/convert-encode-string-to-numbers, but on the python – Victor Sakhno Apr 04 '20 at 08:19
  • There are many different ways to encode the values of interest. You seem to have a particular one in mind, but you haven't clarified what that is. For rsa the values are integers x, 0<= x < n, where n is the modulus. Now, how do you propose to encode these values? – President James K. Polk Apr 04 '20 at 13:57

1 Answers1

0

Found a solution in binascii, which gives me the conversion of a string into a set of numbers.

message = message.strip()
b = message.encode('utf-8')
hex_data  = binascii.hexlify(b)
cipher = int(hex_data, 16)

after all the manipulations I convert back:

h2 = hex(result)[2:]
b2 = h2.encode('ascii')
b3 = binascii.unhexlify(b2)
answer = b3.decode('utf-8')