Jump to content

I need help with XOR-Encryption in C

v0gty

Hi,

 

Before I start with my question, i would like to apologize for my bad english.

For a school project i need to encrypt a *.csv-file using the XOR-Encryption method,

but I don't know how.

I'm a beginner in C, so could someone of you show me an example

for this?

I already googled about my problem, but I couldn't find anything that helps me.

 

v0gty

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Read the file in blocks, write the blocks to another file with the ciphered data using the bitwise operator ^ (for XOR) to apply your key.

Want to solve problems? Check this out.

Link to comment
Share on other sites

Link to post
Share on other sites

Read the file in blocks, write the blocks to another file with the ciphered data using the bitwise operator ^ (for XOR) to apply your key.

I don't really understand what you mean.

Can you explain that in another way?

 

Or can you show me an example (code) ?

Link to comment
Share on other sites

Link to post
Share on other sites

Do you know how the XOR Cipher works, and what it does?

Want to solve problems? Check this out.

Link to comment
Share on other sites

Link to post
Share on other sites

Do you know how the XOR Cipher works, and what it does?

 

No, not really.

Link to comment
Share on other sites

Link to post
Share on other sites

CPU: i7 4770k | GPU: Sapphire 290 Tri-X OC | RAM: Corsair Vengeance LP 2x8GB | MTB: GA-Z87X-UD5HCOOLER: Noctua NH-D14 | PSU: Corsair 760i | CASE: Corsair 550D | DISPLAY:  BenQ XL2420TE


Firestrike scores - Graphics: 10781 Physics: 9448 Combined: 4289


"Nvidia, Fuck you" - Linus Torvald

Link to comment
Share on other sites

Link to post
Share on other sites

Before I continue, when I say XOR Cipher, I mean what people mistakenly call XOR Encryption. :P

 

I'll try to explain it, but I suck at explaining. Basically data is physically stored in bits. 8 bits make 1 byte. C stores characters in ASCII, and each character in C is 1 byte.

The XOR cipher is a very simple cipher technique that, given a key, sequentially ciphers a given string by continuously "xoring" a string character with a key character.

 

First, XOR is a binary operator that does the following: given 2 bits x and y, the xor is true only if x or y are true, exclusively. Physically, true is represented as 1, and false as 0.

So, this list summarizes XOR:

1 xor 1 = 0

1 xor 0 = 1

0 xor 1 = 1

0 xor 0 = 0

 

The XOR operator in C is ^, and it xors a whole byte. Example:

       10101010

xor  11001100

------------------

       01100110

 

The XOR Cipher does exactly this, by repeatedly going through the key characters (or bytes), and xoring them with the characters from the data you want to cipher.

So, in pseudo-code you could write something like:

key = a_key;to_cipher = some_data;ciphered_string = "";i = 0;for each char c in to_cipher do:    put(in ciphered_string, value c ^ key[i]);    i = (i + 1) % length(key);

Hope this is helpful. By the way, the C and C++ code is virtually identical.

Want to solve problems? Check this out.

Link to comment
Share on other sites

Link to post
Share on other sites

Before I continue, when I say XOR Cipher, I mean what people mistakenly call XOR Encryption. :P

 

I'll try to explain it, but I suck at explaining. Basically data is physically stored in bits. 8 bits make 1 byte. C stores characters in ASCII, and each character in C is 1 byte.

The XOR cipher is a very simple cipher technique that, given a key, sequentially ciphers a given string by continuously "xoring" a string character with a key character.

 

First, XOR is a binary operator that does the following: given 2 bits x and y, the xor is true only if x or y are true, exclusively. Physically, true is represented as 1, and false as 0.

So, this list summarizes XOR:

1 xor 1 = 0

1 xor 0 = 1

0 xor 1 = 1

0 xor 0 = 0

 

The XOR operator in C is ^, and it xors a whole byte. Example:

       10101010

xor  11001100

------------------

       01100110

 

The XOR Cipher does exactly this, by repeatedly going through the key characters (or bytes), and xoring them with the characters from the data you want to cipher.

So, in pseudo-code you could write something like:

key = a_key;to_cipher = some_data;ciphered_string = "";i = 0;for each char c in to_cipher do:    put(in ciphered_string, value c ^ key[i]);    i = (i + 1) % length(key);

Hope this is helpful. By the way, the C and C++ code is virtually identical.

 

I'm sorry I don't think that this is very helpful for me.

Maybe I'm just to stupid for this school.

Link to comment
Share on other sites

Link to post
Share on other sites

You say you're a beginner in C... what do you understand in C? What have you done? What is your background?

Link to comment
Share on other sites

Link to post
Share on other sites

You say you're a beginner in C... what do you understand in C? What have you done? What is your background?

I'm 16 years old and i go to a IT school in Switzerland. In this School i had 2 months of basics in C.

I can do basic functions with if, while and this kind of stuff.

But now it is my task to do such a complicated programm.

That's why I need help

Link to comment
Share on other sites

Link to post
Share on other sites

#include <iostream>#include <string>using namespace std;string text = "The XOR cipher is a very simple cipher technique that, given a key, sequentially ciphers a given string by continuously \"xoring\" a string character with a key character.";string encrypted = "";int key = 5;int main(){	for (int i = 0; i < text.length(); i++){			//loop through each char in the string..		encrypted = encrypted + char(int(text[i] ^ key));	//XOR the char at index 'i' and save to different string..	}								//The char at index 'i' needs to be cast into an int for the XOR and then back to a char so it can be written to the                                                                         //new string..									//http://www.asciitable.com/ 	cout << text << "\n" << encrypted << endl;			//Print the strings to console..									//"\n" makes a new line..	cout << "\nPress enter to quit" << endl;	cin.get();	return 0;}

This code is in C++ but it is only the for loop you are going to be interested in.. Anyway this method is not secure at all but is shows the general idea behind XOR encryption.. The reason being is in the output you can see patterns where the spaces are and you can use an ascii table to get the key..  

 

 

Also thanks forum for butchering the code.. 

CPU: i7 4770k | GPU: Sapphire 290 Tri-X OC | RAM: Corsair Vengeance LP 2x8GB | MTB: GA-Z87X-UD5HCOOLER: Noctua NH-D14 | PSU: Corsair 760i | CASE: Corsair 550D | DISPLAY:  BenQ XL2420TE


Firestrike scores - Graphics: 10781 Physics: 9448 Combined: 4289


"Nvidia, Fuck you" - Linus Torvald

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×