Download this file

CharTable.java    454 lines (390 with data), 14.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package cologne.eck.all_peas.gui;
/*
* Peafactory - Production of Password Encryption Archives
* Copyright (C) 2015 Axel von dem Bruch
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* See: http://www.gnu.org/licenses/gpl-2.0.html
* You should have received a copy of the GNU General Public License
* along with this library.
*/
/**
* CharTable provides UTF-8 characters for JPasswordFields.
* The intention behind to increase the entropy of passwords and to hinder key loggers.
*/
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.accessibility.AccessibleContext;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import cologne.eck.all_peas.data.PeaProperties;
import cologne.eck.tools.UnexpectedValueException;
import cologne.eck.tools.Zeroizer;
// TODO use fixedStartPoints for previous and next if possible
@SuppressWarnings("serial")
public class CharTable extends JDialog implements ActionListener {
// avoiding not correctly displayed characters makes the current
// lastUtf a bit unpredictable for the previous button...
private ArrayList<Integer> startPoints;// = new ArrayList<Integer>(8);
// the twelve first start points are fixed
private final int[] fixedStartPoints = {32, 216, 366, 525, 676,
1035, 1192, 1351, 4292, 7570, 7821, 7975};
// background color:
private static final Color color = new Color(247, 247, 247);
// current UTF character value
private int lastUtf = 32;
// current index of table
private int tableNumber = 1;
// the tables' contentPane
private JPanel contentPane = null;
// the password field, this class is used for
private JPasswordField pswField = null;
public CharTable(Window owner, JPasswordField _pswField, int startPoint) {
super(owner);
if (startPoint > 11) {
new UnexpectedValueException("startPoint", "is greater than 11", "can not open CharTable");
return;
// } else if (startPoint == 0) {
// set tableNumber:
// tableNumber = 1;
//startPoints = new ArrayList<Integer>();
} else {
// set start points manually
startPoints = new ArrayList<Integer>();
for (int i = 0; i <= 11; i++) {
startPoints.add(fixedStartPoints[i]);
}
// set tableNumber:
tableNumber = startPoint +1;
// set lastUtf
lastUtf = fixedStartPoints[startPoint];
}
pswField = _pswField;
this.setTitle("Table " + tableNumber);
this.setAlwaysOnTop(true);
this.setModal(false);// maybe used together with Keyboard
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
contentPane.setBackground(color);
JPanel indexPanel = new JPanel();
indexPanel.setLayout(new BoxLayout(indexPanel, BoxLayout.X_AXIS));
indexPanel.setBackground(color);
JLabel zeroLabel = new JLabel(" ");
zeroLabel.setMaximumSize(new Dimension(35, 25));
indexPanel.add(zeroLabel);
// first line
for (int i = 1; i <= 12; i++) {
JLabel label = new JLabel("" + i);
label.setMaximumSize(new Dimension(25, 25));
indexPanel.add(label);
}
contentPane.add(indexPanel);
// second line
JPanel secondPanel = new JPanel();
secondPanel.setLayout(new BoxLayout(secondPanel, BoxLayout.X_AXIS));
secondPanel.setBackground(color);
JLabel indexLabel = new JLabel("" + 1);
indexLabel.setMaximumSize(new Dimension(25, 25));
secondPanel.add(indexLabel);
JButton previousButton = new JButton();//previous");
ImageIcon icon = IconManager.loadIcon("zurueck.png", PeaProperties.getBundle().getString("back_arrow"),
PeaProperties.getBundle().getString("back_arrow"),
PeaProperties.getBundle().getString("show_next_table") );
if (icon != null) {
previousButton.setIcon(icon);
} else { // set characters
previousButton.setText(" <-");
previousButton.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 15));
}
previousButton.setMaximumSize(new Dimension(75,25));
previousButton.setMargin(new Insets(1,1,1,1));
previousButton.addActionListener(this);
previousButton.setActionCommand( "previous");
secondPanel.add(previousButton);
for (int i = 0; i < 9; i++) {
//TODO
if (tableNumber != 1 && (checkChar(lastUtf + i) == false)) {
lastUtf++;
i--;
continue;
}
KeyButton b = new KeyButton();
b.setMaximumSize(new Dimension(25,25));
b.addActionListener(this);
b.setText( Character.toString( (char) (lastUtf + i) ) );
b.setActionCommand( Character.toString( (char) (lastUtf + i) ));
secondPanel.add(b);
//lastUtf++;
}
lastUtf += 9;
contentPane.add(secondPanel);
// 10 middle lines
for (int i = 0; i < 11; i++) {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.setBackground(color);
JLabel label = new JLabel("" + (i + 2));
label.setMaximumSize(new Dimension(25, 25));
panel.add(label);
for(int j = 0; j < 12; j++) {
//TODO
if (tableNumber != 1 && (checkChar(lastUtf + j) == false)) {
lastUtf++;
j--;
continue;
}
KeyButton b = new KeyButton();
b.setMaximumSize(new Dimension(25,25));
b.addActionListener(this);
if( tableNumber == 1 && i == 7 && j == 2) { //TODO
lastUtf += 34;//avoid control and no break space
}
b.setText( Character.toString( (char) (lastUtf + j) ) );
b.setActionCommand( Character.toString( (char) (lastUtf + j) ));
panel.add(b);
//lastUtf++;
}
lastUtf += 12;
contentPane.add(panel);
}
// last line
JPanel lastPanel = new JPanel();
lastPanel.setLayout(new BoxLayout(lastPanel, BoxLayout.X_AXIS));
lastPanel.setBackground(color);
JLabel lastIndexLabel = new JLabel("" + 12);
lastIndexLabel.setMaximumSize(new Dimension(25, 25));
lastPanel.add(lastIndexLabel);
for (int i = 0; i < 9; i++) {
//TODO
if (tableNumber != 1 && (checkChar(lastUtf + i) == false)) {
lastUtf++;
i--;
continue;
}
KeyButton b = new KeyButton();
b.setMaximumSize(new Dimension(25,25));
b.addActionListener(this);
b.setText( Character.toString( (char) (lastUtf + i) ) );
b.setActionCommand( Character.toString( (char) (lastUtf + i) ));
lastPanel.add(b);
//lastUtf++;
}
lastUtf += 9;
startPoints.add(new Integer(lastUtf));
JButton nextButton = new JButton();
icon = IconManager.loadIcon("weiter.png", PeaProperties.getBundle().getString("continue_arrow"),
PeaProperties.getBundle().getString("continue_arrow"),
PeaProperties.getBundle().getString("show_previous_table") );
if (icon != null) {
nextButton.setIcon(icon);
} else { // set characters
nextButton.setText("-> ");
nextButton.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 15));
}
//JButton nextButton = new JButton("next");
nextButton.setMaximumSize(new Dimension(75,25));
nextButton.setMargin(new Insets(1,1,1,1));
nextButton.addActionListener(this);
nextButton.setActionCommand( "next");
lastPanel.add(nextButton);
contentPane.add(lastPanel);
//this.setSize(new Dimension(335, 375));//optimal openSuse LXDE
this.setSize(new Dimension(370, 400));
// set location: if possible above the password field
int tableHeight = this.getSize().height;
int placeAbovePswField = (int) pswField.getLocationOnScreen().getY()
- pswField.getSize().height;
int positionBelowPswField = (int) pswField.getLocationOnScreen().getY() + 25;
int screenHeight = (int) PswDialogView.getScreenheight();
if (placeAbovePswField > tableHeight){
this.setLocation((int) pswField.getLocationOnScreen().getX(),
placeAbovePswField - tableHeight);
} else if (tableHeight < (screenHeight - positionBelowPswField)){ // set below password field
this.setLocation((int) pswField.getLocationOnScreen().getX(),
positionBelowPswField);
} else { // set position of window owner
this.setLocation(owner.getLocationOnScreen());
}
}
public AccessibleContext getAccessibleContext() {
if (accessibleContext == null) {
accessibleContext = new AccessibleCharTable();
}
return accessibleContext;
}
@Override
public void actionPerformed(ActionEvent ape) {
String com = ape.getActionCommand();
//=========== TEST ============= works only on Java 1.7
// System.out.println(com + " " + (Character.getName(com.charAt(0))));
if (com.equals("next")){
//System.out.print(lastUtf + ", ");
this.setTitle("Table " + ++tableNumber);
// first line
JPanel p1 = (JPanel) contentPane.getComponent(1);
for (int i = 0; i < 9; i++) {
//if ( (Character.getName(lastUtf + i) == null)) {// works only on Java 1.7
if ( (checkChar(lastUtf + i) == false)) {
lastUtf++;
i--;
continue;
}
KeyButton b = (KeyButton) p1.getComponent(i+2);
b.setText( Character.toString( (char) (lastUtf + i) ) );
b.setActionCommand( Character.toString( (char) (lastUtf + i) ));
}
lastUtf += 9;
// 10 middle lines
for (int i = 0; i < 11; i++) {
JPanel p = (JPanel) contentPane.getComponent(i+2);
for (int j = 0; j <= 11; j++) {
if ( (checkChar(lastUtf + j) == false)) {
lastUtf++;
j--;
continue;
}
KeyButton b = (KeyButton) p.getComponent(j+1);
b.setText( Character.toString( (char) (lastUtf + j) ) );
b.setActionCommand( Character.toString( (char) (lastUtf + j) ));
}
lastUtf += 12;
}
// last line
JPanel p12 = (JPanel) contentPane.getComponent(13);
for (int i = 0; i <= 8; i++) {
if ( (checkChar(lastUtf + i) == false)) {
lastUtf++;
i--;
continue;
}
KeyButton b = (KeyButton) p12.getComponent(i+1);
b.setText( Character.toString( (char) (lastUtf + i) ) );
b.setActionCommand( Character.toString( (char) (lastUtf + i) ));
}
lastUtf += 9;
if (tableNumber > startPoints.size()) {
startPoints.add(new Integer(lastUtf));
}
} else if (com.equals("previous")){
if (tableNumber == 1) {
// do nothing
}
else if(tableNumber == 2){ // -> new instance of CharTable:
Point loc = this.getLocation();
Window win = this.getOwner();
this.dispose();
CharTable ct = new CharTable(win, pswField, 0);
ct.setLocation(loc);
ct.setVisible(true);
}
else {
this.setTitle("Table " + --tableNumber);
/* lastUtf = startPoints.get(startPoints.size() - 3);
startPoints.remove(startPoints.size() - 1);
startPoints.trimToSize(); */
lastUtf = startPoints.get(tableNumber -1);
// first line
JPanel p1 = (JPanel) contentPane.getComponent(1);
for (int i = 0; i < 9; i++) {
if ( (checkChar(lastUtf + i) == false)) {
lastUtf++;
i--;
continue;
}
KeyButton b = (KeyButton) p1.getComponent(i+2);
b.setText( Character.toString( (char) (lastUtf + i) ) );
b.setActionCommand( Character.toString( (char) (lastUtf + i) ));
}
lastUtf += 9;
// 10 middle lines
for (int i = 0; i < 11; i++) {
JPanel p = (JPanel) contentPane.getComponent(i+2);
for (int j = 0; j <= 11; j++) {
if ( (checkChar(lastUtf + j) == false)) {
lastUtf++;
j--;
continue;
}
KeyButton b = (KeyButton) p.getComponent(j+1);
b.setText( Character.toString( (char) (lastUtf + j) ) );
b.setActionCommand( Character.toString( (char) (lastUtf + j) ));
}
lastUtf += 12;
}
// last line
JPanel p12 = (JPanel) contentPane.getComponent(13);
for (int i = 0; i <= 8; i++) {
if ( (checkChar(lastUtf + i) == false)) {
lastUtf++;
i--;
continue;
}
KeyButton b = (KeyButton) p12.getComponent(i+1);
b.setText( Character.toString( (char) (lastUtf + i) ) );
b.setActionCommand( Character.toString( (char) (lastUtf + i) ));
}
lastUtf += 9;
}
} else { // one of the character buttons: append to chars in password field
if (pswField != null) {
char[] oldPsw = pswField.getPassword();
char[] tmp = new char[oldPsw.length + 1];
System.arraycopy(oldPsw, 0, tmp, 0, oldPsw.length);
Zeroizer.zero(oldPsw);
tmp[tmp.length-1] = com.charAt(0);
pswField.setText(new String(tmp));
Zeroizer.zero(tmp);
} else {
// System.out.println("Type: " + Character.getType(com.charAt(0)));
System.err.println("Password field for table was closed.");
}
}
}
private boolean checkChar(int utf) {
int type = Character.getType(utf);
// System.out.println(type);
if ( // avoid characters which are not displayed correctly
type == 1 || type == 2
// || type == 24 //OTHER_PUNCTUATION
|| type == 25 // MATH_SYMBOL
// || type == 26 // CURRENCY_SYMBOL
|| type == 28 // OTHER_SYMBOL
) {
//System.out.println(Character.getType(utf));
return true;
} else {
// (! (Character.isValidCodePoint(utf)) )
/*
|| Character.getType(utf) == 0 // UNASSIGNED + MIN_VALUE + MIN_CODE_POINT
|| Character.getType(utf) == 27 // MODIFIER_SYMBOL
|| Character.getType(utf) == 6 // NON_SPACING_MARK + DIRECTIONALITY_ARABIC_NUMBER
|| Character.getType(utf) == 5 // OTHER_LETTER + DIRECTIONALITY_EUROPEAN_NUMBER_TERMINATOR
|| Character.getType(utf) == 15 // CONTROL + DIRECTIONALITY_LEFT_TO_RIGHT_OVERRIDE +
|| Character.getType(utf) == 16 // SIZE + FORMAT + DIRECTIONALITY_RIGHT_TO_LEFT_EMBEDDING +
*/
return false;
}
}
protected class AccessibleCharTable extends AccessibleJDialog {
//Inherit everything, override nothing.
}
}