Reputation: 4185
How to write AES key and iv from Arduino library in PHP?
I found this example how to encrypt and decrypt string in PHP
$encrypt_method = "AES-256-CBC";
$secret_key = 'This is my secret key';
$secret_iv = 'This is my secret iv';
Also I found AES library for Arduino but it has different format for key and iv (hex)
// AES Encryption Key
byte aes_key[] = { 0x15, 0x2B, 0x7E, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C };
// General initialization vector (you must use your own IV's in production for full security!!!)
byte aes_iv[N_BLOCK] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
How to write key and iv from Arduino code in PHP properly? I mean can I just convert hex format to unicode string manually or can I do it with PHP by some function? Or may be there is some more convinient way/method to pass encrypted messages between Arduino and PHP? Any advice will be appreciated!
Upvotes: 1
Views: 944
Reputation: 93968
If you want to write it down directly you can use hexadecimal escapes to a string, e.g.
$secret_key = "\x15\x2B\x7E\x16\x28\xAE\xD2\xA6\xAB\xF7\x15\x88\x09\xCF\x4F\x3C"
Note the required double quotes, which are required to recognize the \x
escapes.
PHP strings, like the strings in many languages, are just bytes internally. How these bytes are interpreted may differ, but that's of no consequence if you use it as byte array.
Upvotes: 1