1 /*
2 * Copyright (C) 2000 Bill Bereza
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Bill Bereza
19 * email : bereza@pobox.com
20 * url : http://www.pobox.com/~bereza/
21 */
22 /*
23 * Copyright (C) 2000 Bill Bereza
24 *
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation; either version 2 of the License, or
28 * (at your option) any later version.
29 *
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
34 *
35 * You should have received a copy of the GNU General Public License
36 * along with this program; if not, write to the Free Software
37 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38 *
39 * Author: Bill Bereza
40 * email : bereza@pobox.com
41 * url : http://www.pobox.com/~bereza/
42 */
43 package net.bereza.cipher;
44
45 import java.io;
46 import java.util;
47 import java.text;
48
49 import java.awt;
50 import java.awt.event;
51
52 import javax.swing;
53 import javax.swing.event;
54 import javax.swing.text;
55 import javax.swing.border;
56
57 class BookTable extends Hashtable
58 {
59 int words=1;
60
61 private Hashtable numToChar=new Hashtable();
62
63 public synchronized void addWord(String word)
64 {
65 Character c=new Character(Character.toLowerCase(word.charAt(0)));
66 if(!Character.isLetter(c.charValue()))
67 {
68 return;
69 }
70
71 Vector v=(Vector)get(c);
72
73 if(v==null)
74 {
75 v=new Vector();
76 }
77
78 System.err.println(word+"("+words+") ");
79
80 Integer i=new Integer(words);
81
82 v.addElement(i);
83 numToChar.put(i, c);
84 words++;
85
86 put(c, v);
87 }
88
89 public synchronized char decryptNum(int num)
90 {
91 Character c=(Character)numToChar.get(new Integer(num));
92 if(c!=null)
93 {
94 return c.charValue();
95 }
96 else
97 {
98 return ' ';
99 }
100 }
101
102 public synchronized int encryptChar(char c)
103 {
104 Vector v=(Vector)get(new Character(Character.toLowerCase(c)));
105 if(v!=null)
106 {
107 Integer i=(Integer)v.elementAt((int)(v.size()*Math.random()));
108 if(i!=null)
109 {
110 return i.intValue();
111 }
112 }
113
114 return 0;
115 }
116 }
117
118 public class Beale extends JPanel implements DocumentListener
119 {
120 protected JTextArea sourceText;
121 //protected JTextComponent keyText;
122 protected JTextArea cipherText;
123 protected JButton keyFile;
124 protected JLabel keyLabel;
125 protected JButton cipher;
126 protected JLabel cipherLabel;
127 protected JButton plainFile;
128 protected JLabel plainLabel;
129
130 protected JScrollPane sourcePane;
131 //protected JScrollPane keyPane;
132 protected JScrollPane cipherPane;
133
134 protected JFileChooser keyChooser;
135
136 protected BookTable letterIndex=new BookTable();
137
138 protected DecimalFormat df=new DecimalFormat("00000 ");
139
140 class BealeDocument extends PlainDocument
141 {
142 public void insertString(int offs, String str, AttributeSet a)
143 throws BadLocationException
144 {
145 if (str == null)
146 {
147 return;
148 }
149 char[] upper = str.toCharArray();
150 StringBuffer ns=new StringBuffer();
151
152 for (int i = 0; i < upper.length; i++)
153 {
154 int l=letterIndex.encryptChar(upper[i]);
155 ns.append(df.format(l));
156 }
157
158 super.insertString(offs*6, ns.toString(), a);
159 }
160
161 public void remove(int offs, int len)
162 throws BadLocationException
163 {
164 int noff=offs*6;
165 int nlen=len*6;
166
167 super.remove(noff, nlen);
168 }
169 }
170
171 public Beale()
172 {
173 super();
174
175 GridBagLayout gridBag=new GridBagLayout();
176 setLayout(gridBag);
177 GridBagConstraints c=new GridBagConstraints();
178
179 sourceText=new JTextArea();
180 sourceText.setLineWrap(true);
181 sourcePane=new JScrollPane(sourceText);
182
183 sourceText.getDocument().addDocumentListener(this);
184
185 //keyText=new JEditorPane();
186 //keyPane=new JScrollPane(keyText);
187 cipherText=new JTextArea();
188 cipherText.setLineWrap(true);
189 cipherText.setEditable(false);
190 cipherText.setDocument(new BealeDocument());
191 cipherPane=new JScrollPane(cipherText);
192
193 plainFile=new JButton("Plain File");
194 plainFile.addActionListener(new ActionListener()
195 {
196 public void actionPerformed(ActionEvent e)
197 {
198 getPlainFile();
199 }
200 });
201
202 plainLabel=new JLabel("Plain file");
203
204 keyLabel=new JLabel("Choose key file");
205
206 keyFile=new JButton("Key File");
207 keyFile.addActionListener(new ActionListener()
208 {
209 public void actionPerformed(ActionEvent e)
210 {
211 getKeyFile();
212 }
213 });
214
215 cipher=new JButton("Decrypt");
216 cipher.addActionListener(new ActionListener()
217 {
218 public void actionPerformed(ActionEvent e)
219 {
220 getCipherFile();
221 }
222 });
223
224 cipherLabel=new JLabel("Encrypted file");
225
226 c.weightx=0.0;
227 c.fill = GridBagConstraints.NONE;
228
229 gridBag.setConstraints(keyFile, c);
230 add(keyFile);
231
232 c.weightx=1.0;
233 c.gridwidth = GridBagConstraints.REMAINDER; //end row
234 c.fill = GridBagConstraints.HORIZONTAL;
235
236 gridBag.setConstraints(keyLabel, c);
237 add(keyLabel);
238
239 c.weightx=0.0;
240 c.gridwidth = 1;
241 c.fill = GridBagConstraints.NONE;
242
243 gridBag.setConstraints(plainFile, c);
244 add(plainFile);
245
246 c.weightx=1.0;
247 c.gridwidth = GridBagConstraints.REMAINDER; //end row
248 c.fill = GridBagConstraints.HORIZONTAL;
249
250 gridBag.setConstraints(plainLabel, c);
251 add(plainLabel);
252
253 c.weightx=0.0;
254 c.gridwidth = 1;
255 c.fill = GridBagConstraints.NONE;
256
257 gridBag.setConstraints(cipher, c);
258 add(cipher);
259
260 c.weightx=1.0;
261 c.gridwidth = GridBagConstraints.REMAINDER; //end row
262 c.fill = GridBagConstraints.HORIZONTAL;
263
264 gridBag.setConstraints(cipherLabel, c);
265 add(cipherLabel);
266
267 c.gridwidth = 1;
268 c.fill = GridBagConstraints.BOTH;
269 c.weightx=1.0;
270 c.weighty=1.0;
271
272 gridBag.setConstraints(sourcePane, c);
273 add(sourcePane);
274
275 gridBag.setConstraints(cipherPane, c);
276 add(cipherPane);
277
278 //gridBag.setConstraints(keyPane, c);
279 //add(keyPane);
280
281 //setPreferredSize(new Dimension(600,300));
282 }
283
284 protected synchronized File getFile()
285 {
286 if(keyChooser == null)
287 {
288 keyChooser=new JFileChooser();
289 }
290
291 int ret=keyChooser.showOpenDialog(this);
292 if(ret == JFileChooser.APPROVE_OPTION)
293 {
294 File file=keyChooser.getSelectedFile();
295 return file;
296 }
297 return null;
298 }
299
300 protected synchronized void getPlainFile()
301 {
302 File file=getFile();
303
304 if(file == null)
305 {
306 return;
307 }
308
309 plainLabel.setText(file.getName());
310 try
311 {
312 BufferedReader bin=new BufferedReader(new FileReader(file));
313
314 String lin=bin.readLine();
315
316 while(lin != null)
317 {
318 sourceText.append(lin);
319 lin=bin.readLine();
320 }
321 bin.close();
322 }
323 catch(IOException e)
324 {
325 e.printStackTrace(System.err);
326 JOptionPane.showMessageDialog(this,
327 e,
328 "File Open Error",
329 JOptionPane.ERROR_MESSAGE);
330 }
331
332 return;
333 }
334
335 protected synchronized void getCipherFile()
336 {
337 File file=getFile();
338
339 if(file == null)
340 {
341 return;
342 }
343
344 cipherLabel.setText(file.getName());
345 try
346 {
347 BufferedReader bin=new BufferedReader(new FileReader(file));
348
349 String lin=bin.readLine();
350
351 while(lin != null)
352 {
353 StringTokenizer tz=new StringTokenizer(lin, ",- \t");
354
355 StringBuffer bif=new StringBuffer();
356 while(tz.hasMoreTokens())
357 {
358 String l=tz.nextToken();
359 int i=Integer.parseInt(l);
360 if(i>0)
361 {
362 bif.append(letterIndex.decryptNum(i));
363 }
364 else
365 {
366 bif.append(' ');
367 }
368 }
369
370 sourceText.append(bif.toString());
371 lin=bin.readLine();
372 }
373 bin.close();
374 }
375 catch(IOException e)
376 {
377 e.printStackTrace(System.err);
378 JOptionPane.showMessageDialog(this,
379 e,
380 "File Open Error",
381 JOptionPane.ERROR_MESSAGE);
382 }
383
384 return;
385 }
386
387 protected synchronized void getKeyFile()
388 {
389 File file=getFile();
390
391 if(file == null)
392 {
393 return;
394 }
395
396
397 keyLabel.setText(file.getName());
398
399 try
400 {
401 BufferedReader bin=new BufferedReader(new FileReader(file));
402
403 //keyText.setText("");
404
405 String lin=bin.readLine();
406 letterIndex=new BookTable();
407
408 while(lin != null)
409 {
410 java.text.BreakIterator boundary=java.text.BreakIterator.getWordInstance();
411 boundary.setText(lin);
412 int start = boundary.first();
413 for (int end = boundary.next();
414 end != java.text.BreakIterator.DONE;
415 start = end, end = boundary.next())
416 {
417 //System.err.println(lin.substring(start,end));
418 letterIndex.addWord(lin.substring(start,end));
419 }
420
421 lin=bin.readLine();
422 }
423 bin.close();
424 }
425 catch(IOException e)
426 {
427 e.printStackTrace(System.err);
428 JOptionPane.showMessageDialog(this,
429 e,
430 "File Open Error",
431 JOptionPane.ERROR_MESSAGE);
432 }
433 }
434
435 public void insertUpdate(DocumentEvent e)
436 {
437 try
438 {
439 cipherText.getDocument().insertString(e.getOffset(),
440 sourceText.getDocument()
441 .getText(e.getOffset(),
442 e.getLength()),
443 null);
444 }
445 catch(Exception f)
446 {
447 f.printStackTrace(System.err);
448 }
449
450 return;
451 }
452
453 public void removeUpdate(DocumentEvent e)
454 {
455 try
456 {
457 cipherText.getDocument().remove(e.getOffset(),
458 e.getLength());
459 }
460 catch(Exception f)
461 {
462 f.printStackTrace(System.err);
463 }
464
465 return;
466 }
467
468 public void changedUpdate(DocumentEvent e)
469 {
470 return;
471 }
472
473 public static void main(String argv[])
474 {
475 JFrame tFrame=new JFrame("Bealer Encipherer");
476
477 JPanel tp=new Beale();
478
479 tFrame.getContentPane().add("Center",tp);
480
481 tFrame.addWindowListener(new WindowAdapter()
482 {
483 public void windowClosing(WindowEvent e)
484 {
485 System.exit(0);
486 }
487 });
488
489
490
491 tFrame.pack();
492 tFrame.show();
493 }
494 }