Search code examples
javacryptographykeystore

JAVA: Unable to resolve java.security.KeyStoreException: unrecgonized keystore format


I'm running into some problems while coding a class for encryption for my application. This class first checks if an empty KeyStore exists. This KeyStore exists solely for storing symmetric encryption keys for my application only. If not, it then runs this code

    private void createKeyStore() throws Exception
    {
        KeyStore newKeyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        newKeyStore.load(null, password.toCharArray());
        newKeyStore.store(outputStream, password.toCharArray());
    }

This code creates a KeyStore file using the default JKS (Java Keystore) format. It then stores said KeyStore file using the .store() function in the directory outputStream encapsulates. After it creates the new KeyStore file, it then tries to load it again.

    private void loadKeyStore() throws Exception
    {
        keyStore = KeyStore.getInstance(new File(keyStoreAddress), password.toCharArray());
        keyStore.load(inputStream, password.toCharArray());
        System.out.println("Key stored loaded");
    }

Problem is that whenever I try to load it, I run into the KeyStoreException: unrecognized keystore format error. The error can be found in the first line of this function. I'm not sure why a Java program doesn't recognize the Java's default JKS format, but I think I may have messed somewhere in the code.

Please advise.

NOTES:

I am not using KeyTool. I'm not familiar with what KeyTool is and I prefer not to spend time learning a new tool.

I am also new to coding in cryptography. Sorry in advance if my request sounds simple to solve.

**EDIT 1: ** Here is the full code that produces the error with all unnecessary code stripped away.

import java.security.*;
import java.io.*;

public final class Cryptographer
{
    private final String keyStoreAddress = "C:\\Users\\LeafarYart\\Desktop\\tempDirectory\\lmskeystore";
    private final String password = "%x9~SK5XS9xz9zK`";

    private KeyStore keyStore;
    private InputStream inputStream;
    private OutputStream outputStream;

    public static void main(String[] args) throws Exception
    {
        Cryptographer crypt = new Cryptographer();
    }

    public Cryptographer() throws Exception
    {
        try
        {
            setInputStream();
        } catch(Exception e)
            {
                //Intentionally left blank.
            }

        setOutputStream();

        File keyStoreFile = new File(keyStoreAddress);
        if (keyStoreFile.exists())
        {
            loadKeyStore();
        } else
            {
                createKeyStore();
                setInputStream();
                loadKeyStore();
            }
    }

    private void createKeyStore() throws Exception
    {
        KeyStore newKeyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        newKeyStore.load(null, password.toCharArray());
        newKeyStore.store(outputStream, password.toCharArray());
    }
    
    private void loadKeyStore() throws Exception
    {
        keyStore = KeyStore.getInstance(new File(keyStoreAddress), password.toCharArray());
        keyStore.load(inputStream, password.toCharArray());
        System.out.println("Key stored loaded");
    }

    private void setInputStream() throws FileNotFoundException
    {
        inputStream = new FileInputStream(keyStoreAddress);
    }
    
    private void setOutputStream() throws FileNotFoundException
    {
        outputStream = new FileOutputStream(keyStoreAddress);
    }
}

Solution

  • Resolved. I figured out the problem myself. Just as @user207421 and @dave_thompson_085 mentioned, my error was not properly setting up a KeyStore instance using the PKCS12. PKCS12 seems to be the only KeyStore format Java supports without relying on external Provider for cryptography.

    I also set the KeyStore file to the extension 'ks'. That is the extension for all KeyStore files.

    Thank you to everyone who helped me debug my program.

    Please see the code below.

    
    import javax.crypto.*;
    import java.security.*;
    import java.io.*;
    
    public final class Cryptographer
    {
        private final String keyStoreAddress = "C:\\Users\\LeafarYart\\Desktop\\tempDirectory\\lmskeystore.ks";
        private final String password = "%x9~SK5XS9xz9zK`";
    
        private SecretKey key;
        private KeyStore keyStore;
    
        public static void main(String[] args) throws Exception
        {
            Cryptographer crypt = new Cryptographer();
        }
    
        public Cryptographer() throws Exception
        {
            File targetFile = new File(keyStoreAddress);
            if(targetFile.exists())
            {
                loadKeyStore();
            }
            else
                {
                    createKeyStore();
                    loadKeyStore();
                }
        }
        
        //creation and loading of key stores
        private void createKeyStore() throws Exception
        {
            OutputStream outputStream = new FileOutputStream(keyStoreAddress);
    
            keyStore = KeyStore.getInstance("PKCS12");
            keyStore.load(null, password.toCharArray());
            keyStore.store(outputStream, password.toCharArray());
            System.out.println("Key stored created");
        }
    
        private void loadKeyStore() throws Exception
        {
            InputStream inputStream = new FileInputStream(keyStoreAddress);
    
            keyStore = KeyStore.getInstance("PKCS12");
            keyStore.load(inputStream, password.toCharArray());
            System.out.println("Key stored loaded");
        }
    }