Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xor operator working diferent in PHP and Visual Basic

Tags:

php

vba

vb6

I'm having some troubles with xor operator.

I have a visual basic application that has a function whit this line:

numeroCaracter = Asc(password.Substring(contador, 1)) Xor Asc(CadenaEncriptacion.Substring(contador, 1))

password is a string that is received by the function, and CadenaEncriptacion is this constant:

Private Const CadenaEncriptacion As String = "eNcRiPtAcIoNmUyChUlAyGuAyDeLpArAgUaYeNcRiPtAcIoNmUyChUlAyGuAyDeLpArAgUaYeNcRiPtAcIoNmUyChUlAyGuAyDeLpArAgUaYeNcRiPtAcIoNmUyChUlAyGuAyDeLpArAgUaY"

I need translate the function to PHP, I traslated that line on this way:

$numeroCaracter = ord(substr($password, $contador, 1)) xor ord(substr($CadenaEncriptacion, $contador, 1));

The ord function in PHP and Asc in vb are giving the same values in both languages, but numeroCaracter has diferent value in PHP and VB, by the XOR operator...

In php numeroCaracer is always the ord value to each character, in vb the asc function give me other value.

Thanks!

like image 853
msolla Avatar asked Dec 03 '25 16:12

msolla


1 Answers

As in php xor has lower precedence than = your code is interpreted like:

($numeroCaracter = ord(substr($password, $contador, 1))) xor ord(substr($CadenaEncriptacion, $contador, 1));

So, $numeroCaracter gets value of ord(substr($password, $contador, 1)). Add parentheses or use ^ operator instead of xor:

$numeroCaracter = ord(substr($password, $contador, 1)) ^ ord(substr($CadenaEncriptacion, $contador, 1));
like image 62
u_mulder Avatar answered Dec 05 '25 09:12

u_mulder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!