Git init
[external/xmlsec1.git] / docs / api / xmlsec-decrypt-with-signle-key.html
1 <html>
2 <head>
3 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
4 <title>Decrypting data with a single key.</title>
5 <meta name="GENERATOR" content="Modular DocBook HTML Stylesheet Version 1.79">
6 <link rel="HOME" title="XML Security Library Reference Manual" href="index.html">
7 <link rel="UP" title="Examples." href="xmlsec-examples.html">
8 <link rel="PREVIOUS" title="Encrypting data with a session key." href="xmlsec-encrypt-with-session-key.html">
9 <link rel="NEXT" title="Decrypting data with keys manager." href="xmlsec-decrypt-with-keys-mngr.html">
10 <style type="text/css">.synopsis, .classsynopsis {
11     background: #eeeeee;
12     border: solid 1px #aaaaaa;
13     padding: 0.5em;
14 }
15 .programlisting {
16     background: #eeeeff;
17     border: solid 1px #aaaaff;
18     padding: 0.5em;
19 }
20 .variablelist {
21     padding: 4px;
22     margin-left: 3em;
23 }
24 .navigation {
25     background: #ffeeee;
26     border: solid 1px #ffaaaa;
27     margin-top: 0.5em;
28     margin-bottom: 0.5em;
29 }
30 .navigation a {
31     color: #770000;
32 }
33 .navigation a:visited {
34     color: #550000;
35 }
36 .navigation .title {
37     font-size: 200%;
38 }</style>
39 </head>
40 <body><table witdh="100%" valign="top"><tr valign="top">
41 <td valign="top" align="left" width="210">
42 <img src="../images/logo.gif" alt="XML Security Library" border="0"><p></p>
43 <ul>
44 <li><a href="../index.html">Home</a></li>
45 <li><a href="../download.html">Download</a></li>
46 <li><a href="../news.html">News</a></li>
47 <li><a href="../documentation.html">Documentation</a></li>
48 <ul>
49 <li><a href="../faq.html">FAQ</a></li>
50 <li><a href="../api/xmlsec-notes.html">Tutorial</a></li>
51 <li><a href="../api/xmlsec-reference.html">API reference</a></li>
52 <li><a href="../api/xmlsec-examples.html">Examples</a></li>
53 </ul>
54 <li><a href="../xmldsig.html">XML Digital Signature</a></li>
55 <ul><li><a href="http://www.aleksey.com/xmlsec/xmldsig-verifier.html">Online Verifier</a></li></ul>
56 <li><a href="../xmlenc.html">XML Encryption</a></li>
57 <li><a href="../c14n.html">XML Canonicalization</a></li>
58 <li><a href="../bugs.html">Reporting Bugs</a></li>
59 <li><a href="http://www.aleksey.com/pipermail/xmlsec">Mailing list</a></li>
60 <li><a href="../related.html">Related</a></li>
61 <li><a href="../authors.html">Authors</a></li>
62 </ul>
63 <table width="100%">
64 <tr>
65 <td width="15"></td>
66 <td><a href="http://xmlsoft.org/"><img src="../images/libxml2-logo.png" alt="LibXML2" border="0"></a></td>
67 </tr>
68 <tr>
69 <td width="15"></td>
70 <td><a href="http://xmlsoft.org/XSLT"><img src="../images/libxslt-logo.png" alt="LibXSLT" border="0"></a></td>
71 </tr>
72 <tr>
73 <td width="15"></td>
74 <td><a href="http://www.openssl.org/"><img src="../images/openssl-logo.png" alt="OpenSSL" border="0"></a></td>
75 </tr>
76 <!--Links - start--><!--Links - end-->
77 </table>
78 </td>
79 <td valign="top"><table width="100%" valign="top"><tr><td valign="top" align="left" id="xmlsecContent">
80 <table width="100%" class="navigation" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle">
81 <td><a accesskey="p" href="xmlsec-encrypt-with-session-key.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
82 <td><a accesskey="u" href="xmlsec-examples.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
83 <td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
84 <th width="100%" align="center">XML Security Library Reference Manual</th>
85 <td><a accesskey="n" href="xmlsec-decrypt-with-keys-mngr.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
86 </tr></table>
87 <br clear="all"><div class="SECT1">
88 <h1 class="SECT1"><a name="XMLSEC-DECRYPT-WITH-SIGNLE-KEY">Decrypting data with a single key.</a></h1>
89 <br clear="all"><div class="SECT2">
90 <h2 class="SECT2"><a name="XMLSEC-EXAMPLE-DECRYPT1">decrypt1.c</a></h2>
91 <p></p>
92 <div class="INFORMALEXAMPLE">
93 <p></p>
94 <a name="AEN800"></a><pre class="PROGRAMLISTING">/** 
95  * XML Security Library example: Decrypting an encrypted file using a single key.
96  * 
97  * Decrypts encrypted XML file using a single DES key from a binary file
98  * 
99  * Usage: 
100  *      ./decrypt1 &lt;xml-enc&gt; &lt;des-key-file&gt; 
101  *
102  * Example:
103  *      ./decrypt1 encrypt1-res.xml deskey.bin
104  *      ./decrypt1 encrypt2-res.xml deskey.bin
105  *
106  * This is free software; see Copyright file in the source
107  * distribution for preciese wording.
108  * 
109  * Copyright (C) 2002-2003 Aleksey Sanin &lt;aleksey@aleksey.com&gt;
110  */
111 #include &lt;stdlib.h&gt;
112 #include &lt;string.h&gt;
113 #include &lt;assert.h&gt;
114
115 #include &lt;libxml/tree.h&gt;
116 #include &lt;libxml/xmlmemory.h&gt;
117 #include &lt;libxml/parser.h&gt;
118
119 #ifndef XMLSEC_NO_XSLT
120 #include &lt;libxslt/xslt.h&gt;
121 #endif /* XMLSEC_NO_XSLT */
122
123 #include &lt;xmlsec/xmlsec.h&gt;
124 #include &lt;xmlsec/xmltree.h&gt;
125 #include &lt;xmlsec/xmlenc.h&gt;
126 #include &lt;xmlsec/crypto.h&gt;
127
128 int decrypt_file(const char* enc_file, const char* key_file);
129
130 int 
131 main(int argc, char **argv) {
132     assert(argv);
133
134     if(argc != 3) {
135         fprintf(stderr, "Error: wrong number of arguments.\n");
136         fprintf(stderr, "Usage: %s &lt;enc-file&gt; &lt;key-file&gt;\n", argv[0]);
137         return(1);
138     }
139
140     /* Init libxml and libxslt libraries */
141     xmlInitParser();
142     LIBXML_TEST_VERSION
143     xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
144     xmlSubstituteEntitiesDefault(1);
145 #ifndef XMLSEC_NO_XSLT
146     xmlIndentTreeOutput = 1; 
147 #endif /* XMLSEC_NO_XSLT */
148                 
149     /* Init xmlsec library */
150     if(xmlSecInit() &lt; 0) {
151         fprintf(stderr, "Error: xmlsec initialization failed.\n");
152         return(-1);
153     }
154
155     /* Check loaded library version */
156     if(xmlSecCheckVersion() != 1) {
157         fprintf(stderr, "Error: loaded xmlsec library version is not compatible.\n");
158         return(-1);
159     }
160
161     /* Load default crypto engine if we are supporting dynamic
162      * loading for xmlsec-crypto libraries. Use the crypto library
163      * name ("openssl", "nss", etc.) to load corresponding 
164      * xmlsec-crypto library.
165      */
166 #ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING
167     if(xmlSecCryptoDLLoadLibrary(BAD_CAST XMLSEC_CRYPTO) &lt; 0) {
168         fprintf(stderr, "Error: unable to load default xmlsec-crypto library. Make sure\n"
169                         "that you have it installed and check shared libraries path\n"
170                         "(LD_LIBRARY_PATH) envornment variable.\n");
171         return(-1);     
172     }
173 #endif /* XMLSEC_CRYPTO_DYNAMIC_LOADING */
174
175     /* Init crypto library */
176     if(xmlSecCryptoAppInit(NULL) &lt; 0) {
177         fprintf(stderr, "Error: crypto initialization failed.\n");
178         return(-1);
179     }
180
181     /* Init xmlsec-crypto library */
182     if(xmlSecCryptoInit() &lt; 0) {
183         fprintf(stderr, "Error: xmlsec-crypto initialization failed.\n");
184         return(-1);
185     }
186
187     if(decrypt_file(argv[1], argv[2]) &lt; 0) {
188         return(-1);
189     }    
190     
191     /* Shutdown xmlsec-crypto library */
192     xmlSecCryptoShutdown();
193     
194     /* Shutdown crypto library */
195     xmlSecCryptoAppShutdown();
196     
197     /* Shutdown xmlsec library */
198     xmlSecShutdown();
199
200     /* Shutdown libxslt/libxml */
201 #ifndef XMLSEC_NO_XSLT
202     xsltCleanupGlobals();            
203 #endif /* XMLSEC_NO_XSLT */
204     xmlCleanupParser();
205     
206     return(0);
207 }
208
209 /**
210  * decrypt_file:
211  * @enc_file:           the encrypted XML  file name.
212  * @key_file:           the Triple DES key file.
213  *
214  * Decrypts the XML file #enc_file using DES key from #key_file and 
215  * prints results to stdout.
216  *
217  * Returns 0 on success or a negative value if an error occurs.
218  */
219 int 
220 decrypt_file(const char* enc_file, const char* key_file) {
221     xmlDocPtr doc = NULL;
222     xmlNodePtr node = NULL;
223     xmlSecEncCtxPtr encCtx = NULL;
224     int res = -1;
225     
226     assert(enc_file);
227     assert(key_file);
228
229     /* load template */
230     doc = xmlParseFile(enc_file);
231     if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){
232         fprintf(stderr, "Error: unable to parse file \"%s\"\n", enc_file);
233         goto done;      
234     }
235     
236     /* find start node */
237     node = xmlSecFindNode(xmlDocGetRootElement(doc), xmlSecNodeEncryptedData, xmlSecEncNs);
238     if(node == NULL) {
239         fprintf(stderr, "Error: start node not found in \"%s\"\n", enc_file);
240         goto done;      
241     }
242
243     /* create encryption context, we don't need keys manager in this example */
244     encCtx = xmlSecEncCtxCreate(NULL);
245     if(encCtx == NULL) {
246         fprintf(stderr,"Error: failed to create encryption context\n");
247         goto done;
248     }
249
250     /* load DES key */
251     encCtx-&gt;encKey = xmlSecKeyReadBinaryFile(xmlSecKeyDataDesId, key_file);
252     if(encCtx-&gt;encKey == NULL) {
253         fprintf(stderr,"Error: failed to load des key from binary file \"%s\"\n", key_file);
254         goto done;
255     }
256     
257     /* set key name to the file name, this is just an example! */
258     if(xmlSecKeySetName(encCtx-&gt;encKey, key_file) &lt; 0) {
259         fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", key_file);
260         goto done;
261     }
262
263     /* decrypt the data */
264     if((xmlSecEncCtxDecrypt(encCtx, node) &lt; 0) || (encCtx-&gt;result == NULL)) {
265         fprintf(stderr,"Error: decryption failed\n");
266         goto done;
267     }
268         
269     /* print decrypted data to stdout */
270     if(encCtx-&gt;resultReplaced != 0) {
271         fprintf(stdout, "Decrypted XML data:\n");
272         xmlDocDump(stdout, doc);
273     } else {
274         fprintf(stdout, "Decrypted binary data (%d bytes):\n", xmlSecBufferGetSize(encCtx-&gt;result));
275         if(xmlSecBufferGetData(encCtx-&gt;result) != NULL) {
276             fwrite(xmlSecBufferGetData(encCtx-&gt;result), 
277                   1, 
278                   xmlSecBufferGetSize(encCtx-&gt;result),
279                   stdout);
280         }
281     }
282     fprintf(stdout, "\n");
283         
284     /* success */
285     res = 0;
286
287 done:    
288     /* cleanup */
289     if(encCtx != NULL) {
290         xmlSecEncCtxDestroy(encCtx);
291     }
292     
293     if(doc != NULL) {
294         xmlFreeDoc(doc); 
295     }
296     return(res);
297 }\r</pre>
298 <p></p>
299 </div>
300 </div>
301 </div>
302 <table class="navigation" width="100%" summary="Navigation footer" cellpadding="2" cellspacing="2"><tr valign="middle">
303 <td align="left"><a accesskey="p" href="xmlsec-encrypt-with-session-key.html"><b>&lt;&lt;&lt; Encrypting data with a session key.</b></a></td>
304 <td align="right"><a accesskey="n" href="xmlsec-decrypt-with-keys-mngr.html"><b>Decrypting data with keys manager. &gt;&gt;&gt;</b></a></td>
305 </tr></table>
306 </td></tr></table></td>
307 </tr></table></body>
308 </html>