4242
4343public class BouncyCastleCrypto {
4444
45- public static byte [] signData (byte [] data , final X509Certificate signingCertificate , final PrivateKey signingKey )
46- throws CertificateEncodingException , OperatorCreationException , CMSException , IOException {
45+ public static byte [] signData (byte [] data , final X509Certificate signingCertificate , final PrivateKey signingKey ) throws CertificateEncodingException , OperatorCreationException , CMSException , IOException {
4746 byte [] signedMessage = null ;
4847 List <X509Certificate > certList = new ArrayList <X509Certificate >();
4948 CMSTypedData cmsData = new CMSProcessableByteArray (data );
5049 certList .add (signingCertificate );
5150 Store certs = new JcaCertStore (certList );
5251 CMSSignedDataGenerator cmsGenerator = new CMSSignedDataGenerator ();
5352 ContentSigner contentSigner = new JcaContentSignerBuilder ("SHA256withRSA" ).build (signingKey );
54- cmsGenerator .addSignerInfoGenerator (
55- new JcaSignerInfoGeneratorBuilder (new JcaDigestCalculatorProviderBuilder ().setProvider ("BC" ).build ())
56- .build (contentSigner , signingCertificate ));
53+ cmsGenerator .addSignerInfoGenerator (new JcaSignerInfoGeneratorBuilder (new JcaDigestCalculatorProviderBuilder ().setProvider ("BC" ).build ()).build (contentSigner , signingCertificate ));
5754 cmsGenerator .addCertificates (certs );
5855 CMSSignedData cms = cmsGenerator .generate (cmsData , true );
5956 signedMessage = cms .getEncoded ();
6057 return signedMessage ;
6158 }
6259
63- public static boolean verifSignData (final byte [] signedData )
64- throws CMSException , IOException , OperatorCreationException , CertificateException {
60+ public static boolean verifSignData (final byte [] signedData ) throws CMSException , IOException , OperatorCreationException , CertificateException {
6561 ByteArrayInputStream bIn = new ByteArrayInputStream (signedData );
6662 ASN1InputStream aIn = new ASN1InputStream (bIn );
6763 CMSSignedData s = new CMSSignedData (ContentInfo .getInstance (aIn .readObject ()));
@@ -81,16 +77,14 @@ public static boolean verifSignData(final byte[] signedData)
8177 return true ;
8278 }
8379
84- public static byte [] encryptData (final byte [] data , X509Certificate encryptionCertificate )
85- throws CertificateEncodingException , CMSException , IOException {
80+ public static byte [] encryptData (final byte [] data , X509Certificate encryptionCertificate ) throws CertificateEncodingException , CMSException , IOException {
8681 byte [] encryptedData = null ;
8782 if (null != data && null != encryptionCertificate ) {
8883 CMSEnvelopedDataGenerator cmsEnvelopedDataGenerator = new CMSEnvelopedDataGenerator ();
8984 JceKeyTransRecipientInfoGenerator jceKey = new JceKeyTransRecipientInfoGenerator (encryptionCertificate );
9085 cmsEnvelopedDataGenerator .addRecipientInfoGenerator (jceKey );
9186 CMSTypedData msg = new CMSProcessableByteArray (data );
92- OutputEncryptor encryptor = new JceCMSContentEncryptorBuilder (CMSAlgorithm .AES128_CBC ).setProvider ("BC" )
93- .build ();
87+ OutputEncryptor encryptor = new JceCMSContentEncryptorBuilder (CMSAlgorithm .AES128_CBC ).setProvider ("BC" ).build ();
9488 CMSEnvelopedData cmsEnvelopedData = cmsEnvelopedDataGenerator .generate (msg , encryptor );
9589 encryptedData = cmsEnvelopedData .getEncoded ();
9690 }
0 commit comments