1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| #include <windows.h> #include <ntdef.h> #include <stdio.h> #include <bcrypt.h>
void DecryptWithBCrypt() { BCRYPT_ALG_HANDLE hAesAlg = NULL; BCRYPT_KEY_HANDLE hKey = NULL; DWORD cbData = 0, cbKeyObject = 0, cbBlockLen = 0, cbBlob = 0; PBYTE pbKeyObject = NULL, pbIV = NULL, pbBlob = NULL, pbPlainText = NULL;
BYTE rgbIV[16] = {0x93, 0x6A, 0xF2, 0x25, 0xFA, 0x68, 0x10, 0xB8, 0xD0, 0x7C, 0x3E, 0x5E, 0x9E, 0xE8, 0xEE, 0x0D};
BYTE rgbAES128Key[16] = {0x4C, 0x9D, 0x7B, 0x3E, 0xEC, 0xD0, 0x66, 0x1F, 0xA0, 0x34, 0xDC, 0x86, 0x3F, 0x5F, 0x1F, 0xE2};
BYTE rgbCipherText[] = {0xA4, 0xE1, 0x0F, 0x1C, 0x53, 0xBC, 0x42, 0xCD, 0x8E, 0x71, 0x54, 0xB7, 0xF1, 0x75, 0xE3, 0x50, 0x97, 0x20, 0x71, 0x97, 0xA8, 0x3B, 0x77, 0x61, 0x40, 0x69, 0x68, 0xC1, 0xB4, 0x7B, 0x88, 0x54, 0x9F, 0x19, 0x03, 0x44, 0x70, 0x78, 0x24, 0x25, 0xF0, 0xA9, 0x65, 0x35, 0x91, 0x3A, 0x04, 0x9C, 0x4E, 0x66, 0xBE, 0xD2, 0x8B, 0x8B, 0x20, 0x73, 0xCE, 0xA0, 0xCB, 0xE9, 0x39, 0xBD, 0x6D, 0x83};
if (!NT_SUCCESS(BCryptOpenAlgorithmProvider(&hAesAlg, BCRYPT_AES_ALGORITHM, NULL, 0))) { goto Cleanup; }
if (!NT_SUCCESS(BCryptGetProperty(hAesAlg, BCRYPT_OBJECT_LENGTH, (PBYTE)&cbKeyObject, sizeof(DWORD), &cbData, 0))) { goto Cleanup; }
pbKeyObject = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbKeyObject); if (NULL == pbKeyObject) { goto Cleanup; }
if (!NT_SUCCESS(BCryptGenerateSymmetricKey(hAesAlg, &hKey, pbKeyObject, cbKeyObject, (PBYTE)rgbAES128Key, sizeof(rgbAES128Key), 0))) { goto Cleanup; }
if (!NT_SUCCESS(BCryptGetProperty(hAesAlg, BCRYPT_BLOCK_LENGTH, (PBYTE)&cbBlockLen, sizeof(DWORD), &cbData, 0))) { goto Cleanup; }
pbIV = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbBlockLen); if (NULL == pbIV) { goto Cleanup; }
memcpy(pbIV, rgbIV, cbBlockLen);
pbPlainText = (PBYTE)HeapAlloc(GetProcessHeap(), 0, sizeof(rgbCipherText)); if (NULL == pbPlainText) { goto Cleanup; }
if (!NT_SUCCESS(BCryptDecrypt(hKey, rgbCipherText, sizeof(rgbCipherText), NULL, pbIV, cbBlockLen, pbPlainText, sizeof(rgbCipherText), &cbData, 0))) { goto Cleanup; }
printf("Decrypted text: %s\n", pbPlainText);
Cleanup: if (hAesAlg) { BCryptCloseAlgorithmProvider(hAesAlg, 0); } if (hKey) { BCryptDestroyKey(hKey); } if (pbKeyObject) { HeapFree(GetProcessHeap(), 0, pbKeyObject); } if (pbIV) { HeapFree(GetProcessHeap(), 0, pbIV); } if (pbPlainText) { HeapFree(GetProcessHeap(), 0, pbPlainText); } }
int main() { DecryptWithBCrypt(); return 0; }
|