mEncrypt/Decrypt binary mp3 with mcrypt, missing mimetype

Posted by Jeremy Dicaire on Stack Overflow See other posts from Stack Overflow or by Jeremy Dicaire
Published on 2012-03-19T01:18:57Z Indexed on 2012/03/19 2:04 UTC
Read the original article Hit count: 341

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about php

Related posts about mime-types