File: KeyGen.java

package info (click to toggle)
jsch 0.2.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,980 kB
  • sloc: java: 36,477; xml: 1,352; sh: 184; python: 57; makefile: 2
file content (71 lines) | stat: -rw-r--r-- 2,631 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
 * This progam will demonstrate the keypair generation. You will be asked a passphrase for
 * output_keyfile. If everything works fine, you will get the keypair, output_keyfile and
 * output_keyfile+".pub". The private key and public key are in the OpenSSH format.
 */
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.KeyPair;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class KeyGen {
  public static void main(String[] arg) {
    int key_size = 1024;
    if (arg.length < 3) {
      System.err.println("usage: java KeyGen rsa output_keyfile comment\n"
          + "       java KeyGen dsa output_keyfile comment\n"
          + "       java KeyGen ecdsa-sha2-256 output_keyfile comment\n"
          + "       java KeyGen ecdsa-sha2-384 output_keyfile comment\n"
          + "       java KeyGen ecdsa-sha2-521 output_keyfile comment");
      System.exit(-1);
    }
    String _type = arg[0];
    int type = 0;
    if (_type.equals("rsa")) {
      type = KeyPair.RSA;
    } else if (_type.equals("dsa")) {
      type = KeyPair.DSA;
    } else if (_type.equals("ecdsa-sha2-nistp256")) {
      type = KeyPair.ECDSA;
      key_size = 256;
    } else if (_type.equals("ecdsa-sha2-nistp384")) {
      type = KeyPair.ECDSA;
      key_size = 384;
    } else if (_type.equals("ecdsa-sha2-nistp521")) {
      type = KeyPair.ECDSA;
      key_size = 521;
    } else {
      System.err.println("usage: java KeyGen rsa output_keyfile comment\n"
          + "       java KeyGen dsa output_keyfile comment\n"
          + "       java KeyGen ecdsa-sha2-256 output_keyfile comment\n"
          + "       java KeyGen ecdsa-sha2-384 output_keyfile comment\n"
          + "       java KeyGen ecdsa-sha2-521 output_keyfile comment");
      System.exit(-1);
    }
    String filename = arg[1];
    String comment = arg[2];

    JSch jsch = new JSch();

    String passphrase = "";
    JTextField passphraseField = new JPasswordField(20);
    Object[] ob = {passphraseField};
    int result = JOptionPane.showConfirmDialog(null, ob,
        "Enter passphrase (empty for no passphrase)", JOptionPane.OK_CANCEL_OPTION);
    if (result == JOptionPane.OK_OPTION) {
      passphrase = passphraseField.getText();
    }

    try {
      KeyPair kpair = KeyPair.genKeyPair(jsch, type, key_size);
      kpair.writePrivateKey(filename, passphrase.getBytes());
      kpair.writePublicKey(filename + ".pub", comment);
      System.out.println("Finger print: " + kpair.getFingerPrint());
      kpair.dispose();
    } catch (Exception e) {
      System.out.println(e);
    }
    System.exit(0);
  }
}