Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » Encrypt / Decrypt PNG

This thread is locked; no one can reply to it. rss feed Print
Encrypt / Decrypt PNG
Oregano
Member #14,191
April 2012

Theres an old topic http://www.allegro.cc/forums/thread/604453 about this where Matthew Leverton post routines for for encrypt and decrypt PNG images, the encrypt
works like a charm!!!, but the decrypt does not compile and my knowledge of file streaming is not nearly enough to deal with the errors, is there anybody that can take a look at it? Thanks.

jmasterx
Member #11,410
October 2009

Try this:

#SelectExpand
1 2#include <allegro5/allegro.h> 3#include <stdio.h> 4#include <allegro5/internal/aintern_file.h> 5#include <allegro5/allegro_image.h> 6 7const char *password = "allegro"; 8const int password_len = 7; 9 10ALLEGRO_FILE_INTERFACE encrypted_io; 11 12/* need to remember these two functions */ 13ALLEGRO_FILE *(*stdio_fopen)(const char *path, const char *mode); 14size_t (*stdio_fread)(ALLEGRO_FILE *f, void *ptr, size_t size); 15 16/* open a file using stdio and set the vtable to our interface */ 17static ALLEGRO_FILE *eio_fopen(const char *path, const char *mode) 18{ 19 ALLEGRO_FILE *f = stdio_fopen(path, mode); 20 f->vtable = &encrypted_io; 21 return f; 22} 23 24/* read the buffer via stdio, and then decrypt it */ 25static size_t eio_fread(ALLEGRO_FILE *f, void *ptr, size_t size) 26{ 27 int64_t pos = al_ftell(f); 28 size_t bytes_read = stdio_fread(f, ptr, size); 29 30 unsigned char *s = ptr; 31 const unsigned char *e = s + bytes_read; 32 33 while (s < e) 34 { 35 *s++ ^= password[pos++ % password_len]; 36 } 37 38 return bytes_read; 39} 40 41int main() 42{ 43 ALLEGRO_DISPLAY *display; 44 ALLEGRO_BITMAP *bmp; 45 46 al_init(); 47 al_init_image_addon(); 48 49 /* set up our super advanced encryption system */ 50 memcpy(&encrypted_io, al_get_new_file_interface(), sizeof(ALLEGRO_FILE_INTERFACE)); 51 stdio_fopen = encrypted_io.fi_fopen; 52 stdio_fread = encrypted_io.fi_fread; 53 encrypted_io.fi_fopen = eio_fopen; 54 encrypted_io.fi_fread = eio_fread; 55 al_set_new_file_interface(&encrypted_io); 56 57 display = al_create_display(457, 266); 58 59 bmp = al_load_bitmap("encrypted.png"); 60 if (bmp) 61 { 62 al_clear_to_color(al_map_rgb(255,255,255)); 63 al_draw_bitmap(bmp, 0, 0, 0); 64 al_flip_display(); 65 al_rest(5); 66 } 67 68 return 0; 69}

This should compile for a C compiler, not C++.

Oregano
Member #14,191
April 2012

A couple of errors are gone, but i'm still in trouble because this should be part of a c++ project, cannot ask you for more, thank you jmasterx for your quick answer, can this code be ported to work on c++?, somebody guide me in the right direction?

Go to: