<html>
<head>
<style>P {margin-top:2px;margin-bottom:2px;}</style>
</head>
<body>
<P>Please I want to know how sign and encryption without template file.</P>
<P>Example code in C:\xmlsec-0.0.7\docs\examples\enc1 doesn't work well. </P>
<P>And that example code encrypt for memory data. </P>
<P>so I edit code and it sucessfully compile without error, but it does not work well.</P>
<P>Follow code is reference in C:\xmlsec-0.0.7\apps\xmlsec.c and C:\xmlsec-0.0.7\docs\examples\enc1.</P>
<P>//////////////////////Execute Result/////////////////////////////////</P>
<P>Call Init()<BR>**********xmlSecEncCtxCreate()***********<BR>Call readKeys()<BR>startLoad : c:\test\Debug\des.key<BR>return value:0<BR>Call xmlSecParseFile()<BR>Call encrypt()<BR>********Start encrypt()*********<BR>********xmlSecEncryptUri()*********<BR>xmlSecEvpCipherFinal: evp cipher final failed<BR>xmlSecCipherTransformFlush: cipher final failed<BR>xmlSecCipherTransformFlush: next transform flush failed<BR>xmlSecCipherValueNodeRead: failed to finalize encryption<BR>xmlSecCipherDataNodeRead: failed to read CipherValue node<BR>xmlSecDecrypt: failed to get CipherData node content<BR>xmlSecEncryptedKeyNodeRead: node decrypt failed<BR>********xmlDocDumpMemoryEnc() tmpl *********<BR>xmlDocDumpFormatMemoryEnc: Null DOM tree document pointer.<BR>Error: failed to dump document to memory<BR>********xmlSecEncResultDestroy()*********<BR>********End encrypt()*********<BR>Call shutdown()<BR>Press any key to continue<BR>/////////////////////////////////////////////////////////////////////////</P>
<P> </P>
<P>Please advice to me what is wrong. </P>
<P> </P>
<P>#include <stdlib.h><BR>#include <string.h><BR>#include <time.h><BR>#include <openssl/evp.h><BR>#include <openssl/rand.h><BR>#include <openssl/err.h><BR>#include <libxml/tree.h><BR>#include <libxml/xmlmemory.h><BR>#include <libxml/parser.h><BR>#ifndef XMLSEC_NO_XSLT<BR>#include <libxslt/xslt.h><BR>#include <libxslt/extensions.h> <BR>#include <libxslt/xsltInternals.h><BR>#include <libxslt/xsltutils.h><BR>#include <libexslt/exslt.h><BR>#endif /* XMLSEC_NO_XSLT */<BR>#include <xmlsec/xmlsec.h><BR>#include <xmlsec/xmltree.h><BR>#include <xmlsec/keys.h><BR>#include <xmlsec/keysmngr.h><BR>#include <xmlsec/transforms.h><BR>#include <xmlsec/xmldsig.h><BR>#include <xmlsec/xmlenc.h><BR>#include <xmlsec/debug.h></P>
<P>#define KEY_FILE "c:\\test\\Debug\\des.key"<BR>#define SRC_TYPE 1 //0:xml 1:binary<BR>#define SRC_FILE "c:\\test\\Debug\\test.txt"<BR>//#define TMPL_FILE "c:\\test\\Debug\\enc-des3-test.tmpl"<BR>#define OUT_ENC "c:\\test\\Debug\\enc-test-des.xml"<BR>#define OUT_DEC "c:\\test\\Debug\\after-dec.txt"<BR>/**<BR> * Init/Shutdown<BR> */<BR>int init(void);<BR>int createTmpl(void);<BR>void shutdown(void);<BR>int readKeys(char *file);<BR> <BR>char *data = NULL;<BR>int type=1; //1:encrypt 2:decrypt<BR>xmlSecEncCtxPtr encCtx = NULL;</P>
<P>int encrypt(void);<BR>int decrypt(xmlDocPtr doc);</P>
<P>/**<BR> * Global data<BR> */<BR>xmlSecKeysMngrPtr keyMgr = NULL; <BR>xmlSecKeyPtr sessionKey = NULL;<BR>xmlNodePtr encData=NULL;</P>
<P>char *nodeId = NULL;<BR>char *nodeName = NULL;<BR>char *nodeNs = NULL;<BR>int repeats = 1;<BR>int printResult = 0;<BR>clock_t total_time = 0;<BR>char *global_pwd = NULL;</P>
<P>int main(int argc, char **argv) <BR>{<BR> xmlDocPtr doc = NULL;<BR> int ret;</P>
<P> printf("Call Init()\n");<BR> ret = init();<BR> if(ret < 0) <BR> {<BR> fprintf(stdout, "Error: init failed\n");<BR> goto done;<BR> }<BR> printf("Call readKeys()\n");<BR> ret = readKeys(KEY_FILE);<BR> data = SRC_FILE;<BR> if(ret<0)<BR> {<BR> fprintf(stdout, "Error: read keys \n");<BR> goto done;<BR> }</P>
<P> printf("Call xmlSecParseFile()\n");<BR> //doc = xmlSecParseFile(TM);<BR> ret=createTmpl();<BR> <BR> if(ret<0)<BR> {<BR> fprintf(stdout, "Error: create Template Object\n");<BR> goto done;<BR> }</P>
<P>/* if(doc == NULL) <BR> {<BR> fprintf(stderr, "Error: failed to read XML file\n");<BR> goto done;<BR> }*/<BR> <BR> switch(type) <BR> {<BR> case 1:<BR> printf("Call encrypt()\n");<BR> ret = encrypt();<BR> break;<BR> case 2:<BR> printf("Call decrypt()\n");<BR> ret = decrypt(doc);<BR> break;<BR> }<BR> if(ret < 0) <BR> {<BR> fprintf(stderr, "Error: operation failed\n");<BR> goto done; <BR> }</P>
<P>done: <BR> if(doc != NULL) <BR> {<BR> printf("Call xmlFreeDoc()\n");<BR> xmlFreeDoc(doc); <BR> }<BR> printf("Call shutdown()\n");<BR> shutdown();<BR> return 1;<BR>}<BR>int createTmpl(void)<BR>{<BR> xmlNodePtr encKey = NULL;<BR> xmlNodePtr cur;<BR> xmlDocPtr doc = NULL;<BR> <BR> encData = xmlSecEncDataCreate(NULL, NULL, NULL, NULL);<BR> if(encData == NULL) <BR> {<BR> fprintf(stderr, "Error: template creation failed\n");<BR> goto done; <BR> }<BR> <BR> cur = xmlSecEncDataAddEncMethod(encData, xmlSecEncDes3Cbc);<BR> if(cur == NULL) <BR> {<BR> fprintf(stderr, "Error: failed to add Enc Method\n");<BR> goto done; <BR> }<BR> <BR> cur = xmlSecEncDataAddCipherValue(encData); <BR> if(cur == NULL) <BR> {<BR> fprintf(stderr, "Error: failed to add CipherValue\n");<BR> goto done; <BR> }<BR> cur = xmlSecEncDataAddKeyInfo(encData);<BR> if(cur == NULL) <BR> {<BR> fprintf(stderr, "Error: failed to add KeyInfo\n");<BR> goto done; <BR> }<BR> <BR> encKey = xmlSecKeyInfoAddEncryptedKey(cur, NULL, NULL, NULL);<BR> if(encKey == NULL) <BR> {<BR> fprintf(stderr, "Error: failed to add EncryptedKey\n");<BR> goto done; <BR> }<BR> <BR> /**<BR> * Set the encryption method for encrypting the key<BR> */<BR> cur = xmlSecEncDataAddEncMethod(encKey, xmlSecEncDes3Cbc);<BR> if(cur == NULL) <BR> {<BR> fprintf(stderr, "Error: failed to add EncryptedKey Enc Method\n");<BR> goto done; <BR> }<BR> cur = xmlSecEncDataAddCipherValue(encKey); <BR> if(cur == NULL) <BR> {<BR> fprintf(stderr, "Error: failed to add EncryptedKey CipherValue\n");<BR> goto done; <BR> }<BR> <BR> cur = xmlSecEncDataAddKeyInfo(encKey);<BR> if(cur == NULL) <BR> {<BR> fprintf(stderr, "Error: failed to add EncryptedKey KeyInfo\n");<BR> goto done; <BR> }</P>
<P> cur = xmlSecKeyInfoAddKeyName(cur);<BR> if(cur == NULL) <BR> {<BR> fprintf(stderr, "Error: failed to add EncryptedKey KeyName\n");<BR> goto done; <BR> }<BR>done: <BR> <BR> return(0);</P>
<P>}<BR>int init(void) <BR>{<BR> time_t t = 0;</P>
<P> OpenSSL_add_all_algorithms();<BR> ERR_load_crypto_strings(); <BR> <BR> time(&t);<BR> while (RAND_status() != 1) <BR> {<BR> RAND_seed(&t, sizeof(t));<BR> }<BR> xmlInitParser();<BR> LIBXML_TEST_VERSION</P>
<P> xmlSecInit(); </P>
<P> keyMgr = xmlSecSimpleKeysMngrCreate(); <BR> if(keyMgr == NULL) <BR> {<BR> fprintf(stderr, "Error: failed to create keys manager\n");<BR> return(-1);<BR> }</P>
<P> printf("**********xmlSecEncCtxCreate()***********\n");<BR> encCtx = xmlSecEncCtxCreate(keyMgr);<BR> <BR> if(encCtx == NULL) <BR> {<BR> fprintf(stderr,"Error: failed to create Enc context\n");<BR> return(-1);<BR> } <BR> return(0); <BR>}</P>
<P>void shutdown(void) <BR>{<BR> /* destroy xmlsec objects */<BR> if(encCtx != NULL) <BR> {<BR> xmlSecEncCtxDestroy(encCtx);<BR> } <BR> if(keyMgr != NULL) <BR> {<BR> xmlSecSimpleKeysMngrDestroy(keyMgr);<BR> }<BR> if(encData !=NULL)<BR> {<BR> xmlSecEncDataDestroy(encData);<BR> }<BR> <BR> xmlSecShutdown();<BR> <BR> xsltCleanupGlobals(); <BR> xmlCleanupParser();</P>
<P> RAND_cleanup();<BR> ERR_clear_error();<BR>}</P>
<P>int readKeys(char *file) <BR>{<BR> int ret=0;<BR> <BR> printf("startLoad : %s\n",file);<BR> ret = xmlSecSimpleKeysMngrLoad(keyMgr,file, 0);<BR> printf("return value:%d\n",ret);<BR> if(ret < 0) <BR> {<BR> fprintf(stderr, "Error: failed to load keys from \"%s\".\n", file);<BR> return(-1);<BR> }<BR> return(0);<BR>}<BR>int encrypt(void) <BR>{<BR> xmlSecEncResultPtr encResult = NULL;<BR> xmlChar *result = NULL; <BR> xmlDocPtr doc = NULL;<BR> FILE *fp;<BR> int len;<BR> int ret;<BR> int res = -1;</P>
<P> printf("********Start encrypt()*********\n");</P>
<P> if(SRC_TYPE && (data != NULL) && encCtx !=NULL && encData !=NULL) <BR> {<BR> printf("********xmlSecEncryptUri()*********\n");<BR> ret = xmlSecEncryptUri(encCtx, NULL, NULL, encData, data, &encResult);<BR> <BR> if(ret < 0) <BR> {<BR> fprintf(stderr,"Error: xmlSecEncryptUri() failed \n");<BR> goto done; <BR> } </P>
<P> }<BR> else if(!SRC_TYPE && (data != NULL)) <BR> { <BR> xmlNodePtr cur;<BR> <BR> printf("********xmlParseFile()*********\n");<BR> doc = xmlParseFile(data);<BR> <BR> if (doc == NULL) <BR> {<BR> fprintf(stderr, "Error: unable to parse file \"%s\"\n", data);<BR> goto done; <BR> }</P>
<P> if(nodeId != NULL) <BR> {<BR> printf("********xmlSecFindNodeById()*********\n");<BR> cur = xmlSecFindNodeById(encData, BAD_CAST nodeId);<BR> } <BR> else if(nodeName != NULL) <BR> {<BR> printf("********xmlSecFindNode()*********\n");<BR> cur = xmlSecFindNode(encData, BAD_CAST nodeName, BAD_CAST nodeNs);<BR> } <BR> else <BR> {<BR> printf("********xmlDocGetRootElement()*********\n");<BR> cur = encData;<BR> }<BR> <BR> if(cur == NULL) <BR> {<BR> fprintf(stderr,"Error: empty document for file \"%s\" or unable to find node\n", data);<BR> goto done; <BR> }</P>
<P> printf("********xmlSecEncryptXmlNode()*********\n");<BR> ret = xmlSecEncryptXmlNode(encCtx, NULL, sessionKey,encData, cur, &encResult); <BR> <BR> if(ret < 0) <BR> {<BR> fprintf(stderr,"Error: xmlSecEncryptXmlNode() failed \n");<BR> goto done; <BR> } <BR> }<BR> else <BR> printf("Error: haha\n");<BR> <BR> if((encResult != NULL) && (encResult->replaced) && (doc != NULL)) <BR> {<BR> printf("********xmlDocDumpMemoryEnc() doc *********\n"); <BR> xmlDocDumpMemoryEnc(doc, &result, &len, NULL); <BR> }<BR> else <BR> {<BR> printf("********xmlDocDumpMemoryEnc() tmpl *********\n");<BR> xmlDocDumpMemoryEnc(encData->doc, &result, &len, NULL);<BR> }<BR> if(result == NULL) {<BR> fprintf(stderr,"Error: failed to dump document to memory\n");<BR> goto done;<BR> }<BR> if((fp=fopen(OUT_ENC,"w"))==NULL)<BR> printf("Failure to file open\n");<BR> else<BR> fwrite(result, len, 1, fp);<BR> <BR>done: <BR> if(doc != NULL) <BR> {<BR> printf("********xmlFreeDoc()*********\n");<BR> xmlFreeDoc(doc);<BR> }<BR> if(result != NULL) <BR> {<BR> printf("********xmlFree()*********\n");<BR> xmlFree(result); <BR> }<BR> if(encResult != NULL) <BR> {<BR> printf("********xmlSecEncResultDestroy()*********\n");<BR> xmlSecEncResultDestroy(encResult);<BR> }</P>
<P> printf("********End encrypt()*********\n");<BR> return 1;<BR>}</P>
<P>int decrypt(xmlDocPtr doc) <BR>{ <BR> xmlSecEncResultPtr encResult = NULL;<BR> xmlNodePtr cur;<BR> FILE *fp;<BR> int ret;</P>
<P> printf("********Start decrypt()*********\n");</P>
<P> printf("********xmlSecFindNode()*********\n");<BR> cur = xmlSecFindNode(encData, BAD_CAST "EncryptedData", xmlSecEncNs);</P>
<P> if(cur == NULL) <BR> {<BR> fprintf(stderr,"Error: unable to find EncryptedData node\n");<BR> goto done; <BR> }</P>
<P> printf("********xmlSecDecrypt()*********\n");<BR> ret = xmlSecDecrypt(encCtx, NULL, NULL, cur, &encResult);</P>
<P> if(ret < 0) <BR> {<BR> fprintf(stderr,"Error: xmlSecDecrypt() failed \n");<BR> goto done; <BR> } </P>
<P> if((encResult != NULL) && encResult->replaced && (encResult->buffer != NULL)) <BR> {<BR> printf("********xmlDocDump()*********\n");<BR> ret = xmlDocDump(stdout, doc); <BR> } <BR> else if((encResult != NULL) && !encResult->replaced) <BR> {<BR> printf("****fwrite(xmlBufferContent())****\n");</P>
<P> if((fp=fopen(OUT_DEC,"w"))==NULL)<BR> printf("Failure to file open\n");<BR> else<BR> ret = fwrite(xmlBufferContent(encResult->buffer), xmlBufferLength(encResult->buffer),1, fp); <BR> } <BR> else <BR> {<BR> fprintf(stderr,"Error: bad results \n");<BR> goto done; <BR> }</P>
<P> if(ret < 0) <BR> {<BR> fprintf(stderr,"Error: failed to print out the result \n");<BR> goto done; <BR> }<BR> <BR> <BR>done: <BR> if(encResult != NULL) <BR> {<BR> printf("********xmlSecEncResultDestroy()*********\n");<BR> xmlSecEncResultDestroy(encResult);<BR> }<BR> printf("********End decrypt()*********\n");<BR> return 1;<BR>}<BR></P>
<img src='http://mail.freechal.com/messagecenter/email/RcvCheck.asp?uid=20765161&userid=kjho80&key=FMTNKHJVFZFNPQCVFLZGVT0' width=1 height=1><br><br>---------------------------------------<br><font size=2>°¨¼º¼øµµ 100% ÇÁ¸®Ã§ äÆÃ<br><a href='http://chat.freechal.com'>http://chat.freechal.com/</a></font></body>
</html>