0

I would need to write in PHP a function that outputs me the same result just like the one in C# bellow.

C#

public void Criptami()
   {
       szEncryptionKey = 200;
       szPlainText = input.text;
       StringBuilder szInputStringBuild = new StringBuilder(szPlainText);
       StringBuilder szOutStringBuild = new StringBuilder(szPlainText.Length);
       char Textch;
       for (int iCount = 0; iCount < szPlainText.Length; iCount++)
       {
           Textch = szInputStringBuild[iCount];
           Textch = (char)(Textch ^ szEncryptionKey);
           szOutStringBuild.Append(Textch);
       }
       Debug.Log(szOutStringBuild.ToString());
   }

For now I have tried using function that I have found in another dicussion here on Stackoverflow (Encrypt/decrypt with XOR in PHP), but having the same input, the final output is different.

function xor_this($string) {

    // Let's define our key here
    $key = (200);

    // Our plaintext/ciphertext
    $text = $string;

    // Our output text
    $outText = '';

    // Iterate through each character
    for($i=0; $i<strlen($text); )
    {
        for($j=0; ($j<strlen($key) && $i<strlen($text)); $j++,$i++)
        {
            $outText .= $text{$i} ^ $key{$j};
            //echo 'i=' . $i . ', ' . 'j=' . $j . ', ' . $outText{$i} . '<br />'; // For debugging
        }
    }
    return $outText;
}

I was given the piece of code in C#, but for what I can gather - eventhough my knowldge of C# is almost non existant - it seems to me it should be operating in exactly the same way?

Thanks a million for your time!

EDIT: I have added input and output values

PHP input: ciao output: QYQ]

C# input: ciao output: «¡©§

Pavel
  • 3
  • 3
  • 2
    Can you provide a sample input for testing and what you would expect as output(not everyone has access to C#). – Nigel Ren Sep 16 '19 at 10:33
  • I'm no PHP expert, but why are you looping through the `strlen` of `$key`? – MindSwipe Sep 16 '19 at 10:38
  • @NigelRen but everyone (at least if you're on this site) has access to the Internet: https://dotnetfiddle.net so you could run it – MindSwipe Sep 16 '19 at 10:40
  • @MindSwipe, as part of asking any question - you are expected to provide a [MCVE](https://meta.stackoverflow.com/questions/366988/what-does-mcve-mean) which would mean including relevant information as to what you are using as input and what you expect as output - along with where it is wrong. – Nigel Ren Sep 16 '19 at 10:45
  • I have added the input and output in both PHP and C# as an edit of the OP. – Pavel Sep 16 '19 at 11:06

1 Answers1

0

For some reason you are iterating through strlen($key). I'm guessing you don't want to do that. Here's what my PHP incompetent self manged to do:

function xor_this($string) {

// Let's define our key here
$key = (200);

// Our plaintext/ciphertext
$text = $string;

// Our output text
$outText = '';

// Iterate through each character
for($i=0; $i<strlen($text); $i++)
{
    $char = substr($text, $i, 1);
    $char = chr(ord($char) ^ $key);
    $outText .= $char;
}

return $outText;
}

With help from this SO answer. And as shown in the other answer this can be done a lot more concisely:

function xor_this($string)
{
    $key = (200);
    for($i = 0; $i < strlen($string); $i++) 
            $string[$i] = chr(ord($string[$i]) ^ $key);

    return $string;
}
MindSwipe
  • 7,193
  • 24
  • 47