【问题标题】:Encrypt/Decrypt binary mp3 with mcrypt, missing mimetype使用 mcrypt 加密/解密二进制 mp3,缺少 mimetype
【发布时间】:2012-03-19 01:18:57
【问题描述】:

我有一个读取 mp3 文件并对其进行加密的脚本,我希望能够解密此文件并将其转换为 base64,以便它可以在 html5 中播放。

key 1 将存储在页面上并且是静态的,key2 对于每个文件都是唯一的,用于我使用的测试:

$key1 = md5(time());
$key2 = md5($key1.time());

这是我的编码 php 代码:

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

这是不起作用的代码(解码文件大小相同,但没有 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);

【问题讨论】:

  • “没有 MIME 类型”是什么意思?解密后的文件是否与原始文件相同(是否产生相同的哈希)?你如何确定它“没有 MIME 类型”?
  • 变量 $file64 的值是多少?
  • @ deceze :在比较哈希时,我注意到我的... md5(time()) 作为密钥,加密文件然后解密文件...重新生成 md5(time())哈希,所以我得到一个与原始文件大小相同的文件,但不是其中的好内容......感觉很愚蠢 - 你解决了我的问题:)
  • @Christopher Pelayo:很少有人可以访问服务器上的文件,我不希望“坏”文件掌握在“坏”人手中 :)
  • @Christopher Pelayo:抱歉,错字,应该是 $fileE

标签: php mime-types binaryfiles mcrypt


【解决方案1】:

我猜你指向错误的文件看看这段代码:

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

然后检查您尝试解密的文件:

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

我认为您的文件名有误。不确定无法看到变量 $file64 的值是什么。尽管您已将 $fileE 值上的文件名值分配为“test.mp3e”,但它仍然会得到您在 $file64 上定义的内容。 :)

【讨论】:

    猜你喜欢
    • 2013-12-28
    • 2011-01-27
    • 2015-11-10
    • 2011-01-31
    • 1970-01-01
    • 2023-03-03
    • 2019-04-13
    • 2013-08-13
    • 2012-07-27
    相关资源
    最近更新 更多