Principles of Operating Systems

CMSC 421-04 - Spring 2016


Exclusive Or Cipher

As a part of Project 1, you must implement a very simple encryption method known as an exclusive or (XOR) cipher. An XOR cipher is a very simple, reversible "encryption" method. While it is simple, it is also very much insecure. Much like the standard example of ROT-13 for text-based communications, you wouldn't actually use an XOR cipher for any sort of sensitive communications.

As you should know from CMSC 203, XOR is a simple bitwise operation that follows the truth table shown below:

0 1
0 0 1
1 1 0

That is to say, that if only one bit of the two inputs is 1, then the output bit will be 1. Otherwise if both inputs are 1 or both are 0, the output will be 0. This means that if you XOR something with a constant, you can get the original value back by XORing the output with the same constant, as shown below (for 4 bits, but you get the idea):

0110 ⊕ 1100 = 1010
1010 ⊕ 1100 = 0110

This means that XOR can be used for encryption and subsequent decryption, so long as the same key value is used for both operations. In your project, you are asked to do this on 32-bit blocks of data, with a 32-bit key that is passed in to your system calls. There are essentially two ways that you can handle this in your project code. Either, you can do all operations on an 8-bit level and simply rotate what part of the key you use (which is less efficient), or you can simply pad the data out to an even 32-bit (4-byte) boundary — rounding up the number of bytes so that you can successfully handle the encryption in the manner specified. Personally, I would recommend the latter approach. When a user passes in n bytes of data, if n is not evenly divisible by 4, round it up to the nearest multiple of 4, filling in the empty space with zero bytes. Then you can perform the encryption on the data in 32-bit increments — just XOR (^) each 32-bits of data with the key passed in to the function.


Last modified