在bouncycastle java中验证签名时的编译错误

时间:2015-08-14 08:23:26

标签: java exception bouncycastle signature verification

我想问一下这个例外:

java.lang.ClassCastException: org.bouncycastle.cms.SignerId cannot be cast to java.security.cert.CertSelector
    at tn.ance.signature.VerificationSignature.verifierSignature(VerificationSignature.java:27)
    at tn.ance.signature.main_project.main(main_project.java:24)

这是我的VerificationSignature类

package tn.ance.signature;
import java.io.*;
import java.util.*;
import java.security.cert.CertSelector;
import java.security.cert.CertStore;
import java.security.cert.X509Certificate;

import org.bouncycastle.cms.CMSSignedData;
import org.bouncycastle.cms.CMSProcessable;
import org.bouncycastle.cms.SignerInformation;


public class VerificationSignature {

    public void verifierSignature(){
        try {
            // Chargement du fichier signé
            File f = new File("fichier_a_signer.txt.pk7");
            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((CertSelector) signer.getSID()).iterator();
            X509Certificate certificate = (X509Certificate) iter.next();
            CMSProcessable sc = signature.getSignedContent();
            byte[] data = (byte[]) sc.getContent();

            // Verifie la signature
            System.out.println(signer.verify(certificate, "BC"));

            FileOutputStream envfos = new FileOutputStream("document_non_signer.txt");
            envfos.write(data);
            envfos.close();
            System.out.println("Verification signature: done!");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Verification signature: error!");
            return ;
        }
}
}

错误来自此行

        SignerInformation signer = (SignerInformation)signature.getSignerInfos().getSigners().iterator().next();

但我不知道如何解决它。有什么帮助吗?我是bouncycastle图书馆的新手!

0 个答案:

没有答案