RSAPrivateKey privateRSAKey = (RSAPrivateKey)privKey;
RSAKeyParameters keyParams = new RSAKeyParameters(true, privateRSAKey.getModulus(), privateRSAKey.getPrivateExponent());
ContentSigner sigGen = new BcRSAContentSignerBuilder(sigAlgId, digAlgId).build(keyParams);
CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
gen.addSignerInfoGenerator(
new SignerInfoGeneratorBuilder(new BcDigestCalculatorProvider())
.build(sigGen, new X509CertificateHolder(certificate)));
CMSProcessableInputStream processable = new CMSProcessableInputStream(content);
CMSSignedData signedData = gen.generate(processable, false);
return signedData.getEncoded();
}