Simple encryption algorithm

Few days ago, I was looking for one encryption algorithm for testing and fun purposes. In fact, there are plenty of encryption algorithms are available which some could be found in the form of ready made libraries for various programming languages. For instance, DSA, RSA, SHA-1, MD5 and so on. But I was looking for simple algorithm to write the code by myself and not just using ready made library and pass parameters without understanding the concept of the algorithm. Therefore, I found simplest encryption algorithm which is called XOR (eXclusive OR) cipher. In this post, I will demonstrate about this algorithm and also add implementation of the code.

XOR cipher uses concepts of XOR which is logical operation to encode the data. Simply, XOR output is true whenever both inputs differ. This means that one of the input should be true (1) and the other false (0) to get true (1) result.

Now the question is how XOR could be used in data encryption. The answer is simple, since data is kept in the computer in the binary form, it is possible to define one phrase or character as key, then XOR the key with the data which encrypts the code for you.

For instance, imagine, you want to encrypt one character with XOR cipher. For reaching to this, you should define a character as a key and then XOR the key with the character to generate encrypted data.

Now for applying XOR operation, firstly you need to get ASCII code of the character plus key code and then convert them in the form of binary and apply XOR operation on them. See below example,

Selected character =’A’
ASCII code of ‘A’= 65
Binary code of ’65’= 1000001
Selected key=’Z’
ASCII code of ‘Z’= 90
Binary code of ’90’= 1011010
Result of XOR=0011011
Decimal code of the result = 27
ACII code of ’27’= Escape character

Based on the above example, the encoded result of ‘A’ character with ‘Z’ key is escape character. If we extend the work for string of the characters by applying XOR on each character of the string finally, the string will be encrypted. As a result, it is possible to encode a text file with using XOR cipher. In addition, since everything in computer and files are 0 and 1, the work could be extended for all types of files. In other word, XOR cipher could be easily written in less than 50 lines of code which applicable for all types of the files. For reaching to this ‘^’ XOR sign in C could be utilized, which converts the character to binary and convert it and the returned result is also in the form of decimal number or ASCII code.

The implemented code is available at GitHub from following link.

https://github.com/kasramp/XorCipher-GeeksWeb

The code is written in C with GCC compiler and after compiling the code you just need to set name of source file for encryption, name of encrypted file (output file) and a key which here is a character. The code is also applicable for all types of the file. Below example shows how to compile and use the code in Linux.

$ gcc XorCipher.c -o XorCoding
$ ./XorCoding MyFile.txt MyEncryptedFile.txt K

In the above example, the file will be coded with ‘K’ character.

Now the question is how to decode, encrypted file. The answer is really really easy, just apply XOR with key one more time on encrypted file, see the following example,

$ ./XorCoding MyEncryptedFile.txt DecodedFile.txt K

In fact, XOR concepts are used for decryption the code, based on the concept if XOR apply on a code even times, the result will be the same as original code. For instance, if XOR is applied 2,4,6,8,… times on a code, the code will not be changed.

Finally, breaking XOR cypher without key is very simple, since in the above code ASCII is used, therefore, a code maximumly has 128 different forms and as a result, if you try to decode an encrypted file which is used ASCII character for encryption without the key, in the worst situation you will break the code in maximum 128 (test from 0 to 127) times attempt which could be automated easily with writing simple script.

For more information refer to following links,
Different computer algorithms including encryption algorithms.
http://en.wikipedia.org/wiki/List_of_algorithms
XOR cipher.
http://en.wikipedia.org/wiki/XOR_cipher
ASCII table.
http://www.asciitable.com/

send your idea and information to kasra.madadipouya@geeksweb.eu.org

Leave a Reply