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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:46:53+00:00 2026-06-18T00:46:53+00:00

I have a generated a private key using CertAndKeyGen class. Now I want to

  • 0

I have a generated a private key using CertAndKeyGen class. Now I want to encrypt the private key with a password and use it as key while starting PostgreSQL server. Can some one please help me with java code to encrypt the private key, where googling dint help me out. Below is my code I use to generate Private Key.

    CertAndKeyGen keypair = new CertAndKeyGen("RSA", "SHA1WithRSA", null);
    PrivateKey privKey = keypair.getPrivateKey();

Any help or suggestion will be really appreciated.

  • 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-18T00:46:54+00:00Added an answer on June 18, 2026 at 12:46 am
    public class KeyEncryptExample {
    
        public static void main(String[] args) {
            try {
                String key = "mariahussain"; // needs to be at least 8 characters for DES
    
                FileInputStream fis = new FileInputStream("C:/Users/hussain.a/Desktop/original.txt");
                FileOutputStream fos = new FileOutputStream("C:/Users/hussain.a/Desktop/encrypted.txt");
                encrypt(key, fis, fos);
    
                FileInputStream fis2 = new FileInputStream("C:/Users/hussain.a/Desktop/encrypted.txt");
                FileOutputStream fos2 = new FileOutputStream("C:/Users/hussain.a/Desktop/decrypted.txt");
                decrypt(key, fis2, fos2);
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    
        public static void encrypt(String key, InputStream is, OutputStream os) throws Throwable {
            encryptOrDecrypt(key, Cipher.ENCRYPT_MODE, is, os);
        }
    
        public static void decrypt(String key, InputStream is, OutputStream os) throws Throwable {
            encryptOrDecrypt(key, Cipher.DECRYPT_MODE, is, os);
        }
    
        public static void encryptOrDecrypt(String key, int mode, InputStream is, OutputStream os) throws Throwable {
    
            DESKeySpec dks = new DESKeySpec(key.getBytes());
            SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
            SecretKey desKey = skf.generateSecret(dks);
            Cipher cipher = Cipher.getInstance("DES"); // DES/ECB/PKCS5Padding for SunJCE
    
            if (mode == Cipher.ENCRYPT_MODE) {
                cipher.init(Cipher.ENCRYPT_MODE, desKey);
                CipherInputStream cis = new CipherInputStream(is, cipher);
                doCopy(cis, os);
            } else if (mode == Cipher.DECRYPT_MODE) {
                cipher.init(Cipher.DECRYPT_MODE, desKey);
                CipherOutputStream cos = new CipherOutputStream(os, cipher);
                doCopy(is, cos);
            }
        }
    
        public static void doCopy(InputStream is, OutputStream os) throws IOException {
            byte[] bytes = new byte[64];
            int numBytes;
            while ((numBytes = is.read(bytes)) != -1) {
                os.write(bytes, 0, numBytes);
            }
            os.flush();
            os.close();
            is.close();
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mapping class using JPA annotations, and I want it to use
I have a private key that I decrypt using a password from the user,
I have generated a pdf file with password protection by using the following code:
I have generated a DAL layer with Subsonic2.2 for a .NET4.0 application, using c#
I have generated a C# SharePoint Sequential Workflow project using the very handy STSDEV
I have generated inputs with price values. Example: <input type=text value=59,00/> Now I should
I have generated using openssl mycert.pem which contents the certificate. And I converted the
I have generated a bunch of png files to use as markers on my
I have generated class diagram by gight clicking and selecting generate class diagram usign
Using Java 1.4.2 with unlimited jurisdiction policy files installed. I have a class that

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.