Как зашифровать / расшифровать исполняемый файл с использованием алгоритма XTEA? - PullRequest
0 голосов
/ 30 апреля 2020

У меня проблема с кодом ниже (получен из здесь , но здесь по этому вопросу - только часть, которая терпит неудачу), когда я дешифрую исполняемый файл. Код запускается без CRA sh или чего-то подобного. Странно, что файл не расшифровывается, а остается поврежденным. Чего не хватает, это нормально работает? спасибо за любое предложение.

// xtea.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <fstream>
#include <stdint.h>
#include <cstring>
#include <cstdlib>
#include <stdlib.h>

using namespace std;

unsigned int key[4] = { 0xBB8,0xFA0,0x1388,0x1B58 };  
#define BLOCK_SIZE 8                               

/*
    XTea reference code taken from
    http://en.wikipedia.org/wiki/XTEA
*/
void encipher(unsigned int num_rounds, uint32_t v[2], uint32_t const key[4]) {
    unsigned int i;
    uint32_t v0 = v[0], v1 = v[1], sum = 0, delta = 0x9E3779B9;
    for (i = 0; i < num_rounds; i++) {
        v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]);
        sum += delta;
        v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum >> 11) & 3]);
    }
    v[0] = v0; v[1] = v1;
}

void decipher(unsigned int num_rounds, uint32_t v[2], uint32_t const key[4]) {
    unsigned int i;
    uint32_t v0 = v[0], v1 = v[1], delta = 0x9E3779B9, sum = delta * num_rounds;
    for (i = 0; i < num_rounds; i++) {
        v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum >> 11) & 3]);
        sum -= delta;
        v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]);
    }
    v[0] = v0; v[1] = v1;
}

void crypto(char filepath[], bool cipher) {
    fstream dat(filepath, ios::in | ios::out | ios::binary); //Open the file
    if (!dat) {
        cout << "I can not read from the file you provided. Sorry :\(" << endl;
        system("pause");
        exit(-1);
    }

    unsigned size;

    dat.seekg(0, ios::end);
    size = dat.tellg();
    dat.seekg(ios::beg);

    dat.clear();

    unsigned pos;

    int n_blocks = size / BLOCK_SIZE;
    if (size % BLOCK_SIZE != 0)
        ++n_blocks;

    for (int i = 0; i < n_blocks; i++) { //for every character in the executable, encipher the character
        unsigned char data[BLOCK_SIZE];
        pos = dat.tellg();

        dat.read((char*)data, BLOCK_SIZE);

        if (cipher) encipher(32, (uint32_t*)data, key);
        else decipher(32, (uint32_t*)data, key);

        dat.seekp(pos);
        dat.write((char*)data, BLOCK_SIZE);

        memset(data, 0, BLOCK_SIZE);
    }
    dat.close(); //all the enciphered data was sent back the the executable
}

int main(int argc, char* argv[]) {

    if (argc < 2) { //RTFM
        fprintf(stderr, "Usage: %s [filepath]\n", argv[0]);
        return 1;
    }

    crypto(argv[1], true); //true > cipher, false > decipher
    cout << "Finished Ciphering " << endl;
    system("pause"); //press any key to continue
    return 0;
}
...