// GrannyConverter.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include #pragma warning(push) #pragma warning(disable:4100) #pragma warning(disable:4127) #pragma warning(disable:4189) #pragma warning(disable:4512) #pragma warning(disable:4706) #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #pragma warning(pop) #include #pragma comment(lib, "granny2.lib") struct CryptoTester { CryptoTester() : Encryptor(NULL), Decryptor(NULL), elapsedTime(0.0) {} CryptoPP::StreamTransformation* Encryptor; CryptoPP::StreamTransformation* Decryptor; double elapsedTime; }; template void SetupCrypto(std::vector& vector, const byte* key, const byte* iv) { CryptoTester res; res.Encryptor = new N::Encryption; res.Decryptor = new N::Decryption; CryptoPP::SimpleKeyingInterface* keyInterface = dynamic_cast(res.Encryptor); CryptoPP::SimpleKeyingInterface* keyInterface2 = dynamic_cast(res.Decryptor); if (keyInterface->IVRequirement() != CryptoPP::SimpleKeyingInterface::NOT_RESYNCHRONIZABLE) { keyInterface->SetKeyWithIV(key, keyInterface->MaxKeyLength(), iv, keyInterface->MaxIVLength()); keyInterface2->SetKeyWithIV(key, keyInterface->MaxKeyLength(), iv, keyInterface->MaxIVLength()); } else { keyInterface->SetKey(key, keyInterface->MaxKeyLength()); keyInterface2->SetKey(key, keyInterface->MaxKeyLength()); } vector.push_back(res); } int _tmain(int argc, char* argv[]) { if (argc != 2) return 1; char arg1[1024 + 1]; strcpy_s(arg1, sizeof(arg1), argv[1]); CFilename Gr2FileName(arg1); CFilename RawFileName(Gr2FileName.NoExtension()); RawFileName += ".ygr"; CFilename NewGr2FileName(Gr2FileName.NoExtension()); NewGr2FileName += "_new.gr2"; granny_file* gr2File = GrannyReadEntireFile(Gr2FileName.c_str()); granny_int32x FileSectionCount; granny_file_info * FileInfo; if (gr2File) { FileSectionCount = gr2File->SectionCount; FileInfo = GrannyGetFileInfo(gr2File); GrannyConvertFileInfoToRaw(FileInfo, RawFileName.c_str()); } FILE * fp; if (0 == fopen_s(&fp, RawFileName.c_str(), "rb") && fp) { fseek(fp, 0L, SEEK_END); long FileSize = ftell(fp); fseek(fp, 0L, SEEK_SET); char * Buffer = (char *) malloc(FileSize); printf("FileSize: %u\n", fread(Buffer, sizeof(char), FileSize, fp)); fclose(fp); // ÆÄÀÏ À̸§À¸·Î ºÎÅÍ ¾Ïȣȭ Å°¸¦ »ý¼ºÇÔ (´Ù¸¥ ÆÄÀÏ·Î À̸§ ¹Ù²ã¼­ »ç¿ëÇϱâ Èûµé¾îÁü) std::string SrcStringForKey(RawFileName); CryptoPP::HashTransformation* hm = NULL; unsigned int idx = GetCRC32(SrcStringForKey.c_str(), SrcStringForKey.length()) & 3; // Å° ¾Ïȣȭ byte key[32]; CryptoPP::HashTransformation* hm1 = NULL; CryptoPP::HashTransformation* hm2 = NULL; static CryptoPP::Tiger tiger; static CryptoPP::SHA1 sha1; static CryptoPP::RIPEMD128 ripemd128; static CryptoPP::Whirlpool whirlpool; switch (idx & 3) { case 0: hm1 = &whirlpool; break; case 1: hm1 = &tiger; break; case 2: hm1 = &sha1; break; case 3: hm1 = &ripemd128; break; } CryptoPP::StringSource(SrcStringForKey, true, new CryptoPP::HashFilter(*hm1, //new CryptoPP::HexEncoder( new CryptoPP::ArraySink(key, sizeof(key) - 16) //) // HexEncoder ) // HashFilter ); // StringSource // ¸¸µé¾îÁø Å°ÀÇ Ã¹¹ø° 4¹ÙÀÌÆ®·Î ´ÙÀ½ 16¹ÙÀÌÆ® Å° »ý¼º ¾Ë°í¸®Áò ¼±Åà unsigned int idx2 = *(unsigned int*) key; switch (idx2 & 3) { case 0: hm2 = &sha1; break; case 1: hm2 = &ripemd128; break; case 2: hm2 = &whirlpool; break; case 3: hm2 = &tiger; break; } CryptoPP::StringSource(SrcStringForKey, true, new CryptoPP::HashFilter(*hm2, //new CryptoPP::HexEncoder( new CryptoPP::ArraySink(key + 16, sizeof(key) - 16) //) // HexEncoder ) // HashFilter ); // StringSource // Å° »ý¼º ¿Ï·á // IV »ý¼º CryptoPP::AutoSeededRandomPool prng; byte iv[32]; prng.GenerateBlock(iv, sizeof(iv)); // Cryptoµé ¼Â¾÷ std::vector tester; SetupCrypto >(tester, key, iv); SetupCrypto >(tester, key, iv); SetupCrypto >(tester, key, iv); SetupCrypto(tester, key, iv); SetupCrypto(tester, key, iv); SetupCrypto(tester, key, iv); SetupCrypto >(tester, key, iv); SetupCrypto >(tester, key, iv); std::string CipherText; // ¾Ïȣȭ¿¡ »ç¿ëµÉ Àӽà ¹öÆÛ std::string DecryptText; // º¹È£È­¿¡ »ç¿ëµÉ Àӽà ¹öÆÛ double nonono = 0.0; for (int i = 0; i < 1024; ++i) { CipherText.clear(); DecryptText.clear(); std::vector::iterator it = tester.begin(); while (it != tester.end()) { CryptoTester& tester = *(it++); boost::timer t; CryptoPP::ArraySource((const byte*) Buffer, FileSize, true, new CryptoPP::StreamTransformationFilter(*tester.Encryptor, new CryptoPP::StringSink(CipherText) ) ); CryptoPP::ArraySource(CipherText, true, new CryptoPP::StreamTransformationFilter(*tester.Decryptor, new CryptoPP::StringSink(DecryptText) ) ); tester.elapsedTime += t.elapsed(); } boost::timer t; for (int k = 0; k < FileSize >> 5; k += 32) CipherText.replace(k, 32, Buffer + k, 32); const char * text = CipherText.c_str(); for (int k = 0; k < FileSize >> 5; k += 32) DecryptText.replace(k, 32, text + k, 32); nonono += t.elapsed(); } std::vector::iterator it = tester.begin(); while (it != tester.end()) { CryptoTester& tester = *(it++); printf("%s %g\n", tester.Encryptor->AlgorithmName().c_str(), tester.elapsedTime); } printf("No Crypto: %g\n", nonono); // ´Ù½Ã ±×·¡´Ï ÆÄÀÏ·Î ÀúÀå FileInfo = (granny_file_info *)DecryptText.c_str(); GrannyRebasePointers(GrannyFileInfoType, (void*) FileInfo, (int) FileInfo, true); granny_int32x DefaultSectionIndex = 0; int artToolSize = GrannyGetTotalObjectSize(GrannyArtToolInfoType); granny_file_data_tree_writer *DataTreeWriter = GrannyBeginFileDataTreeWriting(GrannyFileInfoType, FileInfo, DefaultSectionIndex, DefaultSectionIndex); GrannyWriteDataTreeToFile(DataTreeWriter, GrannyCurrentGRNStandardTag, NewGr2FileName.c_str(), FileSectionCount); GrannyEndFileDataTreeWriting(DataTreeWriter); free(Buffer); } return 0; }