KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > crypto > SecureInputKeyboardDialog


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.geom.*;
11 import java.awt.im.*;
12 import java.awt.image.*;
13 import java.awt.event.*;
14 import java.awt.font.*;
15 import javax.swing.*;
16
17 import java.security.*;
18 import java.security.spec.*;
19 import javax.crypto.spec.*;
20 import javax.crypto.*;
21 import javax.swing.border.*;
22 import java.io.*;
23 import java.util.zip.*;
24 import java.util.*;
25
26 /** call terminate() at the end !
27 */

28 public final class SecureInputKeyboardDialog extends JWindow
29 {
30   private final Vector<HitListener> keyListeners = new Vector<HitListener>();
31   private final int NumberOfColumns = 13;
32   private final GridLayout3 keysLayout;
33   private final ActionListener keyActionListener;
34   private boolean terminate = false;
35   private final Thread JavaDoc animateThread;
36   private final Font keyFont = new Font("SansSerif", Font.PLAIN, 12);
37   private final FontRenderContext fontRenderContext = new FontRenderContext(new AffineTransform(1,0,0,1,0,0),false,true);
38
39
40   public SecureInputKeyboardDialog(JDialog parent)
41   {
42       super(parent);
43       setFocusable(false);
44       setFocusableWindowState(false);
45
46       getContentPane().setLayout(new BorderLayout());
47
48       JTextArea kexplanationArea = new JTextArea(
49         Language.translate("Entering some chars with the mouse will deceive keyloggers.\nHit Shift for uppercases.")
50       );
51       getContentPane().add(kexplanationArea, BorderLayout.NORTH);
52       kexplanationArea.setOpaque(false);
53       kexplanationArea.setEditable(false);
54       kexplanationArea.setBackground(getBackground());
55       kexplanationArea.setBorder(new EmptyBorder(5,5,5,5));
56
57       JPanel keyPan = new JPanel();
58       keyPan.setLayout(new GridLayout(4,13));
59       keysLayout = new GridLayout3(NumberOfColumns,keyPan);
60       getContentPane().add(keyPan, BorderLayout.CENTER);
61       keyActionListener = new ActionListener()
62       {
63         public void actionPerformed(ActionEvent ae)
64         {
65            String JavaDoc str = ((SecureButton) ae.getSource()).getKey();
66            notifyKeyTyped(str.charAt(0));
67         }
68       };
69    // addKeysLine("0");
70
addKeysLine("0123456789");
71       addKeysLine("abcdefghijklmnopqrstuvwxyz ");
72
73
74       pack();
75       this.setLocationRelativeTo(parent); // to center in X
76
// and manually now in y
77
setLocation( getX(),
78                    (int) (parent.getLocation().getY() + parent.getHeight()+5) );
79
80       animateThread = new Thread JavaDoc()
81       {
82         public void run()
83         {
84           paintLoop();
85         }
86       };
87       animateThread.setDaemon(true);
88       animateThread.start();
89
90       //launchDivertThread();
91
//launchDivertThread();
92

93       /*
94       this.getGlassPane().addMouseListener(new MouseAdapter()
95       {
96          @Override public void mouseEntered(MouseEvent me)
97          {
98            mouseOnKeyboard = true;
99            synchronized(animateThread)
100            {
101             // animateThread.notify();
102            }
103            repaint();
104          }
105
106          @Override public void mouseExited(MouseEvent me)
107          {
108            mouseOnKeyboard = false;
109            animateThread.yield();
110          }
111       });*/

112   } // Constructor
113

114   private final void launchDivertThread()
115   {
116       Thread JavaDoc divertThread = new Thread JavaDoc()
117       {
118         public void run()
119         {
120           double a = 0;
121           int i=0;
122           while(!terminate)
123           {
124             a += Math.sqrt(i++);
125           }
126           System.out.println("a="+a);
127         }
128       };
129       divertThread.setDaemon(true);
130       divertThread.start();
131   }
132
133
134   private boolean hideKeys = false;
135   private boolean mouseOnKeyboard= true;
136
137   public final void terminate()
138   {
139     this.setVisible(false);
140     terminate = true;
141     buttons.removeAllElements();
142     synchronized(keyListeners)
143     {
144       this.keyListeners.removeAllElements();
145     }
146     this.getContentPane().removeAll();
147   }
148
149   private void paintLoop()
150   {
151      while(!terminate)
152      {
153         if(!this.isVisible())
154         {
155           Thread.yield();
156         }
157         else
158         {
159           /*for(SecureButton sb:buttons)
160           {
161             sb.repaint();
162           } */

163
164
165           repaint();
166           try
167           {
168             Thread.sleep(1);
169           } catch(Exception JavaDoc ee) {}
170         }
171      }
172   }
173
174   int addedKeys = 0;
175   private final Vector<SecureButton> buttons = new Vector<SecureButton>();
176   private void addKeysLine(String JavaDoc keys)
177   {
178       Vector<Character JavaDoc> lchars = new Vector<Character JavaDoc>(); // NO NO NOArrays.asList(chars));
179
for(char c : keys.toCharArray()) lchars.add(c);
180       Collections.shuffle(lchars);
181       for(Character JavaDoc c : lchars)
182       {
183          SecureButton bt = new SecureButton(""+c);
184          keysLayout.add(bt);
185          buttons.add(bt);
186          bt.addActionListener(this.keyActionListener);
187          addedKeys++;
188       }
189
190       // fill
191
while(addedKeys%this.NumberOfColumns != 0)
192       {
193          addedKeys++;
194          keysLayout.add("");
195
196       }
197   }
198
199   class SecureButton extends JButton
200   {
201     private final String JavaDoc key;
202     private int repaints = 0;
203     BufferedImage bim = new BufferedImage(13,12,BufferedImage.TYPE_INT_ARGB);
204     BufferedImage[] bimp;
205     final private Vector<int[]> posBlack = new Vector<int[]>();
206
207     public SecureButton(String JavaDoc key)
208     {
209       super("");
210       this.key = key;
211       this.setPreferredSize(new Dimension(30,20));
212
213       Graphics gr = bim.createGraphics();
214       gr.setColor(Color.white);
215       //gr.fillRect(0,0,13,12); // background
216
gr.setColor(Color.black);
217       TextLayout textLayout = new TextLayout(key, keyFont, fontRenderContext);
218       int w = (int) textLayout.getBounds().getWidth();
219       gr.setFont(keyFont);
220       gr.drawString(key,6-w/2,10);
221       gr.dispose();
222
223       // RGB= 0 for transparent, 0 for white, -16777216 for black
224
//System.out.println(bim.getRGB(0,0));
225
// count black pixels
226
int bc = 0;
227       for(int j=0;j<bim.getHeight();j++)
228       {
229          for(int i=0; i<bim.getWidth(); i++)
230          {
231            if(bim.getRGB(i,j)==-16777216)
232            {
233              posBlack.add(new int[]{i,j});
234            }
235          }
236       }
237
238       Collections.shuffle(posBlack);
239
240       bimp = new BufferedImage[3];
241       int n=posBlack.size()/bimp.length;
242       int pos=0;
243       for(int i=0; i<bimp.length; i++)
244       {
245
246              BufferedImage bi = new BufferedImage(bim.getWidth(), bim.getHeight(), bim.getType());
247              bimp[i] = bi;
248              Graphics gri = bi.createGraphics();
249              gri.setColor(Color.white);
250              //gri.fillRect(0,0,13,12); // background
251

252              gri.dispose();
253              // set n next pixels
254
int start = pos;
255              int end = pos+n;
256              int cnt = 0;
257              for(;pos<end && pos<posBlack.size();pos++)
258              {
259                int[] xy = posBlack.elementAt(pos);
260                bi.setRGB(xy[0], xy[1], -16777216);
261                cnt++;
262              }
263       }
264
265     }
266
267     public String JavaDoc getKey() {return key; }
268
269     public void paintComponent(Graphics g)
270     {
271
272       super.paintComponent(g);
273       if(!mouseOnKeyboard)
274       {
275         g.drawString("*",12,15);
276         return;
277       }
278
279       /*if(Math.random()<0.01)
280       {
281         g.dispose();
282         return;
283       } */

284
285       //g.setClip(12,5,10,10);
286
//g.setColor(Color.white);
287
//g.setColor(Color.blue);
288

289       //g.drawRect(10,4,13,12);
290

291 // g.setColor(Color.black);
292
if(bimp.length==0) return;
293
294
295       g.drawImage(bimp[ repaints%bimp.length],5,5,this);
296
297       //g.drawImage(bimp[ (int)(Math.random()*bimp.length)],5,5,this);
298

299
300       //setClip(g); //.setClip(10,10,20,20);
301
//g.drawString(key,12,15);
302
//g.dispose();
303
/*g.drawImage(bim,
304          12,5,this);*/

305 // (int) (Math.random()*13),
306
// (int) (Math.random()*12),
307
// this);
308
repaints++;
309       g.dispose();
310     }
311   }
312
313
314   /** not nice !
315   */

316   private void setRandomClip(Graphics g)
317   {
318     g.setClip(
319       10+(int)(Math.random()*10.0),
320       4+(int)(Math.random()*10.0),
321       5,5);
322   }
323
324
325
326   /** used to listen to the clicked keys
327   */

328   interface HitListener
329   {
330      public void keyTyped(char c);
331   }
332
333   public final void addKeyListener(HitListener kl)
334   {
335     synchronized(keyListeners)
336     {
337       this.keyListeners.add(kl);
338     }
339   }
340
341   public final void removeKeyListener(HitListener kl)
342   {
343     synchronized(keyListeners)
344     {
345       this.keyListeners.remove(kl);
346     }
347   }
348
349   private final void notifyKeyTyped(char c)
350   {
351     // subtle: when closing, one receive a java.util.ConcurrentModificationException !
352
// => must iterate "classically"
353
synchronized(keyListeners)
354     {
355       for(int i=0; i<keyListeners.size(); i++)
356       {
357         HitListener kl = keyListeners.elementAt(i);
358         kl.keyTyped(c);
359       }
360     }
361   }
362
363
364   public static void main(String JavaDoc[] a)
365   {
366     JFrame fra = new JFrame("test key dialog");
367     fra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
368     JDialog fr = new JDialog(fra, "test", false);
369     fr.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE); // no effect !!!
370
final JTextField tf = new JTextField(20);
371     fra.getContentPane().add(tf,BorderLayout.CENTER);
372
373     fra.setSize(600,200);
374     fra.setLocationRelativeTo(null);
375     fra.setVisible(true);
376     fr.setSize(600,200);
377     fr.setLocationRelativeTo(null);
378     //fr.setVisible(true);
379

380
381     SecureInputKeyboardDialog sid = new SecureInputKeyboardDialog(fr);
382     sid.setVisible(true);
383
384     sid.addKeyListener(new HitListener()
385     {
386        public void keyTyped(char c)
387        {
388          tf.setText(tf.getText()+c);
389        }
390     });
391   }
392
393 } // SecureInputKeyboardDialog
Popular Tags