| #include <windows.h> |
| #include <wincrypt.h> |
| #include <winhttp.h> |
| #include <stdio.h> |
|
|
| void encryptData(const char* data, BYTE** encryptedData, DWORD* encryptedDataLen) { |
| HCRYPTPROV hProv; |
| HCRYPTKEY hKey; |
| HCRYPTHASH hHash; |
| BYTE* pbKeyBlob; |
| DWORD dwKeyBlobLen; |
| DWORD dwDataLen = strlen(data) + 1; |
|
|
| if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) { |
| printf("CryptAcquireContext failed: %d\n", GetLastError()); |
| return; |
| } |
|
|
| if (!CryptCreateHash(hProv, CALG_SHA_256, 0, 0, &hHash)) { |
| printf("CryptCreateHash failed: %d\n", GetLastError()); |
| CryptReleaseContext(hProv, 0); |
| return; |
| } |
|
|
| if (!CryptHashData(hHash, (BYTE*)data, dwDataLen, 0)) { |
| printf("CryptHashData failed: %d\n", GetLastError()); |
| CryptDestroyHash(hHash); |
| CryptReleaseContext(hProv, 0); |
| return; |
| } |
|
|
| if (!CryptDeriveKey(hProv, CALG_AES_256, hHash, 0, &hKey)) { |
| printf("CryptDeriveKey failed: %d\n", GetLastError()); |
| CryptDestroyHash(hHash); |
| CryptReleaseContext(hProv, 0); |
| return; |
| } |
|
|
| *encryptedDataLen = dwDataLen; |
| *encryptedData = (BYTE*)malloc(*encryptedDataLen); |
|
|
| memcpy(*encryptedData, data, dwDataLen); |
|
|
| if (!CryptEncrypt(hKey, 0, TRUE, 0, *encryptedData, encryptedDataLen, *encryptedDataLen)) { |
| printf("CryptEncrypt failed: %d\n", GetLastError()); |
| free(*encryptedData); |
| *encryptedData = NULL; |
| *encryptedDataLen = 0; |
| } |
|
|
| CryptDestroyKey(hKey); |
| CryptDestroyHash(hHash); |
| CryptReleaseContext(hProv, 0); |
| } |
|
|
| void establishSecureConnection() { |
| HINTERNET hSession = WinHttpOpen(L"A WinHTTP Example Program/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0); |
| if (!hSession) { |
| printf("WinHttpOpen failed: %d\n", GetLastError()); |
| return; |
| } |
|
|
| HINTERNET hConnect = WinHttpConnect(hSession, L"example.com", INTERNET_DEFAULT_HTTPS_PORT, 0); |
| if (!hConnect) { |
| printf("WinHttpConnect failed: %d\n", GetLastError()); |
| WinHttpCloseHandle(hSession); |
| return; |
| } |
|
|
| HINTERNET hRequest = WinHttpOpenRequest(hConnect, L"GET", NULL, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE); |
| if (!hRequest) { |
| printf("WinHttpOpenRequest failed: %d\n", GetLastError()); |
| WinHttpCloseHandle(hConnect); |
| WinHttpCloseHandle(hSession); |
| return; |
| } |
|
|
| BOOL bResults = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0); |
| if (!bResults) { |
| printf("WinHttpSendRequest failed: %d\n", GetLastError()); |
| WinHttpCloseHandle(hRequest); |
| WinHttpCloseHandle(hConnect); |
| WinHttpCloseHandle(hSession); |
| return; |
| } |
|
|
| bResults = WinHttpReceiveResponse(hRequest, NULL); |
| if (!bResults) { |
| printf("WinHttpReceiveResponse failed: %d\n", GetLastError()); |
| } else { |
| printf("Secure connection established successfully.\n"); |
| } |
|
|
| WinHttpCloseHandle(hRequest); |
| WinHttpCloseHandle(hConnect); |
| WinHttpCloseHandle(hSession); |
| } |
|
|
| int main() { |
| const char* data = "Sensitive Data"; |
| BYTE* encryptedData = NULL; |
| DWORD encryptedDataLen = 0; |
|
|
| encryptData(data, &encryptedData, &encryptedDataLen); |
| if (encryptedData) { |
| printf("Encrypted Data: "); |
| for (DWORD i = 0; i < encryptedDataLen; i++) { |
| printf("%02x", encryptedData[i]); |
| } |
| printf("\n"); |
| free(encryptedData); |
| } |
|
|
| establishSecureConnection(); |
|
|
| return 0; |
| } |
|
|