سؤال

I am trying to encrypt all the files in a particular folder and that folder has sub folders when i try to print all the files it works good but when i try to encrypt all the files it keeps on encrypting the same file again and again

void dirListFiles(wchar_t *startDir) {
HANDLE hFind;
WIN32_FIND_DATA wfd;
wchar_t path[99999];
char *enName;
const char *extension = ".enc";
int wcsChars;

wsprintf(path, L"%s\\*", startDir);

if ((hFind = FindFirstFile(path, &wfd)) == INVALID_HANDLE_VALUE) {
    return;
}

do {
    if ((wcsncmp(L".", wfd.cFileName, 1) !=0) && (wcsncmp(L"..", wfd.cFileName, 2) != 0) ) {
        wsprintf(path, L"%s\\%s", startDir, wfd.cFileName);
        if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
            dirListFiles(path);
        } else {
            wcsChars = wcslen(path);
            char *szTo = new char[wcsChars + 1];
            szTo[wcsChars] = '\0';
            WideCharToMultiByte(CP_ACP, 0, path, -1, szTo, wcsChars, NULL, NULL);

            enName = (char *)malloc(strlen(szTo) + 1 + 4);
            strcpy(enName, szTo);
            strcat(enName, extension);
            // If i add this line it keeps on encrypting the same file
            //fencrypt(szTo, enName, (unsigned const char*)"1234567812345678");

            printf("%s\n", enName);

            delete[] szTo;
            free(enName);
        }
    }
} while(FindNextFile(hFind, &wfd));

if (GetLastError() != ERROR_NO_MORE_FILES) {
    FindClose(hFind);
    return;
}
}

If i add fencrypt(szTo, enName, (unsigned const char*)"1234567812345678"); then it encrypts the files in the main folder that is G:\WinApp but when it enters G:\WinApp\ipch\winapp-1918e0a3 it keeps on encrypting the same file again and again there is no file in G:\WinApp\ipch\ only a folder winapp-1918e0a3 this is my encryption function please tell where am i wrong

void fencrypt(char* read, char* write, const unsigned char* enc_key) {
RAND_bytes(iv, AES_BLOCK_SIZE);

readFile = fopen(read,"rb");
writeFile = fopen(write,"wb");

fwrite(iv, 1, 8, writeFile);
fwrite("\0\0\0\0\0\0\0\0", 1, 8, writeFile);

AES_set_encrypt_key(enc_key, 256, &key);

init_ctr(&state, iv);
while(1) {
    bytes_read = fread(indata, 1, AES_BLOCK_SIZE, readFile); 
    AES_ctr128_encrypt(indata, outdata, bytes_read, &key, state.ivec, state.ecount, &state.num);

    bytes_written = fwrite(outdata, 1, bytes_read, writeFile); 
    if (bytes_read < AES_BLOCK_SIZE) {
        break;
    }
}

fclose(writeFile);
fclose(readFile);
}
هل كانت مفيدة؟

المحلول

I'm far from 100% sure, but I have a feeling that when you create a new file in your directory, the FindNextFile gets confused and finds the same filename again because it's not actually the same file. You may need to either collect a list of all files in a directory and process them one at a time, or keep a list of what you have already done and skip over the ones you have done.

نصائح أخرى

The FindNextFile call will probably see the newly appeared encrypted file and therefore you will keep processing the new files also.

Since you are appending an ".enc" extension to your encrypted files, it would be relatively simple to skip files that already have this extension.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top