Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication2.cpp : Ce fichier contient la fonction 'main'. L'exécution du programme commence et se termine à cet endroit.
- //
- #include "pch.h"
- #include <iostream>
- #include <tchar.h>
- #include <stdio.h>
- #include <windows.h>
- #include <wincrypt.h>
- #include <conio.h>
- // Link with the Advapi32.lib file.
- #pragma comment (lib, "advapi32")
- #define ENCRYPT_ALGORITHM CALG_AES_256
- #define ENCRYPT_BLOCK_SIZE 16
- bool MyDecryptFile();
- int main()
- {
- MyDecryptFile();
- return 0;
- }
- //-------------------------------------------------------------------
- // Code for the function MyDecryptFile called by main.
- //-------------------------------------------------------------------
- // Parameters passed are:
- // pszSource, the name of the input file, an encrypted file.
- // pszDestination, the name of the output, a plaintext file to be
- // created.
- // pszPassword, either NULL if a password is not to be used or the
- // string that is the password.
- bool MyDecryptFile()
- {
- //---------------------------------------------------------------
- // Declare and initialize local variables.
- bool fReturn = false;
- HANDLE hSourceFile = INVALID_HANDLE_VALUE;
- HANDLE hDestinationFile = INVALID_HANDLE_VALUE;
- HCRYPTKEY hKey = NULL;
- HCRYPTHASH hHash = NULL;
- HCRYPTPROV hCryptProv = NULL;
- DWORD dwCount = NULL;
- PBYTE pbBuffer = NULL;
- DWORD dwBlockLen = NULL;
- DWORD dwBufferLen = NULL;
- LPCTSTR UserName = L"MyKeyContainer";
- LPCTSTR pszPassword = L" ";
- LPCTSTR pszSourceFile = L"//./F:/test/Holiday.jpg";
- LPCTSTR pszDestinationFile = L"//./F:/test/DEC_Holiday.jpg";
- 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);
- //---------------------------------------------------------------
- // Get the handle to the default provider.
- if(!CryptAcquireContext(&hCryptProv, UserName, NULL, PROV_RSA_AES, 0))
- std::cout << "1 " << GetLastError() << "L ";
- //-----------------------------------------------------------
- // Decrypt the file with a session key derived from a
- // password.
- //-----------------------------------------------------------
- // Create a hash object.
- if(!CryptCreateHash(hCryptProv, CALG_SHA_256, 0, 0, &hHash))
- std::cout << "2 " << GetLastError() << "L ";
- //-----------------------------------------------------------
- // Hash in the password data.
- if(!CryptHashData(hHash, (BYTE *)pszPassword, 20, 0))
- std::cout << "3 " << GetLastError() << "L ";
- //-----------------------------------------------------------
- // Derive a session key from the hash object.
- if(!CryptDeriveKey(hCryptProv, ENCRYPT_ALGORITHM, hHash, 0, &hKey))
- std::cout << "4 " << GetLastError() << "L ";
- //---------------------------------------------------------------
- // The decryption key is now available, either having been
- // imported from a BLOB read in from the source file or having
- // been created by using the password. This point in the program
- // is not reached if the decryption key is not available.
- //---------------------------------------------------------------
- // Determine the number of bytes to decrypt at a time.
- // This must be a multiple of ENCRYPT_BLOCK_SIZE.
- dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
- dwBufferLen = dwBlockLen;
- //---------------------------------------------------------------
- // Allocate memory for the file read buffer.
- pbBuffer = (PBYTE)malloc(dwBufferLen);
- //---------------------------------------------------------------
- // Decrypt the source file, and write to the destination file.
- bool fEOF = false;
- do
- {
- //-----------------------------------------------------------
- // Read up to dwBlockLen bytes from the source file.
- ReadFile(hSourceFile, pbBuffer, dwBlockLen, &dwCount, NULL);
- if (dwCount < dwBlockLen)
- {
- fEOF = TRUE;
- std::cout << dwCount << " restants ";
- }
- //-----------------------------------------------------------
- // Decrypt the block of data.
- if(!CryptDecrypt(hKey, 0, fEOF, 0, pbBuffer, &dwCount))
- std::cout << "5 " << GetLastError() << "L ";
- //-----------------------------------------------------------
- // Write the decrypted data to the destination file.
- WriteFile(hDestinationFile, pbBuffer, dwCount, &dwCount, NULL);
- //-----------------------------------------------------------
- // End the do loop when the last block of the source file
- // has been read, encrypted, and written to the destination
- // file.
- } while (!fEOF);
- fReturn = true;
- //---------------------------------------------------------------
- // Free the file read buffer.
- if (pbBuffer)
- {
- free(pbBuffer);
- }
- //---------------------------------------------------------------
- // Close files.
- if (hSourceFile)
- {
- CloseHandle(hSourceFile);
- }
- if (hDestinationFile)
- {
- CloseHandle(hDestinationFile);
- }
- //-----------------------------------------------------------
- // Release the hash object.
- if (hHash)
- {
- CryptDestroyHash(hHash);
- hHash = NULL;
- }
- //---------------------------------------------------------------
- // Release the session key.
- if (hKey)
- {
- CryptDestroyKey(hKey);
- }
- //---------------------------------------------------------------
- // Release the provider handle.
- if (hCryptProv)
- {
- CryptReleaseContext(hCryptProv, 0);
- }
- return fReturn;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement