3 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
4 <title>Decrypting an encrypted document</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="Verifing and decrypting documents." href="xmlsec-notes-verify-decrypt.html">
8 <link rel="PREVIOUS" title="Verifying a signed document" href="xmlsec-notes-verify.html">
9 <link rel="NEXT" title="Keys." href="xmlsec-notes-keys.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-notes-verify.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
82 <td><a accesskey="u" href="xmlsec-notes-verify-decrypt.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-notes-keys.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-NOTES-DECRYPT">Decrypting an encrypted document</a></h1>
89 <p>The typical decryption process includes following steps:
93 <li><p> Load keys, X509 certificates, etc. in the <a href="xmlsec-keysmngr.html#XMLSECKEYSMNGR">keys manager</a> .
95 <li><p> Create encryption context <a href="xmlsec-xmlenc.html#XMLSECENCCTX">xmlSecEncCtx</a>
96 using <a href="xmlsec-xmlenc.html#XMLSECENCCTXCREATE">xmlSecEncCtxCreate</a> or
97 <a href="xmlsec-xmlenc.html#XMLSECENCCTXINITIALIZE">xmlSecEncCtxInitialize</a>
100 <li><p> Select start decryption <enc:EncryptedData> node.
102 <li><p> Decrypt by calling <a href="xmlsec-xmlenc.html#XMLSECENCCTXDECRYPT">xmlSecencCtxDecrypt</a>
105 <li><p> Check returned value and if necessary consume encrypted data.
107 <li><p> Destroy encryption context <a href="xmlsec-xmlenc.html#XMLSECENCCTX">xmlSecEncCtx</a>
108 using <a href="xmlsec-xmlenc.html#XMLSECENCCTXDESTROY">xmlSecEncCtxDestroy</a> or
109 <a href="xmlsec-xmlenc.html#XMLSECENCCTXFINALIZE">xmlSecEncCtxFinalize</a>
114 <div class="EXAMPLE">
115 <a name="AEN333"></a><p><b>Example 2. Decrypting a document.</b></p>
116 <pre class="PROGRAMLISTING">int
117 decrypt_file(const char* enc_file, const char* key_file) {
118 xmlDocPtr doc = NULL;
119 xmlNodePtr node = NULL;
120 xmlSecEncCtxPtr encCtx = NULL;
127 doc = xmlParseFile(enc_file);
128 if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){
129 fprintf(stderr, "Error: unable to parse file \"%s\"\n", enc_file);
133 /* find start node */
134 node = xmlSecFindNode(xmlDocGetRootElement(doc), xmlSecNodeEncryptedData, xmlSecEncNs);
136 fprintf(stderr, "Error: start node not found in \"%s\"\n", enc_file);
140 /* create encryption context, we don't need keys manager in this example */
141 encCtx = xmlSecEncCtxCreate(NULL);
143 fprintf(stderr,"Error: failed to create encryption context\n");
148 encCtx->encKey = xmlSecKeyReadBinaryFile(xmlSecKeyDataDesId, key_file);
149 if(encCtx->encKey == NULL) {
150 fprintf(stderr,"Error: failed to load des key from binary file \"%s\"\n", key_file);
154 /* set key name to the file name, this is just an example! */
155 if(xmlSecKeySetName(encCtx->encKey, key_file) < 0) {
156 fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", key_file);
160 /* decrypt the data */
161 if((xmlSecEncCtxDecrypt(encCtx, node) < 0) || (encCtx->result == NULL)) {
162 fprintf(stderr,"Error: decryption failed\n");
166 /* print decrypted data to stdout */
167 if(encCtx->resultReplaced != 0) {
168 fprintf(stdout, "Decrypted XML data:\n");
169 xmlDocDump(stdout, doc);
171 fprintf(stdout, "Decrypted binary data (%d bytes):\n", xmlSecBufferGetSize(encCtx->result));
172 if(xmlSecBufferGetData(encCtx->result) != NULL) {
173 fwrite(xmlSecBufferGetData(encCtx->result),
175 xmlSecBufferGetSize(encCtx->result),
179 fprintf(stdout, "\n");
187 xmlSecEncCtxDestroy(encCtx);
196 <p><a href="xmlsec-decrypt-with-signle-key.html#XMLSEC-EXAMPLE-DECRYPT1">Full Program Listing</a></p>
199 <table class="navigation" width="100%" summary="Navigation footer" cellpadding="2" cellspacing="2"><tr valign="middle">
200 <td align="left"><a accesskey="p" href="xmlsec-notes-verify.html"><b><<< Verifying a signed document</b></a></td>
201 <td align="right"><a accesskey="n" href="xmlsec-notes-keys.html"><b>Keys. >>></b></a></td>
203 </td></tr></table></td>