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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:46:30+00:00 2026-06-09T12:46:30+00:00

Reading Oracle documentation, I see that by default JKS files are encrypted using PBEWithMD5AndTripleDES

  • 0

Reading Oracle documentation, I see that by default JKS files are encrypted using PBEWithMD5AndTripleDES. While DES alone makes me feel uneasy, MD5 lights a big red light. I’d like to use PBEWithSHA256And256BitAES-CBC-BC or PBEWithSHA256And128bitAES-CBC-BC to encrypt private keys.

Do I have to write new Cryptography Service Provider implementing whole KeyStore interface or is it possible to parametrise the creation of KeyStore (either using plain java or BouncyCastle)?

EDIT:
A little bit of background.

I know that 3DES isn’t broken, just as is MD5 used as KDF (or in PBE). The problem is, that this is the situation for now. For all we know, MD5 may be broken to the level MD4 is broken tomorrow. My application life is at least 10 years, and it’s very likely it’s much more. Somehow I don’t see people after those 10 years delving deep into working crypto code just because it may not be secure. One just needs to look at last few of the big “mishaps” with password leaks to see how likely is that, and that were obvious things to anyone that saw the raw database.

That being said: NSA crypto suite B allows only AES for symmetric encryption, of any kind.
NIST list only SHA-1 and SHA-2 algorithms for HMAC and KDF use, while SHA-1 use is not recommended. Suite B allows only SHA-2 hash functions. Those algorithms are publicly available, so why shouldn’t I use them?

  • 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-09T12:46:32+00:00Added an answer on June 9, 2026 at 12:46 pm

    In the end I went with PKCS#8 files encrypted using PBEWithSHA256And256BitAES-CBC-BC

    Encryption:

    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.security.AlgorithmParameters;
    import java.security.InvalidAlgorithmParameterException;
    import java.security.InvalidKeyException;
    import java.security.KeyPair;
    import java.security.KeyPairGenerator;
    import java.security.NoSuchAlgorithmException;
    import java.security.NoSuchProviderException;
    import java.security.SecureRandom;
    import java.security.Security;
    import java.security.spec.InvalidKeySpecException;
    import java.security.spec.InvalidParameterSpecException;
    
    import javax.crypto.BadPaddingException;
    import javax.crypto.Cipher;
    import javax.crypto.EncryptedPrivateKeyInfo;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.NoSuchPaddingException;
    import javax.crypto.SecretKey;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.PBEKeySpec;
    import javax.crypto.spec.PBEParameterSpec;
    
    import org.bouncycastle.asn1.bc.BCObjectIdentifiers;
    
    public class EncodePKCS8 {
    
    /**
     * @param args
     * @throws NoSuchAlgorithmException 
     * @throws InvalidKeySpecException 
     * @throws NoSuchPaddingException 
     * @throws InvalidAlgorithmParameterException 
     * @throws InvalidKeyException 
     * @throws BadPaddingException 
     * @throws IllegalBlockSizeException 
     * @throws InvalidParameterSpecException 
     * @throws IOException 
     * @throws NoSuchProviderException 
     */
    public static void main(String[] args) throws NoSuchAlgorithmException,
    InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException,
    InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException,
    InvalidParameterSpecException, IOException, NoSuchProviderException
    {
        // before we can do anything with BouncyCastle we have to register its provider
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    
        String password = "Very long and complex password";
    
        // generate RSA key pair
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048);
        KeyPair keyPair = keyPairGenerator.genKeyPair();
    
        byte[] encryptedPkcs8 = encryptPrivateKey(password, keyPair);
    
        FileOutputStream fos = new FileOutputStream("privkey.p8");
        fos.write(encryptedPkcs8);
        fos.close();
    
        return;
    }
    
    private static byte[] encryptPrivateKey(String password, KeyPair keyPair)
        throws InvalidKeyException, NoSuchAlgorithmException, NoSuchProviderException,
        InvalidKeySpecException, NoSuchPaddingException,
        InvalidAlgorithmParameterException, IllegalBlockSizeException,
        BadPaddingException, InvalidParameterSpecException, IOException
    {
        int count = 100000; // hash iteration count, best to leave at default or increase
        return encryptPrivateKey(password, keyPair, count);
    }
    
    /**
     * 
     * @param password
     * @param keyPair
     * @param count
     * @return PKCS#8 encoded, encrypted keyPair
     * @throws NoSuchAlgorithmException
     * @throws NoSuchProviderException
     * @throws InvalidKeySpecException
     * @throws NoSuchPaddingException
     * @throws InvalidKeyException
     * @throws InvalidAlgorithmParameterException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     * @throws InvalidParameterSpecException
     * @throws IOException
     */
    private static byte[] encryptPrivateKey(String password, 
            KeyPair keyPair, int count) throws NoSuchAlgorithmException,
            NoSuchProviderException, InvalidKeySpecException,
            NoSuchPaddingException, InvalidKeyException,
            InvalidAlgorithmParameterException, IllegalBlockSizeException,
            BadPaddingException, InvalidParameterSpecException, IOException
    {
        // extract the encoded private key, this is an unencrypted PKCS#8 private key
            byte[] encodedprivkey = keyPair.getPrivate().getEncoded();
    
            // Use a PasswordBasedEncryption (PBE) algorithm, OID of this algorithm will be saved
            // in the PKCS#8 file, so changing it (when more standard algorithm or safer
            // algorithm is available) doesn't break backwards compatibility.
            // In other words, decryptor doesn't need to know the algorithm before it will be
            // able to decrypt the PKCS#8 object.
            String encAlg = BCObjectIdentifiers.bc_pbe_sha256_pkcs12_aes256_cbc.getId();
    
            SecureRandom random = new SecureRandom();
            byte[] salt = new byte[16];
            random.nextBytes(salt);
    
            // Create PBE parameter set
            PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, count);
            PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
            SecretKeyFactory keyFac = SecretKeyFactory.getInstance(encAlg, "BC");
            SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
    
            Cipher pbeCipher = Cipher.getInstance(encAlg, "BC");
    
            // 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(encAlg, "BC");
            algparms.init(pbeParamSpec);
            EncryptedPrivateKeyInfo encinfo = new EncryptedPrivateKeyInfo(algparms, ciphertext);
    
            // DER encoded PKCS#8 encrypted key
            byte[] encryptedPkcs8 = encinfo.getEncoded();
    
            return encryptedPkcs8;
        }
    }
    

    Decryption:

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.security.AlgorithmParameters;
    import java.security.InvalidAlgorithmParameterException;
    import java.security.InvalidKeyException;
    import java.security.Key;
    import java.security.KeyFactory;
    import java.security.NoSuchAlgorithmException;
    import java.security.PrivateKey;
    import java.security.PublicKey;
    import java.security.Security;
    import java.security.interfaces.RSAKey;
    import java.security.interfaces.RSAPrivateCrtKey;
    import java.security.interfaces.RSAPublicKey;
    import java.security.spec.InvalidKeySpecException;
    import java.security.spec.KeySpec;
    import java.security.spec.RSAPublicKeySpec;
    
    import javax.crypto.Cipher;
    import javax.crypto.EncryptedPrivateKeyInfo;
    import javax.crypto.NoSuchPaddingException;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.PBEKeySpec;
    
    public class DecodePKCS8 {
    
        /**
         * @param args
         * @throws IOException 
         * @throws NoSuchPaddingException When file is corrupted
         * @throws NoSuchAlgorithmException When no BC provider has been loaded 
         * @throws InvalidKeySpecException When decryption of file failed
         * @throws InvalidAlgorithmParameterException When file is corrupted
         * @throws InvalidKeyException When Unlimited cryptography extensions are not installed
         */
        public static void main(String[] args) throws
        IOException, NoSuchAlgorithmException, NoSuchPaddingException,
        InvalidKeySpecException, InvalidKeyException, InvalidAlgorithmParameterException
        {
            // before we can do anything with BouncyCastle we have to register its provider
            Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    
            String password = "Very long and complex password";
    
            // read DER encoded key from files
            byte[] encodedprivkey = getFileBytes("privkey.p8");
    
            // this is a encoded PKCS#8 encrypted private key
            EncryptedPrivateKeyInfo ePKInfo = new EncryptedPrivateKeyInfo(encodedprivkey);
    
            // first we have to read algorithm name and parameters (salt, iterations) used
            // to encrypt the file
            Cipher cipher = Cipher.getInstance(ePKInfo.getAlgName());
            PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
    
            SecretKeyFactory skFac = SecretKeyFactory.getInstance(ePKInfo
                    .getAlgName());
            Key pbeKey = skFac.generateSecret(pbeKeySpec);
    
            // Extract the iteration count and the salt
            AlgorithmParameters algParams = ePKInfo.getAlgParameters();
            cipher.init(Cipher.DECRYPT_MODE, pbeKey, algParams);
    
            // Decrypt the encryped private key into a PKCS8EncodedKeySpec
            KeySpec pkcs8KeySpec = ePKInfo.getKeySpec(cipher);
    
            // Now retrieve the RSA Public and private keys by using an
            // RSA key factory.
            KeyFactory rsaKeyFac = KeyFactory.getInstance("RSA");
            // First get the private key
            PrivateKey rsaPriv = rsaKeyFac.generatePrivate(pkcs8KeySpec);
            // Now derive the RSA public key from the private key
            RSAPublicKeySpec rsaPubKeySpec = new RSAPublicKeySpec(((RSAKey) rsaPriv).getModulus(),
                    ((RSAPrivateCrtKey) rsaPriv).getPublicExponent());
            PublicKey rsaPubKey = (RSAPublicKey) rsaKeyFac.generatePublic(rsaPubKeySpec);
    
        System.out.println("Key extracted, public part: " + rsaPubKey);
        }
    
        private static byte[] getFileBytes(String path)
        {
            File f = new File(path);
            int sizecontent = ((int) f.length()); // no key file will ever be bigger than 4GiB...
            byte[] data = new byte[sizecontent];
            try 
                {
                FileInputStream freader = new FileInputStream(f);
                freader.read(data, 0, sizecontent) ;
                freader.close();
                return data;
                }
            catch(IOException ioe)
            {
                System.out.println(ioe.toString());
                return null;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While I was reading Oracle Swing documentation, I came across this example of a
I'm attempting to deploy ODP.Net with my application using the Oracle Instaclient. While reading
After searching online and reading the Oracle documentation for this, I need some help
I was reading Code Conventions for Java from http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-137265.html#587 . In that, they have
I am reading the string of data from the oracle database that may or
I am reading about Direct-Path INSERT in oracle documentation Loading Tables It is written
I am reading data from an external organisation using Oracle.DataAccess through the Oracle10g provider.
I was reading Oreilly Java Programming With Oracle JDBC edition and I notice that
Reading blogs and articles on using css in javafx, I see class selectors such
I was reading the official Oracle documentation about Concurrency in Java and I was

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.