|
|
![]() |
|
|
|
|
||
|
Easy Encryption with Exclusive Or Hey, Ted: I have heard that you can use the exclusive OR operation from Boolean algebra to encrypt and decrypt data. Do you know anything about this? --Bill Exclusive OR (XOR) compares two bits and generates a resulting bit. If the two bits are the same, the resulting bit is off (zero). If the two bits are different, the resulting bit is on (one). This is like saying, "I want everyone who is married or left-handed, but not both, to stand up." Right-handed married people would stand up. Left-handed single people would stand up. Left-handed married people, who fulfill both conditions, and right-handed single people, who fulfill neither condition, would remain seated.
One reason XOR encryption is so easy to use is that one key both encrypts and decrypts data. For example, let's encrypt a 16-character secret message: "I like cheese!" For a key, we'll use the value ARITHMETICISGOOD. The following table shows the result of encrypting the first four bytes of the message. Look at the binary value of the Encrypted variable and make sure you see how it was built from XOR'ing the Secret and Key variables.
Now take the encrypted value and XOR it with the key to get the decrypted value.
Do you see that the decrypted variable is the same as the Secret key with which we started? If you are running V5R2, you can use the new bitwise exclusive OR (%bitxor) built-in function to XOR two strings. You can play with the following code in the debugger to see how this works. This is just an illustration, of course. In a real situation, encryption and decryption would likely be done in different programs.
D Secret s 16 inz('I like cheese!')
D Key s 16 inz('ARITHMETICISGOOD')
D Encrypted s 16
D Decrypted s 16
D
/free
Encrypted = %bitxor(Secret: Key);
Decrypted = %bitxor(Encrypted: Key);
*inlr = *on;
/end-free
XOR encryption is not industrial-strength, but it is sufficient for many in-house applications. For a more detailed explanation, go to this link. --Ted
|
Editors
Contact the Editors |
| Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved. |