In this example, we exemplify the usage of eet_write_cipher() and eet_read_cipher().
For it to work, make sure to have your Eet installation with a ciphering backend enabled.
We start by defining the information to record in an Eet file (buffer
), the key to cipher that (key
) and a dummy wrong key to try to access that information, later (key_bad
).
const char *buffer = "Here is a string of data to save !"; const char *key = "This is a crypto key"; const char *key_bad = "This is another crypto key";
After opening our file, we simply use the first cited function to write our string ciphered:
ef = eet_open(file, EET_FILE_MODE_WRITE); if (!ef) { fprintf( stderr, "ERROR: could not access file (%s).\n", file); goto error; } if (!eet_write_cipher(ef, "keys/tests", buffer, strlen(buffer) + 1, 0, key)) { fprintf( stderr, "ERROR: could not access file (%s).\n", file); goto error; } eet_close(ef);
Then, after closing it on purpose, we open it again, to retrieve the encrypted information back, in a readable format:
ef = eet_open(file, EET_FILE_MODE_READ); if (!ef) { fprintf( stderr, "ERROR: could not access file (%s).\n", file); goto error; } test = eet_read_cipher(ef, "keys/tests", &size, key); if (!test) { fprintf( stderr, "ERROR: could decript contents on file %s, with key %s.\n", file, key); goto error; } if (size != (int)strlen(buffer) + 1) { fprintf( stderr, "ERROR: something is wrong with the decripted data\n"); goto error; } if (memcmp(test, buffer, strlen(buffer) + 1) != 0) { fprintf( stderr, "ERROR: something is wrong with the decripted data\n"); goto error; } eet_close(ef); /* Decrypt an eet file, now using our BAD key!! */ ef = eet_open(file, EET_FILE_MODE_READ); if (!ef) { fprintf( stderr, "ERROR: could not access file (%s).\n", file); goto error; } test = eet_read_cipher(ef, "keys/tests", &size, key_bad); if (size == (int)strlen(buffer) + 1) if (memcmp(test, buffer, strlen(buffer) + 1) == 0) { fprintf( stderr, "ERROR: something is wrong with the contents of %s, as" " we accessed it with a different key and it decripted our" " information right.\n", file); goto error; } eet_close(ef);
Note that we do it twice, being the last time with the wrong key. In this last case, if the information is read back and matches the original buffer
, something wrong is going on (we made it to fail on purpose). The former access is OK, and must work.
What we do in sequence is just to delete the file. The complete code of the example follows.
00001 /* 00002 * build: gcc -o eet_data_file_cipher_decipher eet-data-file_cipher_decipher.c `pkg-config --cflags --libs eet eina` 00003 */ 00004 00005 #include <Eina.h> 00006 #include <Eet.h> 00007 #include <stdio.h> 00008 #include <limits.h> 00009 #include <sys/types.h> 00010 #include <sys/stat.h> 00011 #include <unistd.h> 00012 00013 int 00014 main(void) 00015 { 00016 const char *buffer = "Here is a string of data to save !"; 00017 const char *key = "This is a crypto key"; 00018 const char *key_bad = "This is another crypto key"; 00019 00020 char *file = strdup("/tmp/eet_cipher_example_XXXXX"); 00021 Eet_File *ef; 00022 char *test; 00023 int size; 00024 00025 eet_init(); 00026 00027 if (!(file = tmpnam(file))) 00028 { 00029 fprintf( 00030 stderr, "ERROR: could not create temporary file (%s).\n", file); 00031 goto panic; 00032 } 00033 00034 /* Crypt an eet file. */ 00035 ef = eet_open(file, EET_FILE_MODE_WRITE); 00036 if (!ef) 00037 { 00038 fprintf( 00039 stderr, "ERROR: could not access file (%s).\n", file); 00040 goto error; 00041 } 00042 00043 if (!eet_write_cipher(ef, "keys/tests", buffer, strlen(buffer) + 1, 0, key)) 00044 { 00045 fprintf( 00046 stderr, "ERROR: could not access file (%s).\n", file); 00047 goto error; 00048 } 00049 00050 eet_close(ef); 00051 00052 /* Decrypt an eet file. */ 00053 ef = eet_open(file, EET_FILE_MODE_READ); 00054 if (!ef) 00055 { 00056 fprintf( 00057 stderr, "ERROR: could not access file (%s).\n", file); 00058 goto error; 00059 } 00060 00061 test = eet_read_cipher(ef, "keys/tests", &size, key); 00062 if (!test) 00063 { 00064 fprintf( 00065 stderr, "ERROR: could decript contents on file %s, with key %s.\n", 00066 file, key); 00067 goto error; 00068 } 00069 00070 if (size != (int)strlen(buffer) + 1) 00071 { 00072 fprintf( 00073 stderr, "ERROR: something is wrong with the decripted data\n"); 00074 goto error; 00075 } 00076 00077 if (memcmp(test, buffer, strlen(buffer) + 1) != 0) 00078 { 00079 fprintf( 00080 stderr, "ERROR: something is wrong with the decripted data\n"); 00081 goto error; 00082 } 00083 00084 eet_close(ef); 00085 00086 /* Decrypt an eet file, now using our BAD key!! */ 00087 ef = eet_open(file, EET_FILE_MODE_READ); 00088 if (!ef) 00089 { 00090 fprintf( 00091 stderr, "ERROR: could not access file (%s).\n", file); 00092 goto error; 00093 } 00094 00095 test = eet_read_cipher(ef, "keys/tests", &size, key_bad); 00096 00097 if (size == (int)strlen(buffer) + 1) 00098 if (memcmp(test, buffer, strlen(buffer) + 1) == 0) 00099 { 00100 fprintf( 00101 stderr, "ERROR: something is wrong with the contents of %s, as" 00102 " we accessed it with a different key and it decripted our" 00103 " information right.\n", file); 00104 goto error; 00105 } 00106 00107 eet_close(ef); 00108 00109 error: 00110 if (unlink(file) != 0) 00111 { 00112 fprintf( 00113 stderr, "ERROR: could not unlink file (%s).\n", file); 00114 } 00115 00116 panic: 00117 eet_shutdown(); 00118 } 00119