2017-03-27 79 views
-1

它有比通过XOR加密数据更快的选择吗?PHP - 异或加密

private static function getBinary($string) 
{ 
    $output = ''; 
    for($i = 0; $i < strlen($string); $i++) 
    { 
     $value = decbin(ord($string[$i])); 
     $binary = str_pad($value, 8, '0', STR_PAD_LEFT); 
     $output .= $binary; 
    } 

    return ($output); 
} 

private static function getCrypted($key, $value) 
{ 
    $output = ''; 
    $key_index = 0; 
    $max = strlen($key); 

    for($i = 0; $i < strlen($value); $i++) 
    { 
     if($key_index >= $max){ $key_index = 0;} 
     $output .= ($key[$key_index] XOR $value[$i]) ? '1' : '0'; 
     $key_index++; 
    } 

    return ($output); 
} 

对于小文本,它相对较快,但我需要加密大文本(3MB数据)。但是对于这个文件getCrypted()函数需要10-20秒,这是不可接受的。

$bin_text = self::getBinary($text); // cca 4 sec. 
$bin_key = self::getBinary("12345"); // 0 sec. 

return self::getCrypted($bin_key, $bin_text); // 11 sec+ 

那么,它有什么最快的方法吗?也许将文本转换为二进制文件而不使用0和1创建另一个字符串我不知道...谢谢你的回复。

+1

'XOR'不是加密。 – axiac

+0

@axiac http://www.tech-faq.com/xor-encryption.html –

+0

任何你正在使用'^'操作符逐位完成而不是逐字节操作的原因......? – CBroe

回答

0

的解决方案是不转让位,但字节... @CBroe

$byte_text = unpack('C*', 'asasdkasndaksdnkas'); 
$byte_key = unpack('C*', '12345'); 

$output = ''; 
$key_index = 1; 

for($i = 1; $i < count($byte_text)+1; $i++) 
{ 
    if($key_index > count($byte_key)){ $key_index = 1;} 
    $output .= dechex(($byte_text[$i]^$byte_key[$key_index])); 
    $key_index++; 
} 

echo $output; 

这一切,谢谢。