Search in sources :

Example 21 with KeyPair

use of com.jcraft.jsch.KeyPair in project azure-tools-for-java by Microsoft.

the class AzureDockerCertVaultOps method generateSSHKeys.

public static AzureDockerCertVault generateSSHKeys(String passPhrase, String comment) throws AzureDockerException {
    try {
        AzureDockerCertVault result = new AzureDockerCertVault();
        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 : "DockerSSHCerts");
        if (passPhrase == null || passPhrase.isEmpty()) {
            keyPair.writePrivateKey(privateKeyBuff);
        } else {
            keyPair.writePrivateKey(privateKeyBuff, passPhrase.getBytes());
        }
        result.sshKey = privateKeyBuff.toString();
        result.sshPubKey = publicKeyBuff.toString();
        return result;
    } catch (Exception e) {
        throw new AzureDockerException(e.getMessage());
    }
}
Also used : KeyPair(com.jcraft.jsch.KeyPair) AzureDockerException(com.microsoft.azure.docker.model.AzureDockerException) AzureDockerCertVault(com.microsoft.azure.docker.model.AzureDockerCertVault) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JSch(com.jcraft.jsch.JSch) AzureDockerException(com.microsoft.azure.docker.model.AzureDockerException) CloudException(com.microsoft.azure.CloudException)

Example 22 with KeyPair

use of com.jcraft.jsch.KeyPair in project keepass2android by PhilippC.

the class SftpStorage method createKeyPair.

private String createKeyPair(String key_filename) throws JSchException, IOException {
    String public_key_filename = key_filename + ".pub";
    File file = new File(key_filename);
    if (file.exists())
        return public_key_filename;
    int type = KeyPair.RSA;
    KeyPair kpair = KeyPair.genKeyPair(jsch, type, 4096);
    kpair.writePrivateKey(key_filename);
    kpair.writePublicKey(public_key_filename, "generated by Keepass2Android");
    // ret = "Fingerprint: " + kpair.getFingerPrint();
    kpair.dispose();
    return public_key_filename;
}
Also used : KeyPair(com.jcraft.jsch.KeyPair) File(java.io.File)

Example 23 with KeyPair

use of com.jcraft.jsch.KeyPair in project MGit by maks.

the class PrivateKeyGenerate method generateKey.

private void generateKey() {
    String newFilename = mNewFilename.getText().toString().trim();
    if (newFilename.equals("")) {
        showToastMessage(R.string.alert_new_filename_required);
        mNewFilename.setError(getString(R.string.alert_new_filename_required));
        return;
    }
    if (newFilename.contains("/")) {
        showToastMessage(R.string.alert_filename_format);
        mNewFilename.setError(getString(R.string.alert_filename_format));
        return;
    }
    int key_size = Integer.parseInt(mKeyLength.getText().toString());
    if (key_size < 1024) {
        showToastMessage(R.string.alert_too_short_key_size);
        mNewFilename.setError(getString(R.string.alert_too_short_key_size));
        return;
    }
    if (key_size > 16384) {
        showToastMessage(R.string.alert_too_long_key_size);
        mNewFilename.setError(getString(R.string.alert_too_long_key_size));
        return;
    }
    int type = mDSAButton.isChecked() ? KeyPair.DSA : KeyPair.RSA;
    File newKey = new File(PrivateKeyUtils.getPrivateKeyFolder(), newFilename);
    File newPubKey = new File(PrivateKeyUtils.getPublicKeyFolder(), newFilename);
    try {
        JSch jsch = new JSch();
        KeyPair kpair = KeyPair.genKeyPair(jsch, type, key_size);
        kpair.writePrivateKey(new FileOutputStream(newKey));
        kpair.writePublicKey(new FileOutputStream(newPubKey), "sgit");
        kpair.dispose();
    } catch (Exception e) {
        // TODO
        e.printStackTrace();
    }
    ((PrivateKeyManageActivity) getActivity()).refreshList();
}
Also used : KeyPair(com.jcraft.jsch.KeyPair) FileOutputStream(java.io.FileOutputStream) JSch(com.jcraft.jsch.JSch) File(java.io.File)

Example 24 with KeyPair

use of com.jcraft.jsch.KeyPair in project cdap by caskdata.

the class DefaultSSHContext method generate.

@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);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) SSHKeyPair(io.cdap.cdap.runtime.spi.ssh.SSHKeyPair) KeyPair(com.jcraft.jsch.KeyPair) SSHKeyPair(io.cdap.cdap.runtime.spi.ssh.SSHKeyPair) SSHPublicKey(io.cdap.cdap.runtime.spi.ssh.SSHPublicKey) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JSch(com.jcraft.jsch.JSch) KeyException(java.security.KeyException)

Example 25 with KeyPair

use of com.jcraft.jsch.KeyPair in project jcabi-github by jcabi.

the class RtPublicKeysITCase method key.

/**
 * Generates a random public key for test.
 * @return The encoded SSH public key.
 * @throws Exception If a problem occurs.
 */
private String key() throws Exception {
    final ByteArrayOutputStream stream = new ByteArrayOutputStream();
    try {
        final KeyPair kpair = KeyPair.genKeyPair(new JSch(), KeyPair.DSA);
        kpair.writePublicKey(stream, "");
        kpair.dispose();
    } finally {
        stream.close();
    }
    return new String(stream.toByteArray());
}
Also used : KeyPair(com.jcraft.jsch.KeyPair) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JSch(com.jcraft.jsch.JSch)

Aggregations

KeyPair (com.jcraft.jsch.KeyPair)30 JSch (com.jcraft.jsch.JSch)27 JSchException (com.jcraft.jsch.JSchException)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 File (java.io.File)10 IOException (java.io.IOException)9 OutputStream (java.io.OutputStream)3 BasicSSHUserPrivateKey (com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey)2 SSHKeyPair (io.cdap.cdap.runtime.spi.ssh.SSHKeyPair)2 SSHPublicKey (io.cdap.cdap.runtime.spi.ssh.SSHPublicKey)2 FileOutputStream (java.io.FileOutputStream)2 KeyException (java.security.KeyException)2 ArrayList (java.util.ArrayList)2 SuppressLint (android.annotation.SuppressLint)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 AlertDialog (android.support.v7.app.AlertDialog)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1