In this example, we exemplify the usage of \hyperlink{group__Eet__File__Cipher__Group_ga0c08a5881d6fd3474a8cbe03a445aff3}{eet\-\_\-write\-\_\-cipher()} and \hyperlink{group__Eet__File__Cipher__Group_gafde124ec4aa3d6ad934e7cc43c24ae3a}{eet\-\_\-read\-\_\-cipher()}.

For it to work, {\bfseries make sure} to have your Eet installation with a ciphering backend enabled.

We start by defining the information to record in an Eet file ({\ttfamily buffer}), the key to cipher that ({\ttfamily key}) and a dummy wrong key to try to access that information, later ({\ttfamily key\-\_\-bad}).  
\begin{DoxyCodeInclude}
   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *buffer = \textcolor{stringliteral}{"Here is a string of data to save !"};
   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *key = \textcolor{stringliteral}{"This is a crypto key"};
   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *key\_bad = \textcolor{stringliteral}{"This is another crypto key"};

\end{DoxyCodeInclude}


After opening our file, we simply use the first cited function to write our string ciphered\-:  
\begin{DoxyCodeInclude}
   ef = \hyperlink{group__Eet__File__Group_ga5f55956b860aa2c77af9b1a880c4c256}{eet\_open}(file, \hyperlink{group__Eet__File__Group_ggaa8e1a7af50fefaf15eb2f69543d24998a8f395f56fba48be007c65be51d799ee9}{EET\_FILE\_MODE\_WRITE});
   \textcolor{keywordflow}{if} (!ef)
     \{
        fprintf(
          stderr, \textcolor{stringliteral}{"ERROR: could not access file (%s).\(\backslash\)n"}, file);
        \textcolor{keywordflow}{goto} error;
     \}

   \textcolor{keywordflow}{if} (!\hyperlink{group__Eet__File__Cipher__Group_ga0c08a5881d6fd3474a8cbe03a445aff3}{eet\_write\_cipher}(ef, \textcolor{stringliteral}{"keys/tests"}, buffer, strlen(buffer) + 1, 0, key))
     \{
        fprintf(
          stderr, \textcolor{stringliteral}{"ERROR: could not access file (%s).\(\backslash\)n"}, file);
        \textcolor{keywordflow}{goto} error;
     \}

   \hyperlink{group__Eet__File__Group_ga1675fef1f6012c1e02ae313d13dc3e03}{eet\_close}(ef);

\end{DoxyCodeInclude}


Then, after closing it on purpose, we open it again, to retrieve the encrypted information back, in a readable format\-: 
\begin{DoxyCodeInclude}
   ef = \hyperlink{group__Eet__File__Group_ga5f55956b860aa2c77af9b1a880c4c256}{eet\_open}(file, \hyperlink{group__Eet__File__Group_ggaa8e1a7af50fefaf15eb2f69543d24998a416f7e9a76b8131a44bbeedb13060c81}{EET\_FILE\_MODE\_READ});
   \textcolor{keywordflow}{if} (!ef)
     \{
        fprintf(
          stderr, \textcolor{stringliteral}{"ERROR: could not access file (%s).\(\backslash\)n"}, file);
        \textcolor{keywordflow}{goto} error;
     \}

   test = \hyperlink{group__Eet__File__Cipher__Group_gafde124ec4aa3d6ad934e7cc43c24ae3a}{eet\_read\_cipher}(ef, \textcolor{stringliteral}{"keys/tests"}, &size, key);
   \textcolor{keywordflow}{if} (!test)
     \{
        fprintf(
          stderr, \textcolor{stringliteral}{"ERROR: could decript contents on file %s, with key %s.\(\backslash\)n"},
          file, key);
        \textcolor{keywordflow}{goto} error;
     \}

   \textcolor{keywordflow}{if} (size != (\textcolor{keywordtype}{int})strlen(buffer) + 1)
     \{
        fprintf(
          stderr, \textcolor{stringliteral}{"ERROR: something is wrong with the decripted data\(\backslash\)n"});
        \textcolor{keywordflow}{goto} error;
     \}

   \textcolor{keywordflow}{if} (memcmp(test, buffer, strlen(buffer) + 1) != 0)
     \{
        fprintf(
          stderr, \textcolor{stringliteral}{"ERROR: something is wrong with the decripted data\(\backslash\)n"});
        \textcolor{keywordflow}{goto} error;
     \}

   \hyperlink{group__Eet__File__Group_ga1675fef1f6012c1e02ae313d13dc3e03}{eet\_close}(ef);

   \textcolor{comment}{/* Decrypt an eet file, now using our BAD key!! */}
   ef = \hyperlink{group__Eet__File__Group_ga5f55956b860aa2c77af9b1a880c4c256}{eet\_open}(file, \hyperlink{group__Eet__File__Group_ggaa8e1a7af50fefaf15eb2f69543d24998a416f7e9a76b8131a44bbeedb13060c81}{EET\_FILE\_MODE\_READ});
   \textcolor{keywordflow}{if} (!ef)
     \{
        fprintf(
          stderr, \textcolor{stringliteral}{"ERROR: could not access file (%s).\(\backslash\)n"}, file);
        \textcolor{keywordflow}{goto} error;
     \}

   test = \hyperlink{group__Eet__File__Cipher__Group_gafde124ec4aa3d6ad934e7cc43c24ae3a}{eet\_read\_cipher}(ef, \textcolor{stringliteral}{"keys/tests"}, &size, key\_bad);

   \textcolor{keywordflow}{if} (size == (\textcolor{keywordtype}{int})strlen(buffer) + 1)
     \textcolor{keywordflow}{if} (memcmp(test, buffer, strlen(buffer) + 1) == 0)
       \{
          fprintf(
            stderr, \textcolor{stringliteral}{"ERROR: something is wrong with the contents of %s, as"}
                    \textcolor{stringliteral}{" we accessed it with a different key and it decripted our"}
                    \textcolor{stringliteral}{" information right.\(\backslash\)n"}, file);
          \textcolor{keywordflow}{goto} error;
       \}

   \hyperlink{group__Eet__File__Group_ga1675fef1f6012c1e02ae313d13dc3e03}{eet\_close}(ef);

\end{DoxyCodeInclude}


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 {\ttfamily buffer}, something wrong is going on (we made it to fail on purpose). The former access is O\-K, and must work.

What we do in sequence is just to delete the file. The complete code of the example follows.


\begin{DoxyCodeInclude}
1 \textcolor{comment}{//Compile with:}
2 \textcolor{comment}{// gcc -o eet-data-cipher\_decipher eet-data-cipher\_decipher.c `pkg-config --cflags --libs eet eina`}
3 
4 \textcolor{preprocessor}{#include <\hyperlink{Eina_8h}{Eina.h}>}
5 \textcolor{preprocessor}{#include <\hyperlink{Eet_8h}{Eet.h}>}
6 \textcolor{preprocessor}{#include <stdio.h>}
7 \textcolor{preprocessor}{#include <limits.h>}
8 \textcolor{preprocessor}{#include <sys/types.h>}
9 \textcolor{preprocessor}{#include <sys/stat.h>}
10 \textcolor{preprocessor}{#include <unistd.h>}
11 
12 \textcolor{keywordtype}{int}
13 main(\textcolor{keywordtype}{void})
14 \{
15    \textcolor{keyword}{const} \textcolor{keywordtype}{char} *buffer = \textcolor{stringliteral}{"Here is a string of data to save !"};
16    \textcolor{keyword}{const} \textcolor{keywordtype}{char} *key = \textcolor{stringliteral}{"This is a crypto key"};
17    \textcolor{keyword}{const} \textcolor{keywordtype}{char} *key\_bad = \textcolor{stringliteral}{"This is another crypto key"};
18 
19    \textcolor{keywordtype}{char} *file = strdup(\textcolor{stringliteral}{"/tmp/eet\_cipher\_example\_XXXXX"});
20    \hyperlink{group__Eet__File__Group_ga8d9779184a9870c1a225f1f40c76e8a7}{Eet\_File} *ef;
21    \textcolor{keywordtype}{char} *test;
22    \textcolor{keywordtype}{int} size;
23 
24    \hyperlink{group__Eet__Group_ga9903687bfa20fe9a9e59d4c1d213e89a}{eet\_init}();
25 
26    \textcolor{keywordflow}{if} (!(file = tmpnam(file)))
27      \{
28         fprintf(
29           stderr, \textcolor{stringliteral}{"ERROR: could not create temporary file (%s).\(\backslash\)n"}, file);
30         \textcolor{keywordflow}{goto} panic;
31      \}
32 
33    \textcolor{comment}{/* Crypt an eet file. */}
34    ef = \hyperlink{group__Eet__File__Group_ga5f55956b860aa2c77af9b1a880c4c256}{eet\_open}(file, \hyperlink{group__Eet__File__Group_ggaa8e1a7af50fefaf15eb2f69543d24998a8f395f56fba48be007c65be51d799ee9}{EET\_FILE\_MODE\_WRITE});
35    \textcolor{keywordflow}{if} (!ef)
36      \{
37         fprintf(
38           stderr, \textcolor{stringliteral}{"ERROR: could not access file (%s).\(\backslash\)n"}, file);
39         \textcolor{keywordflow}{goto} error;
40      \}
41 
42    \textcolor{keywordflow}{if} (!\hyperlink{group__Eet__File__Cipher__Group_ga0c08a5881d6fd3474a8cbe03a445aff3}{eet\_write\_cipher}(ef, \textcolor{stringliteral}{"keys/tests"}, buffer, strlen(buffer) + 1, 0, key))
43      \{
44         fprintf(
45           stderr, \textcolor{stringliteral}{"ERROR: could not access file (%s).\(\backslash\)n"}, file);
46         \textcolor{keywordflow}{goto} error;
47      \}
48 
49    \hyperlink{group__Eet__File__Group_ga1675fef1f6012c1e02ae313d13dc3e03}{eet\_close}(ef);
50 
51    \textcolor{comment}{/* Decrypt an eet file. */}
52    ef = \hyperlink{group__Eet__File__Group_ga5f55956b860aa2c77af9b1a880c4c256}{eet\_open}(file, \hyperlink{group__Eet__File__Group_ggaa8e1a7af50fefaf15eb2f69543d24998a416f7e9a76b8131a44bbeedb13060c81}{EET\_FILE\_MODE\_READ});
53    \textcolor{keywordflow}{if} (!ef)
54      \{
55         fprintf(
56           stderr, \textcolor{stringliteral}{"ERROR: could not access file (%s).\(\backslash\)n"}, file);
57         \textcolor{keywordflow}{goto} error;
58      \}
59 
60    test = \hyperlink{group__Eet__File__Cipher__Group_gafde124ec4aa3d6ad934e7cc43c24ae3a}{eet\_read\_cipher}(ef, \textcolor{stringliteral}{"keys/tests"}, &size, key);
61    \textcolor{keywordflow}{if} (!test)
62      \{
63         fprintf(
64           stderr, \textcolor{stringliteral}{"ERROR: could decript contents on file %s, with key %s.\(\backslash\)n"},
65           file, key);
66         \textcolor{keywordflow}{goto} error;
67      \}
68 
69    \textcolor{keywordflow}{if} (size != (\textcolor{keywordtype}{int})strlen(buffer) + 1)
70      \{
71         fprintf(
72           stderr, \textcolor{stringliteral}{"ERROR: something is wrong with the decripted data\(\backslash\)n"});
73         \textcolor{keywordflow}{goto} error;
74      \}
75 
76    \textcolor{keywordflow}{if} (memcmp(test, buffer, strlen(buffer) + 1) != 0)
77      \{
78         fprintf(
79           stderr, \textcolor{stringliteral}{"ERROR: something is wrong with the decripted data\(\backslash\)n"});
80         \textcolor{keywordflow}{goto} error;
81      \}
82 
83    \hyperlink{group__Eet__File__Group_ga1675fef1f6012c1e02ae313d13dc3e03}{eet\_close}(ef);
84 
85    \textcolor{comment}{/* Decrypt an eet file, now using our BAD key!! */}
86    ef = \hyperlink{group__Eet__File__Group_ga5f55956b860aa2c77af9b1a880c4c256}{eet\_open}(file, \hyperlink{group__Eet__File__Group_ggaa8e1a7af50fefaf15eb2f69543d24998a416f7e9a76b8131a44bbeedb13060c81}{EET\_FILE\_MODE\_READ});
87    \textcolor{keywordflow}{if} (!ef)
88      \{
89         fprintf(
90           stderr, \textcolor{stringliteral}{"ERROR: could not access file (%s).\(\backslash\)n"}, file);
91         \textcolor{keywordflow}{goto} error;
92      \}
93 
94    test = \hyperlink{group__Eet__File__Cipher__Group_gafde124ec4aa3d6ad934e7cc43c24ae3a}{eet\_read\_cipher}(ef, \textcolor{stringliteral}{"keys/tests"}, &size, key\_bad);
95 
96    \textcolor{keywordflow}{if} (size == (\textcolor{keywordtype}{int})strlen(buffer) + 1)
97      \textcolor{keywordflow}{if} (memcmp(test, buffer, strlen(buffer) + 1) == 0)
98        \{
99           fprintf(
100             stderr, \textcolor{stringliteral}{"ERROR: something is wrong with the contents of %s, as"}
101                     \textcolor{stringliteral}{" we accessed it with a different key and it decripted our"}
102                     \textcolor{stringliteral}{" information right.\(\backslash\)n"}, file);
103           \textcolor{keywordflow}{goto} error;
104        \}
105 
106    \hyperlink{group__Eet__File__Group_ga1675fef1f6012c1e02ae313d13dc3e03}{eet\_close}(ef);
107 
108 error:
109    \textcolor{keywordflow}{if} (unlink(file) != 0)
110      \{
111         fprintf(
112           stderr, \textcolor{stringliteral}{"ERROR: could not unlink file (%s).\(\backslash\)n"}, file);
113      \}
114 
115 panic:
116    \hyperlink{group__Eet__Group_gacbc7583179399d3b3e7b003e911f866f}{eet\_shutdown}();
117 \}
118 
\end{DoxyCodeInclude}
 