Sign MimeBodyPart which has attachments in it (with Bouncycastle)

妖精的绣舞 提交于 2019-12-11 19:41:25

问题


I am working with OpenAS2Server-1.3.3 library. There sending a single document is working fine..

Now I wanted to modify it to send document with attachments, like we do with emails. In this scenario, all the decription work well, but signature verification failed (MIC is not matched)

This is how I am tring to send attachments with main doc: Create a MimeMultipart and add two MimeBodyPart into it. (main document and the attachment) Finally wrap the MimeMultipart within a MimeBodyPart (I am not sure this is the way to do this, but anyway Bouncycastle do not have API to sign MimeMultipart )

Could anyone tell me the correct way to sign a message with attachment ?

        MimeBodyPart mainBody = new MimeBodyPart();
        mainBody.setDataHandler(new DataHandler(byteSource));

        MimeBodyPart attachemt1 = new MimeBodyPart();
        attachemt1.attachFile("/home/user/Desktop/Test1.txt");

        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(mainBody);
        multipart.addBodyPart(attachemt1);

        MimeBodyPart body = new MimeBodyPart();
        body.setContent(multipart);
        body.setHeader("Content-Type", multipart.getContentType());
        logger.info("--------------Attaching the file...  Done");

回答1:


I was able to get the issue and solution. I am just putting it here for anyone else who will try to do this kind of work.

I just dump the data that use for calculating MIC, at both sending side and receiving side. So the attached image will show the problem clearly.

So I added those header fields manually for all the attachments and main doc, at the sending side, as bellow.

mainBody.setHeader("Content-Type", "application/EDI-X12");
mainBody.setHeader("Content-Transfer-Encoding", "7bit");

Now it solved and "MIC is matched".



来源:https://stackoverflow.com/questions/33802743/sign-mimebodypart-which-has-attachments-in-it-with-bouncycastle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!