Go Back   CodeCall Programming Forum > Software Development > Tutorials > C Tutorials
Register Blogs Search Today's Posts Mark Forums Read

C Tutorials All C Tutorials and Code

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-29-2009, 10:48 AM
ShadenSmith's Avatar
Newbie
 
Join Date: May 2009
Location: Kentucky - USA
Age: 18
Posts: 19
ShadenSmith will become famous soon enough
Send a message via AIM to ShadenSmith Send a message via MSN to ShadenSmith
C Tutorial - XOR Encryption

First of all, XOR encryption alone is very easy to create and can be easily broken (especially if a weak key is used). This tutorial assumes no responsibility for the quality of encryption.

Now, the first thing to now about XOR encryption is what exactly an XOR operation is. XOR stands for exclusive-or, it is a logical operand. XOR returns true if one and only one of the two arguments is true. A few examples:

1 xor 0 = 1
0 xor 1 = 1
1 xor 1 = 0
0 xor 0 = 0

Notice that if you were to xor the result against the key you will end up with the original value. This is how decrypting XOR encryption works. XOR is a symmetrical operation, so if you encrypt a file and then encrypt it again with the same key you will receive the original plaintext.

Now the XOR encryption uses this operand to cycle through each bit of plaintext and XOR's it against a key. The longer and more random a key is, the stronger the encryption.

The algorithm itself is the focus of this tutorial. The remainder of the program (I/O, etc) will be posted at the bottom but not explained outside of comments.

Code:
void encrypt_data(FILE* input_file, FILE* output_file, char* key)
{
	int key_count = 0; //Used to restart key if strlen(key) < strlen(encrypt)
	int encrypt_byte;

	//Loop through each byte of file until EOF
	while( (encrypt_byte = fgetc(input_file)) != EOF) 
	{
		//XOR the data and write it to a file
		fputc(encrypt_byte ^ key[key_count], output_file);

		//Increment key_count and start over if necessary
		key_count++;
		if(key_count == strlen(key))
			key_count = 0;
	}
}
In C (and many other languages) the ^ is the character that represents XOR.

encrypt_data() takes an input and output file and a key to encrypt with. fgetc() returns the next character from the input stream (our file). We XOR the current character of the file against the corresponding index of key. We then use fputc (it places the given character into an output stream) to place the XOR'ed data into out output file. Finally, we loop key to the index of 0 if we have reached the end of key.

Again, your encryption is only as strong as your key. I would not recommend this for any serious encryption (there are MANY MANY better methods of encryption). I have written this purely as a learning resource and an introduction to cryptography. Below is the source code in whole:

Code:
//XOR Encryption

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_SIZE 256

void strip_newline(char* to_strip);
void encrypt_data(FILE* input_file, FILE* output_file, char *key);

int main(int argc, char* argv[])
{
	//Check for valid number of arguments
	if (argc != 3)
	{
		printf("Invalid number of arguments. %d arguments were supplied.\n", argc);
		printf("Usage: %s inputfile outputfile\n", argv[0]); //Usage: ./xortest inputfile outputfile
		exit(0);
	}
	
	FILE* input;
	FILE* output;

	//Open input and output files
	input = fopen(argv[1], "r");
	output = fopen(argv[2], "w");
		

	//Check input file
	if (input == NULL)
	{
		printf("Input file cannot be read.\n");
		exit(0);
	}
		
	//Check output file
	if (output == NULL)
	{
		printf("Output file cannot be written to.\n");
		exit(0);
	}

	//Key strings
	char *key = malloc(MAX_SIZE);

	//Prompt for key
	printf("Passphrase: ");

	//Read in key
	fgets(key, MAX_SIZE, stdin);

	printf("Encrypting %s\n", argv[1]);

	//strip newlines
	strip_newline(key);

	//XOR data and write it to file
	encrypt_data(input, output, key);
	
	printf("Encrypted data written to %s\n", argv[2]);

	//Release memory
	free(key);

	//Close files
	fclose(input);
	fclose(output);

	return 0;

}


void encrypt_data(FILE* input_file, FILE* output_file, char* key)
{
	int key_count = 0; //Used to restart key if strlen(key) < strlen(encrypt)
	int encrypt_byte;
	
	while( (encrypt_byte = fgetc(input_file)) != EOF) //Loop through each byte of file until EOF
	{
		//XOR the data and write it to a file
		fputc(encrypt_byte ^ key[key_count], output_file);

		//Increment key_count and start over if necessary
		key_count++;
		if(key_count == strlen(key))
			key_count = 0;
	}
}

void strip_newline(char* to_strip)
{
	//remove newlines
	if (to_strip[strlen(to_strip) - 1] == '\n')
	{
		to_strip[strlen(to_strip) - 1] = '\0';
	}
}
I hope you found this tutorial useful!

Last edited by ShadenSmith; 06-29-2009 at 12:54 PM..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 06-29-2009, 11:44 AM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
Re: C Tutorial - XOR Encryption

Very nice. I think it's worth pointing out that to decrypt your data, you simply "encrypt" the encrypted file with the same key.
__________________
CodeCall Blog | CodeCall Wiki | Shareware
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-29-2009, 12:52 PM
ShadenSmith's Avatar
Newbie
 
Join Date: May 2009
Location: Kentucky - USA
Age: 18
Posts: 19
ShadenSmith will become famous soon enough
Send a message via AIM to ShadenSmith Send a message via MSN to ShadenSmith
Re: C Tutorial - XOR Encryption

Yes, that's a very important point that I forgot to mention. I'll fix that now. Thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 06-29-2009, 12:54 PM
Jordan's Avatar
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 24,556
Jordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to all
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
Re: C Tutorial - XOR Encryption

Very nice! +rep
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 06-29-2009, 01:04 PM
ShadenSmith's Avatar
Newbie
 
Join Date: May 2009
Location: Kentucky - USA
Age: 18
Posts: 19
ShadenSmith will become famous soon enough
Send a message via AIM to ShadenSmith Send a message via MSN to ShadenSmith
Re: C Tutorial - XOR Encryption

Thanks Jordan! Codecall is really a great community. I'd like to contribute as much as I can to it.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 01-07-2010, 10:34 AM
Newbie
 
Join Date: Jan 2010
Posts: 1
WIREDTECH is an unknown quantity at this point
Re: C Tutorial - XOR Encryption

Can someone help me understand how to XOR this..what is the process? I have the results but I need to know how to get there:

L1: 941FEA4F2B9E7D41 # Mkey Left part 1
L2: C5A2AD40F2A8B733 # Mkey Left part 2

X1: 51BD470FD936CA72 # XOR L1 & L2

Thank you!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 01-07-2010, 11:19 AM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
Re: C Tutorial - XOR Encryption

Convert them to binary, XOR the binary representation, convert back to hexadecimal.
__________________
CodeCall Blog | CodeCall Wiki | Shareware
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 01-31-2010, 11:24 PM
dcs dcs is online now
Guru
 
Join Date: Mar 2008
Posts: 659
dcs is a jewel in the roughdcs is a jewel in the roughdcs is a jewel in the rough
Re: C Tutorial - XOR Encryption

Code:
	input = fopen(argv[1], "r");
	output = fopen(argv[2], "w");
Wouldn't you want to open the files in binary mode to avoid the possibility of text mode translation of bytes?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
c xor encryption tutorial



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tutorial - ListBox, ComboBox & Command button. travy92 VB Tutorials 15 01-22-2010 10:13 AM
CodeCall Tutorial Contest #4 Jordan Announcements 29 02-25-2008 12:25 PM
John's Java Tutorial Index John Java Tutorials 0 01-11-2007 04:05 PM


All times are GMT -5. The time now is 10:59 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0