Search code examples
phpmime-typesbinaryfilesmcrypt

Encrypt/Decrypt binary mp3 with mcrypt, missing mimetype


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);

Solution

  • I guess your pointing on the wrong file take a look on this code:

    $fileE = "test.mp3e"; $fileE = fopen($file64, 'w') or die("can't open file");
    

    Then check the file your trying to decrypt:

    $fileE = file_get_contents('test.mp3e');
    

    I think you have a mistake on the filename. Not sure wasn't able to see what the value of variable $file64 is. Though you have assigned the filename value on $fileE value as "test.mp3e" it would still get what you defined on $file64 though. :)