How do I correctly print a ciphertext encrypted by BCryptEncrypt?

94 views Asked by At

I am trying to use BCryptEncrypt to encrypt some basic plain text and I would like to save it in a Notepad; but I want to actually print and see the encrypted text in the console. What is the correct format for wprintf?

Here's part of my code:

 // BCryptEncrypt

    LPCWSTR textToEncrypt = L"Encrypt me.";
    DWORD numberOfBytes = (DWORD)((wcslen(textToEncrypt)+1) * sizeof(WCHAR));
    ULONG sizeCiphertext;
    PBYTE cipherText = NULL;

// Get the size for cyphertext 

    status = BCryptEncrypt(hKey, (PUCHAR)textToEncrypt, numberOfBytes, NULL, NULL, 0, NULL, 0, &sizeCiphertext, BCRYPT_BLOCK_PADDING);

    if (!NT_SUCCESS(status))
    {
        wprintf(L"Error 0x%x returned by function BCryptEncrypt 1.\n", status);
        
    }

cipherText = (PBYTE)HeapAlloc(GetProcessHeap(), 0, sizeCiphertext);

    if (cipherText == NULL)
    {
        wprintf(L"Memory allocation failed.\n");
        
    }

    status = BCryptEncrypt(hKey, (PUCHAR)textToEncrypt, numberOfBytes, NULL, NULL, 0, cipherText, sizeCiphertext, &sizeCiphertext, BCRYPT_BLOCK_PADDING);

    if (!NT_SUCCESS(status))
    {
        wprintf(L"Error 0x%x returned by function BCryptEncrypt 2.\n", status);
        

    }

    wprintf(L"Plain text: %s \n", textToEncrypt);

//No idea if this is OK
wprintf(L"Ciphertext: %hs \n", cipherText);
0

There are 0 answers