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

📄 testbfacslib.cpp

📁 一个加密库代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
        ::puts(txt);
    }

    virtual void PrintF
        (const char* fmt, ...)
    { 
        va_list lst;
        va_start(lst, fmt);
        ::vprintf(fmt, lst);
        va_end(lst);
    }
};

CTestStdOutImpl tsoi;


extern void main2();
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////


// Check the command line arguments
BOOL IsCmdLineParameter( LPCTSTR lpszParam )
{
    for( int i = 1; i < __argc; i++ )
        if ( __targv[i][0] == _T('\\') )
            continue;
        else
            if ( __targv[i][0] == _T('/') || __targv[i][0] == _T('-') )
            {
                if ( _tcsicmp( __targv[i] + 1, lpszParam ) == 0 )
                    return TRUE;
            }
            else
                return FALSE;

    return FALSE;
}

LPCTSTR GetParamValue( LPCTSTR lpszParam )
{
    DWORD dwParamLength = _tcslen( lpszParam );

    for( int i = 1; i < __argc; i++ )
        if ( __targv[i][0] == _T('\\') || __targv[i][0] == _T('.'))
            continue;
        else
            if ( __targv[i][0] == _T('/') || __targv[i][0] == _T('-') )
            {
                if ( _tcsnicmp( __targv[i] + 1, lpszParam, dwParamLength ) == 0 )
                    return __targv[i] + dwParamLength + 1;
            }
            else
                return NULL;

    return NULL;
}

LPCTSTR GetNthParameter( DWORD n, DWORD& argvIndex )
{
    DWORD index = 0;

    for( int i = 1; i < __argc; i++ )
    {
        if ( __targv[i][0] != _T('/') && __targv[i][0] != _T('-') )
            index++;

        if ( index == n )
        {
            argvIndex = i;
            return __targv[i];
        }
    }

    return NULL;
}

// Gets the arguments parameter
void GetRemoteCommandArguments( LPTSTR lpszCommandArguments )
{
    DWORD dwIndex = 0;
    lpszCommandArguments[0] = _T('\0');

    if ( GetNthParameter( 3, dwIndex ) != NULL )
        for( int i = dwIndex; i < __argc; i++ )
        {
            _tcscat( lpszCommandArguments, __targv[i] );
            if ( i + 1 < __argc )
                _tcscat( lpszCommandArguments, _T(" ") );
        }
}

#define Out(x)       { _ftprintf( stdout, _T("%s"), x); fflush(stdout); }
void ShowUsage()
{
    Out( _T("------------------------------------------------------------------\n") );    
    Out( _T("| Usage: cypt.exe  [options] filename|\n") );
    Out( _T("------------------------------------------------------------------\n") );
    Out( _T("\n") );
    Out( _T("Options:\n") );
    Out( _T("\n") );
    Out( _T(" /enc encrypt \n") );
    Out( _T(" /dec:decrypt\n") );

    Out( _T("   .........................................................................\n") );
}

LPCTSTR lpszFileToProcess=NULL;
BOOL bEnc=TRUE;

void main(DWORD, TCHAR**, TCHAR**)
{
    clock_t clk;


    // Show help, if parameters are incorrect, or /?,/h,/help
    if (  IsCmdLineParameter( _T("h") ) || 
        IsCmdLineParameter( _T("?") ) || 
        IsCmdLineParameter( _T("help") ) )
    {
        ShowUsage();
        return ;
    }

  if(IsCmdLineParameter("enc"))
  {
      printf("encpyt\n");
      bEnc = TRUE;
  }


  if(IsCmdLineParameter("dec"))
  {
      printf("encpyt\n");
      bEnc = FALSE;
  }

  DWORD index=2;
  lpszFileToProcess = GetNthParameter(1, index);


    clk = ::clock();

    ENCRYPT_INFO encrypt_info;


    WORD8 buffer[65536] = "something to encrypt and decrypt, Pan Jiancai &Jing Dongsheng, 加密与解密";
    int datalen = strlen((char*)buffer) +1;

    WORD8 key[128] = "adfdfdfdfsssssssfdfdsffffffffffffeeeeeeeeeeeeaaaaaaaaaaaaafdfdfdsssfdfdf";//最大56个字节
    int i = 0;

    memset(&encrypt_info, 0, sizeof(encrypt_info));
    encrypt_info.type = CRYPT_AES;
    if ( -1 == Crypter_Init( &encrypt_info ,key) )
    {
        printf("Init Crypter Error.");
    }

    printf("Message to encrypt:%s\n", buffer);

    if ( -1 == Crypter_Encrypt( &encrypt_info, buffer,buffer, datalen))
    {
        printf("Encrypt error.");
    }


   // Crypter_Release(&encrypt_info);



    printf("\n\n");
    if ( -1 == Crypter_Decrypt( &encrypt_info, buffer, buffer, datalen ))
    {
        printf("AntiEncrypt error.");
    }

    printf("********** After decrypted*********\n");
    printf("%s\n", (char*)buffer);

    Crypter_Release(&encrypt_info);


    if (lpszFileToProcess)
    {
        FILE *pFile = fopen(lpszFileToProcess,"rb");
        if (pFile== NULL)
        {
            printf("open file file :%s fail\n", lpszFileToProcess);
            return;
        }

        char szFileOut[1024];
        strcpy(szFileOut, lpszFileToProcess);
        strcat(szFileOut,".out");

        FILE *pFileOut = fopen(szFileOut, "w+b");
        if (pFileOut == NULL)
        {
            printf("open out file error\n");
            return;
        }

        ENCRYPT_INFO encpy;
        encpy.type = CRYPT_AES;
        int nFileLen=0;

        if (!bEnc)//decrypt, read data of init data
        {
            fread(&encpy, 1, sizeof(encpy), pFile);
            fread(&nFileLen, 1,4, pFile);
        }


        if(0!=Crypter_Init(&encpy,key))
        {
            printf("init cryter error\n");
            return;
        }

        //write head to out file.
        if (bEnc)
        {
            fwrite(&encpy,1, sizeof(encpy), pFileOut);
            fwrite(&nFileLen, 1,4, pFileOut);
        }
        static const int   BUFF_SIZE =1024;

        BYTE filebuff[BUFF_SIZE];
        BYTE buff2[BUFF_SIZE];
        int nRemain=nFileLen;

        while (feof(pFile) ==0)
        {
            int nRead = fread(filebuff, sizeof(char), sizeof(filebuff), pFile);
            if (nRead)
            {
                nFileLen+= nRead;
                if (bEnc) 
                {
                    //加密
                    memcpy(buff2, filebuff, BUFF_SIZE);
                    Crypter_Encrypt(&encpy, filebuff, filebuff, BUFF_SIZE);
                    fwrite(filebuff, 1, BUFF_SIZE, pFileOut);

                    Crypter_Decrypt(&encpy, filebuff, filebuff, BUFF_SIZE);

                    if (memcmp(filebuff, buff2, BUFF_SIZE) !=0)
                    {
                        printf("Original buff not equal the decrypt\n");
                    }
                }
                else
                {
                    //解密
                    Crypter_Decrypt(&encpy, filebuff, filebuff, BUFF_SIZE);
                    
                    fwrite(filebuff, 1, (nRemain>BUFF_SIZE)?BUFF_SIZE:nRemain, pFileOut);
                    nRemain -= BUFF_SIZE;
                }
            }
        }

        if (bEnc) //如果是加密,那么需要将文件的长度写入到文件中
        {
            fseek(pFileOut,sizeof(encpy), SEEK_SET);
            fwrite(&nFileLen, 1, 4, pFileOut);
        }

        fclose(pFile);
        fclose(pFileOut);



    }
    //main2();
#if 0    if (::TestCipherServer(&tsoi))        //if (::TestCRC32       (&tsoi))        //if (::TestCrunchKey   (&tsoi))        //if (::TestBASE64      (&tsoi))        //if (::TestMD5         (&tsoi, _testFile))        //if (::TestSHA1        (&tsoi, _testFile))        //if (::TestYarrow      (&tsoi))        //if (::TestLZSS        (&tsoi, _testFile))        //if (::TestSHA512      (&tsoi, _testFile))        //if (::TestZLibEx      (&tsoi))    {        clk = ::clock() - clk;        ::printf("%d.%03d seconds\n",             clk / (clock_t)CLOCKS_PER_SEC,            ((clk % (clock_t)CLOCKS_PER_SEC) * 1000) / (clock_t)CLOCKS_PER_SEC);        ::puts("\n++++all tests succeeded++++");        return;    }    ::puts("\n----TESTS FAILED----");#endif
    // getchar();
    return;
}

⌨️ 快捷键说明

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