通过CryptGenKey()函数使用CryptoAPI加密文件

时间:2019-10-12 11:47:09

标签: c++ visual-studio aes cryptoapi

我正在使用CryptoAPI加密文件。 我为此使用AES-128 ECB。 加密时,我正在打印由CryptGenKey()生成的密钥 然后散列并派生密钥。一切都好! 但是,当我尝试对其进行解密时,我使用的是与CryptGenKey()生成的密钥相同的方式,但是没有办法,我的文件已损坏...

加密:

void Encrypt(std::wstring path) 
{
    HCRYPTKEY hKey_fornemty;
    HCRYPTKEY hSessionKey;
    HCRYPTHASH hHash_fornemty;

    if (!CryptGenKey(hProv_fornemty, CALG_AES_128, CRYPT_EXPORTABLE, &hKey_fornemty))
    {
        ExitProcess(0);
    }

    if (!CryptGenKey(hProv_fornemty, AT_KEYEXCHANGE, 512 << 16, &hSessionKey))
    {
        ExitProcess(0);
    }

    DWORD dlen = 0;

    if (!CryptExportKey(hKey_fornemty, hSessionKey, SIMPLEBLOB, 0, NULL, &dlen))
    {
        ExitProcess(0);
    }

    unsigned char* key = new unsigned char[dlen];

    if (!CryptExportKey(hKey_fornemty, hSessionKey, SIMPLEBLOB, 0, key, &dlen))
    {
        ExitProcess(0);
    }

    if (!CryptCreateHash(hProv_fornemty, CALG_SHA_256, 0, 0, &hHash_fornemty))
    {
        CryptReleaseContext(hProv_fornemty, 0);
        ExitProcess(0);
    }
    if (!CryptHashData(hHash_fornemty, (BYTE*)key, 32, 0))
    {
        CryptReleaseContext(hProv_fornemty, 0);
        ExitProcess(0);
    }
    if (!CryptDeriveKey(hProv_fornemty, CALG_AES_128, hHash_fornemty, 0, &hKey_fornemty))
    {
        CryptReleaseContext(hProv_fornemty, 0);
        ExitProcess(0);
    }
        HANDLE hFile;
        DWORD dwBytesRead, dwBytesWritten, dwPos;
        CreateFileW(filePath.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

        LARGE_INTEGER size;
        GetFileSizeEx(hFile, &size);

        unsigned char* buff = new unsigned char[size.QuadPart];
        DWORD lenght = size.QuadPart;

        dwPos = SetFilePointer(hFile, 0, NULL, 0);
        ReadFile(hFile, buff, size.QuadPart, &dwBytesRead, NULL);

        CryptEncrypt(hKey_fornemty, NULL, NULL, 0, buff, &lenght, lenght);

        dwPos = SetFilePointer(hFile, 0, NULL, 0);
        WriteFile(hFile, buff, lenght, &dwBytesWritten, NULL);

        delete[] buff;
}

文件大小-1024字节

解密:

void Decrypt(std::wstring filePath) 
{
    HANDLE hFile;
    DWORD dwBytesRead, dwBytesWritten, dwPos;
    CreateFileW(filePath.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    LARGE_INTEGER size;
    GetFileSizeEx(hFile, &size);
    unsigned char* key= new unsigned char[344];
    dwPos = SetFilePointer(hFile, size.QuadPart - 344, NULL, 0); //key reading (base64)
    ReadFile(hFile, key, 344, &dwBytesRead, NULL);
    HCRYPTHASH hHash;
    HCRYPTKEY hKey;

    if (!CryptCreateHash(hProvs, CALG_SHA_256, 0, 0, &hHash))
    {
        CryptReleaseContext(hProvs, 0);
        ExitProcess(0);
    }
    if (!CryptHashData(hHash, (BYTE*)temp_key, 32, 0))
    {
        CryptReleaseContext(hProvs, 0);
        ExitProcess(0);
    }
    if (!CryptDeriveKey(hProvs, CALG_AES_128, hHash, 0, &hKey))
    {
        CryptReleaseContext(hProvs, 0);
        ExitProcess(0);
    }
        unsigned char* buff = new unsigned char[size.QuadPart];
        DWORD lenght = size.QuadPart;

        dwPos = SetFilePointer(hFile, 0, NULL, 0);
        ReadFile(hFile, buff, size.QuadPart, &dwBytesRead, NULL);

        CryptDecrypt(hKey_fornemty, NULL, NULL, 0, buff, &lenght);

        dwPos = SetFilePointer(hFile, 0, NULL, 0);
        WriteFile(hFile, buff, lenght, &dwBytesWritten, NULL);

        delete[] buff;
}

unsigned char* key来自加密 相同 unsigned char* key来自解密

怎么了?

0 个答案:

没有答案