what is the output encoding of ucryptolib.encrypt ?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
vodkawasserfall
Posts: 4
Joined: Tue Feb 02, 2021 3:10 am

what is the output encoding of ucryptolib.encrypt ?

Post by vodkawasserfall » Thu Feb 04, 2021 2:10 am

hi!

Q1:
what is the output encoding of bytearray?

Code: Select all

bytearray(b'\xe4\xb3\x90\xc3\x0b\x80%\xb3\xc2\n\xc3nY\xdfv\xc9\xd3X8\x82Y\xd8\xd7\xbc\xd0\xafP\xbdJ~\xe5\xdf\x8a\xbc\x9cU\xfd\xa3\x9a\x8d\x1a\xed\xdd\x99\x9a\xa5Ll\xff\xaa\xef\xf0\xfbU)o\xb11\xacC\x981\x0b\xdf')
Q2:
i've tried encrypting strings on an ESP32 to decrypt it in php (webserver).
how do i encode data properly to exchange between µpython and php (and hopefully en/decrypt as well) ?
my code as of now: viewtopic.php?p=54291#p54291

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: what is the output encoding of ucryptolib.encrypt ?

Post by jimmo » Thu Feb 04, 2021 5:40 am

vodkawasserfall wrote:
Thu Feb 04, 2021 2:10 am
what is the output encoding of bytearray?
Not quite sure I understand what you're asking, but when MicroPython (and Python) print a bytes or bytearray object, it attempts to print like a regular string, but any byte that isn't a printable ascii character gets shown as \xAA (where AA is the hex of that byte).
vodkawasserfall wrote:
Thu Feb 04, 2021 2:10 am
i've tried encrypting strings on an ESP32 to decrypt it in php (webserver).
I think the issue there might be the handling of the key -- in the Python example, key is expected to be a 128-bit/256-bit (i.e. 16 or 32 bytes) bytearray. Whereas in the PHP example, the AES key is generated from the SHA1 of the password.

vodkawasserfall
Posts: 4
Joined: Tue Feb 02, 2021 3:10 am

Re: what is the output encoding of ucryptolib.encrypt ?

Post by vodkawasserfall » Mon Feb 08, 2021 3:15 am

jimmo wrote:
Thu Feb 04, 2021 5:40 am
Not quite sure I understand what you're asking, but when MicroPython (and Python) print a bytes or bytearray object, it attempts to print like a regular string, but any byte that isn't a printable ascii character gets shown as \xAA (where AA is the hex of that byte).
thanks! exactly what i meant.
jimmo wrote:
Thu Feb 04, 2021 5:40 am
I think the issue there might be the handling of the key -- in the Python example, key is expected to be a 128-bit/256-bit (i.e. 16 or 32 bytes) bytearray. Whereas in the PHP example, the AES key is generated from the SHA1 of the password.
thanks again. quiet obvious :lol:

now that i try to decrypt a message from php there's a buffer required.
tried creating preallocated bytearray() and array(), buffer() doesn't seem to exist on micropython, do i need to create an object?

Code: Select all

  File "mpyaes.py", line 117, in decrypt
TypeError: object with buffer protocol required
message tried to decode:

Code: Select all

b'\xfa3,\xa3\x1b\x9f\xcav\x0er\xa1\xac\\F\xffx\xc0\xc2_}\xfa\x00\xe5\xe6\xe9\xf5\x9d}>y>\x03\x85\xe1\xc8!MZ\x07\xea[\x9aN\xb7S\x1dz \x02n2\xf7l\xe5\xc8\xd5\xcaz\x14\x93\xc1\xb8L\x04'

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: what is the output encoding of ucryptolib.encrypt ?

Post by jimmo » Mon Feb 08, 2021 6:09 am

vodkawasserfall wrote:
Mon Feb 08, 2021 3:15 am
tried creating preallocated bytearray() and array(), buffer() doesn't seem to exist on micropython, do i need to create an object?
The "buffer protocol" is supported by objects like bytes, bytearray, str, etc.

Can you provide an example?

My guess is that mpyaes need it to be bytearray because it does the decryption in-place.

Code: Select all

    def decrypt(self, ciphertext: bytearray) -> bytearray:
        # Decrypts ciphertext in-place. Returns a zero-copy bytearray up to where
        # the padding begins.
        
So try:

msg = bytearray(b'.....')
result = aes.decrypt(msg)

Post Reply