/** * Gets the public key from input stream. * * @param input the input stream * @return the public * @throws IOException the i/o exception * @throws InvalidKeyException invalid key exception */ public static PublicKey getPublic(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 getPublic(keyBytes); }
/** * Sets the remote public key. * * @param remotePublicKey the new remote public key * @throws GeneralSecurityException the general security exception */ public void setRemotePublicKey(byte[] remotePublicKey) throws GeneralSecurityException { this.remotePublicKey = KeyUtil.getPublic(remotePublicKey); if (LOG.isTraceEnabled()) { LOG.trace("RemotePublicKey {}", this.remotePublicKey != null ? bytesToHex(this.remotePublicKey.getEncoded()) : "empty"); } }
/** * Gets the public key from file. * * @param file the file * @return the public * @throws IOException the i/o exception * @throws InvalidKeyException invalid key exception */ public static PublicKey getPublic(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 getPublic(keyBytes); } finally { IOUtils.closeQuietly(dis); } }
if (file.exists()) { try { publicKey = KeyUtil.getPublic(file); } catch (Exception ex) { LOG.debug("Error loading Public Key", ex);
if (file.exists()) { try { publicKey = KeyUtil.getPublic(file); } catch (Exception ex) { LOG.debug("Error loading public key", ex);
/** * All-args constructor. */ public IpTransportInfo(TransportConnectionInfo parent) { super(parent.getServerType(), new ProtocolMetaData(parent.getAccessPointId(), new ProtocolVersionPair(parent.getTransportId() .getProtocolId(), parent.getTransportId().getProtocolVersion()), ByteBuffer.wrap(parent.getConnectionInfo()))); ByteBuffer buf = md.getConnectionInfo().duplicate(); byte[] publicKeyData = new byte[buf.getInt()]; buf.get(publicKeyData); try { this.publicKey = KeyUtil.getPublic(publicKeyData); } catch (InvalidKeyException ex) { LOG.error("Can't initialize public key", ex); throw new RuntimeException(ex); } byte[] hostData = new byte[buf.getInt()]; buf.get(hostData); this.host = new String(hostData, UTF8); this.port = buf.getInt(); }
privateKeyInput = storage.openForRead(clientPrivateKeyFileLocation); PublicKey publicKey = KeyUtil.getPublic(publicKeyInput); PrivateKey privateKey = KeyUtil.getPrivate(privateKeyInput);
private PublicKey getPublicKey(ClientSync request) throws GeneralSecurityException { PublicKey endpointKey = null; if (request.getProfileSync() != null && request.getProfileSync().getEndpointPublicKey() != null) { byte[] publicKeySrc = request.getProfileSync().getEndpointPublicKey().array(); endpointKey = KeyUtil.getPublic(publicKeySrc); } if (endpointKey == null) { EndpointObjectHash hash = getEndpointObjectHash(request); endpointKey = cacheService.getEndpointKey(hash); } return endpointKey; }
@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()); }
cacheService.putEndpointKey(keyHash, KeyUtil.getPublic(dto.getEndpointKey())); } catch (InvalidKeyException ex) { LOG.error("Can't generate public key for endpoint key: {}. Reason: {}",