Java Code Examples for com.jcraft.jsch.KeyPair#writePublicKey()
The following examples show how to use
com.jcraft.jsch.KeyPair#writePublicKey() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CipherHelper.java From flow-platform-x with Apache License 2.0 | 6 votes |
public static SimpleKeyPair gen(String email) { try (ByteArrayOutputStream pubKeyOS = new ByteArrayOutputStream()) { try (ByteArrayOutputStream prvKeyOS = new ByteArrayOutputStream()) { JSch jsch = new JSch(); SimpleKeyPair rsa = new SimpleKeyPair(); KeyPair kpair = KeyPair.genKeyPair(jsch, KeyPair.RSA, 2048); kpair.writePrivateKey(prvKeyOS); kpair.writePublicKey(pubKeyOS, email); rsa.setPublicKey(pubKeyOS.toString()); rsa.setPrivateKey(prvKeyOS.toString()); kpair.dispose(); return rsa; } } catch (IOException | JSchException e) { throw new StatusException("Unable to generate RSA key pair"); } }
Example 2
Source File: SshTools.java From trigger with GNU General Public License v2.0 | 6 votes |
public static KeyPairData keypairToBytes(KeyPair keypair) { try { ByteArrayOutputStream prvstream = new ByteArrayOutputStream(); ByteArrayOutputStream pubstream = new ByteArrayOutputStream(); keypair.writePrivateKey(prvstream); keypair.writePublicKey(pubstream, keypair.getPublicKeyComment()); prvstream.close(); pubstream.close(); return new KeyPairData(prvstream.toByteArray(), pubstream.toByteArray()); } catch (Exception e) { Log.e(TAG ,"keypairtoBytes " + e.toString()); } return null; }
Example 3
Source File: KeyPairService.java From cloud-portal with MIT License | 6 votes |
public List<File> createKeyPair() { List<File> keyFileList = new ArrayList<>(); try{ File privateKeyFile = File.createTempFile(Constants.KEY_FILE_PREFIX, Constants.CHAR_EMPTY); File publicKeyFile = new File(privateKeyFile.getAbsolutePath() + Constants.KEY_FILE_PUBLIC_SUFFIX); KeyPair keyPair = KeyPair.genKeyPair(JSCH, KeyPair.RSA); keyPair.writePrivateKey(privateKeyFile.getAbsolutePath()); keyPair.writePublicKey(publicKeyFile.getAbsolutePath(), COMMENT); keyPair.dispose(); keyFileList.add(privateKeyFile); keyFileList.add(publicKeyFile); } catch(Exception e){ LOG.error(e.getMessage(), e); } return keyFileList; }
Example 4
Source File: SSHShell.java From azure-libraries-for-java with MIT License | 6 votes |
/** * Automatically generate SSH keys. * @param passPhrase the byte array content to be uploaded * @param comment the name of the file for which the content will be saved into * @return SSH public and private key * @throws Exception exception thrown */ public static SshPublicPrivateKey generateSSHKeys(String passPhrase, String comment) throws Exception { JSch jsch = new JSch(); KeyPair keyPair = KeyPair.genKeyPair(jsch, KeyPair.RSA); ByteArrayOutputStream privateKeyBuff = new ByteArrayOutputStream(2048); ByteArrayOutputStream publicKeyBuff = new ByteArrayOutputStream(2048); keyPair.writePublicKey(publicKeyBuff, (comment != null) ? comment : "SSHCerts"); if (passPhrase == null || passPhrase.isEmpty()) { keyPair.writePrivateKey(privateKeyBuff); } else { keyPair.writePrivateKey(privateKeyBuff, passPhrase.getBytes()); } return new SshPublicPrivateKey(privateKeyBuff.toString(), publicKeyBuff.toString()); }
Example 5
Source File: CipherHelper.java From flow-platform-x with Apache License 2.0 | 6 votes |
public static SimpleKeyPair gen(String email) { try (ByteArrayOutputStream pubKeyOS = new ByteArrayOutputStream()) { try (ByteArrayOutputStream prvKeyOS = new ByteArrayOutputStream()) { JSch jsch = new JSch(); SimpleKeyPair rsa = new SimpleKeyPair(); KeyPair kpair = KeyPair.genKeyPair(jsch, KeyPair.RSA, 2048); kpair.writePrivateKey(prvKeyOS); kpair.writePublicKey(pubKeyOS, email); rsa.setPublicKey(pubKeyOS.toString()); rsa.setPrivateKey(prvKeyOS.toString()); kpair.dispose(); return rsa; } } catch (IOException | JSchException e) { throw new StatusException("Unable to generate RSA key pair"); } }
Example 6
Source File: TransUtil.java From oneops with Apache License 2.0 | 6 votes |
public Map<String,String> keyGen(String passPhrase, String pubDesc) { JSch jsch=new JSch(); String passphrase= (passPhrase == null) ? "" : passPhrase; Map<String,String> result = new HashMap<String,String>(); try{ KeyPair kpair=KeyPair.genKeyPair(jsch, KeyPair.RSA, 2048); kpair.setPassphrase(passphrase); OutputStream prkos = new ByteArrayOutputStream(); kpair.writePrivateKey(prkos); String privateKey = prkos.toString(); //removing "\n" at the end of the string result.put("private", privateKey.substring(0, privateKey.length() - 1)); OutputStream pubkos = new ByteArrayOutputStream(); kpair.writePublicKey(pubkos, pubDesc); String pubKey = pubkos.toString(); //removing "\n" at the end of the string result.put("public", pubKey.substring(0, pubKey.length() - 1)); kpair.dispose(); return result; } catch(Exception e){ System.out.println(e); logger.error(e.getMessage()); throw new TransistorException(CmsError.TRANSISTOR_EXCEPTION, e.getMessage()); } }
Example 7
Source File: SshManager.java From che with Eclipse Public License 2.0 | 6 votes |
/** * Generates and stores ssh pair for specified user. * * @param owner the id of the user who will be the owner of the ssh pair * @param service service name pf ssh pair * @param name name of pair * @return instance of generated ssh pair * @throws ConflictException when given ssh pair cannot be generated or created * @throws ServerException when any other error occurs during ssh pair generating or creating */ public SshPairImpl generatePair(String owner, String service, String name) throws ServerException, ConflictException { KeyPair keyPair; try { keyPair = KeyPair.genKeyPair(genJSch, 2, 2048); } catch (JSchException e) { throw new ServerException("Failed to generate ssh pair.", e); } ByteArrayOutputStream privateBuff = new ByteArrayOutputStream(); keyPair.writePrivateKey(privateBuff); ByteArrayOutputStream publicBuff = new ByteArrayOutputStream(); keyPair.writePublicKey(publicBuff, null); final SshPairImpl generatedSshPair = new SshPairImpl(owner, service, name, publicBuff.toString(), privateBuff.toString()); sshDao.create(generatedSshPair); return generatedSshPair; }
Example 8
Source File: SshkeyExchange.java From onos with Apache License 2.0 | 6 votes |
private boolean generateKeyPair() { KeyPair kpair; StringBuilder command = new StringBuilder() .append("chmod 600 ") .append(HOME_ENV) .append(PRIVATE_KEY); try { kpair = KeyPair.genKeyPair(new JSch(), KeyPair.RSA, KEY_SIZE); kpair.writePrivateKey(HOME_ENV + PRIVATE_KEY); kpair.writePublicKey(HOME_ENV + PUBLIC_KEY, USER_ENV); Runtime.getRuntime().exec(command.toString()); kpair.dispose(); } catch (JSchException | IOException e) { log.error("Exception in generateKeyPair", e); return false; } return true; }
Example 9
Source File: JschHolder.java From super-cloudops with Apache License 2.0 | 5 votes |
@Override public Ssh2KeyPair generateKeypair(AlgorithmType type, String comment) throws Exception { int algType = KeyPair.RSA; if (type == AlgorithmType.DSA) { algType = KeyPair.DSA; } else if (type == AlgorithmType.ECDSA) { algType = KeyPair.ECDSA; } KeyPair kpair = KeyPair.genKeyPair(new JSch(), algType); // 私钥 ByteArrayOutputStream baos = new ByteArrayOutputStream();// 向OutPutStream中写入 kpair.writePrivateKey(baos); final String privateKey = baos.toString(); // 公钥 baos = new ByteArrayOutputStream(); kpair.writePublicKey(baos, comment); final String publicKey = baos.toString(); kpair.dispose(); // To rsa.pub // String publicKeyString = // RSAEncrypt.loadPublicKeyByFile(filePath,filename + ".pub"); // System.out.println(publicKeyString.length()); // System.out.println(publicKeyString); // To rsa // String privateKeyString = // RSAEncrypt.loadPrivateKeyByFile(filePath,filename); // System.out.println(privateKeyString.length()); // System.out.println(privateKeyString); return new Ssh2KeyPair(privateKey, publicKey); }
Example 10
Source File: SSHKeyUtils.java From blueocean-plugin with MIT License | 5 votes |
/** * Gets the public key, with a comment for the given private key * @param privateKey SSH private key to use * @param comment comment with the key * @return SSH public key */ public static String getPublicKey(String privateKey, String comment) { try { JSch jsch = new JSch(); KeyPair pair = KeyPair.load(jsch, privateKey.getBytes("utf-8"), null ); ByteArrayOutputStream keyOut = new ByteArrayOutputStream(); pair.writePublicKey(keyOut, comment); return new String(keyOut.toByteArray(), "utf-8"); } catch(Exception ex) { throw ex instanceof RuntimeException ? (RuntimeException)ex : new RuntimeException(ex); } }
Example 11
Source File: AAWSTest.java From aws-ec2-ssh with MIT License | 5 votes |
protected final User createUser(final String userName) throws JSchException { final JSch jsch = new JSch(); final KeyPair keyPair = KeyPair.genKeyPair(jsch, KeyPair.RSA, 2048); final ByteArrayOutputStream osPublicKey = new ByteArrayOutputStream(); final ByteArrayOutputStream osPrivateKey = new ByteArrayOutputStream(); keyPair.writePublicKey(osPublicKey, userName); keyPair.writePrivateKey(osPrivateKey); final byte[] sshPrivateKeyBlob = osPrivateKey.toByteArray(); final String sshPublicKeyBody = osPublicKey.toString(); this.iam.createUser(new CreateUserRequest().withUserName(userName)); final UploadSSHPublicKeyResult res = this.iam.uploadSSHPublicKey(new UploadSSHPublicKeyRequest().withUserName(userName).withSSHPublicKeyBody(sshPublicKeyBody)); return new User(userName, sshPrivateKeyBlob, res.getSSHPublicKey().getSSHPublicKeyId()); }
Example 12
Source File: SshdServerMock.java From gerrit-events with MIT License | 5 votes |
/** * Generates a rsa key-pair in /tmp/jenkins-testkey for use with authenticating the trigger against the mock * server. * * @return the path to the private key file * * @throws IOException if so. * @throws InterruptedException if interrupted while waiting for ssh-keygen to finish. * @throws JSchException if creation of the keys goes wrong. */ public static KeyPairFiles generateKeyPair() throws IOException, InterruptedException, JSchException { File tmp = new File(System.getProperty("java.io.tmpdir")).getCanonicalFile(); File priv = new File(tmp, "jenkins-testkey"); File pub = new File(tmp, "jenkins-testkey.pub"); if (!(priv.exists() && pub.exists())) { if (priv.exists()) { if (!priv.delete()) { throw new IOException("Could not delete temp private key"); } } if (pub.exists()) { if (!pub.delete()) { throw new IOException("Could not delete temp public key"); } } System.out.println("Generating test key-pair."); JSch jsch = new JSch(); KeyPair kpair = KeyPair.genKeyPair(jsch, KeyPair.RSA); kpair.writePrivateKey(new FileOutputStream(priv)); kpair.writePublicKey(new FileOutputStream(pub), "Test"); System.out.println("Finger print: " + kpair.getFingerPrint()); kpair.dispose(); return new KeyPairFiles(priv, pub); } else { System.out.println("Test key-pair seems to already exist."); return new KeyPairFiles(priv, pub); } }
Example 13
Source File: TestSshTunnel.java From datacollector with Apache License 2.0 | 4 votes |
String getSshPublicKeyText(KeyPair keyPair, String comment) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); keyPair.writePublicKey(baos, comment); return new String(baos.toByteArray()); }
Example 14
Source File: SshUtils.java From datacollector with Apache License 2.0 | 4 votes |
private static String getSshPublicKeyText(KeyPair keyPair, String comment) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); keyPair.writePublicKey(baos, comment); return new String(baos.toByteArray(), StandardCharsets.UTF_8); }