cryptprotectdata

hi everyone
i wanna encrypt some string with cryptprotectdata function then write the encrypt string into a text file with fstream
here is my code its work but i can't write into a text file
thanks

#pragma comment(lib, "crypt32.lib")

#include <stdio.h>
#include <windows.h>
#include <Wincrypt.h>
#include<fstream>

#define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)

void main()
{

std::ofstream WRITE("info.txt", std::ios::binary);


DATA_BLOB DataIn;
DATA_BLOB DataOut;
DATA_BLOB DataVerify;
BYTE *pbDataInput = (BYTE *)"Hello world of data protection.";
DWORD cbDataInput = strlen((char *)pbDataInput) + 1;
DataIn.pbData = pbDataInput;
DataIn.cbData = cbDataInput;


printf("The data to be encrypted is: %s\n", pbDataInput);


//-------------------------------------------------------------------

if (CryptProtectData(
&DataIn,
NULL,
NULL,
NULL,
NULL,
0,
&DataOut))
{

printf("The encryption phase worked. \n");
WRITE << DataOut.pbData;
}
else
{
printf("Encryption error!");
exit(1);
}
//-------------------------------------------------------------------

if (CryptUnprotectData(
&DataOut,
NULL,
NULL,
NULL,
NULL,
0,
&DataVerify))
{
printf("The decrypted data is: %s\n", DataVerify.pbData);

}
else
{
printf("Decryption error!");
exit(1);
}

//-------------------------------------------------------------------

LocalFree(DataOut.pbData);
LocalFree(DataVerify.pbData);
} // End of main
Last edited on
Take a look here:

https://msdn.microsoft.com/en-us/library/windows/desktop/aa379887(v=vs.85).aspx

You can convert the output to a string (for instance with CRYPT_STRING_BASE64) and then write it to stream.


The BLOB structure is this:

https://msdn.microsoft.com/en-us/library/windows/desktop/aa381414(v=vs.85).aspx
thnaks for your answer
Would give me a example
Last edited on
Topic archived. No new replies allowed.