Hello,
I am trying to convert some Arduino code over to some PHP code. So far everything has worked great except on piece, the AES 256 ecb decryption. Here’s the Arduino code I am trying to convert.
#define PASSWORD_HEX_SIZE 32
#define PASSWORD_SIZE 16
#define AES256_CRYPTO_KEY_SIZE 32
#define CHALLENGE_TOKEN_SIZE 16
aes256_context ctx;
aes256_init(&ctx, cryptoKey);
aes256_decrypt_ecb(&ctx, password);
aes256_done(&ctx);
char passwordAsChar[PASSWORD_SIZE + 1];
memset(&passwordAsChar, 0, sizeof(passwordAsChar));
for (int i = 0; i < sizeof(password); ++i)
passwordAsChar[i] = password[i];
if (strcmp(passwordAsChar, PASSWORD) == 0)
{....etc
Here is my example PHP trying to get it to work. The decrypted result should be “testpassword”, but I get garbage. Any ideas? The other end is an iPhone doing the encryption with the Cipher provided which is “Cyber142142” in the example below.
<?php
$submitPassword = "f9d23e949927e41c448df220a3c0cc3e";
$key = "Cyber142142";
echo "Password: " . $submitPassword . "\n";
echo "Cipher: " . $key . "\n";
$encrypted_text = pack('H*',$submitPassword);
echo "ASCII: " . $encrypted_text . "\n";
//echo "ASCII(unpack): " . print_r(unpack('C*',$encrypted_text)) . "\n";
$td = mcrypt_module_open('rijndael-256', '', 'ecb', '');
$iv = mcrypt_create_iv(mcrypt_enc_get_key_size($td));
mcrypt_generic_init($td, $key, $iv);
$decrypted = mdecrypt_generic($td, $encrypted_text);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
echo "Decrypted Text: " . trim($decrypted) . "\n";
//echo "Decrypted Text(unpack): " . print_r(unpack('C*',trim($decrypted))) . "\n";
?>