Page 1 of 1
mpyaes - utility library around ucryptolib.aes
Posted: Sat Sep 12, 2020 2:50 pm
by Iyassou
Hello all.
mpyaes (GitHub) is a utility library around ucryptolib's aes class. It handles AES encryption and decryption of bytes-like objects and files, and implements PKCS7 padding, which is handled in the background.
Encrypting and decrypting files makes use of a block-sized buffer and memoryview to not consume too much memory. Decryption is always zero-copy, made possible by decrypting in place and returning uctypes.bytearray_at up to where the padding begins.
Tested and working are:
Support for CTR mode is there, but I couldn't test it on my ESP32. Thoughts appreciated.
Re: mpyaes - utility library around ucryptolib.aes
Posted: Sun Sep 13, 2020 6:35 pm
by rcolistete
Thanks, with your module it is a lot easier to encrypt/decrypt text and files.
Re: mpyaes - utility library around ucryptolib.aes
Posted: Tue Feb 02, 2021 4:15 am
by vodkawasserfall
hi there!
is it possible to encode the bytearray to something like hex to be compatible with a php approach ?
my goal is to exchange encrypted strings/json between a php/webserver and an ESP32
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')
Code: Select all
1ac012ea060810c35baa48df24d9cc6227ac583ddcdfd4cff796b94819b2c3a276dc9e64fe30d96eff3eca8358b4ebe8cd4be6816489072e3641c6cea8d4202b
i don't really comprehend the encoding of binary bytearrays and strings.. still reading into it
what i've tried so far:
https://github.com/iyassou/mpyaes
Code: Select all
# https://github.com/iyassou/mpyaes
import mpyaes
import binascii
binary_string = binascii.unhexlify("1ac012ea060810c35baa48df24d9cc6227ac583ddcdfd4cff796b94819b2c3a276dc9e64fe30d96eff3eca8358b4ebe8cd4be6816489072e3641c6cea8d4202b")
key ='123456789123456'
aes = mpyaes.new(key, mpyaes.MODE_CBC, binary_string[0:16])
txt = aes.decrypt(binary_string[48:])
txt
https://stackoverflow.com/a/46872528
Code: Select all
<?p]hp
# https://stackoverflow.com/a/46872528
function encrypt($plaintext, $password) {
$method = "AES-256-CBC";
$key = hash('sha256', $password, true);
$iv = openssl_random_pseudo_bytes(16);
$ciphertext = openssl_encrypt($plaintext, $method, $key, OPENSSL_RAW_DATA, $iv);
$hash = hash_hmac('sha256', $ciphertext . $iv, $key, true);
return $iv . $hash . $ciphertext;
}
function decrypt($ivHashCiphertext, $password) {
$method = "AES-256-CBC";
$iv = substr($ivHashCiphertext, 0, 16);
$hash = substr($ivHashCiphertext, 16, 32);
$ciphertext = substr($ivHashCiphertext, 48);
$key = hash('sha256', $password, true);
if (!hash_equals(hash_hmac('sha256', $ciphertext . $iv, $key, true), $hash)) return null;
return openssl_decrypt($ciphertext, $method, $key, OPENSSL_RAW_DATA, $iv);
}
$key = '123456789123456';
$txt = 'plain text';
echo $txt.PHP_EOL;
$enc= encrypt($txt,$key);
echo bin2hex($enc).PHP_EOL;
echo decrypt($enc,$passwd).PHP_EOL;
Re: mpyaes - utility library around ucryptolib.aes
Posted: Thu Feb 04, 2021 5:41 am
by jimmo
Re: mpyaes - utility library around ucryptolib.aes
Posted: Tue May 03, 2022 10:03 am
by puppet13th
Iyassou wrote: ↑Sat Sep 12, 2020 2:50 pm
Hello all.
mpyaes (GitHub) is a utility library around ucryptolib's aes class. It handles AES encryption and decryption of bytes-like objects and files, and implements PKCS7 padding, which is handled in the background.
Encrypting and decrypting files makes use of a block-sized buffer and memoryview to not consume too much memory. Decryption is always zero-copy, made possible by decrypting in place and returning uctypes.bytearray_at up to where the padding begins.
Tested and working are:
Support for CTR mode is there, but I couldn't test it on my ESP32. Thoughts appreciated.
i have been wondering how to use CTR mode too.
Code: Select all
MicroPython v1.18-14-g78cdcdfdc-dirty on 2022-04-23; ESP32C3 module with ESP32C3
Type "help()" for more information.
>>> from ucryptolib import aes
from uos import urandom
iv = urandom(16)
key = urandom(16)
cipher = aes(key, 6, iv)
Traceback (most recent call last):
File "<stdin>", line 6, in <module>
ValueError: mode
>>>
according to the
https://docs.micropython.org/en/latest/ ... tolib.html
Code: Select all
For Counter mode, IV is the initial value for the counter.
fyi : i tried this on esp32 and esp32-c3 port