mgo
OS/400 Edition
Volume 3, Number 10 -- February 14, 2003

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.

 Variable  Hex  Binary
 Secret  C9409389  11001001010000001001001110001001
 Key  C1D9C9E3  11000001110110011100100111100011
 Encrypted  08995A6A  00001000100110010101101001101010

Now take the encrypted value and XOR it with the key to get the decrypted value.

 Variable  Hex  Binary
 Encrypted  08995A6A  00001000100110010101101001101010
 Key  C1D9C9E3  11000001110110011100100111100011
 Decrypted  C9409389  11001001010000001001001110001001

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


Sponsored By
ADVANCED SYSTEM CONCEPTS

Monitor Critical Events

Before your operation goes down!
- iSeries message queues -
- iSeries subsystems -
- Windows Servers -
- TCP/IP devices/services -
- Runaway jobs -

OpCenter Message Alert is the easiest, most versatile way to save your system, and your job! Built on graphical console technology, Message Alert can be configured and monitored remotely, or devices can be paged/emailed in the event of pending disaster. Why continue living in the dark when OpCenter is ready to assist you?
Read More


THIS ISSUE
SPONSORED BY:

ASC
Snap-E Books


BACK ISSUES

TABLE OF
CONTENTS

Easy Encryption with Exclusive Or

Commenting FTP Scripts

Reader Feedback and Insights: Odds and Ends



Editors
Howard Arner
Joe Hertvik
Ted Holt
David Morris

Managing Editor
Shannon Pastore

Publisher and
Advertising Director:

Jenny Thomas

Advertising Sales Representative
Kim Reed

Contact the Editors
Do you have a gripe, inside dope or an opinion?
Email the editors:
editors@itjungle.com

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.