Java Key Create getKeyFromFile(String _sFilename, String _sPassword)

Here you can find the source of getKeyFromFile(String _sFilename, String _sPassword)

Description

getKeyFromFile - Gets key value from file, to be used for encryption/decryption

License

Open Source License

Parameter

Parameter Description
_sFilename - Filename of keystore
_sPassword - Password/passphrase to open keystore

Return

- SecreKey object

Declaration

public static SecretKey getKeyFromFile(String _sFilename, String _sPassword) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import javax.crypto.SecretKey;

public class Main {
    private final static String S_KEY_ALIAS = "KEY";

    /**// w  w  w  . j a  v  a2s .c om
     * getKeyFromFile - Gets key value from file, to be used for encryption/decryption 
     * @param _sFilename - Filename of keystore
     * @param _sPassword - Password/passphrase to open keystore
     * @return - SecreKey object
     */
    public static SecretKey getKeyFromFile(String _sFilename, String _sPassword) {
        SecretKey sk = null;
        File fKeyFile = new File(_sFilename);

        System.out.println("DEBUG->getKeyFromFile...");
        if (fKeyFile.exists()) {
            try {
                KeyStore ks = KeyStore.getInstance("JCEKS");
                ks.load(new FileInputStream(_sFilename), _sPassword.toCharArray());
                sk = (SecretKey) ks.getKey(S_KEY_ALIAS, _sPassword.toCharArray());

            } catch (IOException e) {
                System.out.println("Keyutil, IOException [" + e.getMessage() + "]");
                return null;
            } catch (KeyStoreException e) {
                System.out.println("Keyutil, KeyStoreException [" + e.getMessage() + "]");
                return null;
            } catch (NoSuchAlgorithmException e) {
                System.out.println("Keyutil, NoSuchAlgorithmException [" + e.getMessage() + "]");
                return null;
            } catch (CertificateException e) {
                System.out.println("Keyutil, CertificateException [" + e.getMessage() + "]");
                return null;
            } catch (UnrecoverableKeyException e) {
                System.out.println("Keyutil, UnrecoverableException [" + e.getMessage() + "]");
                return null;
            }

        } else {
            System.out.println("File [" + _sFilename + "] does not exist");
        }

        return sk;
    }
}

Related

  1. getKeyFactory()
  2. getKeyFactory()
  3. getKeyFactory(final String algorithm)
  4. getKeyFactory(final String algorithm)
  5. getKeyFactory(String keyType)
  6. getKeyFromFile(String keyFile)
  7. getKeyGenerator(String algorithm)
  8. getKeyKeyManagerFactory(InputStream keyStore, String password, String algorithm, String keyStoreType)
  9. getKeyKeyManagerFactoryByPfx(InputStream keyStore, String password)