Easy Files Encryption In Irrlicht using AES

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
RedDragCZ
Posts: 9
Joined: Tue Aug 28, 2007 9:32 pm
Location: Czech Republic
Contact:

Easy Files Encryption In Irrlicht using AES

Post by RedDragCZ »

Hello there,
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);
That's all. Yes, this method has few disadvantages, like memory inefficiency or that the whole file must be loaded and encrypted before any reading from it, but I think it is the easiest way to get working some files encryption in Irrlicht.

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
It uses an simple predefined key and iv, so before any real use you have to recompile it and define your own cipher key with iv.
(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 :D ), and it will be useful for someone. :- )
Any suggestions are welcome :-)
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

Cool, simple and effective. People will argue that these things will get cracked anyway if someone wanted to crack them, but theres no reason not to make the job harder, especially if its this easy to implement. :wink:
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
Masterhawk
Posts: 299
Joined: Mon Nov 27, 2006 6:52 pm
Location: GERMANY
Contact:

Post by Masterhawk »

Cool, I just wanted to start development of such an extension for irrlicht cause I need this for my project. Very nice that you did it before, so I have more time to take care of other things.
Will have a look at your code when I need it... :wink:
Image
D-Eagle
Posts: 25
Joined: Fri Jan 25, 2008 11:03 pm

Post by D-Eagle »

Umm.. How do i install cryptopp?
Is there any compiled version of it? Because i get about 150 linker errors with my self-compiled cryptlib.lib :/ Could you upload your lib?
RedDragCZ
Posts: 9
Joined: Tue Aug 28, 2007 9:32 pm
Location: Czech Republic
Contact:

Post by RedDragCZ »

D-Eagle wrote:..
I think there is no downloadable compiled version of it .. I didn't get any linker errors when i was compiling it and I can upload for you my version, but it has aprox. 50MB, so I will upload it sometime later, not today (maybe tomorrow, or in the oncoming week) ..
RedDragCZ
Posts: 9
Joined: Tue Aug 28, 2007 9:32 pm
Location: Czech Republic
Contact:

Post by RedDragCZ »

D-Eagle: Huh, i have compressed it in 7Zip and now it has only something about 5.5 MB :shock:
Anyway, I have uploaded it a you can download it here:

Code: Select all

http://rapidshare.com/files/99030574/cryptopp552-binary.7z
:wink:

EDIT: PS - it's version 5.52 and I have compiled it in MS Visual Studio 9.0
Last edited by RedDragCZ on Wed Mar 12, 2008 6:53 pm, edited 1 time in total.
D-Eagle
Posts: 25
Joined: Fri Jan 25, 2008 11:03 pm

Post by D-Eagle »

Thanks, i'll try with that!
D-Eagle
Posts: 25
Joined: Fri Jan 25, 2008 11:03 pm

Post by D-Eagle »

Hmm.. When i try to compile my project i still get errors :/

Code: Select all

1>Cryptlib.lib(dll.obj) : MSIL .netmodule or module compiled with /GL found; restarting link with /LTCG; add /LTCG to the link command line to improve linker performance
1>LINK : warning LNK4075: ignoring '/INCREMENTAL' due to '/LTCG' specification
1>Cryptlib.lib(dll.obj) : error LNK2005: "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl std::operator+<char,struct std::char_traits<char>,class std::allocator<char> >(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,char const *)" (??$?HDU?$char_traits@D@std@@V?$allocator@D@1@@std@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@0@ABV10@PBD@Z) already defined in msvcprtd.lib(MSVCP90D.dll)
1>Cryptlib.lib(dll.obj) : error LNK2005: "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl std::operator+<char,struct std::char_traits<char>,class std::allocator<char> >(char const *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (??$?HDU?$char_traits@D@std@@V?$allocator@D@1@@std@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@0@PBDABV10@@Z) already defined in msvcprtd.lib(MSVCP90D.dll)
1>Cryptlib.lib(dll.obj) : error LNK2005: "public: char const * __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::c_str(void)const " (?c_str@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEPBDXZ) already defined in msvcprtd.lib(MSVCP90D.dll)
1>Cryptlib.lib(dll.obj) : error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::~basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(void)" (??1?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@XZ) already defined in msvcprtd.lib(MSVCP90D.dll)
1>Cryptlib.lib(dll.obj) : error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@ABV01@@Z) already defined in msvcprtd.lib(MSVCP90D.dll)
1>Cryptlib.lib(dll.obj) : error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(char const *)" (??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@PBD@Z) already defined in msvcprtd.lib(MSVCP90D.dll)
1>libcmt.lib(stdexcpt.obj) : error LNK2005: "public: __thiscall std::exception::exception(void)" (??0exception@std@@QAE@XZ) already defined in MSVCRTD.lib(MSVCR90D.dll)
1>libcmt.lib(stdexcpt.obj) : error LNK2005: "public: __thiscall std::exception::exception(class std::exception const &)" (??0exception@std@@QAE@ABV01@@Z) already defined in MSVCRTD.lib(MSVCR90D.dll)
1>libcmt.lib(stdexcpt.obj) : error LNK2005: "public: virtual __thiscall std::exception::~exception(void)" (??1exception@std@@UAE@XZ) already defined in MSVCRTD.lib(MSVCR90D.dll)
1>libcmt.lib(typinfo.obj) : error LNK2005: "public: char const * __thiscall type_info::_name_internal_method(struct __type_info_node *)const " (?_name_internal_method@type_info@@QBEPBDPAU__type_info_node@@@Z) already defined in MSVCRTD.lib(MSVCR90D.dll)
1>libcmt.lib(typinfo.obj) : error LNK2005: "public: void __thiscall type_info::_type_info_dtor_internal_method(void)" (?_type_info_dtor_internal_method@type_info@@QAEXXZ) already defined in MSVCRTD.lib(MSVCR90D.dll)
1>libcmt.lib(typinfo.obj) : error LNK2005: "public: bool __thiscall type_info::operator==(class type_info const &)const " (??8type_info@@QBE_NABV0@@Z) already defined in MSVCRTD.lib(MSVCR90D.dll)
1>libcmt.lib(typinfo.obj) : error LNK2005: "public: bool __thiscall type_info::operator!=(class type_info const &)const " (??9type_info@@QBE_NABV0@@Z) already defined in MSVCRTD.lib(MSVCR90D.dll)
1>libcmt.lib(typinfo.obj) : error LNK2005: "private: __thiscall type_info::type_info(class type_info const &)" (??0type_info@@AAE@ABV0@@Z) already defined in MSVCRTD.lib(ti_inst.obj)
1>libcmt.lib(typinfo.obj) : error LNK2005: "private: class type_info & __thiscall type_info::operator=(class type_info const &)" (??4type_info@@AAEAAV0@ABV0@@Z) already defined in MSVCRTD.lib(ti_inst.obj)
1>libcmt.lib(invarg.obj) : error LNK2005: __invoke_watson already defined in MSVCRTD.lib(MSVCR90D.dll)
1>libcmt.lib(typname.obj) : error LNK2005: ___clean_type_info_names_internal already defined in MSVCRTD.lib(MSVCR90D.dll)
1>libcmt.lib(setlocal.obj) : error LNK2005: __configthreadlocale already defined in MSVCRTD.lib(MSVCR90D.dll)
1>libcmt.lib(tidtable.obj) : error LNK2005: __encode_pointer already defined in MSVCRTD.lib(MSVCR90D.dll)
1>libcmt.lib(tidtable.obj) : error LNK2005: __decode_pointer already defined in MSVCRTD.lib(MSVCR90D.dll)
1>libcmt.lib(crt0dat.obj) : error LNK2005: __amsg_exit already defined in MSVCRTD.lib(MSVCR90D.dll)
1>libcmt.lib(crt0dat.obj) : error LNK2005: __initterm_e already defined in MSVCRTD.lib(MSVCR90D.dll)
1>libcmt.lib(crt0dat.obj) : error LNK2005: _exit already defined in MSVCRTD.lib(MSVCR90D.dll)
1>libcmt.lib(crt0dat.obj) : error LNK2005: __exit already defined in MSVCRTD.lib(MSVCR90D.dll)
1>libcmt.lib(crt0dat.obj) : error LNK2005: __cexit already defined in MSVCRTD.lib(MSVCR90D.dll)
1>libcmt.lib(mlock.obj) : error LNK2005: __unlock already defined in MSVCRTD.lib(MSVCR90D.dll)
1>libcmt.lib(mlock.obj) : error LNK2005: __lock already defined in MSVCRTD.lib(MSVCR90D.dll)
1>libcmt.lib(winxfltr.obj) : error LNK2005: __XcptFilter already defined in MSVCRTD.lib(MSVCR90D.dll)
1>libcmt.lib(exsup.obj) : error LNK2005: __except_list already defined in MSVCRTD.lib(dllsupp.obj)
1>libcmt.lib(crt0init.obj) : error LNK2005: ___xi_a already defined in MSVCRTD.lib(cinitexe.obj)
1>libcmt.lib(crt0init.obj) : error LNK2005: ___xi_z already defined in MSVCRTD.lib(cinitexe.obj)
1>libcmt.lib(crt0init.obj) : error LNK2005: ___xc_a already defined in MSVCRTD.lib(cinitexe.obj)
1>libcmt.lib(crt0init.obj) : error LNK2005: ___xc_z already defined in MSVCRTD.lib(cinitexe.obj)
1>libcmt.lib(hooks.obj) : error LNK2005: "void __cdecl terminate(void)" (?terminate@@YAXXZ) already defined in MSVCRTD.lib(MSVCR90D.dll)
1>libcmt.lib(crt0.obj) : error LNK2005: _mainCRTStartup already defined in MSVCRTD.lib(crtexe.obj)
1>libcmt.lib(errmode.obj) : error LNK2005: ___set_app_type already defined in MSVCRTD.lib(MSVCR90D.dll)
1>libcpmt.lib(string.obj) : error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::~basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(void)" (??1?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@XZ) already defined in msvcprtd.lib(MSVCP90D.dll)
1>libcpmt.lib(string.obj) : error LNK2005: "public: char const * __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::c_str(void)const " (?c_str@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEPBDXZ) already defined in msvcprtd.lib(MSVCP90D.dll)
1>libcpmt.lib(string.obj) : error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(char const *)" (??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@PBD@Z) already defined in msvcprtd.lib(MSVCP90D.dll)
1>libcpmt.lib(string.obj) : error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@ABV01@@Z) already defined in msvcprtd.lib(MSVCP90D.dll)
1>libcpmt.lib(locale0.obj) : error LNK2005: "public: char const * __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::c_str(void)const " (?c_str@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEPBDXZ) already defined in msvcprtd.lib(MSVCP90D.dll)
1>libcpmt.lib(locale0.obj) : error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::~basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(void)" (??1?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@XZ) already defined in msvcprtd.lib(MSVCP90D.dll)
1>libcpmt.lib(locale0.obj) : error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(char const *)" (??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@PBD@Z) already defined in msvcprtd.lib(MSVCP90D.dll)
1>LINK : warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs; use /NODEFAULTLIB:library
1>MSVCRTD.lib(cinitexe.obj) : warning LNK4098: defaultlib 'libcmt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library

Here's my code:

Code: Select all

IReadFile* file=m_device->getFileSystem()->createAndOpenFile("gamedata.dpf");

	u32 fileSize = file->getSize(); 
	c8* fileContent = new char[fileSize];

	file->seek(0,false); 
	file->read(fileContent,fileSize);

	u8 key[CryptoPP::AES::DEFAULT_KEYLENGTH] = "a146fea5w1fe5q1";
    u8 iv[AES::BLOCKSIZE] = "y64eQ46ef46b8F1";

	u8* decryptedFile = new u8[fileSize];

	try
    {
      OFB_Mode<AES>::Decryption decryptor(key, CryptoPP::AES::DEFAULT_KEYLENGTH, iv);
      decryptor.ProcessData(decryptedFile, (u8*)fileContent, fileSize);
    }
    catch(CryptoPP::Exception &e)
    {
      printf("Encryption exception caught: %s\nFile may me broken.\n",e.what()); 
    }

	m_device->getFileSystem()->addZipFileArchiveFromMemory("gamedata.dpf", decryptedFile, fileSize, true);

	delete[] fileContent;
    delete decryptedFile;
RedDragCZ
Posts: 9
Joined: Tue Aug 28, 2007 9:32 pm
Location: Czech Republic
Contact:

Post by RedDragCZ »

Maybe you get the "already defined in" errors cause I have compiled it in Release mode and you try to compile your project in Debug mode (I guess it because the lib's name's at which you get the errors end with the "D" char)
Post Reply