GoogleCloudPlatform / iot-core-micropython

Apache License 2.0
52 stars 14 forks source link

ESP32 encrypt/decrypt issue (Micropython) #17

Open RCS-it1 opened 3 years ago

RCS-it1 commented 3 years ago

Hello Team,

SCENARIO: We are using micropython ESP32 with this micropython release: esp32spiram-idf3-20200902-v1.13.bin

PROBLEM: We are looking a way to encrypt and decrypt a string using public/privet keys, and we found your example on github. Unfortunately, it seems that doesn't work, and we cannot understand why. Can you help us?

TEST 1: Encrypt. It's seems that it works..

>>> (pub_key, priv_key) = rsa.key.newkeys(256)
>>> print(pub_key)
PublicKey(61228621406785622916966555480977921591795104883963122609901081135924619511951, 65537)
>>> message = b'hello'
>>> crypto = rsa.encrypt(message, pub_key)
>>> print(crypto)
b'>]aC*VH\x15\xd8$\x7f\xfe*-\xbe\xb8\xe5\te\xf1Uy $\x92\\qM\x8e\x82\x13~'

TEST 2: Decrypt the 'hello' encrypted.

>>> res = rsa.decrypt(crypto, priv_key)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "third_party/rsa/pkcs1.py", line 239, in decrypt
DecryptionError: Decryption failed

TEST 3: Now trying in binary. Fails again.

>>> crypto = rsa.encrypt(b'\x00\x00\x00\x00\x01', pub_key)
>>> rsa.decrypt(crypto, priv_key)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "third_party/rsa/pkcs1.py", line 239, in decrypt
DecryptionError: Decryption failed

>>> len(crypto) == rsa.common.byte_size(pub_key.n)
True
>>> 

Looking at your code, it seems that the cause is because the 'decrypted clear text' doesn't start with: x00 x02, but we don't know why..

    # If we can't find the cleartext marker, decryption failed.
    if cleartext[0:2] != b'\x00\x02':
        raise DecryptionError('Decryption failed')
RSC-Games commented 2 years ago

I have the same issue described above. Nothing decrypts properly after I encrypt it. I used the examples in the code itself and decryption still failed.