XOR has almost magical properties:
Consider:
if ( condition ) { toggle = ! toggle; }
toggle ^= condition;
Further, the generated byte code for the xor version will be more compact and the generated machine code will execute more quickly.
Here is the essence of a scheme I cooked up for Peter Norton used in his Norton Floppy Backup. The idea was if a diskette was scratched destroying a circular track, or scratched radially, or if a big buttery thumbprint destroyed a blob of the disk, it was still recoverable because the data were recorded redundantly. For every 10 sectors of data recorded, there was one extra sector, the xor of those ten sectors. That extra sector was computed If a sector were destroyed, you would XOR together the remaining 10 sectors to reconstruct the missing sector. The trick was placing the sectors so that damage would unlikely hit two sectors in the same group. If that happened, you were hosed.
If a[] is a byte array of data and k[] is an equally long byte a array of truly random gibberish, not just pseudorandom, then you can encrypt the data
// encrypting a[] with key k[] into e[] // a[] = array to be encrypted // k[] = key of truly random gibberish // e[] = encrypted data byte[] e = new byte[a.length]; for ( int i=0; i<a.length; i++ ) { e[i] = (byte)( a[i] ^ k[i] ); }
Someone peeking at e[] who has no copy of k[] has no way on earth of guessing what a[] is. Every possible encryption of every possible message is equally probable. The decrypted message could equally well say, plan B tonight at seven or "The Snow Queen---------". You could get either result just using two different decrypting keys. Who is to say which is the real one? To decrypt, you use xor again with the same k[]:
// decrypting e[] with key k[] into a[] // e[] = array to be decrypted // k[] = key of truly random gibberish // a[] = original data byte[] a = new byte[e.length]; for ( int i=0; i<e.length; i++ ) { a[i] = (byte)( e[i] ^ k[i] ); }
For this to be secure, you must never reuse a key k. A pre-computer era version of this was known as the Vernam cipher. It is used for diplomatic messages that absolutely must not be cracked. The main problem with it is you somehow have to get the keys to the other end without them being viewed. You can do that with CD (Compact Disc) and secure courier. If you suspect compromise, you don’t use the keys. You need software to make sure you never reuse a key.
This page is posted |
http://mindprod.com/jgloss/xor.html | |
Optional Replicator mirror
|
J:\mindprod\jgloss\xor.html | |
Please read the feedback from other visitors,
or send your own feedback about the site. Contact Roedy. Please feel free to link to this page without explicit permission. | ||
Canadian
Mind
Products
IP:[65.110.21.43] Your face IP:[3.133.140.88] |
| |
Feedback |
You are visitor number | |