我使用javax.mail 1.5.5
创建一个s / mime无符号的mime消息如下:
如您所见,部分末尾有两个连字符
我创建了s / mime,然后删除了两个连字符,因此签名值不同
public class TestKepSignStackOf {
private static final String UNSIGNED_MIME = "E:/sign/b/unsigned.eml";
private static final String SIGNED_VALUE = "E:/sign/b/smime.p7s";
private static final String SIGNED_MIME = "E:/sign/b/signed.eml";
public static void main(String[] a) {
// create some properties and get the default Session
Properties props = new Properties();
// props.put("mail.smtp.host", host);
Session session = Session.getInstance(props, null);
// session.setDebug(debug);
MimeMessage msg = new MimeMessage(session, null);
try {
// create a message
msg.addHeader("X-TR-REM-iletiTip", "standart");
msg.addHeader("X-TR-REM-iletiID", "");
msg.addHeader("X-TR-REM-iletiDetay", "detay");
msg.setFrom(new InternetAddress("mail@mail.com.tr"));
InternetAddress[] address = {new InternetAddress(
"mail-test@mail.com.tr")};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("Test 4");
msg.setSentDate(new Date());
MultipartSigned multipartSigned = new MultipartSigned();
MimeBodyPart bodyPart = new MimeBodyPart();
bodyPart.setText("<b>Hello World</b>", "utf-8", "html");
Multipart multiPart = new MimeMultipart();
multiPart.addBodyPart(bodyPart);
MimeBodyPart body = new MimeBodyPart();
body.setContent(multiPart);
multipartSigned.addBodyPart(body);
msg.setContent(multipartSigned);
try (OutputStream str = Files.newOutputStream(Paths
.get(UNSIGNED_MIME))) {
msg.writeTo(str);
};
signMimeMessage();
MimeBodyPart attachPart = new MimeBodyPart();
attachPart.attachFile(SIGNED_VALUE,
"application/pkcs7-signature; name=smime.p7s", "base64");
attachPart.setFileName("smime.p7s");
attachPart.addHeader("Content-Description",
"S/MIME Cryptographic Signature");
multipartSigned.addBodyPart(attachPart);
msg.saveChanges();
try (OutputStream str = Files.newOutputStream(Paths
.get(SIGNED_MIME))) {
msg.writeTo(str);
str.close();
}
} catch (Exception ex) {
System.out.println("error = " + ex.getMessage());
}
}
非常感谢任何建议或指导 感谢
答案 0 :(得分:0)
您的未签名示例有误。它已经是多部分/签名消息,但是无符号消息只包含内部多部分/混合内容。签名应该在整个multipart / mixed内容上,而不是用于在multipart / signed部分中嵌入多部分/混合内容的标题和边界。
答案 1 :(得分:0)
问题是相关的签名过程。因此,在创建无符号文件(要签名的主体)之前,应使用message.saveChanges()方法保存整个消息。 更新的代码位于下方
MimeBodyPart body = new MimeBodyPart();
body.setContent(multiPart);
multipartSigned.addBodyPart(body);
msg.setContent(multipartSigned);
msg.saveChanges();
try (OutputStream str = Files.newOutputStream(Paths
.get(UNSIGNED_MIME))) {
body.writeTo(str);
str.close();
};