XOR encrypt / decrypt

problem with printf statement

Page 1 of 1

3 Replies - 5360 Views - Last Post: 23 March 2010 - 01:10 PM Rate Topic: -----

#1 IngeniousHax   User is offline

  • |>|20-514<|{3|2

Reputation: 84
  • View blog
  • Posts: 1,385
  • Joined: 28-March 09

XOR encrypt / decrypt

Posted 23 March 2010 - 11:54 AM

So got it this thing to encrypt using bitwise XOR and decrypt... The only problem im having now is the first print statement... It seems to get all jumbled up in a random mess of crap...

the word "output" comes out all wrong, as if it is encrypted, though it shouldn't... The second portion works just fine...

I know my code is messy, I thre it together in about 10 minutes to make sure it could be done.
#include <iostream>

int main()
{
	 int i = 0;
	char x[13] = {"This is a te"};
	char key[13] = {"kalsnhjdlkit"};
	char output[13];
	char decrypt[13];

	for(i = 0; i < 13; i++)
	{
		output[i] = x[i]^key[i];
	}
	printf("output XORed: "); // <---- Problem is here
	i = 0;
	while(i < 13)
	{
		printf("%c", output[i]);
		++i;
	}
	printf("\n");
	for(i = 0; i < 13; i++)
	{
		decrypt[i] = output[i]^key[i];
	}
	printf("Un XORed: ");
	i = 0;
	while(i < 13)
	{	
		printf("%c", decrypt[i]);
		++i;
	}
	printf("\n");
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: XOR encrypt / decrypt

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: XOR encrypt / decrypt

Posted 23 March 2010 - 11:59 AM

Looks like it's doing what it is supposed to. You aren't keeping the limits within printable characters (hence the random mess of crap), but it xor'ed:

int main(){
    int i = 0;
    char x[13] = {"This is a te"};
    char key[13] = {"kalsnhjdlkit"};
    char output[13];
    char decrypt[13];

    for(i = 0; i < 13; i++)
    {
            output[i] = x[i]^key[i];
    }
    printf("output XORed: %s\n\n", output);
    printf("\n");
    for(i = 0; i < 13; i++)
    {
            decrypt[i] = output[i]^key[i];
    }
    printf("Un XORed: %s\n\n", decrypt);
    printf("\n");
    return 0;
}




I got rid of the needless loops, printf can handle a char*
Was This Post Helpful? 0
  • +
  • -

#3 IngeniousHax   User is offline

  • |>|20-514<|{3|2

Reputation: 84
  • View blog
  • Posts: 1,385
  • Joined: 28-March 09

Re: XOR encrypt / decrypt

Posted 23 March 2010 - 12:11 PM

How can I go abouts keeping them with-in the printable character bounds?

I know its something from like, 32-128 or 62(4)-128, but how do I limit it?

This post has been edited by IngeniousHax: 23 March 2010 - 12:12 PM

Was This Post Helpful? 0
  • +
  • -

#4 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: XOR encrypt / decrypt

Posted 23 March 2010 - 01:10 PM

The quickest fix would be to use the loops you have now and do a condition to check its bounds and just not print if it's not in the range.


Alternately, add/subtract an appropriate amount to put it in the bounds, but you'd have to do it on an individual basis.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1