⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chkvir.cpp

📁 使用CRC进行病毒检测的简单程序
💻 CPP
字号:
# include "dos.h"
# include "dir.h"
# include "stdlib.h"
# include "iostream.h"
# include "fstream.h"
# include "string.h"
unsigned int CalCRC(char * filename,long FSize);
void main(void)
{
  struct ffblk Fblk;
  ofstream Result("CRCSUM.DAT");
  unsigned int CRC;
  if(!findfirst("*.*",&Fblk,FA_ARCH|FA_RDONLY))
  //check all files in the current directory
  {
    if(strcmpi(Fblk.ff_name,"crcsum.dat")!=0)
    {
      cout<<Fblk.ff_name<<"  Check..."<<endl;
      CRC=CalCRC(Fblk.ff_name,Fblk.ff_fsize);
      Result<<Fblk.ff_name<<" "<<CRC<<endl;
    }
    while(!findnext(&Fblk))
    {
      if(strcmpi(Fblk.ff_name,"crcsum.dat")!=0)
      {
        cout<<Fblk.ff_name<<"  Check..."<<endl;
        CRC=CalCRC(Fblk.ff_name,Fblk.ff_fsize);
        Result<<Fblk.ff_name<<" "<<CRC<<endl;
      }
    }
  }  //find all sorts of files in current directory
}
unsigned int CalCRC(char * filename,long FSize)
{
  static unsigned long Gx=0x18005;//The IBM generate polynomial
  static unsigned long ChkSum=0;
  static unsigned int CRC=0xaa;
  unsigned char tmp;
  int i;
  ifstream ChkFile;
  Gx=0x18005;
  ChkSum=0;
  CRC=0;
  ChkFile.open(filename,ios::binary);
  if(FSize>=3)
  {
    tmp=ChkFile.get();
    ChkSum|=tmp;
    ChkSum<<=8;
    tmp=ChkFile.get();
    ChkSum|=tmp; //Read 2 byte for deviding soon
    tmp=ChkFile.get();
    do
    {
      for(i=0;i<8;i++)//move the next bits to value ChkSum
      {
        ChkSum<<=1;
        if(tmp&0x80)
          ChkSum|=1;
        tmp<<=1;
        if(ChkSum & 0x10000)
        {
          CRC=ChkSum^Gx;
          ChkSum=CRC;
        }
      }
      tmp=ChkFile.get();
    }while(!ChkFile.eof());
    for(i=0;i<8;i++)//Set the CRC bits to Zero
    {
      ChkSum<<=1;//append the bit of zero
      if(ChkSum & 0x10000)
      {
        CRC=ChkSum^Gx;
        ChkSum=CRC;
      }
    }
    for(i=0;i<8;i++)
    {
      ChkSum<<=1;//append the bit of zero
      if(ChkSum & 0x10000)
      {
        CRC=ChkSum^Gx;
        ChkSum=CRC;
      }
    }
    CRC=ChkSum;
    return(CRC);
  }
  else //the length of file <=2
  {
    for(i=0;i<FSize;i++)
    {
      ChkSum<<=8*i;
      tmp=ChkFile.get();
      ChkSum|=tmp;
    }
    for(i=0;i<8;i++)//Set the CRC bits to Zero
    {
      ChkSum<<=1;//append the bit of zero
      if(ChkSum & 0x10000)
      {
        CRC=ChkSum^Gx;
        ChkSum=CRC;
      }
    }
    for(i=0;i<8;i++)
    {
      ChkSum<<=1;//append the bit of zero
      if(ChkSum & 0x10000)
      {
        CRC=ChkSum^Gx;
        ChkSum=CRC;
      }
    }
    CRC=ChkSum;
    return(CRC);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -