I've been trying to implement the exact same function in PHP as the one in C. However, I have not seen the exact same outcome. I think the problem is with the "count" or iteration that I still do not totally understand.
Function definition:
int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md,
const unsigned char *salt, const unsigned char *data, int datal,
int count, unsigned char *key, unsigned char *iv)
Here is the link to the C function implementation: evp_key.c
.
Here is what I found on Stack Overflow, which is close, but without the "count" that the author briefly mentioned: Encrypting data in Cocoa, decoding in PHP. The key lines as provided by user myztikjenz are here:
$cipher = MCRYPT_TRIPLEDES;
$cipherMode = MCRYPT_MODE_CBC;
$keySize = mcrypt_get_key_size( $cipher, $cipherMode );
$ivSize = mcrypt_get_iv_size( $cipher, $cipherMode );
$rawKey = "ThisIsMyKey";
$genKeyData = '';
do
{
$genKeyData = $genKeyData.md5( $genKeyData.$rawKey, true );
} while( strlen( $genKeyData ) < ($keySize + $ivSize) );
$generatedKey = substr( $genKeyData, 0, $keySize );
$generatedIV = substr( $genKeyData, $keySize, $ivSize );
$output = mcrypt_decrypt( $cipher, $generatedKey, $encodedData, $cipherMode, $generatedIV );
echo "output (hex)" . bin2hex($output);`
However I still do not know where the "count" would go.
Any help is greatly appreciated.