📄 testbfacslib_orig.cpp
字号:
/* * 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"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// (all the test together)enum emCrpytType{ CrptyBlofish, CrptyAes,};void main2(){ clock_t clk; clk = ::clock(); PCIPHERCTX pCtx; PCIPHERSESSION chandle; CIPHERINFOBLOCK infoblock; WORD32 lResult; emCrpytType crpttype = CrptyBlofish; WORD8 _inbuf[65536]="something to encrypt and decrypt"; WORD8 _outbuf[65536]; WORD8 _lastbuf[65536];//解密后保存在这里 switch(crpttype) { case CrptyBlofish: { lResult = ::CipherServer_GetCipherInfo(BLOWFISH_CIPHERNAME, &infoblock); if (lResult != CIPHERSERVER_ERROR_NOERROR) { printf("ERROR #%d\n", lResult); return ; } // open the cipher (using the built-in random generator with no additional seed) lResult = ::CipherServer_Create(BLOWFISH_CIPHERNAME, &pCtx, NULL, NULL, NULL, 0); if (lResult != CIPHERSERVER_ERROR_NOERROR) { printf("ERROR #%d\n", lResult); return ; } // execute the cipher's self test lResult = ::CipherServer_ExecuteSelfTest(pCtx, BOOL_TRUE); if (lResult != CIPHERSERVER_ERROR_NOERROR) { printf("ERROR #%d\n", lResult); ::CipherServer_Destroy(pCtx); return ; } lResult = ::CipherServer_GetInfoBlock(pCtx, &infoblock); if (lResult != CIPHERSERVER_ERROR_NOERROR) { printf("ERROR #%d\n", lResult); ::CipherServer_Destroy(pCtx); return ; } // open a new session for encryption, using a simply created key int lKeySize = infoblock.lKeySize; //std::cout << "key len" <<lKeySize <<std::endl; WORD8 key[128] ="fdfdsaaaaaaaaaaakyefdfddfdf"; WORD8 *pInitData = (WORD8*) malloc(infoblock.lInitDataSize); lResult = ::CipherServer_OpenSession( CIPHERSERVER_MODE_ENCRYPT , key, lKeySize, pCtx, pInitData, &chandle); if (lResult != CIPHERSERVER_ERROR_NOERROR) { printf("ERROR #%d\n", lResult); ::CipherServer_Destroy(pCtx); free(pInitData); return ; } // strcpy((char*)_inbuf, "something to encrypt and decrypt"); // (don't forget the ending zero -> + 1) int nNumOfBlocks = (int)::strlen((char*)_inbuf) + 1; if (nNumOfBlocks % infoblock.lBlockSize) { nNumOfBlocks = nNumOfBlocks / infoblock.lBlockSize + 1; } else { nNumOfBlocks /= infoblock.lBlockSize; } // encrypt this data ::CipherServer_EncryptBlocks(chandle, _inbuf, _outbuf, nNumOfBlocks); printf("encrypted message: "); for (int lI = 0; lI < nNumOfBlocks * infoblock.lBlockSize; lI++) { printf("%02x", _outbuf[lI]); if (0 == ((lI + 1) % infoblock.lBlockSize)) { printf(" "); } } // close the session lResult = ::CipherServer_CloseSession(chandle); if (lResult != CIPHERSERVER_ERROR_NOERROR) { printf("ERROR #%d\n", lResult); ::CipherServer_Destroy(pCtx); free(pInitData); return ; } lResult = ::CipherServer_Destroy(pCtx); if (lResult != CIPHERSERVER_ERROR_NOERROR) { printf("ERROR #%d\n", lResult); return ; } //下面是解密 // open the cipher (using the built-in random generator with no additional seed) lResult = ::CipherServer_Create(BLOWFISH_CIPHERNAME, &pCtx, NULL, NULL, NULL, 0); if (lResult != CIPHERSERVER_ERROR_NOERROR) { printf("ERROR #%d\n", lResult); return ; } // open a session for decryption lResult = ::CipherServer_OpenSession( CIPHERSERVER_MODE_DECRYPT , key, lKeySize, pCtx, pInitData, &chandle); if (lResult != CIPHERSERVER_ERROR_NOERROR) { printf("ERROR #%d\n", lResult); ::CipherServer_Destroy(pCtx); free(pInitData); return ; } // reset before decrypting, just for fun here //::CipherServer_ResetSession(chandle, pInitData); // decrypt the data, interrupt after the first block, if possible if (nNumOfBlocks < 2) { ::CipherServer_DecryptBlocks(chandle, _outbuf, _lastbuf, nNumOfBlocks, CIPHER_NULL); } else { ::CipherServer_DecryptBlocks(chandle, _outbuf, _lastbuf, 1, CIPHER_NULL); ::CipherServer_DecryptBlocks(chandle, &_outbuf[infoblock.lBlockSize], &_lastbuf[infoblock.lBlockSize], nNumOfBlocks - 1, _outbuf); } printf("decrypted message: >>>%s<<<\n", (char*) _lastbuf); // close the session lResult = ::CipherServer_CloseSession(chandle); if (lResult != CIPHERSERVER_ERROR_NOERROR) { printf("ERROR #%d\n", lResult); ::CipherServer_Destroy(pCtx); free(pInitData); return ; } // free the init. data buffer free(pInitData); // close the cipher lResult = ::CipherServer_Destroy(pCtx); if (lResult != CIPHERSERVER_ERROR_NOERROR) { printf("ERROR #%d\n", lResult); return ; } } break; case CrptyAes: { } break; }#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}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -