|
| 1 | +package com.baeldung.bouncycastle; |
| 2 | + |
| 3 | +import java.io.ByteArrayInputStream; |
| 4 | +import java.io.IOException; |
| 5 | +import java.security.PrivateKey; |
| 6 | +import java.security.cert.CertificateEncodingException; |
| 7 | +import java.security.cert.CertificateException; |
| 8 | +import java.security.cert.X509Certificate; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Collection; |
| 11 | +import java.util.Iterator; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +import org.bouncycastle.asn1.ASN1InputStream; |
| 15 | +import org.bouncycastle.asn1.cms.ContentInfo; |
| 16 | +import org.bouncycastle.cert.X509CertificateHolder; |
| 17 | +import org.bouncycastle.cert.jcajce.JcaCertStore; |
| 18 | +import org.bouncycastle.cms.CMSAlgorithm; |
| 19 | +import org.bouncycastle.cms.CMSEnvelopedData; |
| 20 | +import org.bouncycastle.cms.CMSEnvelopedDataGenerator; |
| 21 | +import org.bouncycastle.cms.CMSException; |
| 22 | +import org.bouncycastle.cms.CMSProcessableByteArray; |
| 23 | +import org.bouncycastle.cms.CMSSignedData; |
| 24 | +import org.bouncycastle.cms.CMSSignedDataGenerator; |
| 25 | +import org.bouncycastle.cms.CMSTypedData; |
| 26 | +import org.bouncycastle.cms.KeyTransRecipientInformation; |
| 27 | +import org.bouncycastle.cms.RecipientInformation; |
| 28 | +import org.bouncycastle.cms.SignerInformation; |
| 29 | +import org.bouncycastle.cms.SignerInformationStore; |
| 30 | +import org.bouncycastle.cms.jcajce.JcaSignerInfoGeneratorBuilder; |
| 31 | +import org.bouncycastle.cms.jcajce.JcaSimpleSignerInfoVerifierBuilder; |
| 32 | +import org.bouncycastle.cms.jcajce.JceCMSContentEncryptorBuilder; |
| 33 | +import org.bouncycastle.cms.jcajce.JceKeyTransEnvelopedRecipient; |
| 34 | +import org.bouncycastle.cms.jcajce.JceKeyTransRecipient; |
| 35 | +import org.bouncycastle.cms.jcajce.JceKeyTransRecipientInfoGenerator; |
| 36 | +import org.bouncycastle.operator.ContentSigner; |
| 37 | +import org.bouncycastle.operator.OperatorCreationException; |
| 38 | +import org.bouncycastle.operator.OutputEncryptor; |
| 39 | +import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder; |
| 40 | +import org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder; |
| 41 | +import org.bouncycastle.util.Store; |
| 42 | + |
| 43 | +public class BouncyCastleCrypto { |
| 44 | + |
| 45 | + public static byte[] signData(byte[] data, final X509Certificate signingCertificate, final PrivateKey signingKey) |
| 46 | + throws CertificateEncodingException, OperatorCreationException, CMSException, IOException { |
| 47 | + byte[] signedMessage = null; |
| 48 | + List<X509Certificate> certList = new ArrayList<X509Certificate>(); |
| 49 | + CMSTypedData cmsData = new CMSProcessableByteArray(data); |
| 50 | + certList.add(signingCertificate); |
| 51 | + Store certs = new JcaCertStore(certList); |
| 52 | + CMSSignedDataGenerator cmsGenerator = new CMSSignedDataGenerator(); |
| 53 | + ContentSigner contentSigner = new JcaContentSignerBuilder("SHA256withRSA").build(signingKey); |
| 54 | + cmsGenerator.addSignerInfoGenerator( |
| 55 | + new JcaSignerInfoGeneratorBuilder(new JcaDigestCalculatorProviderBuilder().setProvider("BC").build()) |
| 56 | + .build(contentSigner, signingCertificate)); |
| 57 | + cmsGenerator.addCertificates(certs); |
| 58 | + CMSSignedData cms = cmsGenerator.generate(cmsData, true); |
| 59 | + signedMessage = cms.getEncoded(); |
| 60 | + return signedMessage; |
| 61 | + } |
| 62 | + |
| 63 | + public static boolean verifSignData(final byte[] signedData) |
| 64 | + throws CMSException, IOException, OperatorCreationException, CertificateException { |
| 65 | + ByteArrayInputStream bIn = new ByteArrayInputStream(signedData); |
| 66 | + ASN1InputStream aIn = new ASN1InputStream(bIn); |
| 67 | + CMSSignedData s = new CMSSignedData(ContentInfo.getInstance(aIn.readObject())); |
| 68 | + aIn.close(); |
| 69 | + bIn.close(); |
| 70 | + Store certs = s.getCertificates(); |
| 71 | + SignerInformationStore signers = s.getSignerInfos(); |
| 72 | + Collection<SignerInformation> c = signers.getSigners(); |
| 73 | + SignerInformation signer = c.iterator().next(); |
| 74 | + Collection<X509CertificateHolder> certCollection = certs.getMatches(signer.getSID()); |
| 75 | + Iterator<X509CertificateHolder> certIt = certCollection.iterator(); |
| 76 | + X509CertificateHolder certHolder = certIt.next(); |
| 77 | + boolean verifResult = signer.verify(new JcaSimpleSignerInfoVerifierBuilder().build(certHolder)); |
| 78 | + if (!verifResult) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + return true; |
| 82 | + } |
| 83 | + |
| 84 | + public static byte[] encryptData(final byte[] data, X509Certificate encryptionCertificate) |
| 85 | + throws CertificateEncodingException, CMSException, IOException { |
| 86 | + byte[] encryptedData = null; |
| 87 | + if (null != data && null != encryptionCertificate) { |
| 88 | + CMSEnvelopedDataGenerator cmsEnvelopedDataGenerator = new CMSEnvelopedDataGenerator(); |
| 89 | + JceKeyTransRecipientInfoGenerator jceKey = new JceKeyTransRecipientInfoGenerator(encryptionCertificate); |
| 90 | + cmsEnvelopedDataGenerator.addRecipientInfoGenerator(jceKey); |
| 91 | + CMSTypedData msg = new CMSProcessableByteArray(data); |
| 92 | + OutputEncryptor encryptor = new JceCMSContentEncryptorBuilder(CMSAlgorithm.AES128_CBC).setProvider("BC") |
| 93 | + .build(); |
| 94 | + CMSEnvelopedData cmsEnvelopedData = cmsEnvelopedDataGenerator.generate(msg, encryptor); |
| 95 | + encryptedData = cmsEnvelopedData.getEncoded(); |
| 96 | + } |
| 97 | + return encryptedData; |
| 98 | + } |
| 99 | + |
| 100 | + public static byte[] decryptData(final byte[] encryptedData, final PrivateKey decryptionKey) throws CMSException { |
| 101 | + byte[] decryptedData = null; |
| 102 | + if (null != encryptedData && null != decryptionKey) { |
| 103 | + CMSEnvelopedData envelopedData = new CMSEnvelopedData(encryptedData); |
| 104 | + Collection<RecipientInformation> recip = envelopedData.getRecipientInfos().getRecipients(); |
| 105 | + KeyTransRecipientInformation recipientInfo = (KeyTransRecipientInformation) recip.iterator().next(); |
| 106 | + JceKeyTransRecipient recipient = new JceKeyTransEnvelopedRecipient(decryptionKey); |
| 107 | + decryptedData = recipientInfo.getContent(recipient); |
| 108 | + } |
| 109 | + return decryptedData; |
| 110 | + } |
| 111 | +} |
0 commit comments