I'm playing with some code and trying to make it work but seems that I'm missing something...
so can anyone please tell me what did I miss or do wrong? program breaks at
*(sData->pFileBuffer+i) ^=*(sData->pKey+j);
Here is the full code: I'm compiling in visual studio 2012 if that has something to do with it...
#include <iostream>
using namespace std;
/*
struct StubData{
char * pFileBuffer;
long long FileSize;
char * pKey;
long KeySize;
};*/
class StubData{
public:
char *pFileBuffer;
long long FileSize;
char *pKey;
long KeySize;
StubData(){}
};
void Decrypt(StubData * sData){
int i=0,j=0;
for(i;i<sData->FileSize;i++){
*(sData->pFileBuffer+i) ^=*(sData->pKey+j);
j++;
if (j>=sData->KeySize)j=0;
}
}
void Encrypt(StubData * sData){
int i,j;
sData->pKey="mysecretpassword";
sData->KeySize=strlen(sData->pKey);
j=0;
printf("[*]Encoding\n");
for(i=0;i<sData->FileSize;i++)
{
*(sData->pFileBuffer+i) ^=*(sData->pKey+j);
j++;
if (j>=sData->KeySize)j=0;
}
}
void main(){
//StubData S;
StubData *S = (StubData *)malloc(sizeof(StubData));
new (S) StubData;
S->pFileBuffer="MARKO";
S->FileSize=strlen(S->pFileBuffer);
Encrypt(S);
cout<<"\nenc\n"<<S->pFileBuffer;
Decrypt(S);
cout<<"\ndec\n"<<S->pFileBuffer;
}
You assign a charater string literal to S->pFileBuffer
S->pFileBuffer="MARKO";
These character strings literals are immutable (often compiled into .rodata
). If you want a mutable character string you should allocate it somewhere.
You can do something like
char str[] = "MARKO"; //be careful, if this goes out of scope before S, then S has a dangling pointer
S->pFileBuffer=str;
This is not very C++-like. But the rest of your code doesn't seem very C++-like either.