1

我在使用 x509 证书验证 S/Mime 签名消息时遇到了一些问题。这是我的代码:

public class verifyMsg {

private static void verify(SMIMESignedParser s) throws Exception {

    Security.addProvider(new BouncyCastleProvider());
    System.out.println("wbilem");
    CertStore certs = s.getCertificatesAndCRLs("Collection", "BC");

    SignerInformationStore signers = s.getSignerInfos();
    Collection c = signers.getSigners();
    Iterator it = c.iterator();

    while (it.hasNext()) {
        File f = new File("signature.crt");
        FileInputStream fis = new FileInputStream(f);
        DataInputStream dis = new DataInputStream(fis);
        byte[] keyBytes = new byte[(int) f.length()];
        dis.readFully(keyBytes);
        dis.close();
        fis.close();
        SignerInformation signer = (SignerInformation) it.next();
        Collection certCollection = certs.getCertificates(signer.getSID());
        Iterator certIt = certCollection.iterator();
        FileInputStream fr = new FileInputStream("signature.crt");
        CertificateFactory cf = CertificateFactory.getInstance("X509");
        X509Certificate cert = (X509Certificate) cf.generateCertificate(fr);


        if (signer.verify(cert, "BC")) { //problem is there...
            System.out.println("signature verified");
        } else {
            System.out.println("signature failed!");
        }

    }
}

public static void main(String[] args) throws Exception {
    Properties props = System.getProperties();
    Session session = Session.getDefaultInstance(props, null);

    try {
        FileInputStream fr = new FileInputStream("signature.crt");
        CertificateFactory cf = CertificateFactory.getInstance("X509");
        X509Certificate c = (X509Certificate) cf.generateCertificate(fr);
        System.out.println("Read in the following certificate:");
        System.out.println("\tCertificate for: " + c.getSubjectDN());
        System.out.println("\tCertificate issued by: " + c.getIssuerDN());
        System.out.println("\tThe certificate is valid from " + c.getNotBefore() + " to " + c.getNotAfter());
        System.out.println("\tCertificate SN# " + c.getSerialNumber());
        System.out.println("\tGenerated with " + c.getSigAlgName());
        System.out.println(c.getPublicKey());
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        MimeMessage msg = new MimeMessage(session, new SharedFileInputStream("G:\\MIME.txt"));
        if (msg.isMimeType("multipart/signed")) {

            SMIMESignedParser s = new SMIMESignedParser((MimeMultipart) msg.getContent());
            System.out.println("Status:");
            verify(s);
        } else if (msg.isMimeType("application/pkcs7-mime")) {

            // in this case the content is wrapped in the signature block.
            //
            SMIMESignedParser s = new SMIMESignedParser(msg);
            System.out.println("Status1:");
            verify(s);
        } else {
            System.err.println("Not a signed message!");
        }

    } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (CMSException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

我有这个例外的问题:

CMSSignerDigestMismatchException:消息摘要属性值与计算值不匹配。我不知道我在做什么错。我使用 jdk 1.4.2。

4

2 回答 2

1

我只是发现问题出在消息上。我将字节数组转换为字符串,然后将此字符串转换为输入流。现在我给 inputstream 字节数组而不进行转换,一切都很好:)

于 2011-12-22T14:55:04.130 回答
0

我解决了这个问题。如果您使用 S/MIME 对数据进行签名,那么根据 S/MIME 标准,我们应该有 MIME-Version、Content-Type...等,并且标头与要使用空行签名的消息分开。大多数当消息为“text/plain”类型时,我们不会在消息中添加标头,这很明显。但是如果您使用 S/MIME 对数据进行签名,它将查找 S/MIME 标准标头和诸如 miss mach 之类的问题在摘要中会发生。

在签署 MIME-Version 之前将以下标头添加到消息中:1.0 Content-Type: text/plain;

或者只是在消息前添加一个空行。

以上方法对我来说效果很好。

于 2013-12-18T03:36:56.407 回答