Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7976893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:02:13+00:00 2026-06-04T09:02:13+00:00

I want to sign a text file (may be a .exe file or something

  • 0

I want to sign a text file (may be a .exe file or something else in the future)
using PKCS#7 and verify the signature using Java.

  1. What do I need to know?
  2. Where will I find an API (.jar and documentation)?
  3. What are the steps I need to follow in order to sign data and verify the data?

Please provide me code snippet if possible.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T09:02:15+00:00Added an answer on June 4, 2026 at 9:02 am

    I reckon you need the following 2 Bouncy Castle jars to generate the PKCS7 digital signature:

    • bcprov-jdk15on-147.jar (for JDK 1.5 – JDK 1.7)

    • bcmail-jdk15on-147.jar (for JDK 1.5 – JDK 1.7)

    You can download the Bouncy Castle jars from here.

    You need to setup your keystore with the public & private key pair.
    You need only the private key to generate the digital signature & the public key to verify it.

    Here’s how you’d pkcs7 sign content (Exception handling omitted for brevity) :

    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.security.KeyStore;
    import java.security.PrivateKey;
    import java.security.Security;
    import java.security.cert.Certificate;
    import java.security.cert.X509Certificate;
    import java.util.ArrayList;
    import java.util.List;
    import org.bouncycastle.cert.jcajce.JcaCertStore;
    import org.bouncycastle.cms.CMSProcessableByteArray;
    import org.bouncycastle.cms.CMSSignedData;
    import org.bouncycastle.cms.CMSSignedDataGenerator;
    import org.bouncycastle.cms.CMSTypedData;
    import org.bouncycastle.cms.jcajce.JcaSignerInfoGeneratorBuilder;
    import org.bouncycastle.jce.provider.BouncyCastleProvider;
    import org.bouncycastle.operator.ContentSigner;
    import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
    import org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder;
    import org.bouncycastle.util.Store;
    import org.bouncycastle.util.encoders.Base64;
    
    public final class PKCS7Signer {
    
        private static final String PATH_TO_KEYSTORE = "/path/to/keyStore";
        private static final String KEY_ALIAS_IN_KEYSTORE = "My_Private_Key";
        private static final String KEYSTORE_PASSWORD = "MyPassword";
        private static final String SIGNATUREALGO = "SHA1withRSA";
    
        public PKCS7Signer() {
        }
    
        KeyStore loadKeyStore() throws Exception {
    
            KeyStore keystore = KeyStore.getInstance("JKS");
            InputStream is = new FileInputStream(PATH_TO_KEYSTORE);
            keystore.load(is, KEYSTORE_PASSWORD.toCharArray());
            return keystore;
        }
    
        CMSSignedDataGenerator setUpProvider(final KeyStore keystore) throws Exception {
    
            Security.addProvider(new BouncyCastleProvider());
    
            Certificate[] certchain = (Certificate[]) keystore.getCertificateChain(KEY_ALIAS_IN_KEYSTORE);
    
            final List<Certificate> certlist = new ArrayList<Certificate>();
    
            for (int i = 0, length = certchain == null ? 0 : certchain.length; i < length; i++) {
                certlist.add(certchain[i]);
            }
    
            Store certstore = new JcaCertStore(certlist);
    
            Certificate cert = keystore.getCertificate(KEY_ALIAS_IN_KEYSTORE);
    
            ContentSigner signer = new JcaContentSignerBuilder(SIGNATUREALGO).setProvider("BC").
                    build((PrivateKey) (keystore.getKey(KEY_ALIAS_IN_KEYSTORE, KEYSTORE_PASSWORD.toCharArray())));
    
            CMSSignedDataGenerator generator = new CMSSignedDataGenerator();
    
            generator.addSignerInfoGenerator(new JcaSignerInfoGeneratorBuilder(new JcaDigestCalculatorProviderBuilder().setProvider("BC").
                    build()).build(signer, (X509Certificate) cert));
    
            generator.addCertificates(certstore);
    
            return generator;
        }
    
        byte[] signPkcs7(final byte[] content, final CMSSignedDataGenerator generator) throws Exception {
    
            CMSTypedData cmsdata = new CMSProcessableByteArray(content);
            CMSSignedData signeddata = generator.generate(cmsdata, true);
            return signeddata.getEncoded();
        }
    
        public static void main(String[] args) throws Exception {
    
            PKCS7Signer signer = new PKCS7Signer();
            KeyStore keyStore = signer.loadKeyStore();
            CMSSignedDataGenerator signatureGenerator = signer.setUpProvider(keyStore);
            String content = "some bytes to be signed";
            byte[] signedBytes = signer.signPkcs7(content.getBytes("UTF-8"), signatureGenerator);
            System.out.println("Signed Encoded Bytes: " + new String(Base64.encode(signedBytes)));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to sign and verify a string using my company's digital certificate. The
I am developing a text highlighter in a file using the java regex pattern
I´m trying to sign some text or XML node using my certificates, installed on
I want to find a text * 50 (multiply sign space 50). How ?
I want to pass the ampersand '&' sign in text via ajax to php
I want a '$' Sign in the text field which cannot be erased. A
I am having a text file that contains users name.I want to extract users
I'm trying to write a sign up form using CodeIgniter and want to use
This is my xml document. I want to sign only the userID part using
I want to know how to determine someone's star sign from their birthday using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.