Java Code Examples for com.jcraft.jsch.KeyPair#dispose()

The following examples show how to use com.jcraft.jsch.KeyPair#dispose() . 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 vote down vote up
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: KeyPairService.java    From cloud-portal with MIT License 6 votes vote down vote up
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 3
Source File: CipherHelper.java    From flow-platform-x with Apache License 2.0 6 votes vote down vote up
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 4
Source File: TransUtil.java    From oneops with Apache License 2.0 6 votes vote down vote up
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 5
Source File: KeySettingActivity.java    From mOrgAnd with GNU General Public License v2.0 6 votes vote down vote up
private String GetKeyprint(String keyfilePath, String passphrase) {
    try {
        KeyPair keyPair = KeyPair.load(new JSch(), keyfilePath);
        if (!passphrase.isEmpty() && keyPair.isEncrypted())
            keyPair.decrypt(passphrase);
        else if (passphrase.isEmpty() && keyPair.isEncrypted()) {
            Toast.makeText(this, R.string.error_key_need_pass, Toast.LENGTH_LONG).show();
            return "";
        }
        String fingerprint = keyPair.getFingerPrint();
        keyPair.dispose();
        return fingerprint;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}
 
Example 6
Source File: SshkeyExchange.java    From onos with Apache License 2.0 6 votes vote down vote up
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 7
Source File: JschHolder.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
@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 8
Source File: SshdServerMock.java    From gerrit-events with MIT License 5 votes vote down vote up
/**
 * 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);
    }
}