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