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

📄 testbfacslib.cpp

📁 一个加密库代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
* Copyright 1997-2005 Markus Hahn 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
*     http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <windows.h>

#include <stdio.h>
#include <stdarg.h>
#include <time.h>

#include "TestInterface.h"
#include "Blowfish.h"
#include "AES.h"
#include "arcfour.h"
#include "Serpent.h"
#include "TMS.h"
#include "TripleDES.h"
#include "cast.h"
#include <tchar.h>

//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////


void PrintInfoBlock
( CIPHERINFOBLOCK* infoblock)
{
    printf(	
        "block size      : %d\n"	\
        "key size        : %d\n"	\
        "hashes key      : %d\n"	\
        "init. data size : %d\n"	\
        "context size    : %d\n"	\
        "sizeof          : %d\n"	\
        "cipher is       : 0x%02x\n",
        infoblock->lBlockSize,
        infoblock->lKeySize,
        infoblock->blOwnHasher,
        infoblock->lInitDataSize,
        infoblock->lContextSize,
        infoblock->lSizeOf,
        infoblock->bCipherIs);
}

//////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////

// simple mapper to stdout


//////////////////////////////////////////////////////////////////////////////

// (all the test together)
typedef enum emCrpytType
{
    CRYPT_BLOWFISH,
    CRYPT_AES,
    CRYPT_DES,
    CRYPT_TMS,
    CRYPT_CAST,
    CRYPT_ARCFOUR,
    CRYPT_SERPENT,
}CRYPTER_TYPE;

typedef struct tagENCRYPT_INFO
{
    CRYPTER_TYPE type;  //加密算法
    PCIPHERCTX pCtx;
    PCIPHERSESSION chandle_encrypt;
    PCIPHERSESSION chandle_antiencrypt;
    CIPHERINFOBLOCK infoblock;
    BYTE InitData[32];//目前这几种算法中,最大16字节, 这个数据很重要, 加密后必须将这个值保存起来,在解密的时候传到session里面,这个东西搞了我一天才搞懂,操
}ENCRYPT_INFO, *PENCRYPT_INFO;


void CRYPTPAK_CALLCONV MyRandomGenerator
(WORD8* pTargetBuffer,
 WORD32 lNumOfRandomBytes,
 const void* pData) 
{
    memset(pTargetBuffer, '1', lNumOfRandomBytes);
}


LONG Crypter_Init( PENCRYPT_INFO pencrypt_info , PBYTE key )
{
    LONG lResult = 0;

    char CryptType[CIPHER_MAX_NAME_LEN];

    switch ( pencrypt_info->type )
    {
    case CRYPT_BLOWFISH:
        strcpy( ( char* )CryptType, BLOWFISH_CIPHERNAME );
        break;
    case CRYPT_AES:
        strcpy( ( char* )CryptType, AES_CIPHERNAME );
        break;
    case CRYPT_DES:
        strcpy( ( char* )CryptType, TRIPLEDES_CIPHERNAME );
        break;
    case CRYPT_TMS:
        strcpy( ( char* )CryptType, TMS_CIPHERNAME );
        break;
    case CRYPT_CAST:
        strcpy( ( char* )CryptType, CAST_CIPHERNAME );
        break;
    case CRYPT_ARCFOUR:
        strcpy( ( char* )CryptType, ARCFOUR_CIPHERNAME );
        break;
    case CRYPT_SERPENT:
        strcpy( ( char* )CryptType, SERPENT_CIPHERNAME );
        break;
    default:
        return -1;
        break;
    }

    pencrypt_info->infoblock.lSizeOf = sizeof(CIPHERINFOBLOCK);

    lResult = CipherServer_GetCipherInfo( CryptType,&pencrypt_info->infoblock );
    if ( lResult != CIPHERSERVER_ERROR_NOERROR )
    {		
        return -1;
    }

    // open the cipher (using the built-in random generator with no additional seed)

    lResult = CipherServer_Create( CryptType,
        &pencrypt_info->pCtx,
        MyRandomGenerator,
        NULL,
        NULL,
        0 );
    if ( lResult != CIPHERSERVER_ERROR_NOERROR )
    {
        return -1;
    }


    // execute the cipher's self test

    lResult = CipherServer_ExecuteSelfTest( pencrypt_info->pCtx,
        BOOL_TRUE );
    if (lResult != CIPHERSERVER_ERROR_NOERROR)
    {
#if DPRINT
        DebugPrint2(20,"self test fail");
#else
        //        printf("self test fail\n");
#endif
        CipherServer_Destroy( pencrypt_info->pCtx );
        pencrypt_info->pCtx = NULL;
        return -1;
    }


    lResult = CipherServer_GetInfoBlock( pencrypt_info->pCtx, &pencrypt_info->infoblock );
    if (lResult != CIPHERSERVER_ERROR_NOERROR)
    {
        CipherServer_Destroy( pencrypt_info->pCtx );
        pencrypt_info->pCtx = NULL;
        return -1;
    }


    lResult = CipherServer_OpenSession(
        CIPHERSERVER_MODE_ENCRYPT,
        (const WORD8*)key,
        pencrypt_info->infoblock.lKeySize,
        pencrypt_info->pCtx,
        pencrypt_info->InitData ,
        &pencrypt_info->chandle_encrypt );
    if ( lResult != CIPHERSERVER_ERROR_NOERROR )
    {
        CipherServer_Destroy( pencrypt_info->pCtx );
        pencrypt_info->pCtx = NULL;
        return -1;
    }

    lResult = CipherServer_OpenSession(
        CIPHERSERVER_MODE_DECRYPT ,
        (const WORD8*)key,
        pencrypt_info->infoblock.lKeySize,
        pencrypt_info->pCtx,
        pencrypt_info->InitData ,
        &pencrypt_info->chandle_antiencrypt );
    if ( lResult != CIPHERSERVER_ERROR_NOERROR )
    {
        CipherServer_Destroy( pencrypt_info->pCtx );
        pencrypt_info->pCtx = NULL;
        return -1;
    }

    return 0;
}


//
//需要保证insize是pencrypt_info->infoblock.lBlockSize的整数倍, indata and outdata can be the same buffer
LONG Crypter_Encrypt( PENCRYPT_INFO pencrypt_info,  PBYTE indata, PBYTE outdata, ULONG insize)
{
    int nNumOfBlocks =insize;

    nNumOfBlocks = insize / pencrypt_info->infoblock.lBlockSize;
    if (insize % pencrypt_info->infoblock.lBlockSize)
    {
        nNumOfBlocks++;
    }

    CipherServer_EncryptBlocks( pencrypt_info->chandle_encrypt, indata, outdata, nNumOfBlocks );

    return 0;
}


//注意outdata 的大小必须要大于或者等于nNumOfBlocks*pencrypt_info->infoblock.lBlockSize, 输入输出缓冲可以一样
LONG Crypter_Decrypt( PENCRYPT_INFO pencrypt_info,  PBYTE indata, PBYTE outdata, ULONG insize )
{
    int nNumOfBlocks =0;

    nNumOfBlocks = insize / pencrypt_info->infoblock.lBlockSize;
    if (insize % pencrypt_info->infoblock.lBlockSize)
    {
        nNumOfBlocks++;
    }

    CipherServer_DecryptBlocks( pencrypt_info->chandle_antiencrypt, 
        indata, 
        outdata, 
        nNumOfBlocks, 
        CIPHER_NULL);

    return 0;

}

LONG Crypter_Release(  PENCRYPT_INFO pencrypt_info )
{
    LONG lResult = 0;   

    // close the session

    lResult = CipherServer_CloseSession(pencrypt_info->chandle_antiencrypt);
    if (lResult != CIPHERSERVER_ERROR_NOERROR)
    {
#if DPRINT
        DebugPrint2(20,"ERROR #%i", lResult);
#else
        //        printf("ERROR #%d\n", lResult);
#endif
        CipherServer_Destroy( pencrypt_info->pCtx );
        pencrypt_info->pCtx = NULL;
        return -1;
    }

    lResult = CipherServer_CloseSession(pencrypt_info->chandle_encrypt);
    if (lResult != CIPHERSERVER_ERROR_NOERROR)
    {
#if DPRINT
        DebugPrint2(20,"ERROR #%i", lResult);
#else
        //		printf("ERROR #%d\n", lResult);
#endif
        CipherServer_Destroy(pencrypt_info->pCtx);
        pencrypt_info->pCtx = NULL;
        return -1;
    }

    lResult = CipherServer_Destroy( pencrypt_info->pCtx );
    if (lResult != CIPHERSERVER_ERROR_NOERROR)
    {
        return -1;
    }

    return 0;
}


//////////////////////////////////////////////////////////////////////////////

// simple mapper to stdout

class CTestStdOutImpl : public CTestStdOut
{
public:

    virtual void Puts 
        (const char* txt) 
    { 

⌨️ 快捷键说明

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