encrypt folder in NTFS (WinXP)

anybody have source or know aobut encrypt folder in NTFS (WinXP) ??
I have an exam that I must code a program like magic folder ( choice one folder, and encrypt with password. And user need a password to access this folder. User can't access this folder in Dos mode too).

Please help me :)
quynh85Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
You can do that using 'DeviceIoControl()' with FSCTL_SET_COMPRESSION, e.g.

#define SHARE_ALL (FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE)

void MakePathCompress(LPCTSTR /*FilePath*/ )
{
     LPCTSTR FilePath = "C:\\sqnexportxx";
     HANDLE hFile;
     USHORT Format = COMPRESSION_FORMAT_DEFAULT;
     DWORD dummy;

     CreateDirectory(FilePath, NULL);
     
     hFile = CreateFile(FilePath,
          GENERIC_ALL,
          SHARE_ALL,
          NULL,
          CREATE_ALWAYS,          
          FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS,
          NULL);
     DWORD dwErr = GetLastError();
     
     DeviceIoControl(hFile, FSCTL_SET_COMPRESSION, &Format, sizeof(USHORT), NULL, 0, &dummy, NULL);
     
     CloseHandle(hFile);
}

The drawback is that you'll have to go through the underlying directory tree to set that for each file in that folder, e.g. like

void CompressTree ( char* pszPath,  char*   pszBase)
{
WIN32_FIND_DATA w32fd;
HANDLE hFind;
DWORD dwAtt;
char acPath [ MAX_PATH];
char acBase [ MAX_PATH];

printf  (   "CheckFiles():\tcalled with '%s' '%s'\n",   pszPath,    pszBase);

if ( '.' == * (pszPath + lstrlen ( pszPath) - 1))
               return;

if  (   pszBase)
  sprintf (   acPath, "%s\\%s",   pszBase,    pszPath);
else
  lstrcpy ( acPath, pszPath);

printf ( "path is %s\n",    acPath);
lstrcpy ( acBase, acPath);

dwAtt = GetFileAttributes ( acPath);

if ( 0xffffffff == dwAtt)
{
// error ...
}

if ( FILE_ATTRIBUTE_DIRECTORY & dwAtt)
{
   if  (   '\\'    ==  acPath  [   lstrlen (   acPath) -   1])
           lstrcat (   acPath, "*.*");
    else
           lstrcat (   acPath, "\\*.*");

   printf ( "path is now %s\n",    acPath);
}


hFind = FindFirstFile ( acPath, &w32fd);

if ( INVALID_HANDLE_VALUE == hFind)
{
// error

printf ( "ERROR %d\n",  GetLastError    ());

return;
}

// recurse if directory...
if ( FILE_ATTRIBUTE_DIRECTORY == w32fd.dwFileAttributes)
{

   CompressTree ( w32fd.cFileName,    acBase);
}
else
HandleFile ( &w32fd);

while ( FindNextFile ( hFind, &w32fd))
{
   // recurse if directory...
   if ( FILE_ATTRIBUTE_DIRECTORY == w32fd.dwFileAttributes)
   {

    CompressTree ( w32fd.cFileName,    acBase);
  }
   else
     HandleFile ( &w32fd);
}

if ( ERROR_NO_MORE_FILES != GetLastError())
{
// error
}
FindClose ( hFind);
}

void HandleFile ( WIN32_FIND_DATA* pw32fd)
{
HANDLE hFile;
USHORT Format = COMPRESSION_FORMAT_DEFAULT;
DWORD dummy;

hFile = CreateFile(pw32fs->cFileName,
  GENERIC_ALL,
  SHARE_ALL,
  NULL,
  OPEN_EXISTING,
  FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS,
  NULL);

DeviceIoControl(hFile, FSCTL_SET_COMPRESSION, &Format, sizeof(USHORT),
NULL, 0, &dummy, NULL);

CloseHandle(hFile);
}
quynh85Author Commented:
can you help me more detail :)
I'm sorry about my limit skill.

jkrCommented:
Well, that is all the detail you can have - for a new folder,

#define SHARE_ALL (FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE)

void CreateCompressedFolder ( LPCTSTR pszFolder)
{
    HANDLE hFile;
    USHORT Format = COMPRESSION_FORMAT_DEFAULT;
    DWORD dummy;

    CreateDirectory(pszFolder, NULL);

     hFile = CreateFile(pszFolder,
         GENERIC_ALL,
         SHARE_ALL,
         NULL,
         CREATE_ALWAYS,          
         FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS,
         NULL);
   
     DeviceIoControl(hFile, FSCTL_SET_COMPRESSION, &Format, sizeof(USHORT), NULL, 0, &dummy, NULL);

}

is all you need to do. If you want to apply that to an existing folder that already has files in it, you need to traverse them and set the compression attibute individually, as shown above.
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

quynh85Author Commented:
.... and I have I stupi question.
I don't know more about C++ and encrypt ....
but I have to code a program for my exam that like "magic folder" software. (Choice one folder, and encrypt it with password. And anybody want to access this folder , they must use this program again. Without password, no one can access this folder (in Window and in Dos mode too)).

So, you can help me, please
jkrCommented:
Argh, sorry, I wrote 'compression' all the time where it should be *encryption* - my bad, but that makes things easier. In this case, all you need to do calling 'EncryptFile()', e.g.

void CreateEncryptedFolder ( LPCTSTR pszFolder)
{
   HANDLE hFile;;

   CreateDirectory(pszFolder, NULL);

   EncryptFile(pszFolder);
 
}

See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/encryptfile.asp ("EncryptFile") and http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/handling_encrypted_files_and_directories.asp ("Handling Encrypted Files and Directories")
grg99Commented:
Are you supposed to write the encryption code too?    If not , the program is trivial, just: system("cipher /E dir" );
jkrCommented:
>>choice one folder, and encrypt with password

Um, seems that I missed that one, too.  In this case, http://msdn.microsoft.com/library/default.asp?url=/library/en-us/seccrypto/security/example_c_program_encrypting_a_file.asp ("Example C Program: Encrypting a File") would help.
quynh85Author Commented:
hi.
Thank for all of your help :)
But if I need encrypt all file in folder (include subfolder) and then hidden them.
How can I do ??
If any one know or have source about this question, please send to me :)
Thank a lot :)
jkrCommented:
Go through the files as they're listed in the directory. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/listing_the_files_in_a_directory.asp ("Listing the Files in a Directory"):

#define _WIN32_WINNT 0x0501

#include <windows.h>
#include <string.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
   WIN32_FIND_DATA FindFileData;
   HANDLE hFind = INVALID_HANDLE_VALUE;
   char DirSpec[MAX_PATH + 1];  // directory specification
   DWORD dwError;

   printf ("Target directory is %s.\n", argv[1]);
   strncpy (DirSpec, argv[1], strlen(argv[1])+1);
   strncat (DirSpec, "\\*", 3);

   hFind = FindFirstFile(DirSpec, &FindFileData);

   if (hFind == INVALID_HANDLE_VALUE)
   {
      printf ("Invalid file handle. Error is %u\n", GetLastError());
      return (-1);
   }
   else
   {
      printf ("First file name is %s\n", FindFileData.cFileName);
      while (FindNextFile(hFind, &FindFileData) != 0)
      {
         printf ("Next file name is %s\n", FindFileData.cFileName);
      }
   
      dwError = GetLastError();
      FindClose(hFind);
      if (dwError != ERROR_NO_MORE_FILES)
      {
         printf ("FindNextFile error. Error is %u\n", dwError);
         return (-1);
      }
   }
   return (0);
}


To 'hide' them, you can set FILE_ATTRIBUTE_HIDDEN for each file, e.g. like

DWORD dwAtt = GetFileAttributes(pszFile);
dwAtt |= FILE_ATTRIBUTE_HIDDEN; // set flag
SetFileAttributes(pszFile,dwAtt);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jhshuklaCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.
I will leave the following recommendation for this question in the Cleanup topic area:
Accept: jkr

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

jhshukla
EE Cleanup Volunteer
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.