www.pudn.com > FileSecurity.rar > xEncrypt.cpp
// xEncrypt.cpp : 定义 DLL 应用程序的入口点。 // #include "stdafx.h" #include "xEncrypt.h" #include#include #include #include #define BLOCK_SIZE 1000 #define BUFFER_SIZE 1008 #ifdef _MANAGED #pragma managed(push, off) #endif BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; } #ifdef _MANAGED #pragma managed(pop) #endif // 加密文件 XENCRYPT_API BOOL xEncrypt (LPTSTR lpszSource, LPTSTR lpszDestination, LPTSTR lpszPassword) { FILE *hSrcFile = NULL; FILE *hDestFile = NULL; HCRYPTPROV hProv = 0; HCRYPTHASH hHash = 0; HCRYPTKEY hKey = 0; HCRYPTKEY hXchgKey = 0; PBYTE pbBuffer = NULL; PBYTE pbKeyBlob = NULL; BOOL bEOF = 0; BOOL bReturn = FALSE; DWORD dwCount; DWORD dwKeyBlobLen; if ((hSrcFile = _wfopen (lpszSource, TEXT("rb"))) == NULL) { goto exit; } if ((hDestFile = _wfopen (lpszDestination, TEXT("wb"))) == NULL) { goto exit; } if (!CryptAcquireContext (&hProv, NULL, NULL, PROV_RSA_FULL, 0)) { DWORD ret = GetLastError(); if (ret != NTE_BAD_KEYSET) { goto exit; } else { if (!CryptAcquireContext (&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET)) { goto exit; } } } if (lpszPassword == NULL) { if (!CryptGenKey (hProv, CALG_RC2, CRYPT_EXPORTABLE, &hKey)) { goto exit; } if (!CryptGetUserKey (hProv, AT_KEYEXCHANGE, &hXchgKey)) { DWORD ret = GetLastError(); if (ret != NTE_NO_KEY) { goto exit; } if (!CryptGenKey(hProv,AT_KEYEXCHANGE,NULL,&hXchgKey)) { goto exit; } } if (!CryptExportKey (hKey, hXchgKey, SIMPLEBLOB, 0, NULL, &dwKeyBlobLen)) { goto exit; } if ((pbKeyBlob = (PBYTE)malloc(dwKeyBlobLen))==NULL) { goto exit; } if (!CryptExportKey (hKey, hXchgKey, SIMPLEBLOB, 0, pbKeyBlob, &dwKeyBlobLen)) { goto exit; } fwrite (&dwKeyBlobLen, sizeof (DWORD), 1, hDestFile); if (ferror (hDestFile)) { goto exit; } fwrite (pbKeyBlob, 1, dwKeyBlobLen, hDestFile); if (ferror (hDestFile)) { goto exit; } } else { if (!CryptCreateHash (hProv, CALG_MD5, 0, 0, &hHash)) { goto exit; } if (!CryptHashData (hHash, (PBYTE)lpszPassword, wcslen (lpszPassword), 0)) { goto exit; } if (!CryptDeriveKey (hProv, CALG_RC2, hHash, 0, &hKey)) { goto exit; } } if ((pbBuffer=(PBYTE)malloc(BUFFER_SIZE))==NULL) { goto exit; } do { dwCount = fread (pbBuffer, 1, BLOCK_SIZE, hSrcFile); if (ferror (hSrcFile)) { goto exit; } bEOF = feof (hSrcFile); if (!CryptEncrypt (hKey, 0, bEOF, 0, pbBuffer, &dwCount, BUFFER_SIZE)) { goto exit; } fwrite (pbBuffer, 1, dwCount, hDestFile); if (ferror (hDestFile)) { goto exit; } } while (!bEOF); bReturn = TRUE; exit: if (hSrcFile) fclose (hSrcFile); if (hDestFile) fclose (hDestFile); if (pbKeyBlob) free (pbKeyBlob); if (pbBuffer) free (pbBuffer); if (hKey) CryptDestroyKey (hKey); if (hXchgKey) CryptDestroyKey (hXchgKey); if (hHash) CryptDestroyHash (hHash); if (hProv) CryptReleaseContext (hProv, 0); return bReturn; } // 解密文件 XENCRYPT_API BOOL xDecrypt (LPTSTR lpszSource, LPTSTR lpszDestination, LPTSTR lpszPassword) { FILE *hSrcFile = NULL; FILE *hDestFile = NULL; HCRYPTPROV hProv = 0; HCRYPTHASH hHash = 0; HCRYPTKEY hKey = 0; PBYTE pbBuffer = NULL; PBYTE pbKeyBlob = NULL; BOOL bEOF = 0; BOOL bReturn = FALSE; DWORD dwCount; DWORD dwKeyBlobLen; if ((hSrcFile = _wfopen (lpszSource, TEXT("rb"))) == NULL) { goto exit; } if ((hDestFile = _wfopen (lpszDestination, TEXT("wb"))) == NULL) { goto exit; } if (!CryptAcquireContext (&hProv, NULL, NULL, PROV_RSA_FULL, 0)) { DWORD ret = GetLastError(); if (ret != NTE_BAD_KEYSET) { goto exit; } else { if (!CryptAcquireContext (&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET)) { goto exit; } } } if (lpszPassword == NULL) { fread (&dwKeyBlobLen, sizeof (DWORD), 1, hSrcFile); if (ferror (hSrcFile) || feof (hSrcFile)) { goto exit; } if ((pbKeyBlob = (PBYTE)malloc (dwKeyBlobLen)) == NULL) { goto exit; } fread (pbKeyBlob, 1, dwKeyBlobLen, hSrcFile); if (ferror (hSrcFile) || feof (hSrcFile)) { goto exit; } if (!CryptImportKey (hProv, pbKeyBlob, dwKeyBlobLen, 0, 0, &hKey)) { goto exit; } } else { if (!CryptCreateHash (hProv, CALG_MD5, 0, 0, &hHash)) { goto exit; } if (!CryptHashData (hHash, (PBYTE)lpszPassword, wcslen (lpszPassword), 0)) { goto exit; } if (!CryptDeriveKey (hProv, CALG_RC2, hHash, 0, &hKey)) { goto exit; } } if ((pbBuffer = (PBYTE)malloc (BUFFER_SIZE)) == NULL) { goto exit; } do { dwCount = fread (pbBuffer, 1, BLOCK_SIZE, hSrcFile); if (ferror (hSrcFile)) { goto exit; } bEOF = feof (hSrcFile); if (!CryptDecrypt (hKey, 0, bEOF, 0, pbBuffer, &dwCount)) { goto exit; } fwrite (pbBuffer, 1, dwCount, hDestFile); if (ferror (hDestFile)) { goto exit; } } while (!bEOF); bReturn = TRUE; exit: if (hSrcFile) fclose (hSrcFile); if (hDestFile) fclose (hDestFile); if (pbKeyBlob) free (pbKeyBlob); if (pbBuffer) free (pbBuffer); if (hKey) CryptDestroyKey (hKey); if (hHash) CryptDestroyHash (hHash); if (hProv) CryptReleaseContext (hProv, 0); return bReturn; }