mEncrypt/Decrypt binary mp3 with mcrypt, missing mimetype
- by Jeremy Dicaire
I have a script that read a mp3 file and encrypt it, I want to be able to decrypt this file and convert it to base64 so it can play in html5.
Key 1 will be stored on the page and static, key2 will be unique for each file, for testing I used:
$key1 = md5(time());
$key2 = md5($key1.time());
Here is my encode php code :
//Get file content
$file = file_get_contents('test.mp3');
//Encrypt file
$Encrypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key1, $file, MCRYPT_MODE_CBC, $key2);
$Encrypt = trim(base64_encode($Encrypt));
//Create new file
$fileE = "test.mp3e"; $fileE = fopen($file64, 'w') or die("can't open file");
//Put crypted content
fwrite($fileE, $Encrypt);
//Close file
fclose($fileE);
Here is the code that doesnt work (decoded file is same size, but no mimetype):
//Get file content
$fileE = file_get_contents('test.mp3e');
//Decode
$fileDecoded = base64_decode($fileE);
//Decrypt file
$Decrypt = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key1, $fileDecoded, MCRYPT_MODE_CBC, $key2);
$Decrypt = trim($Decrypt);
//Create new file
$file = "test.mp3"; $file = fopen($file, 'w') or die("can't open file");
//Put crypted content
fwrite($file, $Decrypt);
//Close file
fclose($file);