public String getPublicKey() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); keyPair.writePublicKey(baos, ""); return baos.toString(); }
public String getPublicKey() { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); keyPair.writePublicKey(baos, ""); return baos.toString(); }
/** * Writes the public key with the specified comment to the file. * * @param name file name * @param comment comment * @see #writePublicKey(java.io.OutputStream out, String comment) */ public void writePublicKey(String name, String comment) throws java.io.FileNotFoundException, java.io.IOException { FileOutputStream fos = new FileOutputStream(name); writePublicKey(fos, comment); fos.close(); }
/** * Writes the public key with the specified comment to the file. * @param name file name * @param comment comment * @see #writePublicKey(java.io.OutputStream out, String comment) */ public void writePublicKey(String name, String comment) throws java.io.FileNotFoundException, java.io.IOException{ FileOutputStream fos=new FileOutputStream(name); writePublicKey(fos, comment); fos.close(); }
public void writePublicKey(String name, String comment) throws java.io.FileNotFoundException, java.io.IOException{ FileOutputStream fos=new FileOutputStream(name); writePublicKey(fos, comment); fos.close(); }
/** * Writes the public key with the specified comment to the file. * @param name file name * @param comment comment * @see #writePublicKey(java.io.OutputStream out, String comment) */ public void writePublicKey(String name, String comment) throws java.io.FileNotFoundException, java.io.IOException{ FileOutputStream fos=new FileOutputStream(name); writePublicKey(fos, comment); fos.close(); }
/** * Writes the public key with the specified comment to the file. * @param name file name * @param comment comment * @see #writePublicKey(java.io.OutputStream out, String comment) */ public void writePublicKey(String name, String comment) throws java.io.FileNotFoundException, java.io.IOException{ FileOutputStream fos=new FileOutputStream(name); writePublicKey(fos, comment); fos.close(); }
protected String getPublicKey(com.jcraft.jsch.KeyPair keyPair, String name) { ByteArrayOutputStream out = new ByteArrayOutputStream(); keyPair.writePublicKey(out, name); return getUTF8String(out.toByteArray()); }
public byte[] getFormattedKey() throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); switch (format) { case SshPublicKeyFile.SECSH_FORMAT: jschKey.keyPair.writeSECSHPublicKey(baos, comment); break; case SshPublicKeyFile.OPENSSH_FORMAT: jschKey.keyPair.writePublicKey(baos, comment); break; default: throw new UnsupportedOperationException("Public key file format not supported."); } return baos.toByteArray(); }
public static String generateKeyPair(Context context) { JSch jsch = new JSch(); KeyPair kpair = null; try { kpair = KeyPair.genKeyPair(jsch, KeyPair.RSA); kpair.writePrivateKey(context.getFilesDir().getAbsoluteFile() + "/" + AuthData.PRIVATE_KEY); kpair.writePublicKey(context.getFilesDir().getAbsoluteFile() + "/" + AuthData.PUBLIC_KEY, "MobileOrgPubKey"); } catch (JSchException | IOException e){ Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show(); return ""; } return AuthData.getPublicKey(context); }
public DeployKeyPair generateKeyPair() { final JSch jsch = new JSch(); try { final KeyPair kpair = KeyPair.genKeyPair(jsch, KeyPair.RSA); final ByteArrayOutputStream privateKeyStream = new ByteArrayOutputStream(); kpair.writePrivateKey(privateKeyStream); final ByteArrayOutputStream publicKeyStream = new ByteArrayOutputStream(); kpair.writePublicKey(publicKeyStream, ""); return new DeployKeyPair(new String(publicKeyStream.toByteArray(), "UTF-8"), new String(privateKeyStream.toByteArray(), "UTF-8")); } catch (final JSchException e) { throw new RuntimeException(e); } catch (final UnsupportedEncodingException e) { throw new RuntimeException(e); } } }
public static File getPublicKeyEnsure(File privateKey) { File publicKey = getPublicKey(privateKey); if (!publicKey.exists()) { try { JSch jsch=new JSch(); KeyPair kpair=KeyPair.load(jsch, privateKey.getAbsolutePath()); kpair.writePublicKey(new FileOutputStream(publicKey), "sgit"); kpair.dispose(); } catch (Exception e) { //TODO e.printStackTrace(); } } return publicKey; }
public static File getPublicKeyEnsure(File privateKey) { File publicKey = getPublicKey(privateKey); if (!publicKey.exists()) { try { JSch jsch=new JSch(); KeyPair kpair=KeyPair.load(jsch, privateKey.getAbsolutePath()); kpair.writePublicKey(new FileOutputStream(publicKey), "mgit"); kpair.dispose(); } catch (Exception e) { //TODO e.printStackTrace(); } } return publicKey; }
private void generatePublicKey(File file) throws IOException, JSchException { KeyPair kpair = KeyPair.load(jsch, getPrivateKey().getAbsolutePath()); kpair.writePublicKey(file.getAbsolutePath(), PUBLIC_KEY_COMMENT); kpair.dispose(); } }
/** * return a "public" -> rsa public key, "private" -> its corresponding * private key */ public static Map<String,String> generate(String passPhrase) throws JSchException { com.jcraft.jsch.KeyPair pair = com.jcraft.jsch.KeyPair.genKeyPair( new JSch(), com.jcraft.jsch.KeyPair.RSA); if (passPhrase != null) { pair.setPassphrase(passPhrase); } ByteArrayOutputStream publicKeyOut = new ByteArrayOutputStream(); ByteArrayOutputStream privateKeyOut = new ByteArrayOutputStream(); pair.writePublicKey(publicKeyOut, "whirr"); pair.writePrivateKey(privateKeyOut); String publicKey = new String(publicKeyOut.toByteArray()); String privateKey = new String(privateKeyOut.toByteArray()); return ImmutableMap.<String, String> of("public", publicKey, "private", privateKey); }
@Override public SSHKeyPair generate(String user, int bits) throws KeyException { JSch jsch = new JSch(); try { KeyPair keyPair = KeyPair.genKeyPair(jsch, KeyPair.RSA, bits); ByteArrayOutputStream bos = new ByteArrayOutputStream(); keyPair.writePublicKey(bos, user); SSHPublicKey publicKey = new SSHPublicKey(user, new String(bos.toByteArray(), StandardCharsets.UTF_8)); bos.reset(); keyPair.writePrivateKey(bos); byte[] privateKey = bos.toByteArray(); return new SSHKeyPair(publicKey, () -> privateKey); } catch (JSchException e) { throw new KeyException("Failed to generate ssh key pair", e); } }
@Override public SSHKeyPair generate(String user, int bits) throws KeyException { JSch jsch = new JSch(); try { KeyPair keyPair = KeyPair.genKeyPair(jsch, KeyPair.RSA, bits); ByteArrayOutputStream bos = new ByteArrayOutputStream(); keyPair.writePublicKey(bos, user); SSHPublicKey publicKey = new SSHPublicKey(user, new String(bos.toByteArray(), StandardCharsets.UTF_8)); bos.reset(); keyPair.writePrivateKey(bos); byte[] privateKey = bos.toByteArray(); return new SSHKeyPair(publicKey, () -> privateKey); } catch (JSchException e) { throw new KeyException("Failed to generate ssh key pair", e); } }
@Override public String generateKey(UserInfo userInfo, boolean createEdge) { log.debug("Generating new key pair for user {}", userInfo.getName()); try (ByteArrayOutputStream publicKeyOut = new ByteArrayOutputStream(); ByteArrayOutputStream privateKeyOut = new ByteArrayOutputStream()) { KeyPair pair = KeyPair.genKeyPair(new JSch(), KeyPair.RSA, configuration.getPrivateKeySize()); pair.writePublicKey(publicKeyOut, userInfo.getName()); pair.writePrivateKey(privateKeyOut); uploadKey(userInfo, new String(publicKeyOut.toByteArray()), createEdge); return new String(privateKeyOut.toByteArray()); } catch (JSchException | IOException e) { log.error("Can not generate private/public key pair due to: {}", e.getMessage()); throw new DlabException("Can not generate private/public key pair due to: " + e.getMessage(), e); } }
public static KeyPair create() { com.jcraft.jsch.KeyPair k; synchronized (jsch) { try { k = com.jcraft.jsch.KeyPair.genKeyPair(jsch, DEFAULT_KEY_TYPE, DEFAULT_KEY_SIZE); } catch (JSchException e) { throw new SecurityException(e); } } byte[] publicKey = array(out -> k.writePublicKey(out, DEFAULT_KEY_COMMENT)); byte[] privateKey = array(k::writePrivateKey); return new KeyPair(publicKey, privateKey); }
/** * Adds an authorized public key from the given {@link KeyPair}. */ public void addAuthorizedKey(KeyPair keyPair, String user) throws IOException, GeneralSecurityException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); keyPair.writePublicKey(bos, user); AuthorizedKeyEntry entry = AuthorizedKeyEntry.parseAuthorizedKeyEntry(new String(bos.toByteArray(), StandardCharsets.US_ASCII)); addAuthorizedKey(entry.resolvePublicKey(PublicKeyEntryResolver.IGNORING)); }