Go Down

Topic: can you help convert this RSA python encryption system to Arduino (Read 98 times) previous topic - next topic

Cybertonic

I am working on a message encryption project, involving RSA encryption project, and I am planning on creating an external encryption/decryption device revolving around Arduino. To do this I need to convert this Python script into Arduino code. I am having trouble knowing how and where to start in converting the Python code to Arduino code.

Here is the following python code:
__________________________________________________________________________________

  import binascii

  n = 9516311845790656153499716760847001433441357         # p*q = modulus (this is an example n key)
  e = 65537                                               #(this is an example e key)
  d = 5617843187844953170308463622230283376298685         #(this is a example d key)

  message='Rosetta Code!'
  print('message                 ', message)

  hex_data   = binascii.hexlify(message.encode())
  print('hex data                ', hex_data)

  plain_text = int(hex_data, 16)
  print('plain text integer      ', plain_text)

  if plain_text > n:
    raise Exception('plain text too large for key')

  encrypted_text = pow(plain_text,     e, n)
  print('encrypted text integer  ', encrypted_text)

  decrypted_text = pow(encrypted_text, d, n)
  print('decrypted text integer  ', decrypted_text)

  print('message                 ', binascii.unhexlify(hex(decrypted_text)[2:]).decode())     # [2:] slicing, to strip the 0x part
___________________________________________________________________________________

So far I have only been able to convert the text to hexadecimal by using this:

___________________________________________________________________________________

  char msgArray[] = "Hello World";
  void setup()
  {
    Serial.begin(9600);

    Serial.println(msgArray);
    for (int i = 0; i<sizeof(msgArray)-1; i++)
    {
      Serial.print(msgArray, HEX);//excludes NULL byte
    }
    Serial.println();
    Serial.println(sizeof(msgArray)-1, DEC);//excluding NULL byte

  }

  void loop()
  {

  }

____________________________________________________________________________________

After converting the text into hexadecimal I have no clue on how to do the rest. I know I will have to convert the hexadecimal into an integer, but then I do not know to do x to the power of y, modulus z. I would be using an Arduino Duo for this project, which has 32 bits.


____________________________________________________________________________________
So basically I am asking how to two things in Arduino code:

x to the power of y, modulus z.


How to convert hexadecimal into an integer, and save it as a variable.

___________________________________________________________________________________



Any help anyone can provide is greatly appreciated.

Delta_G

Quote
but then I do not know to do x to the power of y, modulus z.
Code: [Select]
pow(x,y)%z

Quote
How to convert hexadecimal into an integer, and save it as a variable.
Use strtoul
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Delta_G

Quote
After converting the text into hexadecimal I have no clue on how to do the rest.
It should be noted that you didn't convert it to hex.  You just printed it as hex.  Not quite the same thing. 

I don't think you need any conversion there.  If you've got a number represented as ascii then you can use strtoul or atoi to get it to an integer.  There's no need to convert it to hex. 

Do you understand why the python code converted to hex?  If you don't understand everything about that python code then you need to stop what you're doing on Arduino until you do.  Just copying what it did isn't going to get you the result you want. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

jremington

Quote
x to the power of y, modulus z.
pow() works only for 32 bit floating point numbers on the Arduino.

In python, those operations are defined for arbitrarily large integers. On standard Arduinos, you will have to use the BigNumber library to implement something similar, and you will probably run out of RAM memory for medium sized messages.

There are many encryption libraries that work "out of the box" on Arduino, so save yourself the trouble and just use one of them. Google "rsa encryption library arduino" for links.

Go Up