Decrypting an encrypted document

Home Download News Documentation FAQ Tutorial API reference Examples XML Digital Signature Online Verifier XML Encryption XML Canonicalization Reporting Bugs Mailing list Related
XML Security Library Reference Manual
<<< Previous Page | Home | Up | Next Page >>>
# Decrypting an encrypted document The typical decryption process includes following steps: Load keys, X509 certificates, etc. in the keys manager . Create encryption context xmlSecEncCtx using xmlSecEncCtxCreate or xmlSecEncCtxInitialize functions. Select start decryption <enc:EncryptedData> node. Decrypt by calling xmlSecencCtxDecrypt function. Check returned value and if necessary consume encrypted data. Destroy encryption context xmlSecEncCtx using xmlSecEncCtxDestroy or xmlSecEncCtxFinalize functions. Example 2. Decrypting a document.
    int
    decrypt_file(const char* enc_file, const char* key_file) {
        xmlDocPtr doc = NULL;
        xmlNodePtr node = NULL;
        xmlSecEncCtxPtr encCtx = NULL;
        int res = -1;

        assert(enc_file);
        assert(key_file);

        /* load template */
        doc = xmlParseFile(enc_file);
        if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){
    	fprintf(stderr, "Error: unable to parse file \"%s\"\n", enc_file);
    	goto done;
        }

        /* find start node */
        node = xmlSecFindNode(xmlDocGetRootElement(doc), xmlSecNodeEncryptedData, xmlSecEncNs);
        if(node == NULL) {
    	fprintf(stderr, "Error: start node not found in \"%s\"\n", enc_file);
    	goto done;
        }

        /* create encryption context, we don't need keys manager in this example */
        encCtx = xmlSecEncCtxCreate(NULL);
        if(encCtx == NULL) {
            fprintf(stderr,"Error: failed to create encryption context\n");
    	goto done;
        }

        /* load DES key */
        encCtx->encKey = xmlSecKeyReadBinaryFile(xmlSecKeyDataDesId, key_file);
        if(encCtx->encKey == NULL) {
            fprintf(stderr,"Error: failed to load des key from binary file \"%s\"\n", key_file);
    	goto done;
        }

        /* set key name to the file name, this is just an example! */
        if(xmlSecKeySetName(encCtx->encKey, key_file) < 0) {
        	fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", key_file);
    	goto done;
        }

        /* decrypt the data */
        if((xmlSecEncCtxDecrypt(encCtx, node) < 0) || (encCtx->result == NULL)) {
            fprintf(stderr,"Error: decryption failed\n");
    	goto done;
        }

        /* print decrypted data to stdout */
        if(encCtx->resultReplaced != 0) {
    	fprintf(stdout, "Decrypted XML data:\n");
    	xmlDocDump(stdout, doc);
        } else {
    	fprintf(stdout, "Decrypted binary data (%d bytes):\n", xmlSecBufferGetSize(encCtx->result));
    	if(xmlSecBufferGetData(encCtx->result) != NULL) {
    	    fwrite(xmlSecBufferGetData(encCtx->result),
    	          1,
    	          xmlSecBufferGetSize(encCtx->result),
    	          stdout);
    	}
        }
        fprintf(stdout, "\n");

        /* success */
        res = 0;

    done:
        /* cleanup */
        if(encCtx != NULL) {
    	xmlSecEncCtxDestroy(encCtx);
        }

        if(doc != NULL) {
    	xmlFreeDoc(doc);
        }
        return(res);
    }
Full Program Listing
<<< Previous Page | Home | Up | Next Page >>>
Verifying a signed document | Keys.
Aleksey Sanin