- Home
- Download
- News
- Documentation

- FAQ
- Tutorial
- API reference
- Examples

- XML Digital Signature

- Online Verifier

- XML Encryption
- XML Canonicalization
- Reporting Bugs
- Mailing list
- Related
- Authors

# Encrypting data.

The typical encryption process includes following steps:

- Prepare data for encryption.

- Create or load encryption template and select start <enc:EncryptedData/> node.

- Create encryption context xmlSecEncCtx using xmlSecEncCtxCreate or xmlSecEncCtxInitialize functions.

- Load encryption key in keys manager or generate a session key and set it in the encryption context (`encKey` member of xmlSecEncCtx structure).

- Encrypt data by calling one of the following functions:

  - xmlSecEncCtxBinaryEncrypt

  - xmlSecEncCtxXmlEncrypt

  - xmlSecEncCtxUriEncrypt

- Check returned value and if necessary consume encrypted data.

- Destroy encryption context xmlSecEncCtx using xmlSecEncCtxDestroy or xmlSecEncCtxFinalize functions.

Example 2. Encrypting binary data with a template.

    /**
     * encrypt_file:
     * @tmpl_file:		the encryption template file name.
     * @key_file:		the Triple DES key file.
     * @data:		the binary data to encrypt.
     * @dataSize:		the binary data size.
     *
     * Encrypts binary #data using template from #tmpl_file and DES key from
     * #key_file.
     *
     * Returns 0 on success or a negative value if an error occurs.
     */
    int
    encrypt_file(const char* tmpl_file, const char* key_file, const unsigned char* data, size_t dataSize) {
        xmlDocPtr doc = NULL;
        xmlNodePtr node = NULL;
        xmlSecEncCtxPtr encCtx = NULL;
        int res = -1;

        assert(tmpl_file);
        assert(key_file);
        assert(data);

        /* load template */
        doc = xmlParseFile(tmpl_file);
        if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){
    	fprintf(stderr, "Error: unable to parse file \"%s\"\n", tmpl_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", tmpl_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;
        }

        /* encrypt the data */
        if(xmlSecEncCtxBinaryEncrypt(encCtx, node, data, dataSize) < 0) {
            fprintf(stderr,"Error: encryption failed\n");
        	goto done;
        }

        /* print encrypted data with document to stdout */
        xmlDocDump(stdout, doc);

        /* success */
        res = 0;

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

        if(doc != NULL) {
    	xmlFreeDoc(doc);
        }
        return(res);
    }

Full program listing

Simple encryption template file