1 2 import com.jcraft.jsch.*; 3 import javax.swing.*; 4 5 class KeyGen{ 6 public static void main(String [] arg){ 7 if(arg.length<3){ 8 System.err.println( 9 "usage: java KeyGen rsa output_keyfile comment\n"+ 10 " java KeyGen dsa output_keyfile comment"); 11 System.exit(-1); 12 } 13 String _type=arg[0]; 14 int type=0; 15 if(_type.equals("rsa")){type=KeyPair.RSA;} 16 else if(_type.equals("dsa")){type=KeyPair.DSA;} 17 else { 18 System.err.println( 19 "usage: java KeyGen rsa output_keyfile comment\n"+ 20 " java KeyGen dsa output_keyfile comment"); 21 System.exit(-1); 22 } 23 String filename=arg[1]; 24 String comment=arg[2]; 25 26 JSch jsch=new JSch(); 27 28 String passphrase=""; 29 JTextField passphraseField=(JTextField)new JPasswordField(20); 30 Object [] ob={passphraseField}; 31 int result= 32 JOptionPane.showConfirmDialog(null, ob, "Enter passphrase (empty for no passphrase)", 33 JOptionPane.OK_CANCEL_OPTION); 34 if(result==JOptionPane.OK_OPTION){ 35 passphrase=passphraseField.getText(); 36 } 37 38 try{ 39 KeyPair kpair=KeyPair.genKeyPair(jsch, type); 40 kpair.setPassphrase(passphrase); 41 kpair.writePrivateKey(filename); 42 kpair.writePublicKey(filename+".pub", comment); 43 System.out.println("Finger print: "+kpair.getFingerPrint()); 44 kpair.dispose(); 45 } 46 catch(Exception e){ 47 System.out.println(e); 48 } 49 System.exit(0); 50 } 51 } 52 | Popular Tags |