当前位置: 首页>>代码示例>>Java>>正文


Java KeyPair.writePrivateKey方法代码示例

本文整理汇总了Java中com.jcraft.jsch.KeyPair.writePrivateKey方法的典型用法代码示例。如果您正苦于以下问题:Java KeyPair.writePrivateKey方法的具体用法?Java KeyPair.writePrivateKey怎么用?Java KeyPair.writePrivateKey使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.jcraft.jsch.KeyPair的用法示例。


在下文中一共展示了KeyPair.writePrivateKey方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: generateSSHKeys

import com.jcraft.jsch.KeyPair; //导入方法依赖的package包/类
/**
 * 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());
}
 
开发者ID:Azure-Samples,项目名称:acr-java-manage-azure-container-registry,代码行数:24,代码来源:SSHShell.java

示例2: createKeyPair

import com.jcraft.jsch.KeyPair; //导入方法依赖的package包/类
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_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;
	}
 
开发者ID:chrisipa,项目名称:cloud-portal,代码行数:23,代码来源:KeyPairService.java

示例3: keyGen

import com.jcraft.jsch.KeyPair; //导入方法依赖的package包/类
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());
       }
}
 
开发者ID:oneops,项目名称:oneops,代码行数:26,代码来源:TransUtil.java

示例4: generatePair

import com.jcraft.jsch.KeyPair; //导入方法依赖的package包/类
/**
 * 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;
}
 
开发者ID:eclipse,项目名称:che,代码行数:31,代码来源:SshManager.java

示例5: generateNewIdentity

import com.jcraft.jsch.KeyPair; //导入方法依赖的package包/类
public static final void generateNewIdentity(File keyDir, String comment, int strength) throws Exception {
	JSch jsch = new JSch();
	
	KeyPair newKeyPair = KeyPair.genKeyPair(jsch, KeyPair.RSA, strength);
	
	File privateKeyFile = new File(keyDir, "id_rsa");
	File publicKeyFile = new File(keyDir, "id_rsa.pub");
	
	if (privateKeyFile.exists()) {
		privateKeyFile.delete();
	}
	
	if (publicKeyFile.exists()) {
		publicKeyFile.delete();
	}
	
	newKeyPair.writePrivateKey(new FileOutputStream(privateKeyFile));
	newKeyPair.writePublicKey(new FileOutputStream(publicKeyFile), comment);
    
	newKeyPair.dispose();
}
 
开发者ID:h42i,项目名称:d00r-app,代码行数:22,代码来源:SshTools.java

示例6: genKeyPair

import com.jcraft.jsch.KeyPair; //导入方法依赖的package包/类
public void genKeyPair() {
	createFilePath();

	KeyPair kp = null;
	try {
		kp = KeyPair.genKeyPair(new JSch(), keypairType, keypairSize);
		kp.writePrivateKey(keypairFileName);
		kp.writePublicKey(keypairFileName + ".pub", keypairComment);
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (kp != null) {
			kp.dispose();
		}
	}
}
 
开发者ID:CSCLabTW,项目名称:CloudDOE,代码行数:17,代码来源:SSHKeyGen.java

示例7: generateKeys

import com.jcraft.jsch.KeyPair; //导入方法依赖的package包/类
private void generateKeys() throws JSchException, IOException {
	String instance_name = InstanceStatus.getStatic().summary.getInstanceName();
	String host_name = InstanceStatus.getStatic().summary.getHostName();
	String comment = "mydmam-" + instance_name + "@" + host_name;
	
	JSch jsch = new JSch();
	
	KeyPair kpair = KeyPair.genKeyPair(jsch, KeyPair.RSA);
	kpair.writePrivateKey(private_key);
	kpair.writePublicKey(public_key, comment);
	kpair.dispose();
	
	String os_name = System.getProperty("os.name").toLowerCase();
	if (os_name.indexOf("win") == -1) {
		ArrayList<String> params = new ArrayList<String>();
		params.add("600");
		params.add(private_key);
		ExecprocessGettext exec = new ExecprocessGettext(ExecBinaryPath.get("chmod"), params);
		exec.start();
	}
	
	Loggers.Ssh.info("Generate SSH Keys for MyDMAM, public key: " + new File(public_key) + ", private key: " + new File(private_key) + ", finger print: " + kpair.getFingerPrint());
}
 
开发者ID:hdsdi3g,项目名称:MyDMAM,代码行数:24,代码来源:Ssh.java

示例8: init

import com.jcraft.jsch.KeyPair; //导入方法依赖的package包/类
@Before
public void init() throws Exception {
  knownHosts = tmpFolder.newFile(KNOWN_HOSTS);

  JSch jSch = new JSch();
  KeyPair keyPair = null;

  identityKey1 = tmpFolder.newFile(IDENTITY_KEY_1);
  keyPair = KeyPair.genKeyPair(jSch, KeyPair.RSA);
  keyPair.writePrivateKey(identityKey1.getAbsolutePath());

  identityKey2 = tmpFolder.newFile(IDENTITY_KEY_2);
  keyPair = KeyPair.genKeyPair(jSch, KeyPair.RSA);
  keyPair.writePrivateKey(identityKey2.getAbsolutePath());
}
 
开发者ID:HotelsDotCom,项目名称:circus-train,代码行数:16,代码来源:SessionFactorySupplierTest.java

示例9: generateRsaKey

import com.jcraft.jsch.KeyPair; //导入方法依赖的package包/类
@Override
public RSAKeyPair generateRsaKey() {
    String comment = "FLOWCI";
    int type = KeyPair.RSA;
    final int keySize = 2048; // default 1024, bitbucket support at least 2048
    JSch jsch = new JSch();

    try {
        KeyPair kpair = KeyPair.genKeyPair(jsch, type, keySize);
        RSAKeyPair pair = new RSAKeyPair();

        // private key
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
            kpair.writePrivateKey(baos);
            pair.setPrivateKey(baos.toString());
        }

        // public key
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
            kpair.writePublicKey(baos, comment);
            pair.setPublicKey(baos.toString());
        }

        kpair.dispose();
        return pair;

    } catch (JSchException | IOException e) {
        return null;
    }
}
 
开发者ID:FlowCI,项目名称:flow-platform,代码行数:31,代码来源:CredentialServiceImpl.java

示例10: createUser

import com.jcraft.jsch.KeyPair; //导入方法依赖的package包/类
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());
}
 
开发者ID:widdix,项目名称:aws-ec2-ssh,代码行数:14,代码来源:AAWSTest.java

示例11: doInBackground

import com.jcraft.jsch.KeyPair; //导入方法依赖的package包/类
protected Exception doInBackground(String... strings) {
    int length = Integer.parseInt(strings[0]);
    String passphrase = strings[1];
    String comment = strings[2];

    JSch jsch = new JSch();
    try {
        KeyPair kp = KeyPair.genKeyPair(jsch, KeyPair.RSA, length);

        File file = new File(getFilesDir() + "/.ssh_key");
        FileOutputStream out = new FileOutputStream(file, false);
        if (passphrase.length() > 0) {
            kp.writePrivateKey(out, passphrase.getBytes());
        } else {
            kp.writePrivateKey(out);
        }

        file = new File(getFilesDir() + "/.ssh_key.pub");
        out = new FileOutputStream(file, false);
        kp.writePublicKey(out, comment);
        return null;
    } catch (Exception e) {
        System.out.println("Exception caught :(");
        e.printStackTrace();
        return e;
    }
}
 
开发者ID:zeapo,项目名称:Android-Password-Store,代码行数:28,代码来源:SshKeyGen.java

示例12: generateKeyPair

import com.jcraft.jsch.KeyPair; //导入方法依赖的package包/类
/**
 * 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);
    }
}
 
开发者ID:sonyxperiadev,项目名称:gerrit-events,代码行数:40,代码来源:SshdServerMock.java

示例13: generatePrivateKey

import com.jcraft.jsch.KeyPair; //导入方法依赖的package包/类
private void generatePrivateKey(File file) throws IOException, JSchException {
    KeyPair kpair = KeyPair.genKeyPair(jsch, KeyPair.RSA, PRIVATE_KEY_LENGTH);
    kpair.writePrivateKey(file.getAbsolutePath());
    kpair.dispose();
}
 
开发者ID:Skarafaz,项目名称:mercury,代码行数:6,代码来源:SshManager.java

示例14: generateKey

import com.jcraft.jsch.KeyPair; //导入方法依赖的package包/类
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();
   }
 
开发者ID:sheimi,项目名称:SGit,代码行数:46,代码来源:PrivateKeyGenerate.java


注:本文中的com.jcraft.jsch.KeyPair.writePrivateKey方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。