CakeFest 2017 NYC, the Official CakePHP Conference


(PHP 4 >= 4.0.4, PHP 5, PHP 7)

gmp_xorOperação binária XOR


resource gmp_xor ( resource $a , resource $b )

Faz a operação binária exclusiva OR (XOR) de dois números GMP.



Ele pode ser qualquer número GMP resource, ou uma string numérica que é possível convertê-la para um número.


Ele pode ser qualquer número GMP resource, ou uma string numérica que é possível convertê-la para um número.

Valor Retornado

Um número GMP resource no PHP 5.5 e anteriores, ou um objeto GMP do PHP 5.6 em diante.


Exemplo #1 Exemplo da gmp_xor()

$xor2 gmp_init("0110011001011001"2);
$xor3 gmp_xor($xor1$xor2);
gmp_strval($xor32) . "\n";

O exemplo acima irá imprimir:


add a note add a note

User Contributed Notes 5 notes

kimcbrowne at hotmail dot com
11 years ago
To be unbreakable XOR encryption must have a key that is totally random and is never re-used.  If you use a key a second time, it can be broken.  This can be confirmed by reading the page at

Although I am not an expert on cryptography, I understand that decyphering involves recognizing patterns and that it would be possible to decrypt code that was encoded using XOR with the same key if there were enough samples to examine.  Maintaining unique keys for each encryption at both encryption and decryption points to ensure 100 percent unbreakability has security problems of its own - where and how are the keys stored and how are they transmitted to the decryption points?
bukaj at bukaj dot net
11 years ago
XOR encryption is an ultimate encryption algorithm. It can't be be broken. It is used to encrypt stealth submarine's orders. I cannot agree with "kid-sister" post below. If you use vast key (as long as encrypted message) which is random (space noise recorded on a cd), the encrypted message is also radnom - impossible to decrypt without key. Under those conditions, XOR is strongest encryption algorithm ever known.
8 years ago
here's a fast alternative to compute the xor-value of two bitstrings of an arbitrary (but same) length.

* xor-op for bitstrings of arbitrary length
* bitstrings must have same length
* @param string $o1
* @param string $o2
* @return string
function bitxor($o1, $o2) {
$xorWidth = PHP_INT_SIZE*8;
$o1 = str_split($o1, $xorWidth);
$o2 = str_split($o2, $xorWidth);
$res = '';
$runs = count($o1);
$res .= decbin(bindec($o1[$i]) ^ bindec($o2[$i]));       
patm at pureconnection dot net
13 years ago
XOR encryption only works if the key is at liest the same size as the plaintext, and the key is perfectly random. And no, rand() is not perfectly random.
mikko at entris dot net
14 years ago
The logical XOR can be used for encrypting data. Use resource A as your original text and resource B as the key. Be sure to use long enough key so that the key doesn't loop. Decryption works the same way, input encrypted text as res A and key as res B.
To Top