2015-06-01 61 views
-6

我想将我的xor函数转换为PHP。可以做到吗?它需要为它单曲目前正在努力工作...将C++函数转换为PHP?

string encryptDecrypt(string toEncrypt) { 
    char key[9] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' }; 
    string output = toEncrypt; 
    for (int i = 0; i < toEncrypt.size(); i++) 
     output[i] = toEncrypt[i]^key[i % (sizeof(key)/sizeof(char))]; 

    return output; 
} 
+0

你能澄清到底发生了什么功能吗? – Ben

+0

XOR函数中会发生什么? -_-我正在加密我的文件中的文本... – Patratel

+0

我不知道XOR函数是什么,我不熟悉C++。你能否解释到底发生了什么,以便我们能够理解如何在PHP中复制它? – Ben

回答

0

您可以使用几乎相同的语法在PHP作为C++函数的作用:

function encryptDecrypt($toEncrypt) 
{ 
    $key= array('1', '2', '3', '4', '5', '6', '7', '8', '9'); 
    $key_len = count($key); 
    $output = $toEncrypt; 
    for ($i = 0; $i < strlen($toEncrypt); $i++) 
    { 
     $output[$i] = $toEncrypt[$i]^$key[$i % $key_len]; 
    } 

    return $output; 
} 

在线演示了C++函数:https://ideone.com/g9cpHJ

为PHP函数在线演示:https://ideone.com/3prgd0

+0

现在这就是我正在谈论的!是啊!非常感谢你,只有一个问题,这不关键,如果我使用字符如:“£!$£^£$&%&right? – Patratel

+0

是否要使用这些字符作为键?如果他们不是ascii字符,你必须确保你的源被保存为UTF-8,但它应该工作。 –

+0

我将测试,看看再次感谢您! – Patratel

0

在PHP是这样的:

function encryptDecrypt($toEncrypt){ 
    $key = array('1', '2', '3', '4', '5', '6', '7', '8', '9'); 
    $output = $toEncrypt; 
    for ($i = 0; $i < strlen($toEncrypt); $i++) 
     $output = pow($toEncrypt[$i], $key[$i % count($key)];); 

    return $output; } 

我希望没事。

+0

谢谢你我会测试! – Patratel

+0

This returns = 0. – Patratel

+0

Vezi ca nu ai declarat $ i :) – Patratel