So, I'm trying to implement a XOR encryption as follows :
string XOR(char *data, char *key) {
for (int i = 0; i != sizeof(data); i++)
{
data[i] ^= key[i % sizeof(*key)];
}
cout << sizeof(data) << "\n";
return data;
}
This is the function that will take as input data
and key
The important thing here is that for each character in the array data
, a XOR operation is made with the key, hence the for loop : for (int i = 0; i != sizeof(data); i++)...
The problem arises with sizeof(data)
which returns 4
or my expcted output would be 27
(26 letters in the alphabet + the null terminator (I think)).
The program outputs this :
4
#"%efghijklmnopqrstuvwxyz
Only the first 4 letters are processed correctly.
What is strange is that sizeof(str)
returns 27 which is corect so why sizeof(data)
returns 4 ?
So what do I need to change in the parameters of the fucntion XOR
?
Here is the entire code :
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
string XOR(char *data, char *key) {
for (int i = 0; i < sizeof(data) + 1; i++)
{
data[i] ^= key[i % sizeof(*key)];
}
cout << sizeof(data);
return data;
}
int main()
{
char clef[] = "A";
char str[] = "abcdefghijklmnopqrstuvwxyz";
cout << XOR(str, clef) << "\n";
}