CCN Suggestions Comments and Ideas

Mistake in one of the articles – acgeek

acgeek

Junior Member

Posts: 2
From:
Registered: 11-19-2007
There is an off-by-one error in spade's "General Development: Basic XOR Encryption(c++)" article. Instead of:

for(int i=0, j=0; i < strlen(buf); i++, j++){
if(j == strlen(key)-1) j=0;
buf[i] = buf[i]^key[j];
}

The code should read:

for(int i=0, j=0; i < strlen(buf); i++, j++){
if(j==strlen(key)) j=0;
buf[i] = buf[i]^key[j];
}

Otherwise the last character in a key of length 2 or more will never be used.

[This message has been edited by acgeek (edited November 19, 2007).]

spade89

Member

Posts: 561
From: houston,tx
Registered: 11-28-2006
hi and welcome to ccn acgeek

the reason i did that is the last character in strings is usualy the '\0'(string terminating character if i am not mistaken) i didn't want to include that in the key.

let us say the user wanted the key to be "mypassword" when the program encryptes it without the j == strlen(key)-1 it would be encrypted by the key "mypassword\0" and to decrypt it when the user inputs the decryption key he/she knows(i.e. "mypassword") it wouldn't decrypt right ....

does what i say make sense??

try compiling the program and get it to use a files and stuff then see if my version or the version of the code you suggested works....

Edit: but i am glad to see so many people read that article i guess the idea of encryption is intriguing to most people.
------------------
John 14:6

Jesus answered, "I am the way and the truth and the life. No one comes to the Father except through me.

[This message has been edited by spade89 (edited November 19, 2007).]

acgeek

Junior Member

Posts: 2
From:
Registered: 11-19-2007
Hmmm, honestly I'm a Java programmer, so I don't happen to have a c++ compiler handy.

I think the deal with the terminating character might depend on what language you are using and how you are receiving input. You never deal with a terminating character regardless of how you get your input. PHP and Javascript tend not to have them either. Only place I've seen them is from bios calls in 80x86 assembler. Of course seeing as c++ and 80x86 are closely linked that would explain the reason for a \0 character when accepting user input from the console, but I doubt that you will get a \0 character from a textbox.

Anyway, interesting article either way.

PS: Why does the code use strlen(key)? I thought you could code key.length in c++ to get the length of string, just like an array?

[This message has been edited by acgeek (edited November 27, 2007).]

spade89

Member

Posts: 561
From: houston,tx
Registered: 11-28-2006
yes the only place i have dealt with \0 character i think is in c++ but also keep in mind that the program i wrote was meant to be a command line type of program with no gui stuff so if you were using a textbox you wouldn't have to deal with that character and also i could have used the popular string class in c++ instead of a character array but i think iterating through each character this way shows how then encryption works easily.

quote:

Why does the code use strlen(key)? I thought you could code key.length in c++ to get the length of string, just like an array?

it is a character array it doesn't belong to any class so a code like:


char key[256];
key.something=0;//wouldn't work
key.somefunc();//also wouldn't work

key is an array of the basic type char it doesn't inherit anything from anyone and it doesn't belong to any class. unlike in java in c++ you don't need classes to do some stuff.

if i was to replace parts of the code like cin and cout and fix some headers then the code will qualify as c code.

you can do the same in java actually java has a cipher class so..

feel free to make your own example in java,php or other languages and i might add it to the article

the whole article should be seen as language independent you should be able to do xor encryption in most if not all real programming languages.

------------------
John 14:6

Jesus answered, "I am the way and the truth and the life. No one comes to the Father except through me.

Mene-Mene

Member

Posts: 1398
From: Fort Wayne, IN, USA
Registered: 10-23-2006
seremb: Welcome to CCN, what exactly is the relevance of those links to his question?

------------------
MM out-
Thought travels much faster than sound, it is better to think something twice, and say it once, than to think something once, and have to say it twice.
"Frogs and Fauns! The tournament!" - Professor Winneynoodle/HanClinto
I reserve the full right to change my views/theories at any time.