Advertisement
Guest User

Untitled

a guest
Dec 9th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. // ConsoleApplication2.cpp : Ce fichier contient la fonction 'main'. L'exécution du programme commence et se termine à cet endroit.
  2. //
  3.  
  4. #include "pch.h"
  5. #include <iostream>
  6. #include <tchar.h>
  7. #include <stdio.h>
  8. #include <windows.h>
  9. #include <wincrypt.h>
  10. #include <conio.h>
  11.  
  12. // Link with the Advapi32.lib file.
  13. #pragma comment (lib, "advapi32")
  14.  
  15. #define ENCRYPT_ALGORITHM CALG_AES_256
  16. #define ENCRYPT_BLOCK_SIZE 16
  17.  
  18. bool MyDecryptFile();
  19.  
  20.  
  21. int main()
  22. {
  23.  
  24. MyDecryptFile();
  25.  
  26. return 0;
  27. }
  28.  
  29. //-------------------------------------------------------------------
  30. // Code for the function MyDecryptFile called by main.
  31. //-------------------------------------------------------------------
  32. // Parameters passed are:
  33. // pszSource, the name of the input file, an encrypted file.
  34. // pszDestination, the name of the output, a plaintext file to be
  35. // created.
  36. // pszPassword, either NULL if a password is not to be used or the
  37. // string that is the password.
  38. bool MyDecryptFile()
  39. {
  40. //---------------------------------------------------------------
  41. // Declare and initialize local variables.
  42. bool fReturn = false;
  43. HANDLE hSourceFile = INVALID_HANDLE_VALUE;
  44. HANDLE hDestinationFile = INVALID_HANDLE_VALUE;
  45. HCRYPTKEY hKey = NULL;
  46. HCRYPTHASH hHash = NULL;
  47.  
  48. HCRYPTPROV hCryptProv = NULL;
  49.  
  50. DWORD dwCount = NULL;
  51. PBYTE pbBuffer = NULL;
  52. DWORD dwBlockLen = NULL;
  53. DWORD dwBufferLen = NULL;
  54.  
  55. LPCTSTR UserName = L"MyKeyContainer";
  56. LPCTSTR pszPassword = L" ";
  57. LPCTSTR pszSourceFile = L"//./F:/test/Holiday.jpg";
  58. LPCTSTR pszDestinationFile = L"//./F:/test/DEC_Holiday.jpg";
  59.  
  60. hSourceFile = CreateFile(
  61. pszSourceFile,
  62. FILE_READ_DATA,
  63. FILE_SHARE_READ,
  64. NULL,
  65. OPEN_EXISTING,
  66. FILE_ATTRIBUTE_NORMAL,
  67. NULL);
  68.  
  69. hDestinationFile = CreateFile(
  70. pszDestinationFile,
  71. FILE_WRITE_DATA,
  72. FILE_SHARE_READ,
  73. NULL,
  74. OPEN_ALWAYS,
  75. FILE_ATTRIBUTE_NORMAL,
  76. NULL);
  77. //---------------------------------------------------------------
  78. // Get the handle to the default provider.
  79. if(!CryptAcquireContext(&hCryptProv, UserName, NULL, PROV_RSA_AES, 0))
  80. std::cout << "1 " << GetLastError() << "L ";
  81.  
  82. //-----------------------------------------------------------
  83. // Decrypt the file with a session key derived from a
  84. // password.
  85.  
  86. //-----------------------------------------------------------
  87. // Create a hash object.
  88. if(!CryptCreateHash(hCryptProv, CALG_SHA_256, 0, 0, &hHash))
  89. std::cout << "2 " << GetLastError() << "L ";
  90.  
  91.  
  92. //-----------------------------------------------------------
  93. // Hash in the password data.
  94. if(!CryptHashData(hHash, (BYTE *)pszPassword, 20, 0))
  95. std::cout << "3 " << GetLastError() << "L ";
  96.  
  97. //-----------------------------------------------------------
  98. // Derive a session key from the hash object.
  99. if(!CryptDeriveKey(hCryptProv, ENCRYPT_ALGORITHM, hHash, 0, &hKey))
  100. std::cout << "4 " << GetLastError() << "L ";
  101.  
  102. //---------------------------------------------------------------
  103. // The decryption key is now available, either having been
  104. // imported from a BLOB read in from the source file or having
  105. // been created by using the password. This point in the program
  106. // is not reached if the decryption key is not available.
  107.  
  108. //---------------------------------------------------------------
  109. // Determine the number of bytes to decrypt at a time.
  110. // This must be a multiple of ENCRYPT_BLOCK_SIZE.
  111.  
  112. dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
  113. dwBufferLen = dwBlockLen;
  114.  
  115. //---------------------------------------------------------------
  116. // Allocate memory for the file read buffer.
  117. pbBuffer = (PBYTE)malloc(dwBufferLen);
  118.  
  119. //---------------------------------------------------------------
  120. // Decrypt the source file, and write to the destination file.
  121. bool fEOF = false;
  122. do
  123. {
  124. //-----------------------------------------------------------
  125. // Read up to dwBlockLen bytes from the source file.
  126. ReadFile(hSourceFile, pbBuffer, dwBlockLen, &dwCount, NULL);
  127. if (dwCount < dwBlockLen)
  128. {
  129. fEOF = TRUE;
  130. std::cout << dwCount << " restants ";
  131. }
  132.  
  133. //-----------------------------------------------------------
  134. // Decrypt the block of data.
  135. if(!CryptDecrypt(hKey, 0, fEOF, 0, pbBuffer, &dwCount))
  136. std::cout << "5 " << GetLastError() << "L ";
  137.  
  138. //-----------------------------------------------------------
  139. // Write the decrypted data to the destination file.
  140. WriteFile(hDestinationFile, pbBuffer, dwCount, &dwCount, NULL);
  141.  
  142. //-----------------------------------------------------------
  143. // End the do loop when the last block of the source file
  144. // has been read, encrypted, and written to the destination
  145. // file.
  146. } while (!fEOF);
  147.  
  148. fReturn = true;
  149.  
  150. //---------------------------------------------------------------
  151. // Free the file read buffer.
  152. if (pbBuffer)
  153. {
  154. free(pbBuffer);
  155. }
  156.  
  157. //---------------------------------------------------------------
  158. // Close files.
  159. if (hSourceFile)
  160. {
  161. CloseHandle(hSourceFile);
  162. }
  163.  
  164. if (hDestinationFile)
  165. {
  166. CloseHandle(hDestinationFile);
  167. }
  168.  
  169. //-----------------------------------------------------------
  170. // Release the hash object.
  171. if (hHash)
  172. {
  173. CryptDestroyHash(hHash);
  174.  
  175. hHash = NULL;
  176. }
  177.  
  178. //---------------------------------------------------------------
  179. // Release the session key.
  180. if (hKey)
  181. {
  182. CryptDestroyKey(hKey);
  183. }
  184.  
  185. //---------------------------------------------------------------
  186. // Release the provider handle.
  187. if (hCryptProv)
  188. {
  189. CryptReleaseContext(hCryptProv, 0);
  190. }
  191.  
  192. return fReturn;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement