Feb 25, 2018 at 9:12pm UTC
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 Feb 25, 2018 at 9:20pm UTC
Feb 26, 2018 at 9:12am UTC
thnaks for your answer
Would give me a example
Last edited on Feb 26, 2018 at 9:13am UTC