Sign MimeBodyPart which has attachments in it (wit

2019-09-11 09:49发布

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条回答
倾城 Initia
2楼-- · 2019-09-11 10:21

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".

LHS: sender, RHS: receiver

查看更多
登录 后发表回答