Created
May 28, 2023 10:37
-
-
Save puppis42/092d3b481bbbb7bbfae6f52b3a26a44b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <tchar.h> | |
#include <stdio.h> | |
#include <windows.h> | |
#include <wincrypt.h> | |
#include <conio.h> | |
#pragma comment (lib, "advapi32") | |
#define KEYLENGTH 0x00800000 | |
#define ENCRYPT_ALGORITHM CALG_RC4 | |
#define ENCRYPT_BLOCK_SIZE 8 | |
void EncryptFile( | |
LPCWSTR pszSourceFile, | |
LPCWSTR pszDestinationFile, | |
LPCWSTR pszPassword) | |
{ | |
HANDLE hSourceFile = INVALID_HANDLE_VALUE; | |
HANDLE hDestinationFile = INVALID_HANDLE_VALUE; | |
HCRYPTPROV hCryptProv = NULL; | |
HCRYPTKEY hKey = NULL; | |
HCRYPTKEY hXchgKey = NULL; | |
HCRYPTHASH hHash = NULL; | |
PBYTE pbKeyBlob = NULL; | |
DWORD dwKeyBlobLen; | |
PBYTE pbBuffer = NULL; | |
DWORD dwBlockLen; | |
DWORD dwBufferLen; | |
DWORD dwCount; | |
hSourceFile = CreateFile( | |
pszSourceFile, | |
FILE_READ_DATA, | |
FILE_SHARE_READ, | |
NULL, | |
OPEN_EXISTING, | |
FILE_ATTRIBUTE_NORMAL, | |
NULL); | |
hDestinationFile = CreateFile( | |
pszDestinationFile, | |
FILE_WRITE_DATA, | |
FILE_SHARE_READ, | |
NULL, | |
OPEN_ALWAYS, | |
FILE_ATTRIBUTE_NORMAL, | |
NULL); | |
CryptAcquireContext( | |
&hCryptProv, | |
NULL, | |
MS_ENHANCED_PROV, | |
PROV_RSA_FULL, | |
0); | |
CryptCreateHash( | |
hCryptProv, | |
CALG_MD5, | |
0, | |
0, | |
&hHash); | |
CryptHashData( | |
hHash, | |
(BYTE*)pszPassword, | |
lstrlen(pszPassword), | |
0); | |
CryptDeriveKey( | |
hCryptProv, | |
ENCRYPT_ALGORITHM, | |
hHash, | |
KEYLENGTH, | |
&hKey); | |
dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE; | |
if (ENCRYPT_BLOCK_SIZE > 1) | |
{ | |
dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE; | |
} | |
else | |
{ | |
dwBufferLen = dwBlockLen; | |
} | |
pbBuffer = (BYTE*)malloc(dwBufferLen); | |
bool fEOF = FALSE; | |
do | |
{ | |
!ReadFile( | |
hSourceFile, | |
pbBuffer, | |
dwBlockLen, | |
&dwCount, | |
NULL); | |
if (dwCount < dwBlockLen) | |
{ | |
fEOF = TRUE; | |
} | |
CryptEncrypt( | |
hKey, | |
NULL, | |
fEOF, | |
0, | |
pbBuffer, | |
&dwCount, | |
dwBufferLen); | |
WriteFile( | |
hDestinationFile, | |
pbBuffer, | |
dwCount, | |
&dwCount, | |
NULL); | |
} while (!fEOF); | |
if (hSourceFile) | |
{ | |
CloseHandle(hSourceFile); | |
} | |
if (hDestinationFile) | |
{ | |
CloseHandle(hDestinationFile); | |
} | |
if (pbBuffer) | |
{ | |
free(pbBuffer); | |
} | |
if (hHash) | |
{ | |
CryptDestroyHash(hHash); | |
hHash = NULL; | |
} | |
if (hKey) | |
{ | |
CryptDestroyKey(hKey); | |
} | |
if (hCryptProv) | |
{ | |
CryptReleaseContext(hCryptProv, 0); | |
} | |
} | |
void DecryptFile( | |
LPCWSTR pszSourceFile, | |
LPCWSTR pszDestinationFile, | |
LPCWSTR pszPassword) | |
{ | |
HANDLE hSourceFile = INVALID_HANDLE_VALUE; | |
HANDLE hDestinationFile = INVALID_HANDLE_VALUE; | |
HCRYPTKEY hKey = NULL; | |
HCRYPTHASH hHash = NULL; | |
HCRYPTPROV hCryptProv = NULL; | |
DWORD dwCount; | |
PBYTE pbBuffer = NULL; | |
DWORD dwBlockLen; | |
DWORD dwBufferLen; | |
hSourceFile = CreateFile( | |
pszSourceFile, | |
FILE_READ_DATA, | |
FILE_SHARE_READ, | |
NULL, | |
OPEN_EXISTING, | |
FILE_ATTRIBUTE_NORMAL, | |
NULL); | |
hDestinationFile = CreateFile( | |
pszDestinationFile, | |
FILE_WRITE_DATA, | |
FILE_SHARE_READ, | |
NULL, | |
OPEN_ALWAYS, | |
FILE_ATTRIBUTE_NORMAL, | |
NULL); | |
CryptAcquireContext( | |
&hCryptProv, | |
NULL, | |
MS_ENHANCED_PROV, | |
PROV_RSA_FULL, | |
0); | |
CryptCreateHash( | |
hCryptProv, | |
CALG_MD5, | |
0, | |
0, | |
&hHash); | |
CryptHashData( | |
hHash, | |
(BYTE*)pszPassword, | |
lstrlen(pszPassword), | |
0); | |
CryptDeriveKey( | |
hCryptProv, | |
ENCRYPT_ALGORITHM, | |
hHash, | |
KEYLENGTH, | |
&hKey); | |
dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE; | |
dwBufferLen = dwBlockLen; | |
pbBuffer = (PBYTE)malloc(dwBufferLen); | |
bool fEOF = false; | |
do | |
{ | |
ReadFile( | |
hSourceFile, | |
pbBuffer, | |
dwBlockLen, | |
&dwCount, | |
NULL); | |
if (dwCount < dwBlockLen) | |
{ | |
fEOF = TRUE; | |
} | |
CryptDecrypt( | |
hKey, | |
0, | |
fEOF, | |
0, | |
pbBuffer, | |
&dwCount); | |
WriteFile( | |
hDestinationFile, | |
pbBuffer, | |
dwCount, | |
&dwCount, | |
NULL); | |
} while (!fEOF); | |
if (pbBuffer) | |
{ | |
free(pbBuffer); | |
} | |
if (hSourceFile) | |
{ | |
CloseHandle(hSourceFile); | |
} | |
if (hDestinationFile) | |
{ | |
CloseHandle(hDestinationFile); | |
} | |
if (hHash) | |
{ | |
CryptDestroyHash(hHash); | |
hHash = NULL; | |
} | |
if (hKey) | |
{ | |
CryptDestroyKey(hKey); | |
} | |
if (hCryptProv) | |
{ | |
CryptReleaseContext(hCryptProv, 0); | |
} | |
} | |
int main(){ | |
LPCWSTR pszSource = L"b.png"; | |
LPCWSTR pszDestination = L"c.png"; | |
LPCWSTR pszPassword = L"du4f0cm600000000"; | |
//EncryptFile(pszSource, pszDestination, pszPassword); | |
DecryptFile(pszSource, pszDestination, pszPassword); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment