// CryptProtectData.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #pragma comment (lib, "Crypt32") int _tmain(int argc, _TCHAR* argv[]) { _tprintf(TEXT( "======================================================\n= CryptProtectData wrapper by Passcape Software =\n= Visit http://www.passcape.com for more information =\n======================================================\n\n")); if ( argc<3 || argc>5 ) { _tprintf(TEXT("Syntax: %s secret output_filename [entropy_string] [flags]\n"),argv[0]); return 1; } //Declare variables DATA_BLOB DataIn; DATA_BLOB DataOut; DATA_BLOB DataEntropy; DWORD dwFlags; LPTSTR pFoo; //Initialize the structure DataOut.pbData=NULL; DataOut.cbData=0; // DataIn.pbData=(LPBYTE)(argv[1]); DataIn.cbData=(lstrlen(argv[1])+1) * sizeof(TCHAR) ; // if ( argc>=4 ) { DataEntropy.pbData=(LPBYTE)(argv[3]); DataEntropy.cbData=(lstrlen(argv[3])+1) * sizeof(TCHAR) ; } // if ( argc==5 ) dwFlags=_tcstoul(argv[4],&pFoo,10); else dwFlags=0; //Protect the secret if ( !CryptProtectData( &DataIn, TEXT("CryptProtectData by Passcape Software"), //description string to be included argc>=4?&DataEntropy:NULL, //Optional entropy NULL, //reserved NULL, //prompt structure, not used dwFlags, //flags &DataOut) ) { dwFlags=GetLastError(); _tprintf(TEXT("CryptProtectData failed with the following error code: %lu\n"),dwFlags); exit(1); } //save the output blob FILE *f=NULL; _tfopen_s(&f,argv[2],TEXT("wb")); if ( !f ) { if ( DataOut.pbData ) { LocalFree(DataOut.pbData); DataOut.pbData=NULL; } _tprintf(TEXT("Can't open output file for writing\n")); exit(2); } //write if ( DataOut.pbData ) { size_t written=fwrite(DataOut.pbData,DataOut.cbData,1,f); LocalFree(DataOut.pbData); DataOut.pbData=NULL; if ( written!=1 ) { _tprintf(TEXT("Can't write %lu bytes to output file\n"),DataOut.cbData); exit(3); } } fclose(f); return 0; }