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