Tizen Native API  3.0
Eet data cipher/decipher example

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 //Compile with:
00002 // gcc -o eet-data-cipher_decipher eet-data-cipher_decipher.c `pkg-config --cflags --libs eet eina`
00003 
00004 #include <Eina.h>
00005 #include <Eet.h>
00006 #include <stdio.h>
00007 #include <limits.h>
00008 #include <sys/types.h>
00009 #include <sys/stat.h>
00010 #include <unistd.h>
00011 #include <string.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_XXXXXX");
00021    Eet_File *ef;
00022    char *test;
00023    int size;
00024    int tmpfd;
00025 
00026    eet_init();
00027 
00028    if (-1 == (tmpfd = mkstemp(file)) || !!close(tmpfd))
00029      {
00030         fprintf(
00031                 stderr, "ERROR: could not create temporary file (%s) : %s\n",
00032                 file, strerror(errno));
00033         goto panic;
00034      }
00035 
00036    /* Crypt an eet file. */
00037    ef = eet_open(file, EET_FILE_MODE_WRITE);
00038    if (!ef)
00039      {
00040         fprintf(
00041           stderr, "ERROR: could not access file (%s).\n", file);
00042         goto error;
00043      }
00044 
00045    if (!eet_write_cipher(ef, "keys/tests", buffer, strlen(buffer) + 1, 0, key))
00046      {
00047         fprintf(
00048           stderr, "ERROR: could not access file (%s).\n", file);
00049         goto error;
00050      }
00051 
00052    eet_close(ef);
00053 
00054    /* Decrypt an eet file. */
00055    ef = eet_open(file, EET_FILE_MODE_READ);
00056    if (!ef)
00057      {
00058         fprintf(
00059           stderr, "ERROR: could not access file (%s).\n", file);
00060         goto error;
00061      }
00062 
00063    test = eet_read_cipher(ef, "keys/tests", &size, key);
00064    if (!test)
00065      {
00066         fprintf(
00067           stderr, "ERROR: could decript contents on file %s, with key %s.\n",
00068           file, key);
00069         goto error;
00070      }
00071 
00072    if (size != (int)strlen(buffer) + 1)
00073      {
00074         fprintf(
00075           stderr, "ERROR: something is wrong with the decripted data\n");
00076         goto error;
00077      }
00078 
00079    if (memcmp(test, buffer, strlen(buffer) + 1) != 0)
00080      {
00081         fprintf(
00082           stderr, "ERROR: something is wrong with the decripted data\n");
00083         goto error;
00084      }
00085 
00086    eet_close(ef);
00087 
00088    /* Decrypt an eet file, now using our BAD key!! */
00089    ef = eet_open(file, EET_FILE_MODE_READ);
00090    if (!ef)
00091      {
00092         fprintf(
00093           stderr, "ERROR: could not access file (%s).\n", file);
00094         goto error;
00095      }
00096 
00097    test = eet_read_cipher(ef, "keys/tests", &size, key_bad);
00098 
00099    if (size == (int)strlen(buffer) + 1)
00100      if (memcmp(test, buffer, strlen(buffer) + 1) == 0)
00101        {
00102           fprintf(
00103             stderr, "ERROR: something is wrong with the contents of %s, as"
00104                     " we accessed it with a different key and it decripted our"
00105                     " information right.\n", file);
00106           goto error;
00107        }
00108 
00109    eet_close(ef);
00110 
00111 error:
00112    if (unlink(file) != 0)
00113      {
00114         fprintf(
00115           stderr, "ERROR: could not unlink file (%s).\n", file);
00116      }
00117 
00118 panic:
00119    eet_shutdown();
00120 }
00121