5 Replies - 589 Views - Last Post: 12 May 2013 - 07:11 AM Rate Topic: -----

#1 theshowtekfan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 27-September 11

[help] need help with simple XOR encryption

Posted 11 May 2013 - 09:19 AM

hi guys i am trying to encrypt a byte array with a simple XOR encryption but i can't really get it to work out, please help me out here.

it does "encrypt" the bytes but when i run the encrypted byte array on the same algorithm it won't decrypt :/

here's my code

std::vector<char> XOREncrypt(std::vector<char> bytes, const char* key) {
	int key_length = strlen(key);

	for(int i = 0; i < bytes.size(); i++) {
		bytes[i] ^= 'x';
	}

	return bytes;
}



Is This A Good Question/Topic? 0
  • +

Replies To: [help] need help with simple XOR encryption

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,472
  • Joined: 05-May 12

Re: [help] need help with simple XOR encryption

Posted 11 May 2013 - 09:51 PM

Show us your decryption method. It should also be (incorrectly) using 'x' as the key for decrypting.

You see, the problem is that you aren't using your key to encrypt. You are simply XOR'ing everything with 'x'. So for your decrypt function to work correctly, you will also need to XOR with 'x'.
Was This Post Helpful? 0
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: [help] need help with simple XOR encryption

Posted 11 May 2013 - 10:17 PM

Are you returning the vector to an object?

  bytes = XOREncrypt(bytes, "");


Was This Post Helpful? 0
  • +
  • -

#4 theshowtekfan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 147
  • Joined: 27-September 11

Re: [help] need help with simple XOR encryption

Posted 12 May 2013 - 04:30 AM

yea the returning the vector to a vector ? :P and i am decrypting by running that function again, i do not have a seperate function for decryption :P
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,439
  • Joined: 25-December 09

Re: [help] need help with simple XOR encryption

Posted 12 May 2013 - 06:41 AM

How do you expect to decrypt something using your encryption function?

You need to write a decryption function.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,472
  • Joined: 05-May 12

Re: [help] need help with simple XOR encryption

Posted 12 May 2013 - 07:11 AM

One of the "features" of XOR encryption is that you use the same function for encryption and decryption. It's also the feature that makes it weak.

Anyway, using the XOREncrypt() from post #1, works just fine for me to round trip the message:
#include <iostream>
#include <vector>

using namespace std;

std::vector<char> XOREncrypt(vector<char> bytes, const char* key)
{
	for(size_t i = 0; i < bytes.size(); i++)
		bytes[i] ^= 'x';

	return bytes;
}

void Dump(vector<char> bytes)
{
    for(char byte : bytes)
        cout << hex << (int) byte << " '" << byte << "' " << " ";
    cout << endl;
}

int main()
{
    string s = "Hello, World";
    vector<char> orig(s.begin(), s.end());
    Dump(orig);

    vector<char> cypherText = XOREncrypt(orig, "");
    Dump(cypherText);

    vector<char> clearText = XOREncrypt(cypherText, "");
    Dump(clearText);

    return 0;
}



So at this point I'm unsure as to what the OP's problem is. Perhaps he doesn't understand that the bytes array passed in by value rather than by reference?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1