怎樣通過(guò)C++ copyfile實(shí)現(xiàn)文件加密傳輸

c++
小樊
82
2024-10-16 19:41:23

在C++中,你可以使用CopyFile函數(shù)來(lái)復(fù)制文件。為了實(shí)現(xiàn)文件加密傳輸,你可以在復(fù)制過(guò)程中對(duì)文件內(nèi)容進(jìn)行加密。以下是一個(gè)簡(jiǎn)單的示例,展示了如何使用C++和Win32 API實(shí)現(xiàn)文件加密傳輸:

#include <iostream>
#include <windows.h>

bool EncryptFile(const std::wstring& inputFileName, const std::wstring& outputFileName) {
    // 定義加密密鑰和初始化向量
    const BYTE key[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF};
    const BYTE iv[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF};

    // 打開(kāi)輸入文件
    HANDLE hInputFile = CreateFile(inputFileName.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hInputFile == INVALID_HANDLE_VALUE) {
        std::cerr << "無(wú)法打開(kāi)輸入文件: " << inputFileName << std::endl;
        return false;
    }

    // 打開(kāi)輸出文件
    HANDLE hOutputFile = CreateFile(outputFileName.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hOutputFile == INVALID_HANDLE_VALUE) {
        std::cerr << "無(wú)法打開(kāi)輸出文件: " << outputFileName << std::endl;
        CloseHandle(hInputFile);
        return false;
    }

    // 為輸出文件創(chuàng)建一個(gè)加密句柄
    HCRYPTPROV hCryptProv = 0;
    HCRYPTKEY hCryptKey = 0;
    if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) {
        std::cerr << "無(wú)法獲取加密上下文" << std::endl;
        CloseHandle(hInputFile);
        CloseHandle(hOutputFile);
        return false;
    }

    if (!CryptGenKey(hCryptProv, AES_KEY_SIZE, CRYPT_EXPORTABLE, &hCryptKey)) {
        std::cerr << "無(wú)法生成加密密鑰" << std::endl;
        CryptReleaseContext(hCryptProv, 0);
        CloseHandle(hInputFile);
        CloseHandle(hOutputFile);
        return false;
    }

    if (!CryptSetKeyParam(hCryptKey, KP_IV, iv, sizeof(iv))) {
        std::cerr << "無(wú)法設(shè)置初始化向量" << std::endl;
        CryptDestroyKey(hCryptKey);
        CryptReleaseContext(hCryptProv, 0);
        CloseHandle(hInputFile);
        CloseHandle(hOutputFile);
        return false;
    }

    // 讀取輸入文件內(nèi)容并加密
    BYTE buffer[4096];
    DWORD bytesRead = 0;
    while (ReadFile(hInputFile, buffer, sizeof(buffer), &bytesRead, NULL)) {
        DWORD bytesEncrypted = 0;
        if (!CryptEncrypt(hCryptKey, hCryptProv, buffer, bytesRead, NULL, &bytesEncrypted)) {
            std::cerr << "加密失敗" << std::endl;
            break;
        }

        if (!WriteFile(hOutputFile, buffer, bytesEncrypted, &bytesEncrypted, NULL)) {
            std::cerr << "寫(xiě)入輸出文件失敗" << std::endl;
            break;
        }
    }

    // 清理資源
    CryptDestroyKey(hCryptKey);
    CryptReleaseContext(hCryptProv, 0);
    CloseHandle(hInputFile);
    CloseHandle(hOutputFile);

    return true;
}

int main() {
    std::wstring inputFileName = L"input.txt";
    std::wstring outputFileName = L"output.enc";

    if (EncryptFile(inputFileName, outputFileName)) {
        std::cout << "文件加密傳輸成功" << std::endl;
    } else {
        std::cerr << "文件加密傳輸失敗" << std::endl;
    }

    return 0;
}

這個(gè)示例使用AES加密算法對(duì)文件進(jìn)行加密。請(qǐng)注意,這個(gè)示例僅用于演示目的,實(shí)際應(yīng)用中可能需要更強(qiáng)大的加密算法和安全措施。在使用此代碼時(shí),請(qǐng)確保你了解相關(guān)的安全和法律問(wèn)題。

0