N
N
Nurlan2018-11-10 02:46:47
PHP
Nurlan, 2018-11-10 02:46:47

How to decrypt in PHP what was encrypted in UTF16?

There is an encryption received by a simple XOR:

Approximate code in JAVA, encrypting which we get a complete match
public class HelloWorld {
    public static void main(String[] args)
    {
      String tmp=xorxor("Проверка","keyyek");
        System.out.println(tmp);
        System.out.println(xorxor(tmp,"keyyek"));
    }

    public static String xorxor(String str, String key) {
            StringBuilder crypt = new StringBuilder();
            for (int i = 0; i < str.length(); i++) {
          int xor=str.charAt(i) ^ key.charAt(i % key.length());
           crypt.append((char) xor);
            }
            String result = crypt.toString();
            return result;
    }
}


It is necessary to decrypt in PHP, the algorithm is repeated, but the result is near zero:
Decoder in PHP
$string="Проверка";

echo xorxor($string,'keyyek');

function xorxor($string,$key){
$res='';
  for($i=0;$i<mb_strlen($string);$i++){
    $part1=mb_substr($string,$i,1);
    $part2=mb_substr($key, $i % mb_strlen($key),1);
 		$res.= $part1 ^ $part2;
  }

  return $res;
}

5be61abc58c1c604667641.jpeg
I understand that the matter is in char and its utf-16, but I could not get a positive result on php.
PS On java - an example in an embrace with Google, to confirm the assumption and find an error.
PPS Not sure if I phrased the question correctly.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nurlan, 2018-11-12
@daager

Taki found a way, it still needs to be combed, but it works:

$res .= html_entity_decode("&#".(mb_ord($part1) ^ mb_ord($part2)).";");

R
Rsa97, 2018-11-10
@Rsa97

Convert the key string to UTF-16 and xor it as one-byte, via substr. Then convert the result to UTF-8.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question