-
strlen
Can anybody see why strlen(Encrypted) should return 24?
Code:
#include <stdio.h>
#include <conio.h>
#include <string.h>
void XOREncrypt(const char*, const char*, char*);
void XOREncrypt(const char* Text, const char* Key, char* Out)
{
long CurIndex = 0, KeyIndex = 0;
delete[] Out;
Out = new char[strlen(Text)];
for(CurIndex = 0; CurIndex < strlen(Text); CurIndex++)
{
Out[CurIndex] = Text[CurIndex] ^ Key[KeyIndex];
KeyIndex++;
if(KeyIndex == strlen(Key))
KeyIndex = 0;
}
}
int main()
{
char* Encrypted = new char;
char Text[] = "testing", Key[] = "xSquared";
XOREncrypt(Text,Key,Encrypted);
printf("%s XOR %s = %ld", Text, Key, strlen(Encrypted));
getch();
return 0;
}
-
strlen() looks for a string terminator to determine a string's length. So for strlen() to work, you need something like:
Out[strlen(Text)] = '\0';
at the end of XOREncrypt().
Also, you need to either pass Out as a reference, or make it the return value.
One other problem is if you xor a letter with the same letter, you get 0, which is the same as a string terminator.
-
Okay, first thing. You don't need a function prototype if the function body is *before* main. Just FYI. Anyways, why in main are you only allocating 1 byte for Encrypted? You should probably allocate this elsewhere.
-
Swoopy is precisely correct. Depending on the results of the encryption, calling strlen would do one of three things:
(1) You would get "lucky" and get the correct length.
(2) A zero in the middle of the string would fool strlen into thinking there were less characters.
(3) Strlen cruises straight past the array bounds and possibly raises an exception/crashes the computer!
So store the length of the first function parameter somewhere and consider the resultant "encrypted" array as "non-text", never invoking string-based functions upon it.
Two more suggestions: there is no need to allocate for the char in main like so. Just leave it be. Second, you call strlen in your function EVERY iteration of the loop, making it inefficient, especially if you are processing large chunks of memory.
Overall, I like your style, though. Good work.
Code:
char* XOREncrypt(const char* Text, const char* Key)
{
long CurIndex = 0, KeyIndex = 0, TextLen = strlen(Text), KeyLen = strlen(Key);
char *Out = new char[TextLen + 1];
for(CurIndex = 0; CurIndex < TextLen; CurIndex++)
{
Out[CurIndex] = Text[CurIndex] ^ Key[KeyIndex];
KeyIndex++;
if(KeyIndex == KeyLen)
KeyIndex = 0;
}
Out[CurIndex] = 0; /*...null-term, so you can safely call string funcs (strlen, etc...)!
But remember - even so, that may not function as expected...*/
return Out;
}
int main()
{
char Text[] = "testing", Key[] = "xSquared";
char* Encrypted = XOREncrypt(Text,Key);
printf("%s XOR %s = %s", Text, Key, Encrypted);
printf("Unreliable length: %i", strlen(Encrypted));
getch();
return 0;
}
-
Thanks for your help. I've been programming since grade 5 (I'm now going into gr 11), when I taught myself QBasic. I've taught myself 10 programming languages so far, VB I learned last summer, and I've begun to teach myself C++ over the last two weeks.
-
Hmm, so is programming taught to youth in Canadian schools?
-
i taught myself html, qbasic, c++, acitonscript, javascript, php, and java, in scool im doing a viula basic class (seventh grade)
-
You may already know this, but here are the changes to make.
Code:
>void XOREncrypt(const char*, const char*, char*);
void XOREncrypt(const char*, const char*, char* &);
>void XOREncrypt(const char* Text, const char* Key, char* Out)
void XOREncrypt(const char* Text, const char* Key, char* &Out)
>{
> long CurIndex = 0, KeyIndex = 0;
> delete[] Out;
> Out = new char[strlen(Text)];
Out = new char[strlen(Text)+1];
> for(CurIndex = 0; CurIndex < strlen(Text); CurIndex++)
> {
> Out[CurIndex] = Text[CurIndex] ^ Key[KeyIndex];
> KeyIndex++;
> if(KeyIndex == strlen(Key))
> KeyIndex = 0;
> }
Out[CurIndex] = '\0';
>}
As far as the using strlen(), it's fine. Just make sure there are no matching characters in the same positions for Text and Key. Your example had no matching characters, so you probably already knew this.
-
>Hmm, so is programming taught to youth in Canadian schools?
At my school, we learn VB in grade 11, and Java in grade 12. I'm going into grade 11 and taking gr12 programming and math, which really screwed up my schedule. =(