We are developing an Irrllicht-based FPS game, and I made for it an simple files encryption system. I am using for it the cryptographic C++ library CryptoPP.
Here it is:
- To CFileSystem.h in Irrlicht source add this line:
Code: Select all
//! adds an zip archive loaded from memory to the filesystem virtual bool addZipFileArchiveFromMemory(const c8* filename, void* memory, long len, bool deleteMemoryWhenDropped = false, bool ignoreCase = true, bool ignorePaths = true);
- To CFileSystem.cpp in Irrlicht source add these lines:
Code: Select all
//! adds an zip archive to the filesystem bool CFileSystem::addZipFileArchiveFromMemory(const c8* filename, void* memory, long len, bool deleteMemoryWhenDropped, bool ignoreCase, bool ignorePaths) { IReadFile* file = createMemoryReadFile(memory, len, filename, deleteMemoryWhenDropped); if (file) { CZipReader* zr = new CZipReader(file, ignoreCase, ignorePaths); if (zr) ZipFileSystems.push_back(zr); file->drop(); bool ret = (zr != 0); _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; return ret; } #ifdef _DEBUG os::Printer::log("Could not open file. Zipfile not added", filename, ELL_ERROR); #endif _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; return false; }
- To file IFileSystem.h add this line:
Code: Select all
//! adds an zip archive loaded from memory to the filesystem virtual bool addZipFileArchiveFromMemory(const c8* filename, void* memory, long len, bool deleteMemoryWhenDropped = false, bool ignoreCase = true, bool ignorePaths = true) = 0;
- You need to include these Cryptopp files in your project:
Code: Select all
#include <cryptopp/modes.h> #include <cryptopp/aes.h> #include <cryptopp/filters.h>
- Finally, when you want to load an encrypted archive, do this:
Code: Select all
fstream file; file.open(_fileName.c_str(), ios::ate|ios::binary|ios::in); byte* key; byte* iv; iv = generateCipherIV(fileContent); key = generateCipherKey(iv); file.seekg(0); file.read(fileContent, fileSize); file.close(); byte* decryptedFile = new byte[fileSize]; try { OFB_Mode<AES>::Decryption decryptor(key, CryptoPP::AES::DEFAULT_KEYLENGTH, iv); decryptor.ProcessData(decryptedFile, (byte*)fileContent, fileSize); } catch(CryptoPP::Exception &e) { cout << "Encryption exception caught: " << e.what() << "\nFile may me broken.\n"; } delete[] fileContent; device->getFileSystem()->addZipFileArchiveFromMemory(_fileName.c_str(), decryptedFile, fileSize, true);
To encrypt and decrypt files outside your engine, I have written a small utility application.
You can download it with it's source here:
Code: Select all
http://rapidshare.com/files/97003481/encryptionmngr.7z
(And of course, the key shouldn't be defined statically for security reasons.)
With some small changes it can use other ciphers, like Blowfish, etc.
I release it all under zlib license.
I hope I described it understable (I don't speak very good English ), and it will be useful for someone. :- )
Any suggestions are welcome