Getting pkcs7 format using bouncy castle
843811Sep 5 2008 — edited Sep 9 2008The following code I am using to get the pkcs7 format, But It is not giving correct output. while validation time its giving problem and if I press signatue porperties that time its freezing. Anyone tell me what is the problem in this code.
String alias = null;
KeyStore keyStore;
byte[] dataToSign; // assum its having pdf file content.
char[] pwd = "password".toCharArray();
keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(new FileInputStream(new File("filepath of pfx file")),pwd);
for(Enumeration e = keyStore.aliases() ; e.hasMoreElements() ; ) {
alias = e.nextElement().toString();
}
PrivateKey privKey = (PrivateKey)keyStore.getKey(alias, pwd);
X509Certificate certificate =(X509Certificate) keyStore.getCertificate(alias);
CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
gen.addSigner(privKey, certificate , CMSSignedDataGenerator.DIGEST_SHA1);
ArrayList certList = new ArrayList();
certList.add(certificate);
CertStore certStore = CertStore.getInstance("Collection",
new CollectionCertStoreParameters(certList));
gen.addCertificatesAndCRLs(certStore);
CMSProcessableByteArray process = new CMSProcessableByteArray(dataToSign);
CMSSignedData data = gen.generate(process, null);
byte[] output = data.getEncoded();