将c++函数转换为PHP

Convert c++ function to PHP?

本文关键字:PHP 转换 函数 c++      更新时间:2023-10-16

我想将我的xor函数转换为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;
}

您可以在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

在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; }

我希望一切顺利。