PyObject *xmlsec_EncCtxBinaryEncrypt(PyObject *self, PyObject *args) { PyObject *encCtx_obj, *tmpl_obj; xmlSecEncCtxPtr encCtx; xmlNodePtr tmpl; const xmlSecByte *data; xmlSecSize dataSize; if (CheckArgs(args, "OOSI:encCtxBinaryEncrypt")) { if (!PyArg_ParseTuple(args, "OOsi:encCtxBinaryEncrypt", &encCtx_obj, &tmpl_obj, &data, &dataSize)) return NULL; } else return NULL; encCtx = xmlSecEncCtxPtr_get(encCtx_obj); tmpl = xmlNodePtr_get(tmpl_obj); return (wrap_int(xmlSecEncCtxBinaryEncrypt(encCtx, tmpl, data, dataSize))); }
/** * 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)){ printf_a_ignorar3(stderr, "Error: unable to parse file \"%s\"\n", tmpl_file); goto done; } /* find start node */ node = xmlSecFindNode(xmlDocGetRootElement(doc), xmlSecNodeEncryptedData, xmlSecEncNs); if(node == NULL) { printf_a_ignorar3(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) { printf_a_ignorar3(stderr,"Error: failed to create encryption context\n"); goto done; } /* load DES key, assuming that there is not password */ encCtx->encKey = xmlSecKeyReadBinaryFile(xmlSecKeyDataDesId, key_file); if(encCtx->encKey == NULL) { printf_a_ignorar3(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) { printf_a_ignorar3(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) { printf_a_ignorar3(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); }