Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Decrypting files in memory and reading them.

This thread is locked; no one can reply to it. rss feed Print
Decrypting files in memory and reading them.
ZenRowe
Member #16,081
September 2015

I may be going around this all wrong but basically I wanted to put some light obfuscation on assets.

I have been able to successfully load files, encrypt them, and then save the output (and successfully decrypt them)

however I am having trouble when it comes to loading the files into memory, decrypting them, and then reading the file.

Apologies, it's a bit messy as I'm just trying to figure it out first before I clean it up.

this is the code portion below for handling encrypted images. This assumes the file is already encrypted.

The image variable just becomes NULL at the end and I can't figure out why.

#SelectExpand
1void FileHandler::DecryptFile(ALLEGRO_FILE *file, ALLEGRO_FILE *memFile) 2{ 3 const char* xorkey = "someKey"; 4 int j = 0; 5 unsigned int buffer; 6 for (int i = 0; i < al_fsize(file); i++) 7 { 8 buffer = al_fgetc(file) ^ xorkey[j]; 9 al_fputc(memFile, buffer); 10 11 j++; 12 if (j >= 6) 13 j = 0; 14 } 15 al_fseek(memFile, 0, ALLEGRO_SEEK_SET); 16} 17 18void FileHandler::DecryptImage(ALLEGRO_BITMAP* image, const char* path, const char* fileType) 19{ 20 ALLEGRO_FILE *input = al_fopen(path, "rb"); 21 const int data_size = al_fsize(input); 22 void *data = calloc(1, al_fsize(input)); 23 ALLEGRO_FILE *memFile = al_open_memfile(data, data_size, "r+b"); 24 FileHandler::DecryptFile(input, memFile); 25 image = al_load_bitmap_f(memFile, fileType); 26 al_fclose(input); 27 al_fclose(memFile); 28}

Elias
Member #358
May 2000

Looking at the documentation [1] of al_open_memfile:

Quote:

The mode can be any combination of "r" (readable) and "w" (writable).

You pass a "+" as well as a "b" both of which are invalid.

--
"Either help out or stop whining" - Evert

ZenRowe
Member #16,081
September 2015

I... okay that uhh... that fixed it by just changing it to "rw"... hmm. I thought it just meant that it would use the same "Mode" identifiers that functions like fopen() used.

Thank you for clarifying that for me. I'm both happy it was something simple and also bashing my face against the desk.

Works perfectly now, and surprisingly fast considering the filesize I was testing this on. Everything decrypts and shows up like I had intended.
Thank you very much!

Elias
Member #358
May 2000

Yes, I think it would be a good idea allowing "r+" to mean the same as "rw" (the "b" seems to actually be ignored already anyway)... it's how al_fopen works already. Patch welcome I suppose :)

--
"Either help out or stop whining" - Evert

Matias Persson
Member #15,093
May 2013

I usually use;

#SelectExpand
1void save(const char *fileName, int map[MAP_WIDTH][MAP_HEIGHT][MAP_LAYERS]) { 2 std::ofstream file(fileName, std::ios::binary | std::ios::out); 3 4 for (int x = 0; x < MAP_WIDTH; x++) { 5 for (int y = 0; y < MAP_HEIGHT; y++) { 6 for (int z = 0; z < MAP_LAYERS; z++) { 7 file.write(reinterpret_cast<char *>(&map[x][y][z]), 4); 8 } 9 } 10 } 11}

#SelectExpand
1void load(const char *fileName, int map[MAP_WIDTH][MAP_HEIGHT][MAP_LAYERS]){ 2 std::ifstream file(fileName, std::ios::binary | std::ios::in); 3 4 for (int x = 0; x < MAP_WIDTH; x++) { 5 for (int y = 0; y < MAP_HEIGHT; y++) { 6 for (int z = 0; z < MAP_LAYERS; z++) { 7 file.read(reinterpret_cast<char *>(&map[x][y][z]), 4); 8 } 9 } 10 } 11}

Not sure if it's relevant to the question, but this is an example of map saving/loading I use.

Go to: