KyleBanks/XOREncryption

fixed

Opened this issue · 1 comments

std::string EncryptDecrypt(std::string toEncrypt) { char key[4] = { 'j', 'T', 'J','d' }; std::string output = toEncrypt; for (int i = 0; i < toEncrypt.size(); i++) { output[i] = toEncrypt[i] ^ key[i % (sizeof(key) / sizeof(char))]; if (output[i] == 0) { output[i] = toEncrypt[i]; } } output[toEncrypt.size()] = 0; return output; }

Original function fails, why is that? Yours seems to be good.

static string encryptDecrypt(string toEncrypt)
{
    //char k_v3[8] = {0x18, 0x1d, 0x08, 0x30, 0x6d, 0x19, 0x12, 0x6e}; // good
    //char k_v3[8] = {'1', 'z', 'w','b','j', 'U', '8','7'}; // good
    //char k_v3[8] = {'b', 'H', 'p','O','n', 'k', 'Z','M'}; // FAIL dec=[BEGIN some text to e]
    char k_v3[8] = {0x18, 0x15, 0x0e,0x1c,0x38, 0x2f, 0x6f,0x6e}; // FAIL dec=[BEGIN some text to enc]

    /*
    // FAIL dec=[BEGIN some text to enco0d ]
    char k_v3[30];
    memset(k_v3,0,sizeof(k_v3));
    strcat(k_v3,"gjw9gjw9gjw9gjw9gjw9w9gjw9gjw9");
    */

    string output = toEncrypt;
    
    for (int i = 0; i < toEncrypt.size(); i++)
    {
        output[i] = toEncrypt[i] ^ k_v3[i % (sizeof(k_v3) / sizeof(char))];
    }
    
    return output;
}

// main
std::string enc = encryptDecrypt("BEGIN some text to enco0d go-04-2k0''gf]dh][df hdf END");
printf("enc=[%s]\n",enc.c_str());

std::string dec = encryptDecrypt(enc.c_str());
printf("dec=[%s]\n",dec.c_str());