1

我遇到了一个我无法理解的奇怪行为。

我正在使用 mcrypt xtea(cfb 模式)来加密一些数据。由于 php 7.2 正在摆脱 mcrypt 并且由于 openssl 也不支持 Xtea,因此我必须自己实现该算法。

问题是,无论使用什么算法:

  • 我测试了这里提供的一个:pear implementation which is an ECB mode only (no init vector)

  • 维基百科页面上提供的那个来自这个stackoverflow 主题

  • 我在这里开发的一个(用于CFB模式)基于维基百科here  和here的以下两篇文章以及可以在here找到的mcrypt源代码 :

    /*
    * $v is the data, $k is the 128bits key and $iv is 64bits init vector (size = 8)
    * Code is not optimized
    */
    function encryptCfb($v, $k, $iv) {
    
        $v = array_values(unpack('N*', $v));
        $iv = array_values(unpack('N*', $iv));
        $k = array_values(unpack('N*', $k));
    
        $cipher = [];
    
        //IV ciphering using the 128bits key
        list ($v0, $v1) = cipher($iv[0], $iv[1], $k);
        //Xoring the cipherd block with the first 64bits of data (32bits in V0 and 32 others in V1)
        $cipher[0] =  $v0 ^ $v[0];
        $cipher[1] =  $v1 ^ $v[1];
    
        for ($i=2; $i < count($v); $i+=2) {
            //Now ciphering the latest "cipherd" data using the 128bits key
            list ($y, $z) = cipher($cipher[$i-2], $cipher[$i-1], $k);
    
            //Xoring the cipherd block with the second 64bits of data (32bits in V0 and 32 others in V1)
             $cipher[$i] =  $y ^ $v[$i];
             $cipher[$i+1] =  $z ^ $v[$i+1];
        }
    
    
        $output = "";
        foreach ($cipher as $i) {
            $output .= pack('N', $i);
        }
    
        return $output;
    }
    
    function cipher($v0, $v1, $k) {
    
        $delta=0x9e3779b9;
        $sum = 0;
        $limit = $delta * 32;
    
        for ($i=0; $i < 32; $i++) {
            $v0 += ((($v1<<4) ^ ($v1>>5)) + $v1) ^ ($sum + $k[$sum & 3]);
            $sum += $delta;
            $v1 += ((($v0 << 4) ^ ($v0 >> 5)) + $v0) ^ ($sum + $k[($sum>>11) & 3]);
        }
    
        return [$v0, $v1];
    }
    

我得到了不同的结果,而且,它们都没有给出完全相同的结果 mcrypt 使用:

$cryptModule = mcrypt_module_open('xtea', '', 'ncfb', '');
mcrypt_generic_init($cryptModule, $key, $iv);
mcrypt_generic($cryptModule, $data);

您可以使用相同的 data/key/IV 检查和测试我在这里所做的不同测试:

有谁知道为什么我得到不同的结果?

4

2 回答 2

2

有谁知道为什么我得到不同的结果?

我没有,但我怀疑所有旧的(2006 年及更早的?)userland-php-implementations 从未在 64 位 PHP 上测试过,也不能在 64 位 PHP 上工作。

我只需要在一个项目中使用 XTEA,和你一样,我测试了所有其他可用的 XTEA 实现,它们都是very old,但没有一个产生正确的结果(我怀疑所有 2006 年及更早的实现从未在64 位系统,并且它们不适用于 64 位系统)

长话短说,我从头开始用 PHP 编写了一个 64 位兼容的 XTEA 实现,代码可以在这里找到:https ://github.com/divinity76/php-xtea

例子

<?php 
require_once('XTEA.class.php');
$key_binary = "secret";
$keys_array = XTEA::binary_key_to_int_array($key_binary);
$data = "Hello, World!";
$encrypted = XTEA::encrypt($data, $keys_array);
$decrypted = XTEA::decrypt($encrypted, $keys_array);
var_dump($data, $encrypted, $decrypted);

应该输出类似:

string(13) "Hello, World!"
string(16) "□□Jx□□□□□□□ܴ9"
string(16) "Hello, World!   "

由于 xtea 长度填充,长度不同,可以通过改为禁用

XTEA::encrypt($data, $keys_array, XTEA::PAD_NONE);

这会给你:

PHP Fatal error:  Uncaught InvalidArgumentException: with PAD_NONE the data MUST be a multiple of 8 bytes! in /cygdrive/c/projects/tibia_login_php/xtea.class.php:73
Stack trace:
#0 /cygdrive/c/projects/tibia_login_php/xtea_tests.php(8): XTEA::encrypt('Hello, World!', Array, 0)
#1 {main}
  thrown in /cygdrive/c/projects/tibia_login_php/xtea.class.php on line 73

因为 XTEA 算法要求要加密的数据为 8 字节的倍数。但是如果我们把它改成

<?php 
require_once('XTEA.class.php');
$keys_binary = "secret";
$keys_array = XTEA::binary_key_to_int_array($keys_binary);
$data = "Hello, World!123";
$encrypted = XTEA::encrypt($data, $keys_array, XTEA::PAD_NONE);
$decrypted = XTEA::decrypt($encrypted, $keys_array);
var_dump($data, $encrypted, $decrypted);

你会得到

string(16) "Hello, World!123"
string(16) "%t□□□n□□aʓ'□□H"
string(16) "Hello, World!123"
  • 没有填充,没有长度变化,也不例外。
于 2019-02-25T21:45:58.170 回答
-1

它应该在 $v0 和 $v1 上使用模数:

function cipher($v0, $v1, $k) {
    $delta = 0x9e3779b9;
    $sum = 0;
    $limit = $delta * 32;

    for ($i=0; $i < 32; $i++) {
        $v0 += ((($v1<<4) ^ ($v1>>5)) + $v1) ^ ($sum + $k[$sum & 3]);
        $v0 = $v0 % pow(2, 32);
        $sum += $delta;
        $v1 += ((($v0 << 4) ^ ($v0 >> 5)) + $v0) ^ ($sum + $k[($sum>>11) & 3]);
        $v1 = $v1 % pow(2, 32);
    }

    return [$v0, $v1];
}

您还需要调整输入值的大小$v以确保它具有正确的长度,例如使用您提到_resize(&$data, $size, $nonull = false)的 PEAR 模块的功能。Crypt_Xtea

于 2017-12-07T16:15:03.140 回答