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 {
12 border: solid 1px #aaaaaa;
17 border: solid 1px #aaaaff;
26 border: solid 1px #ffaaaa;
33 .navigation a:visited {
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>
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>
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>
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>
66 <td><a href="http://xmlsoft.org/"><img src="../images/libxml2-logo.png" alt="LibXML2" border="0"></a></td>
70 <td><a href="http://xmlsoft.org/XSLT"><img src="../images/libxslt-logo.png" alt="LibXSLT" border="0"></a></td>
74 <td><a href="http://www.openssl.org/"><img src="../images/openssl-logo.png" alt="OpenSSL" border="0"></a></td>
76 <!--Links - start--><!--Links - end-->
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>
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>
92 <div class="INFORMALEXAMPLE">
94 <a name="AEN800"></a><pre class="PROGRAMLISTING">/**
95 * XML Security Library example: Decrypting an encrypted file using a single key.
97 * Decrypts encrypted XML file using a single DES key from a binary file
100 * ./decrypt1 <xml-enc> <des-key-file>
103 * ./decrypt1 encrypt1-res.xml deskey.bin
104 * ./decrypt1 encrypt2-res.xml deskey.bin
106 * This is free software; see Copyright file in the source
107 * distribution for preciese wording.
109 * Copyright (C) 2002-2003 Aleksey Sanin <aleksey@aleksey.com>
111 #include <stdlib.h>
112 #include <string.h>
113 #include <assert.h>
115 #include <libxml/tree.h>
116 #include <libxml/xmlmemory.h>
117 #include <libxml/parser.h>
119 #ifndef XMLSEC_NO_XSLT
120 #include <libxslt/xslt.h>
121 #endif /* XMLSEC_NO_XSLT */
123 #include <xmlsec/xmlsec.h>
124 #include <xmlsec/xmltree.h>
125 #include <xmlsec/xmlenc.h>
126 #include <xmlsec/crypto.h>
128 int decrypt_file(const char* enc_file, const char* key_file);
131 main(int argc, char **argv) {
135 fprintf(stderr, "Error: wrong number of arguments.\n");
136 fprintf(stderr, "Usage: %s <enc-file> <key-file>\n", argv[0]);
140 /* Init libxml and libxslt libraries */
143 xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
144 xmlSubstituteEntitiesDefault(1);
145 #ifndef XMLSEC_NO_XSLT
146 xmlIndentTreeOutput = 1;
147 #endif /* XMLSEC_NO_XSLT */
149 /* Init xmlsec library */
150 if(xmlSecInit() < 0) {
151 fprintf(stderr, "Error: xmlsec initialization failed.\n");
155 /* Check loaded library version */
156 if(xmlSecCheckVersion() != 1) {
157 fprintf(stderr, "Error: loaded xmlsec library version is not compatible.\n");
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.
166 #ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING
167 if(xmlSecCryptoDLLoadLibrary(BAD_CAST XMLSEC_CRYPTO) < 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");
173 #endif /* XMLSEC_CRYPTO_DYNAMIC_LOADING */
175 /* Init crypto library */
176 if(xmlSecCryptoAppInit(NULL) < 0) {
177 fprintf(stderr, "Error: crypto initialization failed.\n");
181 /* Init xmlsec-crypto library */
182 if(xmlSecCryptoInit() < 0) {
183 fprintf(stderr, "Error: xmlsec-crypto initialization failed.\n");
187 if(decrypt_file(argv[1], argv[2]) < 0) {
191 /* Shutdown xmlsec-crypto library */
192 xmlSecCryptoShutdown();
194 /* Shutdown crypto library */
195 xmlSecCryptoAppShutdown();
197 /* Shutdown xmlsec library */
200 /* Shutdown libxslt/libxml */
201 #ifndef XMLSEC_NO_XSLT
202 xsltCleanupGlobals();
203 #endif /* XMLSEC_NO_XSLT */
211 * @enc_file: the encrypted XML file name.
212 * @key_file: the Triple DES key file.
214 * Decrypts the XML file #enc_file using DES key from #key_file and
215 * prints results to stdout.
217 * Returns 0 on success or a negative value if an error occurs.
220 decrypt_file(const char* enc_file, const char* key_file) {
221 xmlDocPtr doc = NULL;
222 xmlNodePtr node = NULL;
223 xmlSecEncCtxPtr encCtx = NULL;
230 doc = xmlParseFile(enc_file);
231 if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){
232 fprintf(stderr, "Error: unable to parse file \"%s\"\n", enc_file);
236 /* find start node */
237 node = xmlSecFindNode(xmlDocGetRootElement(doc), xmlSecNodeEncryptedData, xmlSecEncNs);
239 fprintf(stderr, "Error: start node not found in \"%s\"\n", enc_file);
243 /* create encryption context, we don't need keys manager in this example */
244 encCtx = xmlSecEncCtxCreate(NULL);
246 fprintf(stderr,"Error: failed to create encryption context\n");
251 encCtx->encKey = xmlSecKeyReadBinaryFile(xmlSecKeyDataDesId, key_file);
252 if(encCtx->encKey == NULL) {
253 fprintf(stderr,"Error: failed to load des key from binary file \"%s\"\n", key_file);
257 /* set key name to the file name, this is just an example! */
258 if(xmlSecKeySetName(encCtx->encKey, key_file) < 0) {
259 fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", key_file);
263 /* decrypt the data */
264 if((xmlSecEncCtxDecrypt(encCtx, node) < 0) || (encCtx->result == NULL)) {
265 fprintf(stderr,"Error: decryption failed\n");
269 /* print decrypted data to stdout */
270 if(encCtx->resultReplaced != 0) {
271 fprintf(stdout, "Decrypted XML data:\n");
272 xmlDocDump(stdout, doc);
274 fprintf(stdout, "Decrypted binary data (%d bytes):\n", xmlSecBufferGetSize(encCtx->result));
275 if(xmlSecBufferGetData(encCtx->result) != NULL) {
276 fwrite(xmlSecBufferGetData(encCtx->result),
278 xmlSecBufferGetSize(encCtx->result),
282 fprintf(stdout, "\n");
290 xmlSecEncCtxDestroy(encCtx);
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><<< 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. >>></b></a></td>
306 </td></tr></table></td>