CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VC/MFC >  基础类

怎样加密文件夹

楼主churennan(楚应兰)2002-06-11 18:04:53 在 VC/MFC / 基础类 提问

各位好!  
      请问各位高手一个问题,我想为文件夹加密,在用户点击它时,会自动跳出一个密码对话框,在密码输入正确后方可进入此文件夹。 问题点数:100、回复次数:3Top

1 楼ckacka(/*小红帽*/ckacka();)回复于 2002-06-11 18:33:09 得分 0

可以用基于html的办法,不过具体的我也没有试过。Top

2 楼spidertan(灭蚊刀)回复于 2002-06-11 20:19:11 得分 0

这里是解密一个文件的一些代码,如果想了解更详细的信息,看EncryptFile()和DecryptFile()方法在MSDN帮助中  
  //--------------------------------------------------------------------  
  //       In   this   and   all   other   sample   and   example   code,    
  //       use   the   #define   and   #include   statements   listed    
  //       under   #includes   and   #defines.  
   
  #include   <stdio.h>  
  #include   <windows.h>  
  #include   <wincrypt.h>  
  #define   MY_ENCODING_TYPE     (PKCS_7_ASN_ENCODING   |   X509_ASN_ENCODING)  
  #define   KEYLENGTH     0x00800000  
  void   HandleError(char   *s);  
   
  //--------------------------------------------------------------------  
  //       The   following   #define   statements   are   also   required.  
   
  #define   ENCRYPT_ALGORITHM   CALG_RC4    
  #define   ENCRYPT_BLOCK_SIZE   8    
   
  //--------------------------------------------------------------------  
  //         Declare   the   function   DecryptFile.   Code   for   the   function   follows  
  //         main.  
     
  BOOL   DecryptFile(  
            PCHAR   szSource,    
            PCHAR   szDestination,    
            PCHAR   szPassword);    
   
  void   main(void)    
  {    
  //--------------------------------------------------------------------  
  //         Declare   and   initialize   variables.  
   
  PCHAR   szSource;    
  PCHAR   szDestination;    
  PCHAR   szPassword;    
  char     response;  
   
  if(!(szSource=(char   *)malloc(100)))  
          HandleError("Memory   allocation   failed.");  
  if(!(szDestination=(char   *)malloc(100)))  
          HandleError("Memory   allocation   failed.");  
  if(!(szPassword=(char   *)malloc(100)))  
          HandleError("Memory   allocation   failed.");  
   
  printf("Decrypt   a   file.   \n\n");  
  printf("Enter   the   name   of   the   file   to   be   decrypted:   ");  
  scanf("%s",szSource);  
  printf("Enter   the   name   of   the   output   file:   ");  
  scanf("%s",szDestination);  
  printf("Was   a   password   used   to   encrypt   this   file?   (   y/n   )   ");  
  getchar();  
  scanf("%c",&response);  
  if(response   ==   'y')  
  {  
          printf("Enter   the   password:");  
          scanf("%s",szPassword);  
  }  
  else  
  {  
          printf("The   key   will   be   generated   without   using   a   password.   \n");  
          free(szPassword);  
          szPassword   =   NULL;  
  }  
  if(!DecryptFile(szSource,   szDestination,   szPassword))  
  {  
          printf("\nError   decrypting   file.   \n");    
  }  
  else  
  {    
          printf("\nDecryption   of   file   %s   succeeded.   \n",   szSource);  
          printf("The   decrypted   file   is   %s   .\n",szDestination);  
  }  
  }   //   End   of   main  
   
  //--------------------------------------------------------------------  
  //         Define   the   function   Decryptfile.  
   
  static   BOOL   DecryptFile(  
            PCHAR   szSource,    
            PCHAR   szDestination,    
            PCHAR   szPassword)    
  {    
  //--------------------------------------------------------------------  
  //       Declare   and   initialize   local   variables.  
   
  FILE   *hSource;    
  FILE   *hDestination;    
   
  HCRYPTPROV   hCryptProv;    
  HCRYPTKEY   hKey;    
  HCRYPTHASH   hHash;    
     
  PBYTE   pbKeyBlob   =   NULL;    
  DWORD   dwKeyBlobLen;    
     
  PBYTE   pbBuffer;    
  DWORD   dwBlockLen;    
  DWORD   dwBufferLen;    
  DWORD   dwCount;    
   
  BOOL   status   =   FALSE;    
     
  //--------------------------------------------------------------------  
  //   Open   source   file.    
  if(!(hSource   =   fopen(szSource,"rb")))    
  {  
        HandleError("Error   opening   ciphertext   file!");  
  }  
  //--------------------------------------------------------------------  
  //   Open   destination   file.    
   
  if(!(hDestination   =   fopen(szDestination,"wb")))  
  {  
          HandleError("Error   opening   plaintext   file!");  
  }    
  //--------------------------------------------------------------------  
  //   Get   a   handle   to   the   default   provider.    
  if(!CryptAcquireContext(  
              &hCryptProv,    
              NULL,    
              MS_ENHANCED_PROV,    
              PROV_RSA_FULL,    
              0))  
  {  
        HandleError("Error   during   CryptAcquireContext!");    
  }  
  //--------------------------------------------------------------------  
  //     Check   for   existence   of   a   password.  
   
  if(!szPassword)    
  {    
  //--------------------------------------------------------------------  
  //   Decrypt   the   file   with   the   saved   session   key.    
   
  //   Read   key   BLOB   length   from   source   file,   and   allocate   memory.    
  fread(&dwKeyBlobLen,   sizeof(DWORD),   1,   hSource);    
  if(ferror(hSource)   ||   feof(hSource))  
  {  
          HandleError("Error   reading   file   header!");    
  }  
  if(!(pbKeyBlob   =   (BYTE   *)malloc(dwKeyBlobLen)))  
  {  
          HandleError("Memory   allocation   error.");    
  }  
  //--------------------------------------------------------------------  
  //   Read   key   BLOB   from   source   file.    
   
  fread(pbKeyBlob,   1,   dwKeyBlobLen,   hSource);    
  if(ferror(hSource)   ||   feof(hSource))  
  {  
          HandleError("Error   reading   file   header!\n");    
  }  
  //--------------------------------------------------------------------  
  //   Import   key   BLOB   into   CSP.    
  if(!CryptImportKey(  
              hCryptProv,    
              pbKeyBlob,    
              dwKeyBlobLen,    
              0,    
              0,    
              &hKey))  
  {  
        HandleError("Error   during   CryptImportKey!");    
  }  
  }    
  else    
  {    
  //--------------------------------------------------------------------  
  //   Decrypt   the   file   with   a   session   key   derived   from   a   password.    
     
  Top

3 楼spidertan(灭蚊刀)回复于 2002-06-11 20:19:41 得分 100

//--------------------------------------------------------------------  
  //   Create   a   hash   object.    
  if(!CryptCreateHash(  
                hCryptProv,    
                CALG_MD5,    
                0,    
                0,    
                &hHash))  
  {  
          HandleError("Error   during   CryptCreateHash!");  
  }  
  //--------------------------------------------------------------------  
  //   Hash   in   the   password   data.    
   
  if(!CryptHashData(  
                hHash,    
                (BYTE   *)szPassword,    
                strlen(szPassword),    
                0))    
  {  
          HandleError("Error   during   CryptHashData!");    
  }  
  //--------------------------------------------------------------------  
  //   Derive   a   session   key   from   the   hash   object.    
   
  if(!CryptDeriveKey(  
              hCryptProv,    
              ENCRYPT_ALGORITHM,    
              hHash,    
              KEYLENGTH,    
              &hKey))  
  {    
        HandleError("Error   during   CryptDeriveKey!");    
  }  
  //--------------------------------------------------------------------  
  //   Destroy   the   hash   object.    
   
  CryptDestroyHash(hHash);    
  hHash   =   0;    
  }    
  //--------------------------------------------------------------------  
  //       The   decryption   key   is   now   available,   either   having   been   imported  
  //       from   a   BLOB   read   in   from   the   source   file   or   having   been   created    
  //       using   the   password.   This   point   in   the   program   is   not   reached   if    
  //       the   decryption   key   is   not   available.  
     
  //--------------------------------------------------------------------  
  //   Determine   the   number   of   bytes   to   decrypt   at   a   time.    
  //   This   must   be   a   multiple   of   ENCRYPT_BLOCK_SIZE.    
   
  dwBlockLen   =   1000   -   1000   %   ENCRYPT_BLOCK_SIZE;    
  dwBufferLen   =   dwBlockLen;    
   
  //--------------------------------------------------------------------  
  //   Allocate   memory.    
   
  if(!(pbBuffer   =   (BYTE   *)malloc(dwBufferLen)))  
  {  
        HandleError("Out   of   memory!\n");    
  }  
  //--------------------------------------------------------------------  
  //   Decrypt   source   file,   and   write   to   destination   file.    
   
  do   {    
  //--------------------------------------------------------------------  
  //   Read   up   to   dwBlockLen   bytes   from   source   file.    
   
  dwCount   =   fread(  
            pbBuffer,    
            1,    
            dwBlockLen,    
            hSource);    
  if(ferror(hSource))  
  {  
          HandleError("Error   reading   ciphertext!");  
  }  
  //--------------------------------------------------------------------  
  //   Decrypt   data.    
  if(!CryptDecrypt(  
              hKey,    
              0,    
              feof(hSource),    
              0,    
              pbBuffer,    
              &dwCount))  
  {  
        HandleError("Error   during   CryptDecrypt!");    
  }  
  //--------------------------------------------------------------------  
  //   Write   data   to   destination   file.    
   
  fwrite(  
          pbBuffer,    
          1,    
          dwCount,    
          hDestination);    
  if(ferror(hDestination))  
  {  
        HandleError("Error   writing   plaintext!");    
  }  
  }    
  while(!feof(hSource));    
  status   =   TRUE;    
   
  //--------------------------------------------------------------------  
  //   Close   files.    
  if(hSource)    
        fclose(hSource);    
  if(hDestination)    
          fclose(hDestination);    
     
  //--------------------------------------------------------------------  
  //   Free   memory.    
   
  if(pbKeyBlob)    
            free(pbKeyBlob);  
   
  if(pbBuffer)    
            free(pbBuffer);    
     
  //--------------------------------------------------------------------  
  //   Destroy   session   key.    
   
  if(hKey)    
          CryptDestroyKey(hKey);    
   
  //--------------------------------------------------------------------  
  //   Destroy   hash   object.    
  if(hHash)    
          CryptDestroyHash(hHash);    
   
  //--------------------------------------------------------------------  
  //   Release   provider   handle.    
   
  if(hCryptProv)    
          CryptReleaseContext(hCryptProv,   0);    
   
  return   status;  
  }   //   End   of   Decryptfile  
   
  //--------------------------------------------------------------------  
  //     This   example   uses   the   function   HandleError,   a   simple   error  
  //     handling   function,   to   print   an   error   message   to   the   standard   error    
  //     (stderr)   file   and   exit   the   program.    
  //     For   most   applications,   replace   this   function   with   one    
  //     that   does   more   extensive   error   reporting.  
   
  void   HandleError(char   *s)  
  {  
          fprintf(stderr,"An   error   occurred   in   running   the   program.   \n");  
          fprintf(stderr,"%s\n",s);  
          fprintf(stderr,   "Error   number   %x.\n",   GetLastError());  
          fprintf(stderr,   "Program   terminating.   \n");  
          exit(1);  
  }   //   End   of   HandleErrorTop

相关问题

  • 文件夹怎样加密!
  • 怎样给文件夹加密?
  • 文件夹加密
  • 文件夹加密?
  • 文件夹加密?
  • 怎样通过修改folder.htt来实现文件夹加密码
  • 有关文件夹加密
  • 文件夹的加密
  • 2000sever怎样给某个文件夹加密啊?呵呵,比较菜
  • 急!文件夹怎么加密?

关键词

  • 文件夹
  • szdestination
  • szsource
  • szpassword
  • decryptfile
  • handleerror
  • pchar
  • define
  • encoding
  • include

得分解答快速导航

  • 帖主:churennan
  • spidertan

相关链接

  • Visual C++类图书
  • Visual C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo