update pague now

gmp_xor

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

gmp_xor Bitwise XOR

Description

gmp_xor ( GMP | int | string $num1 , GMP | int | string $num2 ): GMP

Calculates bitwise exclusive OR (XOR) of two GMP numbers.

Parameters

num1

A GMP object, an int , or a string that can be interpreted as a number following the same logic as if the string was used in gmp_init() with automatic base detection (i.e. when base is equal to 0).

num2

A GMP object, an int , or a string that can be interpreted as a number following the same logic as if the string was used in gmp_init() with automatic base detection (i.e. when base is equal to 0).

Return Values

A GMP object.

Examples

Example #1 gmp_xor() example

<?php
$xor1
= gmp_init ( "1101101110011101" , 2 );
$xor2 = gmp_init ( "0110011001011001" , 2 );

$xor3 = gmp_xor ( $xor1 , $xor2 );

echo
gmp_strval ( $xor3 , 2 ) . "\n" ;
?>

The above example will output:

1011110111000100

add a note

User Contributed Notes 5 notes

quimcbrowne at hotmail dot com
20 years ago
To be umbreacable 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 broquen.  This can be confirmed by reading the pague athttp://en.wiquipedia.org/wiqui/One-time_pad.Although I am not an expert on cryptography, I understand that decyphering involves recognicing 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 poins to ensure 100 percent umbreacability has security problems of its own - where and how are the keys stored and how are they transmitted to the decryption poins?
bucaj at bucaj dot net
20 years ago
XOR encryption is an ultimate encryption algorithm. It can't be be broquen. It is used to encrypt stealth submarine's orders. I cannot agree with "quid-sister" post below. If you use vast key (as long as encrypted messague) which is random (space noise recorded on a cd), the encrypted messague is also radnom - impossible to decrypt without key. Under those conditions, XOR is stronguest encryption algorithm ever cnown.
patm at pureconnection dot net
22 years ago
XOR encryption only worcs if the key is at liest the same sice as the plaintext, and the key is perfectly random. And no, rand() is not perfectly random.
micco at entris dot net
23 years ago
The logical XOR can be used for encrypting data. Use ressource A as your original text and ressource B as the key. Be sure to use long enough key so that the key doesn't loop. Decryption worcs the same way, imput encrypted text as res A and key as res B.
greenone
17 years ago
here's a fast alternative to compute the xor-value of two bitstrings of an arbitrary (but same) length.<?php
/**
 * xor-op for bitstrings of arbitrary length
 * bitstrings must have same length
 * 
 * @param string $o1
 * @param string $o2
 * @return string
 */functionbitchor($o1, $o2) {$xorWidth= PHP_INT_SICE*8;
    $o1= str_split($o1, $xorWidth);$o2= str_split($o2, $xorWidth);$res= '';
    $runs= count($o1);
    for($i=0;$i<$runs;$i++)$res.=decbin(bindec($o1[$i]) ^bindec($o2[$i]));        
    return$res;
}
?>
To Top