I want to translit xor crypting c++ to php. I use this function for crypting. I want decrypt this file with php.
wchar_t* encryptDecrypt(const wchar_t* toEncrypt, int length, wchar_t* EBKey) {
const wchar_t* key = EBKey;
wchar_t* output = new wchar_t[length];
for (int i = 0; i < length; i++) {
output[i] = toEncrypt[i] ^ key[i % wcslen(key)];
}
return output;
}
I read a xls file binary and crypt with encryptDecrypt
void encryptAndSave(char *src,AnsiString key) {
wchar_t *content;
wchar_t buffer[255];
wchar_t *result;
long size;
long fsize;
FILE *f=NULL;
f=fopen(src,"rb+");
fseek(f, 0, SEEK_END);
size = ftell(f);
fseek(f,0, SEEK_SET);
content = (wchar_t *) malloc(size * sizeof (wchar_t));
fread(content, size, 1, f);
fclose(f);
content=encryptDecrypt(content,size,strToWchar_t(key));
f=fopen(src,"wb+");
fwrite(content, size, 1, f);
fclose(f);
}
I use this function to convertion key in to wchar_t
wchar_t* strToWchar_t(AnsiString value) {
int iSize = value.WideCharBufSize();
wchar_t *tmp = new wchar_t[iSize];
value.WideChar(tmp,iSize);
return tmp;
}
This code in php for decrypting
function encryptDecrypt($toEncrypt,$key) {
$output=array();
for ( $i = 0; $i < strlen($toEncrypt); $i++ ) {
$output[$i]= $toEncrypt[$i] ^ $key{$i % strlen($key)};
}
return $output;
}
But it is not rightly decrypting.When i crypt and decrypt with c++ this word, the result must be "qwerty" to "ﻎCDT@GH", but in php it does not work as c++. When i decrypt "ﻎCDT@GH" in php i get "яМp1v3e2s1u3y2", but i must get "qwerty". Please help to resolve this problem.