public SSHKeysHelper(Integer keyLength) { try { keyPair = KeyPair.genKeyPair(new JSch(), KeyPair.RSA, keyLength); } catch (JSchException e) { e.printStackTrace(); } }
public static KeyPair genKeyPair(JSch jsch, int type) throws JSchException{ return genKeyPair(jsch, type, 1024); } public static KeyPair genKeyPair(JSch jsch, int type, int key_size) throws JSchException{
public static KeyPair genKeyPair(JSch jsch, int type) throws JSchException{ return genKeyPair(jsch, type, 1024); } public static KeyPair genKeyPair(JSch jsch, int type, int key_size) throws JSchException{
/** * Creates a new key pair with default key size of 1024 bits. * @param jsch the JSch object used for configuration purposes. * @param type one of {@link #DSA} and {@link #RSA}. * @return null if a unknown type constant is given, else the new * key pair. */ public static KeyPair genKeyPair(JSch jsch, int type) throws JSchException{ return genKeyPair(jsch, type, 1024); }
public static KeyPair genKeyPair(JSch jsch, int type) throws JSchException{ return genKeyPair(jsch, type, 1024); } public static KeyPair genKeyPair(JSch jsch, int type, int key_size) throws JSchException{
public static KeyPair genKeyPair(int type) throws JSchException { return genKeyPair(type, 1024); }
protected com.jcraft.jsch.KeyPair getKeyPair(JSch jsch, int type, int size) { try { return genKeyPair(jsch, type, size); } catch (JSchException e) { throw new IllegalStateException(e); } }
public SSHKeysHelper() { try { keyPair = KeyPair.genKeyPair(new JSch(), KeyPair.RSA); } catch (final JSchException e) { e.printStackTrace(); } }
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); } } }
private void generatePrivateKey(File file) throws IOException, JSchException { KeyPair kpair = KeyPair.genKeyPair(jsch, KeyPair.RSA, PRIVATE_KEY_LENGTH); kpair.writePrivateKey(file.getAbsolutePath()); kpair.dispose(); }
@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 org.apache.airavata.credential.store.credential.impl.ssh.SSHCredential generateKeyPair(SSHCredential credential) throws Exception{ JSch jsch=new JSch(); try{ KeyPair kpair=KeyPair.genKeyPair(jsch, KeyPair.RSA, 2048); File file = File.createTempFile("id_rsa", ""); String fileName = file.getAbsolutePath(); kpair.writePrivateKey(fileName,credential.getPassphrase().getBytes()); kpair.writePublicKey(fileName + ".pub" , ""); kpair.dispose(); byte[] priKey = FileUtils.readFileToByteArray(new File(fileName)); byte[] pubKey = FileUtils.readFileToByteArray(new File(fileName + ".pub")); credential.setPrivateKey(priKey); credential.setPublicKey(pubKey); return credential; } catch(Exception e){ log.error("Error while creating key pair", e); throw new Exception("Error while creating key pair", e); } }
/** * This could take a while. */ public DeployKey create() { log.debug("creating keypair ..."); try { ByteArrayOutputStream privBaos = new ByteArrayOutputStream(); ByteArrayOutputStream pubBaos = new ByteArrayOutputStream(); KeyPair keypair = KeyPair.genKeyPair(new JSch(), type, size); keypair.writePrivateKey(privBaos, null); keypair.writePublicKey(pubBaos, comment); return DeployKey.builder().id(UUID.randomUUID().toString()).publicKey(new String(pubBaos.toByteArray())) .privateKey(new String(privBaos.toByteArray())).fingerprint(keypair.getFingerPrint()).build(); } catch (Exception e) { throw new RuntimeException("Unable to create DeployKey", e); } finally { log.debug("keypair created"); } }
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); }
@BeforeClass public static void init() throws IOException, JSchException, GeneralSecurityException { keyPair = KeyPair.genKeyPair(new JSch(), KeyPair.RSA, 1024); SSH_SERVER.addAuthorizedKey(keyPair, "cdap"); }
@BeforeClass public static void init() throws Exception { keyPair = KeyPair.genKeyPair(new JSch(), KeyPair.RSA, 1024); SSH_SERVER.addAuthorizedKey(keyPair, "cdap"); }
public SshKeyPair generateKeyPair(String keyType, int keyBits) throws SshException { try { int type = -1; if (keyType.equals(SshConfiguration.PUBLIC_KEY_SSHRSA)) { type = KeyPair.RSA; } else if (keyType.equals(SshConfiguration.PUBLIC_KEY_SSHDSA)) { type = KeyPair.DSA; } else { throw new SshException(SshException.UNSUPPORTED_FEATURE, "Algoritm " + keyType + " is not supported."); } KeyPair kpair = KeyPair.genKeyPair(jsch, type, keyBits); return new SshKeyPair(new JschPublicKey(kpair, keyBits), new JschPrivateKey(kpair)); } catch (JSchException e) { throw new SshException(e); } }