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

  • SEARCH
  • Home
  • 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 3963250
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:11:34+00:00 2026-05-20T03:11:34+00:00

In an application I want to store sensitive data (which can be arbitrarily large)

  • 0

In an application I want to store sensitive data (which can be arbitrarily large) so that it can later be used for researching. The point is that I don’t want the data to lie around unprotected, so I want to encrypt it before saving it.

On another computer (in a more trustworthy environment) I want to decrypt that data later. The decryption should be possible with readily available tools, for example OpenSSL. Now my question is: How do I do all that?

I already know the abstract steps:

  1. Once and for all generate a public/private key pair.
  2. Encrypt a byte[] in Java using the public key, store it somewhere.
  3. Copy the encrypted data to a “safer” computer.
  4. Decrypt the saved data using a simple OpenSSL command line.

What I’m missing now are the details:

  • What kind of key pair do I need?
  • What encryption scheme should I use? (it’s probably something like “rsa-encrypt a symmetric key, then encrypt the data with that key”)
  • How does the simple OpenSSL command line look like?

[Update 2011-02-13]

After a little research I found a page that describes exactly what I want to do: http://blog.altudov.com/2010/09/27/using-openssl-for-asymmetric-encryption-of-backups/. Basically the answer to my step 4 is this command line:

openssl smime -decrypt \
  -in data.smime -binary -inform DER \
  -inkey key.pem \
  -out data

Now my question is: How can I generate a file from Java whose format is compatible to OpenSSL so I can decompress it using this command line? I want to do this hundrets of times per second, so invoking an external process seems too slow. That’s why I want to do it directly in Java.

  • 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-05-20T03:11:35+00:00Added an answer on May 20, 2026 at 3:11 am

    I finally found exactly what I was looking for. I had just been confused by all the terms like S/MIME, ASN.1, DER, and so on. But finally, and seemingly by coincident, I reached the CMS (whatever that means) package from bouncycastle, which does everything I want. So here is my code:

    package de.roland_illig.crypto;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.security.GeneralSecurityException;
    import java.security.Security;
    import java.security.cert.CertificateFactory;
    import java.security.cert.X509Certificate;
    
    import org.apache.commons.io.IOUtils;
    import org.bouncycastle.cms.CMSEnvelopedDataStreamGenerator;
    import org.bouncycastle.cms.CMSEnvelopedGenerator;
    import org.bouncycastle.cms.CMSException;
    import org.bouncycastle.jce.provider.BouncyCastleProvider;
    
    public class CryptDemo {
    
      private static final File DIR = new File("c:/program files/cygwin/home/roland/crypto/");
    
      private static X509Certificate certificate() throws IOException, GeneralSecurityException {
        InputStream is = new FileInputStream(new File(DIR, "key.pub.der"));
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        X509Certificate cert = (X509Certificate) cf.generateCertificate(is);
        is.close();
        return cert;
      }
    
      private static void encrypt() throws IOException, GeneralSecurityException, CMSException {
        CMSEnvelopedDataStreamGenerator gen = new CMSEnvelopedDataStreamGenerator();
        gen.addKeyTransRecipient(certificate());
    
        InputStream is = new FileInputStream(new File(DIR, "secret"));
        OutputStream out = new FileOutputStream(new File(DIR, "secret.encrypted"));
        OutputStream encryptingOut = gen.open(out, CMSEnvelopedGenerator.AES128_CBC, "BC");
    
        IOUtils.copy(is, encryptingOut);
    
        is.close();
        encryptingOut.close();
        out.close();
      }
    
      public static void main(String[] args) throws Exception {
        Security.addProvider(new BouncyCastleProvider());
    
        encrypt();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On Gnome/KDE you can select in which application you want to open file (Right
I need to store sensitive information (a symmetric encryption key that I want to
I am creating a rails application that needs to store a large amount of
I want to create an ini file to store some settings for my application.
My C++ application stores some common user data in %CSIDL_COMMON_APPDATA%\Company\Product . I want to
I want my application to clean all the temp files it used, the problem
i work at a place where we build applications that process and store sensitive
I am building a windows application to store backups of sensitive files. The purpose
I created one web application so i want to store my past logged user
I have a Silverlight Business Application. I want to store the username and password

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.