// CryptUnprotectData.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #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<2 || argc>4 ) { _tprintf(TEXT("Syntax: %s input_filename [entropy_string] [flags]\n"),argv[0]); return 1; } //Declare variables DATA_BLOB DataIn; DATA_BLOB DataOut; DATA_BLOB DataEntropy; DWORD dwFlags; LPTSTR pFoo; LPTSTR pszDescription=NULL; //Initialize the structure //read the input blob FILE *f=NULL; _tfopen_s(&f,argv[1],TEXT("rb")); if ( !f ) { _tprintf(TEXT("Can't open input file for reading\n")); return 1; } //allocate memory if ( fseek(f,0,SEEK_END) ) { errseek: _tprintf(TEXT("Can't determine input file size\n")); fclose(f); return 2; } DataIn.cbData=(DWORD)ftell(f); if ( fseek(f,0,SEEK_SET) ) goto errseek; DataIn.pbData=new BYTE [DataIn.cbData]; if ( !DataIn.pbData ) { _tprintf(TEXT("Can't allocate %lu memory bytes\n"),DataIn.cbData); fclose(f); return 3; } //read DPAPI blob if ( fread(DataIn.pbData,DataIn.cbData,1,f)!=1 ) { delete(DataIn.pbData); DataIn.pbData=NULL; _tprintf(TEXT("Can't read data blob\n")); fclose(f); return 4; } fclose(f); // DataOut.pbData=NULL; DataOut.cbData=0; // if ( argc>=3 ) { DataEntropy.pbData=(LPBYTE)(argv[2]); DataEntropy.cbData=(lstrlen(argv[2])+1) * sizeof(TCHAR) ; } // if ( argc==4 ) dwFlags=_tcstoul(argv[3],&pFoo,10); else dwFlags=0; //Unprotect it int iRet=0; if ( !CryptUnprotectData( &DataIn, &pszDescription, //description string to be included argc>=3?&DataEntropy:NULL, //Optional entropy NULL, //reserved NULL, //prompt structure, not used dwFlags, //flags &DataOut) ) { dwFlags=GetLastError(); _tprintf(TEXT("CryptUnprotectData failed with the following error code: %lu\n"),dwFlags); iRet=5; goto ex; } //turn on internationalization _tsetlocale(LC_ALL,TEXT("")); //out put decrypted data _tprintf(TEXT("Data blob description: %s\n"),pszDescription?pszDescription:TEXT("")); _tprintf(TEXT("Data size: %lu\n"),DataOut.cbData); _tprintf(TEXT("Decrypted data: ")); if ( pszDescription && lstrcmp(pszDescription,TEXT("CryptProtectData by Passcape Software"))==0 ) _tprintf(TEXT("%s"),DataOut.pbData?(LPTSTR)DataOut.pbData:TEXT("")); else { for ( DWORD dw=0; dw