首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用BC验证分离的签名

使用BC验证分离的签名
EN

Stack Overflow用户
提问于 2011-11-23 22:08:54
回答 3查看 15.4K关注 0票数 8

如何在Java中使用BouncyCastle提供者验证分离的签名(CMS/pkcs #7签名)?

目前,我的以下代码抛出了一个异常,消息为message-digest attribute value does not match calculated value

代码语言:javascript
复制
Security.addProvider(new BouncyCastleProvider());

File f = new File(filename);
byte[] buffer = new byte[(int)f.length()];
DataInputStream in = new DataInputStream(new FileInputStream(f));
in.readFully(buffer);
in.close();

CMSSignedData signature = new CMSSignedData(buffer);
SignerInformation signer = (SignerInformation) signature.getSignerInfos().getSigners().iterator().next();
CertStore cs = signature.getCertificatesAndCRLs("Collection", "BC");
Iterator iter = cs.getCertificates(signer.getSID()).iterator();
X509Certificate certificate = (X509Certificate) iter.next();

CMSProcessable sc = signature.getSignedContent();

signer.verify(certificate, "BC");
EN

回答 3

Stack Overflow用户

发布于 2012-02-13 21:28:37

您可以通过以下代码验证分离后的签名:

代码语言:javascript
复制
public static boolean verif_Detached(String signed_file_name,String original_file_name) throws IOException, CMSException, NoSuchAlgorithmException, NoSuchProviderException, CertStoreException, CertificateExpiredException, CertificateNotYetValidException{

    boolean result= false;
    Security.addProvider(new BouncyCastleProvider()); 

    File f = new File(signed_file_name);
    byte[] Sig_Bytes = new byte[(int)f.length()];
    DataInputStream in = new DataInputStream(new FileInputStream(f));
    in.readFully(Sig_Bytes);
    in.close();

    File fi = new File(original_file_name);
    byte[] Data_Bytes = new byte[(int)fi.length()];
    DataInputStream input = new DataInputStream(new FileInputStream(fi));
    input.readFully(Data_Bytes);
    input.close();

    try{
        CMSSignedData cms = new CMSSignedData(new CMSProcessableByteArray(Data_Bytes), Sig_Bytes); 
        CertStore certStore = cms.getCertificatesAndCRLs("Collection", "BC"); 
        SignerInformationStore signers = cms.getSignerInfos(); 
        Collection c = signers.getSigners(); 
        Iterator it = c.iterator(); 
        while (it.hasNext()) { 
            SignerInformation signer = (SignerInformation) it.next(); 
            Collection certCollection = certStore.getCertificates(signer.getSID()); 
            Iterator certIt = certCollection.iterator(); 
            X509Certificate cert = (X509Certificate) certIt.next();
            cert_signer=cert;
            result=signer.verify(cert, "BC");
        }
    }catch(Exception e){
        e.printStackTrace();
        result=false;
    }
    return result; 
}
票数 10
EN

Stack Overflow用户

发布于 2011-12-23 14:31:25

验证分离pKCS7的关键是使用CMSTypedStream,如下所示:

代码语言:javascript
复制
public void verifySign(byte[] signedData,byte[]bPlainText) throws Exception {
                InputStream is  = new ByteArrayInputStream(bPlainText);             
                CMSSignedDataParser sp = new CMSSignedDataParser(new CMSTypedStream (is),signedData);
                CMSTypedStream signedContent = sp.getSignedContent();           

                 signedContent.drain();





                  //CMSSignedData s = new CMSSignedData(signedData); 
                  Store certStore = sp.getCertificates(); 

                  SignerInformationStore signers = sp.getSignerInfos(); 
                    Collection c = signers.getSigners();
                    Iterator it = c.iterator();
                    while (it.hasNext()) 
                    { 
                        SignerInformation signer = (SignerInformation)it.next(); 
                        Collection certCollection = certStore.getMatches(signer.getSID()); 

                        Iterator certIt = certCollection.iterator(); 

                        X509CertificateHolder certHolder = (X509CertificateHolder)certIt.next(); 




                        if ( !signer.verify(new 
            JcaSimpleSignerInfoVerifierBuilder().setProvider("BC").build(certHolder))) 
                        { 
                            throw new DENException("Verification FAILED! "); 

                        } 
                        else
                        {
                            logger.debug("verify success" );
                        }


                    } 
    }
票数 1
EN

Stack Overflow用户

发布于 2013-12-19 11:07:19

你可以在here上找到这篇文章的答案。之所以会发生这种情况,是因为当S/MIME报头不是present.Solution时,bouncy castle/open ssl如何处理S/MIME邮件

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8243566

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档