I am reading Programming PHP and it has a section on bitwise operators. They seemed rather useless to me until I thought of encryption, and this broguht up two questions I was hoping I could find the answer to here:
1) Are bitwise operators used for encryption/other security meathods?
and
2) Does it serve any other purpouse?
Bitwise Operators
Moderator: General Moderators
- mydimension
- Moderator
- Posts: 531
- Joined: Tue Apr 23, 2002 6:00 pm
- Location: Lowell, MA USA
- Contact:
yep, they are. bitwise operators come in very handy for encryption cause one in particular is its own inverse function. the logical OR or XOR. i wrote a visual basic program that generated an encryption stream and byte by byte enecrypted the file using XOR.
a logical OR works like this: one OR the other BUT NOT both.
so if you have two binary strings XOR'd together, this would be the result:
now, lets reverse it:
anyway, the way i used it was on each indiviual character of the file, like so:
chrOutput = chrInput XOR chrEncrypt
for use in PHP, use the circumflux (^) to do bitwise operation. hope this has been useful to you.
a logical OR works like this: one OR the other BUT NOT both.
so if you have two binary strings XOR'd together, this would be the result:
Code: Select all
1st 2nd out
01100100 XOR 01001101 = 00101001Code: Select all
out 2nd 1st
00101001 XOR 01001101 = 01100100chrOutput = chrInput XOR chrEncrypt
for use in PHP, use the circumflux (^) to do bitwise operation. hope this has been useful to you.
I hope you do not use this on vital data or your second pattern is truely random and used only once 
There are plenty of purposes for bitwise operations.can be done byok, not very useful in php but e.g. in C it spares a third (temp.) variable 
say you want to store multiple selections made by users in a single databasefield. You might take the values and concatenate them (or use implode() or what ever) and store it in a text-type field. Compared to numerical values strings are slooooow. But you might create a single numerical value with bitwise operations. e.g. havingto see wether test has been selected e.g.
The implementation of the beloved md5() function also makes intense use of bitwise operations
and so on and so on 
There are plenty of purposes for bitwise operations.
Code: Select all
// swapping to numerical values
$tmp = $a;
$a = $b;
$b = $tmp;Code: Select all
a = a ^ b;
b = a ^ b;
a = a ^ b;say you want to store multiple selections made by users in a single databasefield. You might take the values and concatenate them (or use implode() or what ever) and store it in a text-type field. Compared to numerical values strings are slooooow. But you might create a single numerical value with bitwise operations. e.g. having
Code: Select all
<form ....>
<input type="checkbox" name="chkї]" value="1" />...
<input type="checkbox" name="chkї]" value="2" />...
<input type="checkbox" name="chkї]" value="4" /> test
<input type="checkbox" name="chkї]" value="8" />...
...
</form>Code: Select all
$nSelections = 0;
foreach($_POST['chk'] as $value)
$nSelections |= $value;Code: Select all
if ($nSelections & ....- you will encounter bitwise operations in APIs as well
- http://www.php.net/manual/en/function.preg-split.phparray preg_split ( string pattern, string subject [, int limit [, int flags]])
...
flags can be any combination of the following flags (combined with bitwise | operator): - Windows Platform SDK: Storage - GetFileAttributes
to check for directory flag you may performDWORD GetFileAttributes(
LPCTSTR lpFileName
);
...
The attributes can be one or more of the following values.
[long list below here
]Code: Select all
if ( (GetFileAttributes("someFilename") & FILE_ATTRIBUTE_DIRECTORY) != 0) ....
The implementation of the beloved md5() function also makes intense use of bitwise operations
Code: Select all
PHPAPI void PHP_MD5Update(PHP_MD5_CTX * context, const unsigned char *input,
unsigned int inputLen)
{
unsigned int i, index, partLen;
/* Compute number of bytes mod 64 */
index = (unsigned int) ((context->countї0] >> 3) & 0x3F);
/* Update number of bits */
if ((context->countї0] += ((php_uint32) inputLen << 3))
< ((php_uint32) inputLen << 3))
context->countї1]++;
context->countї1] += ((php_uint32) inputLen >> 29);
...