本文整理汇总了Java中com.jcraft.jsch.KeyPair类的典型用法代码示例。如果您正苦于以下问题:Java KeyPair类的具体用法?Java KeyPair怎么用?Java KeyPair使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
KeyPair类属于com.jcraft.jsch包,在下文中一共展示了KeyPair类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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());
}
示例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;
}
示例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());
}
}
示例4: getPublicKeys
import com.jcraft.jsch.KeyPair; //导入依赖的package包/类
private static List<String> getPublicKeys() throws Exception {
return new JschConfigSessionFactory() {
@Override
protected void configure(OpenSshConfig.Host hc, Session session) {
}
List<String> getPublicKeys() throws Exception {
JSch jSch = createDefaultJSch(FS.DETECTED);
List<String> keys = new ArrayList<>();
for (Object o : jSch.getIdentityRepository().getIdentities()) {
Identity i = (Identity) o;
KeyPair keyPair = KeyPair.load(jSch, i.getName(), null);
StringBuilder sb = new StringBuilder();
try (StringBuilderWriter sbw = new StringBuilderWriter(sb);
OutputStream os = new WriterOutputStream(sbw, "UTF-8")) {
keyPair.writePublicKey(os, keyPair.getPublicKeyComment());
} finally {
keyPair.dispose();
}
keys.add(sb.toString().trim());
}
return keys;
}
}.getPublicKeys();
}
示例5: 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;
}
示例6: addPrivateKey
import com.jcraft.jsch.KeyPair; //导入依赖的package包/类
private void addPrivateKey(String filename) throws JSchException {
if (filename == null || filename.isEmpty()) {
return;
}
File file = new File(filename);
if (file.exists()) {
System.out.println("Adding private key: " + filename);
KeyPair keyPair = KeyPair.load(this, filename);
if (keyPair.isEncrypted()) {
this.tryDecryptPrivateKey(filename, keyPair);
this.addIdentity(file.getAbsolutePath(), this.privateKeyPassphrase);
} else {
this.addIdentity(file.getAbsolutePath());
}
}
}
示例7: 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();
}
示例8: 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();
}
}
}
示例9: GetKeyprint
import com.jcraft.jsch.KeyPair; //导入依赖的package包/类
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 "";
}
示例10: 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());
}
示例11: 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());
}
示例12: 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;
}
}
示例13: SSHKeysHelper
import com.jcraft.jsch.KeyPair; //导入依赖的package包/类
public SSHKeysHelper() {
try {
keyPair = KeyPair.genKeyPair(new JSch(), KeyPair.RSA);
} catch (final JSchException e) {
e.printStackTrace();
}
}
示例14: 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());
}
示例15: key
import com.jcraft.jsch.KeyPair; //导入依赖的package包/类
/**
* 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());
}