我想使用bouncycastle生成簡單的CMS簽名。 此代碼有效!使用BouncyCastle向CMS簽名添加已簽名/已驗證的屬性
Security.addProvider(new BouncyCastleProvider());
String password = "123456";
KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(new FileInputStream("c:/cert_123456.p12"), password.toCharArray());
String alias = (String)ks.aliases().nextElement();
PrivateKey key = (PrivateKey)ks.getKey(alias, password.toCharArray());
Certificate[] chain = ks.getCertificateChain(alias);
CMSSignedDataGenerator generator = new CMSSignedDataGenerator();
generator.addSigner(key, (X509Certificate)chain[0], CMSSignedDataGenerator.DIGEST_SHA1);
ArrayList list = new ArrayList();
for (int i = 0; i < chain.length; i++) {
list.add(chain[i]);
}
CertStore chainStore = CertStore.getInstance("Collection", new CollectionCertStoreParameters(list), "BC");
generator.addCertificatesAndCRLs(chainStore);
CMSProcessable content = new CMSProcessableByteArray("test".getBytes());
CMSSignedData signedData = generator.generate(content, false, "BC");
byte[] pk = signedData.getEncoded();
但是,如何添加簽名屬性?
我想刪除默認的簽名屬性並添加簽名策略標識符。
文章非常受歡迎。
如何添加屬性'簽名策略標識符'? –
可能手動,繼承CMSAttribute並使用它。使用轉儲工具查找策略標識符的ASN1代碼。 – soulseekah
這是代碼片段我用於添加簽名策略的標識符:屬性策略=新屬性( \t \t \t \t PKCSObjectIdentifiers.id_aa_ets_sigPolicyId,新DERSet( \t \t \t \t \t \t sigPolicy)); –