1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */ 2 /** 3 * This program will demonstrate how to use "aes128-cbc". 4 * 5 */ 6 import com.jcraft.jsch.*; 7 import java.awt.*; 8 import javax.swing.*; 9 10 public class AES{ main(String[] arg)11 public static void main(String[] arg){ 12 13 try{ 14 JSch jsch=new JSch(); 15 16 //jsch.setKnownHosts("/home/foo/.ssh/known_hosts"); 17 18 String host=null; 19 if(arg.length>0){ 20 host=arg[0]; 21 } 22 else{ 23 host=JOptionPane.showInputDialog("Enter username@hostname", 24 System.getProperty("user.name")+ 25 "@localhost"); 26 } 27 String user=host.substring(0, host.indexOf('@')); 28 host=host.substring(host.indexOf('@')+1); 29 30 Session session=jsch.getSession(user, host, 22); 31 //session.setPassword("your password"); 32 33 // username and password will be given via UserInfo interface. 34 UserInfo ui=new MyUserInfo(); 35 session.setUserInfo(ui); 36 37 session.setConfig("cipher.s2c", "aes128-cbc,3des-cbc,blowfish-cbc"); 38 session.setConfig("cipher.c2s", "aes128-cbc,3des-cbc,blowfish-cbc"); 39 session.setConfig("CheckCiphers", "aes128-cbc"); 40 41 session.connect(); 42 43 Channel channel=session.openChannel("shell"); 44 45 channel.setInputStream(System.in); 46 channel.setOutputStream(System.out); 47 48 channel.connect(); 49 } 50 catch(Exception e){ 51 System.out.println(e); 52 } 53 } 54 55 public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{ getPassword()56 public String getPassword(){ return passwd; } promptYesNo(String str)57 public boolean promptYesNo(String str){ 58 Object[] options={ "yes", "no" }; 59 int foo=JOptionPane.showOptionDialog(null, 60 str, 61 "Warning", 62 JOptionPane.DEFAULT_OPTION, 63 JOptionPane.WARNING_MESSAGE, 64 null, options, options[0]); 65 return foo==0; 66 } 67 68 String passwd; 69 JTextField passwordField=(JTextField)new JPasswordField(20); 70 getPassphrase()71 public String getPassphrase(){ return null; } promptPassphrase(String message)72 public boolean promptPassphrase(String message){ return true; } promptPassword(String message)73 public boolean promptPassword(String message){ 74 Object[] ob={passwordField}; 75 int result= 76 JOptionPane.showConfirmDialog(null, ob, message, 77 JOptionPane.OK_CANCEL_OPTION); 78 if(result==JOptionPane.OK_OPTION){ 79 passwd=passwordField.getText(); 80 return true; 81 } 82 else{ 83 return false; 84 } 85 } showMessage(String message)86 public void showMessage(String message){ 87 JOptionPane.showMessageDialog(null, message); 88 } 89 final GridBagConstraints gbc = 90 new GridBagConstraints(0,0,1,1,1,1, 91 GridBagConstraints.NORTHWEST, 92 GridBagConstraints.NONE, 93 new Insets(0,0,0,0),0,0); 94 private Container panel; promptKeyboardInteractive(String destination, String name, String instruction, String[] prompt, boolean[] echo)95 public String[] promptKeyboardInteractive(String destination, 96 String name, 97 String instruction, 98 String[] prompt, 99 boolean[] echo){ 100 panel = new JPanel(); 101 panel.setLayout(new GridBagLayout()); 102 103 gbc.weightx = 1.0; 104 gbc.gridwidth = GridBagConstraints.REMAINDER; 105 gbc.gridx = 0; 106 panel.add(new JLabel(instruction), gbc); 107 gbc.gridy++; 108 109 gbc.gridwidth = GridBagConstraints.RELATIVE; 110 111 JTextField[] texts=new JTextField[prompt.length]; 112 for(int i=0; i<prompt.length; i++){ 113 gbc.fill = GridBagConstraints.NONE; 114 gbc.gridx = 0; 115 gbc.weightx = 1; 116 panel.add(new JLabel(prompt[i]),gbc); 117 118 gbc.gridx = 1; 119 gbc.fill = GridBagConstraints.HORIZONTAL; 120 gbc.weighty = 1; 121 if(echo[i]){ 122 texts[i]=new JTextField(20); 123 } 124 else{ 125 texts[i]=new JPasswordField(20); 126 } 127 panel.add(texts[i], gbc); 128 gbc.gridy++; 129 } 130 131 if(JOptionPane.showConfirmDialog(null, panel, 132 destination+": "+name, 133 JOptionPane.OK_CANCEL_OPTION, 134 JOptionPane.QUESTION_MESSAGE) 135 ==JOptionPane.OK_OPTION){ 136 String[] response=new String[prompt.length]; 137 for(int i=0; i<prompt.length; i++){ 138 response[i]=texts[i].getText(); 139 } 140 return response; 141 } 142 else{ 143 return null; // cancel 144 } 145 } 146 } 147 } 148 149 150