Wednesday, 20 August 2014

Console Based - XOR encryption

Console Based - XOR Encryption




Continuing with my console based programming practice I decided to create a really basic encryption program in the console using the XOR(^) operator. The XOR operator takes the binary equivalent of any two variables and "ors" them together. For example.

Letter "h" ASCII equivalent is 104, that number as a binary is 1101000.

Number 20 in binary is 0010100.

1101000 ^ 0010100 = 1111100. 
And 1111100 in decimal is 124, which is assigned to ASCII character "|". In a similar fashion, to go back from "|" to "h" we simply XOR "|" with the key again. The key being 20 in this instance. 

1111100(|) ^ 0010100 (20) = 1101000, which is indeed decimal number 104 or "h".

The code

So, all the pre-processor commands are fairly standard at this point so I won't bother explaining them. The first big chunk of the function "main()" is really just there for creating the variables used in the rest of the code as well as providing a user interface with the console operators such as "cout" or "cin". A Boolean variable, "choice" is created and this will later define whether the function to encrypt or decrypt will be used. I chose a Boolean type variable for this because it only needed to have 2 different states and an integer or other type of variable takes up more space in system memory.
The next variable created is of the string type and is called "orig", this variable is the phrase to be encrypted or decrypted and is passed into the appropriate functions later in the program. It is assigned a value using the "cin" operator.
The last variable created is of integer type and is called "key" is is also user-assigned and will be passed into the encryption or decryption functions later on.



The function used to encrypt the user - chosen string is shown above. It is of return type string and takes in two variables, a string type called "encrypt" and an integer type called "key". These variables are assigned by the user as described above.
The for loop in the function starts by creating a variable to increment by one after each cycle (i). Th condition of the for loop is that it can only go ahead while "i" is less than the value of "string.size()". "string.size()" is a function included in the string class in the <string> library and basically just counts the letters in the string and returns a number, so if the user chose the word "blogger" then the "string.size()" function would give a value of 7 (since there are 7 letters in the word "blogger"). So the contents of the for loop take whichever member of the string the counter variable, i, is on (since a string is just like an array of char variables) and XORs it with the "key" variable. It then multiplies that value by 2 and then takes that variable and minuses 110. the reason the last two actions are applied is because I didn't want to just leave it with only one form of encryption. 2 is chosen as the multiplier since any number it multiplies will become even and hence be divided in the decryption stage without decimal places (which would be casted into integer variables and create an error, the numbers would be one out from their original values.). I had to minus 110 from all the values because otherwise they corresponded to characters that cannot be found on a common keyboard. Once all the values have been altered then the function returns the string all together and in the "main" function its new values are printed to the screen.


The decryption function is very similar except all the actions performed on the values are done in reverse order and are opposite to what they were before. Again the XOR operator is used with the key and as I described at the top of this article all the values should return to what they were originally.





Downloads

I enjoyed this project because it involved a little more background research. For example, I only had a vague idea of what the XOR operator does and had no idea it could be used as such a simple encryption device. I also didn't know anything about the string.size() function and had to research that to make my program work. A link to the project code files can be found here. Thanks for your time in reading this and until next time.








1 comment: