/** * Gets the private key from input stream. * * @param input the input stream * @return the private * @throws IOException the i/o exception * @throws InvalidKeyException invalid key exception */ public static PrivateKey getPrivate(InputStream input) throws IOException, InvalidKeyException { ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buffer = new byte[4096]; int len = 0; while (-1 != (len = input.read(buffer))) { output.write(buffer, 0, len); } byte[] keyBytes = output.toByteArray(); return getPrivate(keyBytes); }
/** * Gets the private key from file. * * @param file the file * @return the private * @throws IOException the i/o exception * @throws InvalidKeyException invalid key exception */ public static PrivateKey getPrivate(File file) throws IOException, InvalidKeyException { DataInputStream dis = null; try { FileInputStream fis = new FileInputStream(file); dis = new DataInputStream(fis); byte[] keyBytes = new byte[(int) file.length()]; dis.readFully(keyBytes); return getPrivate(keyBytes); } finally { IOUtils.closeQuietly(dis); } }
if (file.exists()) { try { privateKey = KeyUtil.getPrivate(file); } catch (Exception ex) { LOG.debug("Error loading Private Key", ex);
if (file.exists()) { try { privateKey = KeyUtil.getPrivate(file); } catch (Exception ex) { LOG.debug("Error loading private key", ex);
PrivateKey privateKey = KeyUtil.getPrivate(privateKeyInput);
@Test public void testRecreateKeys() throws IOException, InvalidKeyException { KaaClientState state = new KaaClientPropertiesState(new FilePersistentStorage(), CommonsBase64.getInstance(), getProperties(), true); state.getPublicKey(); state.getPrivateKey(); File pub = new File(WORK_DIR + KEY_PUBLIC); File priv = new File(WORK_DIR + KEY_PRIVATE); assertArrayEquals(KeyUtil.getPrivate(priv).getEncoded(), state.getPrivateKey().getEncoded()); assertArrayEquals(KeyUtil.getPublic(pub).getEncoded(), state.getPublicKey().getEncoded()); pub.delete(); priv.delete(); assertNotNull(state.getPublicKey()); assertNotNull(state.getPrivateKey()); }