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 9165845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:00:15+00:00 2026-06-17T15:00:15+00:00

I am implementing SSL certificate and key in my application. I have created private

  • 0

I am implementing SSL certificate and key in my application. I have created private key using CertAndKeyGen class. I am trying to encrypt the private key with a password, which I have achieved it through PBE and Cipher class. I would like to write the encrypted private key into a file in PEM format. I tried with FileOutputStream which is working but PrintWriter is not working as excepted.

Below is my code,

    final CertAndKeyGen keypair = new CertAndKeyGen("RSA", "SHA1WithRSA", null);
    keypair.generate(1024);
    final PrivateKey privKey = keypair.getPrivateKey();
    byte[] encodedprivkey = privKey.getEncoded();
    String MYPBEALG = "PBEWithSHA1AndDESede";
    String password = "test123";
    int count = 20;// hash iteration count
    Random random = new Random();
    byte[] salt = new byte[8];
    random.nextBytes(salt);
    PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, count);
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
    SecretKeyFactory keyFac = SecretKeyFactory.getInstance(MYPBEALG);
    SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
    Cipher pbeCipher = Cipher.getInstance(MYPBEALG);
    // Initialize PBE Cipher with key and parameters
    pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
    // Encrypt the encoded Private Key with the PBE key
    byte[] ciphertext = pbeCipher.doFinal(encodedprivkey);
    // Now construct  PKCS #8 EncryptedPrivateKeyInfo object
    AlgorithmParameters algparms = AlgorithmParameters.getInstance(MYPBEALG);
    algparms.init(pbeParamSpec);
    EncryptedPrivateKeyInfo encinfo = new EncryptedPrivateKeyInfo(algparms,ciphertext);
    byte[] encryptedPkcs8 = encinfo.getEncoded();

    // Now I am writing the encrypted private key into a file.
    // Using FileOutputStream 

    FileOutputStream out = new FileOutputStream("usingOutEncrypedPrivkey");
    out.write(Base64.encodeBase64(encryptedPkcs8, true));
    out.flush();
    out.close();

    // Using PrintWriter 
    PrintWriter pw = new PrintWriter("usingPwEncryptedPrivKey");
    pw.println("-----BEGIN "+ privKey.getAlgorithm() + " PRIVATE KEY-----");
    pw.println(Base64.encodeBase64(encryptedPkcs8));
    pw.println("-----END "+ privKey.getAlgorithm() +" PRIVATE KEY-----");
    pw.close();

Below are the files saved,

    usingOutEncrypedPrivkey  // Which was saved using FileOutputStream

    MIICoTAbBgoqhkiG9w0BDAEDMA0ECL4xgraq2hXxAgEUBIICgFENaB8EA/kR0ymSC8vcyj1fNFbP
    iR+mXkBk7aH3eF7fpP8yqCvtN0/0VqHi/w/Z2CLgiib2s/zuiVPtWI8vsRRPXmD9PYxZp3ilLpD4
                            .....
                            .....   
    9nH8HdQf584c3sKYEErDQvJR2SmbUPtNq4cB6ocUuiOTztBqRXAHeaWavnqHFxHUT7c=


    usingPwEncryptedPrivKey  // Which was saved using PrintWriter
   -----BEGIN RSA PRIVATE KEY-----
   [B@19e3118a
   -----END RSA PRIVATE KEY-----

Why is the PrintWriter writing as “[B@19e3118a”, instead of the bytes like FileOutputStream. I want to use PrintWriter since I want to make use of PrintWriter.println() function. Please help me out. Thanks in advance.

  • 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-17T15:00:18+00:00Added an answer on June 17, 2026 at 3:00 pm
    • OutputStream.write(byte[]) writes the bytes as binary data.
    • PrintWriter.print(byte[]) calls toString() on the array and writes out the result. The [B@19e3118a is the toString() representation of your array.

    Given that the string only contains ASCII characters, you could use String(byte[]) on your array, and print the result.

    Alternatively, if you’re using the Apache Commons Base64 class, you should simply use the encodeBase64String() method instead of encodeBase64() [Thanks @SimonC].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a site which was not using SSL and SSL just have been
Implementing a simple Login screen using JSF and Spring and Hibernate. I have written
I am implementing CAS for Single Sign on Authentication, I have used SSL for
I have a Rails application with a public REST API which authenticates API clients
I'm trying to understand how hosting providers implementing the support for SSL. As I
I have a REST API which clients connect over SSL (self signed cert 2048bit)
I am implementing a Push Notification server for our iPhone application. I am using
In my application I am implementing the Push Notification Service. I have a Content
implementing publishActivity in PHP using the REST API using this code: $activity = array(
When implementing a hash table using a good hash function (one where the probability

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.