Почему мой код зависает после шифрования AES 256? ⇐ C++
-
Anonymous
Почему мой код зависает после шифрования AES 256?
I have written a code to encrypt according to AES 256 algorithm and it works to give me an output file but I realised that it hangs after running the EncryptDecryptString function and does not perform anything else after that. Why is this so? How can I further improve this code to perform encryption and decryption?
#include #include #include #include #include #include #pragma comment(lib, "bcrypt.lib") #define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0) #define STATUS_UNSUCCESSFUL ((NTSTATUS)0xC0000001L) // Function to decrypt using AES256 algorithm void ENcryptDecryptString(const std::string& inputString, const std::string& outputFile, bool encrypt){ // Constants for encryption #define KEY_SIZE 32 #define IV_SIZE 16 const unsigned char ENCRYPTION_KEY[KEY_SIZE] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0xoF, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F }; const unsigned char IV[IV_SIZE] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F }; BCRYPT_ALG_HANDLE hAesAlg = NULL; BCRYPT_KEY_HANDLE hKey = NULL; NTSTATUS status = STATUS_UNSUCCESSFUL; DWORD cbCipherText = 0, cbPlainText = 0, cbData = 0, cbKeyObject = 0, cbBlockLen = 0, cbBlob = 0; PBYTE pbCipherText = NULL, pbPlainText = NULL, pbKeyObject = NULL, pbIV = NULL, pbBlob = NULL; // Open an algorithm handle if (!NT_SUCCESS(status = BCryptOpenAlgorithmProvider(&hAesAlg, BCRYPT_AES_ALGORITHM, NULL, 0))){ wprintf(L"**** Error 0x%x returned by BCryptOpenAlgorithmProvider\n", status); goto Cleanup; } // Calculate the size of the buffer to hold the KeyObject if (!NT_SUCCESS(status = BCryptGetProperty(hAesAlg, BCRYPT_OBJECT_LENGTH, (PBYTE)&cbKeyObject, sizeof(DWORD), &cbData, 0))){ wprintf(L"**** Error 0x%x returned by BCryptGetProperty\n", status); goto Cleanup; } // Allocate the key object on the heap pbKeyObject = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbKeyObject); if (pbKeyObject == NULL){ wprintf(L"**** memory allocation failed\n"); goto Cleanup; } // Calculate the block length for the IV if (!NT_SUCCESS(status = BCryptGetProperty(hAesAlg, BCRYPT_BLOCK_LENGTH, (PBYTE)&cbBlockLen, sizeof(DWORD), &cbData, 0))){ wprintf(L"**** Error 0x%x returned by BCryptGetProperty_IV\n", status); goto Cleanup; } // Allocate a buffer for the IV pbIV = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbBlockLen); if (pbIV == NULL){ wprintf(L"**** IV memory allocation failed\n"); goto Cleanup; } // Use the provided IV memcpy(pbIV, IV, IV_SIZE); // Set the chaining mode to CBC if (!NT_SUCCESS(status = BCryptSetProperty(hAesAlg, BCRYPT_CHAINING_MODE, (PBYTE)BCRYPT_CHAIN_MODE_CBC, sizeof(BCRYPT_CHAIN_MODE_CBC), 0))){ wprintf(L"**** Error 0x%x returned by BCryptGetProperty_chaining_mode\n", status); goto Cleanup; } // Generate the key from supplied input key bytes if (!NT_SUCCESS(status = BCryptGenerateSymmetricKey(hAesAlg, &hKey, pbKeyObject, cbKeyObject, (PBYTE)ENCRYPTION_KEY, KEY_SIZE, 0))){ wprintf(L"**** Error 0x%x returned by BCryptGenerateSymmetricKey\n", status); goto Cleanup; } // Allocate memory for the plaintext or ciphertext buffercbData = inputString.length(); if (encrypt){ pbPlainText = (PBYTE)inputString.c_str(); cbPlainText = cbData; // Get the size of the encrypted outputFile if (!NT_SUCCESS(status = BCryptEncrypt(hKey, pbPlainText, cbPlainText, NULL, pbIV, cbBlockLen, NULL, 0, &cbCipherText, BCRYPT_BLOCK_PADDING))){ wprintf(L"**** Error 0x%x returned by BCryptEncrypt\n", status); goto Cleanup; } pbCipherText = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbCipherText); if (pbCipherText == NULL){ wprintf(L"**** memory allocation failed_pbCipherText\n"); goto Cleanup; } // Perform encryption if (!NT_SUCCESS(status = BCryptEncrypt(hKey, pbPlainText, cbPlainText, NULL, pbIV, cbBlockLen, pbCipherText, cbCipherText, &cbData, BCRYPT_BLOCK_PADDING))){ wprintf(L"**** Error 0x%x returned by BCryptEncrypt_encryption\n", status); goto Cleanup; } // Write the encrypted data to the output filebuf std::ofstream ofs(outputFile, std::ios::binary); if (!ifs){ std::cerr
Источник: https://stackoverflow.com/questions/781 ... to-aes-256
I have written a code to encrypt according to AES 256 algorithm and it works to give me an output file but I realised that it hangs after running the EncryptDecryptString function and does not perform anything else after that. Why is this so? How can I further improve this code to perform encryption and decryption?
#include #include #include #include #include #include #pragma comment(lib, "bcrypt.lib") #define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0) #define STATUS_UNSUCCESSFUL ((NTSTATUS)0xC0000001L) // Function to decrypt using AES256 algorithm void ENcryptDecryptString(const std::string& inputString, const std::string& outputFile, bool encrypt){ // Constants for encryption #define KEY_SIZE 32 #define IV_SIZE 16 const unsigned char ENCRYPTION_KEY[KEY_SIZE] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0xoF, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F }; const unsigned char IV[IV_SIZE] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F }; BCRYPT_ALG_HANDLE hAesAlg = NULL; BCRYPT_KEY_HANDLE hKey = NULL; NTSTATUS status = STATUS_UNSUCCESSFUL; DWORD cbCipherText = 0, cbPlainText = 0, cbData = 0, cbKeyObject = 0, cbBlockLen = 0, cbBlob = 0; PBYTE pbCipherText = NULL, pbPlainText = NULL, pbKeyObject = NULL, pbIV = NULL, pbBlob = NULL; // Open an algorithm handle if (!NT_SUCCESS(status = BCryptOpenAlgorithmProvider(&hAesAlg, BCRYPT_AES_ALGORITHM, NULL, 0))){ wprintf(L"**** Error 0x%x returned by BCryptOpenAlgorithmProvider\n", status); goto Cleanup; } // Calculate the size of the buffer to hold the KeyObject if (!NT_SUCCESS(status = BCryptGetProperty(hAesAlg, BCRYPT_OBJECT_LENGTH, (PBYTE)&cbKeyObject, sizeof(DWORD), &cbData, 0))){ wprintf(L"**** Error 0x%x returned by BCryptGetProperty\n", status); goto Cleanup; } // Allocate the key object on the heap pbKeyObject = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbKeyObject); if (pbKeyObject == NULL){ wprintf(L"**** memory allocation failed\n"); goto Cleanup; } // Calculate the block length for the IV if (!NT_SUCCESS(status = BCryptGetProperty(hAesAlg, BCRYPT_BLOCK_LENGTH, (PBYTE)&cbBlockLen, sizeof(DWORD), &cbData, 0))){ wprintf(L"**** Error 0x%x returned by BCryptGetProperty_IV\n", status); goto Cleanup; } // Allocate a buffer for the IV pbIV = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbBlockLen); if (pbIV == NULL){ wprintf(L"**** IV memory allocation failed\n"); goto Cleanup; } // Use the provided IV memcpy(pbIV, IV, IV_SIZE); // Set the chaining mode to CBC if (!NT_SUCCESS(status = BCryptSetProperty(hAesAlg, BCRYPT_CHAINING_MODE, (PBYTE)BCRYPT_CHAIN_MODE_CBC, sizeof(BCRYPT_CHAIN_MODE_CBC), 0))){ wprintf(L"**** Error 0x%x returned by BCryptGetProperty_chaining_mode\n", status); goto Cleanup; } // Generate the key from supplied input key bytes if (!NT_SUCCESS(status = BCryptGenerateSymmetricKey(hAesAlg, &hKey, pbKeyObject, cbKeyObject, (PBYTE)ENCRYPTION_KEY, KEY_SIZE, 0))){ wprintf(L"**** Error 0x%x returned by BCryptGenerateSymmetricKey\n", status); goto Cleanup; } // Allocate memory for the plaintext or ciphertext buffercbData = inputString.length(); if (encrypt){ pbPlainText = (PBYTE)inputString.c_str(); cbPlainText = cbData; // Get the size of the encrypted outputFile if (!NT_SUCCESS(status = BCryptEncrypt(hKey, pbPlainText, cbPlainText, NULL, pbIV, cbBlockLen, NULL, 0, &cbCipherText, BCRYPT_BLOCK_PADDING))){ wprintf(L"**** Error 0x%x returned by BCryptEncrypt\n", status); goto Cleanup; } pbCipherText = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbCipherText); if (pbCipherText == NULL){ wprintf(L"**** memory allocation failed_pbCipherText\n"); goto Cleanup; } // Perform encryption if (!NT_SUCCESS(status = BCryptEncrypt(hKey, pbPlainText, cbPlainText, NULL, pbIV, cbBlockLen, pbCipherText, cbCipherText, &cbData, BCRYPT_BLOCK_PADDING))){ wprintf(L"**** Error 0x%x returned by BCryptEncrypt_encryption\n", status); goto Cleanup; } // Write the encrypted data to the output filebuf std::ofstream ofs(outputFile, std::ios::binary); if (!ifs){ std::cerr
Источник: https://stackoverflow.com/questions/781 ... to-aes-256
Мобильная версия