KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > AES


1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2 import com.jcraft.jsch.*;
3 import java.awt.*;
4 import javax.swing.*;
5
6 public class AES{
7   public static void main(String JavaDoc[] arg){
8
9     try{
10       JSch jsch=new JSch();
11
12       //jsch.setKnownHosts("/home/foo/.ssh/known_hosts");
13

14       String JavaDoc host=JOptionPane.showInputDialog("Enter username@hostname",
15                                               System.getProperty("user.name")+
16                                               "@localhost");
17       String JavaDoc user=host.substring(0, host.indexOf('@'));
18       host=host.substring(host.indexOf('@')+1);
19
20       Session session=jsch.getSession(user, host, 22);
21       //session.setPassword("your password");
22

23       // username and password will be given via UserInfo interface.
24
UserInfo ui=new MyUserInfo();
25       session.setUserInfo(ui);
26
27       java.util.Hashtable JavaDoc config=new java.util.Hashtable JavaDoc();
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 JavaDoc e){
42       System.out.println(e);
43     }
44   }
45
46   public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{
47     public String JavaDoc getPassword(){ return passwd; }
48     public boolean promptYesNo(String JavaDoc str){
49       Object JavaDoc[] 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 JavaDoc passwd;
60     JTextField passwordField=(JTextField)new JPasswordField(20);
61
62     public String JavaDoc getPassphrase(){ return null; }
63     public boolean promptPassphrase(String JavaDoc message){ return true; }
64     public boolean promptPassword(String JavaDoc message){
65       Object JavaDoc[] 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 JavaDoc 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 JavaDoc[] promptKeyboardInteractive(String JavaDoc destination,
87                                               String JavaDoc name,
88                                               String JavaDoc instruction,
89                                               String JavaDoc[] 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 JavaDoc[] response=new String JavaDoc[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; // cancel
135
}
136     }
137   }
138 }
139
140
141
Popular Tags