Commit | Line | Data |
---|---|---|
8de51304 H |
1 | import java.io.*; |
2 | ||
3 | /* | |
4 | * hlavokno.java | |
5 | * | |
6 | * Created on 24. září 2007, 0:37 | |
7 | */ | |
8 | ||
9 | /** | |
10 | * | |
11 | * @author harvie | |
12 | */ | |
13 | public class hlavokno extends javax.swing.JFrame { | |
14 | ||
15 | /** Creates new form hlavokno */ | |
16 | public hlavokno () { | |
17 | initComponents (); | |
18 | } | |
19 | ||
20 | /** This method is called from within the constructor to | |
21 | * initialize the form. | |
22 | * WARNING: Do NOT modify this code. The content of this method is | |
23 | * always regenerated by the Form Editor. | |
24 | */ | |
25 | // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents | |
26 | private void initComponents() { | |
27 | jDialog1 = new javax.swing.JDialog(); | |
28 | jFileChooser1 = new javax.swing.JFileChooser(); | |
29 | jButton1 = new javax.swing.JButton(); | |
30 | jTextField1 = new javax.swing.JTextField(); | |
31 | jButton2 = new javax.swing.JButton(); | |
32 | jTextField2 = new javax.swing.JTextField(); | |
33 | jButton3 = new javax.swing.JButton(); | |
34 | jTextField3 = new javax.swing.JTextField(); | |
35 | jButton4 = new javax.swing.JButton(); | |
36 | jTextField4 = new javax.swing.JTextField(); | |
37 | jButton5 = new javax.swing.JButton(); | |
38 | ||
39 | jFileChooser1.setCurrentDirectory(null); | |
40 | ||
41 | org.jdesktop.layout.GroupLayout jDialog1Layout = new org.jdesktop.layout.GroupLayout(jDialog1.getContentPane()); | |
42 | jDialog1.getContentPane().setLayout(jDialog1Layout); | |
43 | jDialog1Layout.setHorizontalGroup( | |
44 | jDialog1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) | |
45 | .add(jFileChooser1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) | |
46 | ); | |
47 | jDialog1Layout.setVerticalGroup( | |
48 | jDialog1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) | |
49 | .add(jFileChooser1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) | |
50 | ); | |
51 | ||
52 | setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); | |
53 | setTitle("One-time pad (XOR) symetric encryption [<-Harvie 2oo7]"); | |
54 | jButton1.setText("Input"); | |
55 | jButton1.addMouseListener(new java.awt.event.MouseAdapter() { | |
56 | public void mouseClicked(java.awt.event.MouseEvent evt) { | |
57 | jButton1MouseClicked(evt); | |
58 | } | |
59 | }); | |
60 | ||
61 | jButton2.setText("Output"); | |
62 | jButton2.addMouseListener(new java.awt.event.MouseAdapter() { | |
63 | public void mouseClicked(java.awt.event.MouseEvent evt) { | |
64 | jButton2MouseClicked(evt); | |
65 | } | |
66 | }); | |
67 | ||
68 | jButton3.setText("Key"); | |
69 | jButton3.addMouseListener(new java.awt.event.MouseAdapter() { | |
70 | public void mouseClicked(java.awt.event.MouseEvent evt) { | |
71 | jButton3MouseClicked(evt); | |
72 | } | |
73 | }); | |
74 | ||
75 | jButton4.setText("Start"); | |
76 | jButton4.addMouseListener(new java.awt.event.MouseAdapter() { | |
77 | public void mouseClicked(java.awt.event.MouseEvent evt) { | |
78 | crypt(evt); | |
79 | } | |
80 | }); | |
81 | ||
82 | jTextField4.setEditable(false); | |
83 | jTextField4.setText("Waiting..."); | |
84 | ||
85 | jButton5.setText(".ccr"); | |
86 | jButton5.addMouseListener(new java.awt.event.MouseAdapter() { | |
87 | public void mouseClicked(java.awt.event.MouseEvent evt) { | |
88 | jButton5MouseClicked(evt); | |
89 | } | |
90 | }); | |
91 | ||
92 | org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); | |
93 | getContentPane().setLayout(layout); | |
94 | layout.setHorizontalGroup( | |
95 | layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) | |
96 | .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() | |
97 | .addContainerGap() | |
98 | .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) | |
99 | .add(org.jdesktop.layout.GroupLayout.LEADING, jButton4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 467, Short.MAX_VALUE) | |
100 | .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup() | |
101 | .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) | |
102 | .add(jButton3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE) | |
103 | .add(org.jdesktop.layout.GroupLayout.LEADING, jButton2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
104 | .add(org.jdesktop.layout.GroupLayout.LEADING, jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)) | |
105 | .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) | |
106 | .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) | |
107 | .add(jTextField3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE) | |
108 | .add(org.jdesktop.layout.GroupLayout.LEADING, jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE) | |
109 | .add(layout.createSequentialGroup() | |
110 | .add(jTextField2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE) | |
111 | .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) | |
112 | .add(jButton5)))) | |
113 | .add(org.jdesktop.layout.GroupLayout.LEADING, jTextField4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 467, Short.MAX_VALUE)) | |
114 | .addContainerGap()) | |
115 | ); | |
116 | layout.setVerticalGroup( | |
117 | layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) | |
118 | .add(layout.createSequentialGroup() | |
119 | .addContainerGap() | |
120 | .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) | |
121 | .add(jButton1) | |
122 | .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 24, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) | |
123 | .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) | |
124 | .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) | |
125 | .add(layout.createSequentialGroup() | |
126 | .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) | |
127 | .add(jTextField2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE) | |
128 | .add(jButton5)) | |
129 | .add(6, 6, 6)) | |
130 | .add(layout.createSequentialGroup() | |
131 | .add(jButton2) | |
132 | .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED))) | |
133 | .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) | |
134 | .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false) | |
135 | .add(jTextField3) | |
136 | .add(jButton3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) | |
137 | .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) | |
138 | .add(jButton4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 26, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) | |
139 | .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) | |
140 | .add(jTextField4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) | |
141 | .addContainerGap()) | |
142 | ); | |
143 | pack(); | |
144 | }// </editor-fold>//GEN-END:initComponents | |
145 | ||
146 | private void crypt (java.awt.event.MouseEvent evt) {//GEN-FIRST:event_crypt | |
147 | // TODO crypt code here: | |
148 | ||
149 | jTextField4.setText("Working..."); | |
150 | //jProgressBar1.setValue(0); | |
151 | ||
152 | if(jTextField1.getText().equals(jTextField2.getText())) { jTextField4.setText("Output to Input - Can't do this!"); return; } | |
153 | ||
154 | try { | |
155 | if( !(new File(jTextField1.getText()).isFile()) ) { jTextField4.setText("Input file not found!"); return; } | |
156 | if( !(new File(jTextField3.getText()).isFile()) ) { jTextField4.setText("Key file not found!"); return; } | |
157 | if( new File(jTextField2.getText()).isFile() ) { jTextField4.setText("Working (Rewriting output file)..."); } | |
158 | long size = (new File(jTextField1.getText()).length()); | |
159 | long keysize = (new File(jTextField3.getText()).length()); | |
160 | long pos = 0; | |
161 | ||
162 | BufferedInputStream in = new BufferedInputStream(new FileInputStream(jTextField1.getText())); | |
163 | BufferedInputStream key = new BufferedInputStream(new FileInputStream(jTextField3.getText())); | |
164 | BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(jTextField2.getText())); | |
165 | ||
166 | //Alghorythm | |
167 | int inb, keyb, progress; | |
168 | while((inb = in.read()) != -1) { | |
169 | while((keyb = key.read()) == -1 /*|| keyb == 0x0A*/ ) { | |
170 | key.close(); | |
171 | key = new BufferedInputStream(new FileInputStream(jTextField3.getText())); | |
172 | } | |
173 | out.write(inb^keyb); | |
174 | //pos++; | |
175 | //jProgressBar1.setValue((int)((pos/size)*100)); | |
176 | } | |
177 | ||
178 | out.close(); | |
179 | in.close(); | |
180 | key.close(); | |
181 | ||
182 | jTextField4.setText("Waiting... ["+size+" b (en|de)crypted using "+keysize+" b key]"); | |
183 | } catch(Exception e) { jTextField4.setText("I/O Error!"); } | |
184 | }//GEN-LAST:event_crypt | |
185 | ||
186 | private void jButton5MouseClicked (java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton5MouseClicked | |
187 | // TODO add your handling code here: | |
188 | jTextField2.setText(jTextField2.getText()+".ccr"); | |
189 | }//GEN-LAST:event_jButton5MouseClicked | |
190 | ||
191 | private void jButton3MouseClicked (java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseClicked | |
192 | // TODO add your handling code here: | |
193 | int returnVal = jFileChooser1.showOpenDialog(jDialog1); | |
194 | if(jFileChooser1.getSelectedFile() != null) { | |
195 | jTextField3.setText(jFileChooser1.getCurrentDirectory()+"/"+jFileChooser1.getSelectedFile().getName()); | |
196 | } | |
197 | }//GEN-LAST:event_jButton3MouseClicked | |
198 | ||
199 | private void jButton2MouseClicked (java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked | |
200 | // TODO add your handling code here: | |
201 | int returnVal = jFileChooser1.showOpenDialog(jDialog1); | |
202 | if(jFileChooser1.getSelectedFile() != null) { | |
203 | jTextField2.setText(jFileChooser1.getCurrentDirectory()+"/"+jFileChooser1.getSelectedFile().getName()); | |
204 | } | |
205 | }//GEN-LAST:event_jButton2MouseClicked | |
206 | ||
207 | private void jButton1MouseClicked (java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked | |
208 | // TODO add your handling code here: | |
209 | int returnVal = jFileChooser1.showOpenDialog(jDialog1); | |
210 | if(jFileChooser1.getSelectedFile() != null) { | |
211 | jTextField1.setText(jFileChooser1.getCurrentDirectory()+"/"+jFileChooser1.getSelectedFile().getName()); | |
212 | jTextField2.setText(jFileChooser1.getCurrentDirectory()+"/"+jFileChooser1.getSelectedFile().getName()); | |
213 | } | |
214 | }//GEN-LAST:event_jButton1MouseClicked | |
215 | ||
216 | /** | |
217 | * @param args the command line arguments | |
218 | */ | |
219 | public static void main (String args[]) { | |
220 | java.awt.EventQueue.invokeLater (new Runnable () { | |
221 | public void run () { | |
222 | new hlavokno ().setVisible (true); | |
223 | } | |
224 | }); | |
225 | } | |
226 | ||
227 | // Variables declaration - do not modify//GEN-BEGIN:variables | |
228 | private javax.swing.JButton jButton1; | |
229 | private javax.swing.JButton jButton2; | |
230 | private javax.swing.JButton jButton3; | |
231 | private javax.swing.JButton jButton4; | |
232 | private javax.swing.JButton jButton5; | |
233 | private javax.swing.JDialog jDialog1; | |
234 | private javax.swing.JFileChooser jFileChooser1; | |
235 | private javax.swing.JTextField jTextField1; | |
236 | private javax.swing.JTextField jTextField2; | |
237 | private javax.swing.JTextField jTextField3; | |
238 | private javax.swing.JTextField jTextField4; | |
239 | // End of variables declaration//GEN-END:variables | |
240 | ||
241 | } |