在文件末尾用mcrypt奇怪的字符解密文件

时间:2014-09-15 12:03:20

标签: php delphi delphi-xe3 mcrypt rijndael

我对自制函数decryptfile有一个非常奇怪的问题。 我想解密一个用我们自己的Delphi程序加密的mp3文件 用php解密这首歌之后,数据与用Delphi解密它的情况略有不同。与Delphi和php相比,这会导致文件大小的差异。 这是我们解密的每个文件,因此读取最后578个字节并不总是相同。

我们的结果数据示例:

Taginfo:filename: 180519: size: 7342031 bytes (Delphi decrypted)

TAGStolen danceMilky ChanceLICHTDICHT RECORDS#$#$2013000000100001000000000000000000000000000000000000000000000, 840900000000000010100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000


Taginfo:filename: 180519: size: 7342032 bytes (PHP decrypted)

AGStolen danceMilky ChanceLICHTDICHT RECORDS#$#$2013000000100001000000000000000000000000000000000000000000000, 8409000000000000101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000T�8߂�j#���#6&

Difference in size: -1

您可以看到数据不等于且高于最终PHP数据不同。

这是我的功能:

require_once('mp3file.class.php'); 

function decryptfile($str){
    //read salt
    $salt = substr($str, 0, 8); //salt ok
    $str_out = substr($str, 8);  //encrypted data

    //read key
    $k = '';
    $bytes = array(*, *, *, *, *, *, *, *, *, *, *, *, *, *, *, *, *, *, *, *, *, *, *, *, *, *, *, *, *, *, *);        
    $string = "";
    foreach ($bytes as $chr) {
        $string .= chr($chr);
    }
    $key = $string;

    //fill hash
    $ctx = hash_init('sha512');
    hash_update($ctx, $salt);  
    hash_update($ctx, $key);
    $xkey = hash_final($ctx, true);

    //assing key
    $k = substr($xkey, 0, 32);

    //iv
    $iv = ''; 
    $size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    $iv_array = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
    $string = "";
    foreach ($iv_array as $chr) {
        $iv .= chr($chr);
    }

    $str = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $k, $str_out, MCRYPT_MODE_CBC,$iv);

    //$pad = ord($str[($len = strlen($str)) - 1]);

    return rtrim($str, "\0");
    //return substr($str, 0, strlen($str)-$pad);
}


    $farray = scandir('/var/www/html/test/in/');

    foreach ($farray as $item) {
        if (($item != '.') && ($item != '..')) {
            $filename = "test/in/".$item;
            $filename2 = "test/out/".$item;
            $filename2b = "test/out/".$item.".mp3";

            $enc = file_get_contents($filename);

            $jak = decryptfile($enc);

            $dec = fopen($filename2b, "w");
            fwrite($dec, $jak);
            fclose($dec);
            $strfile1 = "filename: " . $item . ': size: ' . filesize($filename2) . ' bytes (Delphi decrypted)<br>';
            $strfile2 = "filename: " . $item . ': size: ' . filesize($filename2b) . ' bytes (PHP decrypted)<br>';
            $v1 = filesize($filename2);
            $v2 = filesize($filename2b); 
            $v1 = $v1-$v2; 
            echo '<br>Taginfo:'. $strfile1. '<br>';
            $dec = file_get_contents($filename2);
            $dec2 = file_get_contents($filename2b);
            echo substr($dec, -578);
            echo '<br><br>';
            echo '<br>Taginfo:'. $strfile2. '<br>';
            echo substr($dec2, -578). '<br>';
            echo '<br>Difference in size: '.$v1 . '<br>';
            echo 'original header:<br>';
            $m = new mp3file($filename2);
            $a = $m->get_metadata();

            if ($a['Encoding']=='Unknown')
                $str = "?";
            else if ($a['Encoding']=='VBR')
                $str = print_r($a);
            else if ($a['Encoding']=='CBR')
                $str = print_r($a);
            unset($a);
            echo '<br><br>';
            echo 'decrypted header:<br>';
            $m = new mp3file($filename2b);
            $a = $m->get_metadata();

            if ($a['Encoding']=='Unknown')
                $str = "?";
            else if ($a['Encoding']=='VBR')
                $str = print_r($a);
            else if ($a['Encoding']=='CBR')
                $str = print_r($a);
            unset($a);
            echo '<br><br>';

        }
    }
exit; 

有人可以解释一下这是什么问题吗?

0 个答案:

没有答案