判断zip,rar 文件是否加密

来源:互联网 发布:etl算法 编辑:程序博客网 时间:2024/05/17 06:48

// 原创如果引用请注明出处

#include "stdafx.h"


#include <Windows.h>
#include <iostream>

using namespace  std;
/*
    返回值:  
    -1 : 文件操作出错
    0  : 文件未加密
    1:   文件加密
*/
int CheckZipFile(const char* aszFileName)
{
     HANDLE  hFile = CreateFile(aszFileName,
          GENERIC_READ,  FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

     if(hFile == INVALID_HANDLE_VALUE)
     {
         std::cout <<  "read file :" << aszFileName << " error: " << GetLastError() << std::endl;
         return  -1;
     }
     DWORD dwReadBytes  = 0;

     const  int kZipMagic  = 0x04034b50;
     int     nZipFlag  = 0;
     ReadFile(hFile, &nZipFlag, sizeof(nZipFlag), &dwReadBytes, NULL);

     if(nZipFlag !=  kZipMagic)
     {
         std::cout << "file : " << aszFileName << "is not normal zip file !" << std::endl;
          CloseHandle(hFile);
         return  -1;
     }

     unsigned  short    usCheckValue = 0;
     ReadFile(hFile, &usCheckValue, sizeof(usCheckValue), &dwReadBytes, NULL);
     ReadFile(hFile, &usCheckValue, sizeof(usCheckValue), &dwReadBytes, NULL);

     int iEncryptFlag  = 0;

     if(usCheckValue&0x01)
     {
         std::cout << "file : " << aszFileName << " is encrypted !" << std::endl;
         iEncryptFlag  = 1;
     }
     else
     {
         std::cout << "file: " << aszFileName << " no encrypted !" << std::endl;
     }
      
     CloseHandle(hFile);

     return  iEncryptFlag;
}

/*
    返回值:  
    -1 : 文件操作出错
    0  : 文件未加密
    1:   文件加密
*/
int   CheckRARFile(const char* aszFileName)
{
    HANDLE  hFile = CreateFile(aszFileName,
        GENERIC_READ,  FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    if(hFile == INVALID_HANDLE_VALUE)
    {
        std::cout <<  "read file :" << aszFileName << " error: " << GetLastError() << std::endl;
        return  -1;
    }
    DWORD dwReadBytes  = 0;
    //0x52 0x61 0x72 0x21 0x1a 0x07 0x00
    const int kMagicSize  = 7;

    const   char kFileMagic[kMagicSize] = {0x52, 0x61, 0x72, 0x21, 0x1a,  0x07, 0x00};
    char RarFlag[kMagicSize] = {0};

    ReadFile(hFile, RarFlag, kMagicSize, &dwReadBytes, NULL);
    if(memcmp(kFileMagic, RarFlag, kMagicSize) != 0)
    {
        std::cout << "read file: " << aszFileName << "is not rar !" << std::endl ;
        return -1;
    }

    char dummy[16] = {0};
    ReadFile(hFile, dummy, 16, &dwReadBytes, NULL);

    unsigned short usCheckValue  = 0;
    ReadFile(hFile, &usCheckValue, sizeof(usCheckValue), &dwReadBytes, NULL);

    if(usCheckValue&0x04)
    {
        std::cout << "rar file : " << aszFileName << " is encrypted !" << std::endl;
        return  1;
    }
    else
    {
        std::cout << "rar file: " << aszFileName << " no encrypted !" << std::endl;
        return 0;
    }


}
int _tmain(int argc, _TCHAR* argv[])
{
    CheckZipFile("d:\\chd\\t1.zip");
    CheckZipFile("d:\\chd\\t1x.zip");

    CheckRARFile("d:\\chd\\t1.rar");
    CheckRARFile("d:\\chd\\t1x.rar");
    CheckRARFile("d:\\chd\\t1x.rar");

    system("pause");

    return 0;
}
原创粉丝点击