Here you can find the source of getKeyFromFile(String _sFilename, String _sPassword)
Parameter | Description |
---|---|
_sFilename | - Filename of keystore |
_sPassword | - Password/passphrase to open keystore |
public static SecretKey getKeyFromFile(String _sFilename, String _sPassword)
//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; } }