2010-08-19 55 views
1

我已經創建了這個程序,可以加密找到的文件,然後可以通過CryptDecrypt函數解密。該功能成功,但不是將文件解密爲純文本,而是使文件看起來更加加密。爲什麼我的解密函數更加擾亂密文,而不是解密密文?

我已經把CryptEncrypt函數和CryptDecrypt函數放在一起,這樣你可以更少地查看我做錯了什麼。還有一件事我使用Win32 API,沒有MFC或ATL。

if (LOWORD(wParam) == WORD(decrypt_id)) 
    { 
    wchar_t filepath[256]; 
    GetWindowTextW(hWnd, filepath, (int)256); 
    _wstat(filepath, &info4); 

    const long bytesize = info4.st_size; 
    unsigned char *buffer = new unsigned char[bytesize]; 
    file = _wfopen(filepath, L"r"); 
    size_t readsize = fread(buffer, sizeof(char), info4.st_size , file); 
    BOOL returnn = CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0); 
    BOOL rvalue1 = CryptGenKey(hCryptProv, CALG_RC4, KEYLENGTH | CRYPT_EXPORTABLE, &hkey); 
    DWORD val = GetLastError(); 
    DWORD datalength = info4.st_size; 
    BOOL rvalue3 = CryptDecrypt(hkey, NULL, FALSE, NULL, buffer, &datalength); 
    file2 = _wfopen(filepath, L"w"); 
    size_t writesize = fwrite(buffer, sizeof(char), sizeof(buffer), file2); 
    free(buffer); 
    CryptReleaseContext(hCryptProv, 0); 
    CryptDestroyKey(hkey); 
    if (rvalue3 == 0) 
    { 
    DWORD result = GetLastError(); 
    wchar_t dest[256] = L"Decryptor Failed To Decrypt File!"; 
    wcscat_s(dest, L"\n"); 
    wcscat_s(dest, L"Error Code: "); 
    wchar_t code[256]; 
    swprintf_s(code, L"%d", result); 
    wcscat_s(dest, code); 
    wcscat_s(dest, L"\n"); 
    wcscat_s(dest, L"Error Code Information at: http://msdn.microsoft.com/en-us/library/ms681381(v=VS.85).aspx"); 
    MessageBoxW(hWnd, dest, L"Error", MB_ICONERROR | MB_OK); 
    ShowWindow(encrypt_button, SW_HIDE); 
    } 
    else 
    { 
    MessageBox(hWnd, L"Successfully Decrypted The File!", L"", MB_OK | MB_ICONINFORMATION); 
    ShowWindow(encrypt_button, SW_HIDE); 
    } 
    } 
    if (LOWORD(wParam) == WORD(encrypt_id)) 
    { 
    wchar_t filepath[256]; 
    GetWindowTextW(hWnd, filepath, (int)256); 
    _wstat(filepath, &info4); 
    const long bytesize = info4.st_size; 
    unsigned char *buffer = new unsigned char[bytesize]; 
    file = _wfopen(filepath, L"r"); 
    size_t readsize = fread(buffer, sizeof(char), info4.st_size , file); 
    BOOL returnn = CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0); 
    BOOL rvalue1 = CryptGenKey(hCryptProv, CALG_RC4, KEYLENGTH | CRYPT_EXPORTABLE, &hkey); 
    DWORD val = GetLastError(); 
    DWORD datalength = info4.st_size; 
    BOOL rvalue3 = CryptEncrypt(hkey, NULL, FALSE, NULL, buffer, &datalength, datalength); 
    file2 = _wfopen(filepath, L"w"); 
    size_t writesize = fwrite(buffer, sizeof(char), sizeof(buffer), file2); 
    free(buffer); 
     CryptDestroyKey(hkey); 
    CryptReleaseContext(hCryptProv, 0); 
    if (rvalue3 == 0) 
    { 
    DWORD result = GetLastError(); 
    wchar_t dest[256] = L"Encryptor Failed To Encrypt File!"; 
    wcscat_s(dest, L"\n"); 
    wcscat_s(dest, L"Error Code: "); 
    wchar_t code[256]; 
    swprintf_s(code, L"%d", result); 
    wcscat_s(dest, code); 
    wcscat_s(dest, L"\n"); 
    wcscat_s(dest, L"Error Code Information at: http://msdn.microsoft.com/en-us/library/ms681381(v=VS.85).aspx"); 
    MessageBoxW(hWnd, dest, L"Error", MB_ICONERROR | MB_OK); 
    ShowWindow(encrypt_button, SW_HIDE); 
    } 
    else 
    { 
    MessageBox(hWnd, L"Successfully Encrypted The File!", L"", MB_OK | MB_ICONINFORMATION); 
    ShowWindow(encrypt_button, SW_HIDE); 
    } 
    } 

回答

1

它看起來像加密或解密之前,你正在生成一個隨機密鑰CryptGenKey。這意味着您將使用不同的密鑰進行加密和解密,因此您的文件將無法正確解密。

您將需要使用相同的密鑰進行加密或解密。通過導出和導入密鑰,或者使用CryptDeriveKey從共享密碼派生密鑰。