KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > crypto > PassphraseDialog


1 package SnowMailClient.crypto;
2
3 import snow.utils.gui.*;
4 import SnowMailClient.SnowMailClientApp;
5 import SnowMailClient.Language.Language;
6 import snow.lookandfeel.*;
7 import snow.crypto.*;
8
9 import java.awt.*;
10 import java.awt.event.*;
11 import javax.swing.*;
12
13 import java.security.*;
14 import java.security.spec.*;
15 import javax.crypto.spec.*;
16 import javax.crypto.*;
17 import javax.swing.border.*;
18 import java.io.*;
19 import java.util.zip.*;
20 import java.util.*;
21
22 /** Ask for a passphrase, knowing a skid, immediately validate the dialog if the pass matches !
23     Important: call terminate !!
24     Offers cracking password with dictionary and systematic search.
25 */

26 public final class PassphraseDialog extends JDialog
27 {
28
29   final int fontSize = UIManager.getFont("Label.font").getSize();
30   final private JPasswordField passField = new JPasswordField(20);
31   final private JTextArea explanationArea = new JTextArea();
32   private CloseControlPanel ccp = null;
33   final private SecretKeyID skid;
34   private SecureInputKeyboardDialog keyboardDialog = null; // created at first call
35
private boolean validateIfMatch = true;
36   private final File fileToDecipher;
37
38   /** @param skid null if no verification should be made here
39   */

40   public PassphraseDialog( JFrame owner, String JavaDoc title, boolean isModal, SecretKeyID skid, File fileToDecipher, String JavaDoc explanation )
41   {
42     super(owner,title,isModal);
43     this.skid = skid;
44     this.fileToDecipher = fileToDecipher;
45     explanationArea.setText(explanation);
46     init();
47   }
48
49   public PassphraseDialog( JDialog owner, String JavaDoc title, boolean isModal, SecretKeyID skid, File fileToDecipher, String JavaDoc explanation )
50   {
51     super(owner,title,isModal);
52     this.skid = skid;
53     this.fileToDecipher = fileToDecipher;
54     explanationArea.setText(explanation);
55     init();
56   }
57
58   private void init()
59   {
60     final JPanel cp = new SnowBackgroundPanel(new BorderLayout());
61     setContentPane(cp);
62     cp.setBorder(new EmptyBorder(fontSize/2, fontSize/2, fontSize/2, fontSize/2));
63
64     // South
65
ccp = new CloseControlPanel(this, true, true, Language.translate("Validate"));
66     JButton crackBT = new JButton(Language.translate("Crack"));
67     crackBT.setMargin(new Insets(0,2,0,2));
68     crackBT.setFocusPainted(false);
69     //crackBT.setToolTipText("");
70
ccp.add(crackBT, null, 0);
71     crackBT.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) {
72        crackPassword();
73     } });
74
75     if(validateIfMatch)
76     {
77       ccp.getOkButton().setVisible(false);
78     }
79
80     getContentPane().add(ccp, BorderLayout.SOUTH);
81
82     // North
83
getContentPane().add(this.explanationArea, BorderLayout.NORTH);
84     explanationArea.setOpaque(false);
85     explanationArea.setEditable(false);
86     explanationArea.setBackground(cp.getBackground());
87
88     // Center
89
JPanel centerPanel_ = new JPanel();
90     GridLayout3 grid = new GridLayout3(3, centerPanel_);
91     //JPanel centerPanel = new JPanel(new GridLayout(1,2, fontSize/2, fontSize/2));
92
centerPanel_.setOpaque(false);
93     centerPanel_.setBorder(new EmptyBorder(fontSize/2, fontSize/2, fontSize/2, fontSize/2));
94     getContentPane().add(centerPanel_, BorderLayout.CENTER);
95     grid.add(new JContrastLabel(Language.translate("Passphrase"),
96                   SnowMailClientApp.loadImageIcon("pics/key.PNG"), JLabel.LEFT), false);
97     grid.add(passField, true);
98     passField.addActionListener(new ActionListener()
99     {
100       public void actionPerformed(ActionEvent e)
101       {
102         terminate();
103       }
104     });
105
106     JButton keyboard = new JButton("...");
107
108     keyboard.setMargin(new Insets(0,2,0,2));
109     grid.add(keyboard, false);
110     keyboard.addActionListener(new ActionListener()
111     {
112       public void actionPerformed(ActionEvent e)
113       {
114         if(keyboardDialog==null)
115         {
116           // create
117
keyboardDialog = new SecureInputKeyboardDialog(PassphraseDialog.this);
118           keyboardDialog.addKeyListener(new SecureInputKeyboardDialog.HitListener()
119           {
120              public void keyTyped(char c)
121              {
122                 if(shiftDown)
123                 {
124                   passField.setText( new String JavaDoc(passField.getPassword()) + Character.toUpperCase(c) );
125                 }
126                 else
127                 {
128                   passField.setText( new String JavaDoc(passField.getPassword()) + c );
129                 }
130                 lookIfmatches();
131              }
132           });
133
134         }
135
136         if(keyboardDialog.isVisible())
137         {
138           keyboardDialog.setVisible(false);
139           passField.requestFocus();
140           return;
141         }
142         else
143         {
144           keyboardDialog.setVisible(true);
145         }
146
147       }
148     });
149
150     if(skid!=null)
151     {
152       passField.addKeyListener(new KeyAdapter()
153       {
154         @Override JavaDoc public void keyReleased(KeyEvent e)
155         {
156           lookIfmatches();
157
158           shiftDown = e.isShiftDown();
159         }
160
161         @Override JavaDoc public void keyPressed(KeyEvent e)
162         {
163           shiftDown = e.isShiftDown();
164         }
165
166       });
167       ccp.setOkEnabled(false);
168       ccp.setOkBackground(ThemesManager.getInstance().getRed());
169     }
170
171
172     this.pack();
173     this.setLocationRelativeTo(this.getOwner());
174
175     this.addComponentListener(new ComponentAdapter()
176     {
177       @Override JavaDoc public void componentShown(ComponentEvent e)
178       {
179         passField.requestFocus();
180       }
181     });
182
183     passField.requestFocus();
184     passField.requestFocusInWindow();
185
186
187     addWindowListener(new WindowAdapter()
188     {
189         @Override JavaDoc public void windowClosed(WindowEvent e)
190         {
191            terminate();
192         }
193         @Override JavaDoc public void windowClosing(WindowEvent e)
194         {
195            terminate();
196         }
197     });
198
199     this.setVisible(true);
200   } // initialize
201

202   private boolean shiftDown = false;
203
204   /** very important: offers to crack the password, this is the unique good way to entrust it !
205   * when ones see that either dictionary attack fails and systematic search will take billions of years to complete !
206   */

207   private void crackPassword()
208   {
209      new PasswordSearchDialog(this, skid.getKeySignature(), fileToDecipher);
210   }
211
212   private final void lookIfmatches()
213   {
214      if(matchesID())
215      {
216         ccp.setOkEnabled(true);
217         ccp.setOkBackground(ThemesManager.getInstance().getGreen());
218         if(validateIfMatch)
219         {
220            // quick accept without waiting for "enter"
221
terminate();
222         }
223      }
224      else
225      {
226         ccp.setOkEnabled(false);
227         ccp.setOkBackground(ThemesManager.getInstance().getRed());
228      }
229   }
230
231   public boolean wasCancelled()
232   {
233     return ccp.getWasCancelled();
234   }
235
236   public boolean matchesID()
237   {
238     if(this.skid==null) return false;
239     try
240     {
241       SecretKeyID skid2 = SecretKeyUtilities.computeSignature( getKey() );
242       return skid2.equals(skid);
243     }
244     catch(Exception JavaDoc e)
245     {
246       return false;
247     }
248   }
249
250   public final SecretKey getKey() throws Exception JavaDoc
251   {
252
253     byte[] pass = new String JavaDoc(passField.getPassword()).getBytes();
254     int len = 16;
255     if(skid!=null)
256     {
257       len = skid.getKeyLangth();
258     }
259     return SecretKeyUtilities.generateSecretKeyFromPassphrase(pass, len);
260   }
261
262
263   private void terminate()
264   {
265     setVisible(false);
266     if(keyboardDialog!=null)
267     {
268       this.keyboardDialog.terminate();
269     }
270   }
271
272
273 } // PassphraseDialog
Popular Tags