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;