KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > crypto > SimpleFileEncryptorUI


1 package SnowMailClient.crypto;
2
3 import javax.swing.*;
4 import javax.swing.filechooser.*;
5 import java.util.*;
6 import java.util.zip.*;
7
8 import java.security.*;
9 import java.security.spec.*;
10 import javax.crypto.spec.*;
11 import javax.crypto.*;
12
13
14 import snow.utils.gui.*;
15 import snow.utils.storage.*;
16 import snow.crypto.*;
17 import snow.FileEncryptor.*;
18
19 import snow.Language.Language;
20 import java.io.*;
21
22 /** encrypt/decrypt dialog, just calls the methods of
23      snow.FileEncryptor.SimpleFileEncryptor;
24 */

25 public final class SimpleFileEncryptorUI
26 {
27
28   private SimpleFileEncryptorUI()
29   {
30
31   } // Constructor
32

33
34   public static void simpleDecryptFileUI(AppProperties props, JFrame parent)
35   {
36     String JavaDoc base = props.getProperty("FileDecrypt_last_base_dir", "");
37     JFileChooser chooser = new JFileChooser(base);
38     chooser.setDialogTitle(Language.translate("Chose a file to decrypt"));
39     chooser.setDialogType(chooser.FILES_ONLY);
40
41     int rep = chooser.showOpenDialog(parent);
42     if(rep==chooser.APPROVE_OPTION)
43     {
44       File file = chooser.getSelectedFile();
45       int bufferSize = 256;
46       int progressToDo = (int) (file.length()/bufferSize);
47
48       props.setProperty("FileDecrypt_last_base_dir", file.getParent());
49       ProgressModalDialog progressDialog = null;
50       try
51       {
52         SecretKeyID skid = SimpleFileEncryptor.getKeyIDFromFile(file);
53         PassphraseDialog passDialog = new PassphraseDialog(parent,
54           Language.translate("Enter the passphrase to decipher the file"), true, skid,
55               file, Language.translate("Passphrase"));
56         if(!passDialog.wasCancelled())
57         {
58           File decFile = null;
59           if(file.getAbsolutePath().toLowerCase().endsWith(".snowenc"))
60           {
61             // remove the encoded extension
62
String JavaDoc pa = file.getAbsolutePath();
63             decFile = new File(pa.substring(0,pa.length()-8));
64           }
65           else
66           {
67             decFile = new File(file.getAbsolutePath()+".dec");
68           }
69
70           if(decFile.exists())
71           {
72             throw new Exception JavaDoc(
73               Language.translate("Destination file \n %\nalready exist. Please delete it first.", decFile.getAbsolutePath()));
74           }
75           progressDialog = new ProgressModalDialog(
76            parent, Language.translate("Decrypting file"), false, true);
77           progressDialog.setProgressBounds(progressToDo);
78           progressDialog.start();
79           progressDialog.setProgressValue(0, Language.translate("Decrypting file")+"...");
80
81           try
82           {
83             SimpleFileEncryptor.decryptFile(file, decFile, passDialog.getKey(), bufferSize, progressDialog);
84           }
85           catch(Exception JavaDoc ee)
86           {
87             throw ee;
88           }
89           finally
90           {
91             progressDialog.closeDialog();
92             progressDialog = null;
93           }
94         }
95       }
96       catch(Exception JavaDoc e)
97       {
98         JOptionPane.showMessageDialog(progressDialog,
99           e.getMessage(), Language.translate("Error during file deciphering"), JOptionPane.ERROR_MESSAGE);
100         //e.printStackTrace();
101
}
102     }
103   }
104
105
106
107   /** must be called from a thread !
108   */

109   public static void simpleEncryptFileUI(AppProperties props, JFrame parent)
110   {
111     String JavaDoc base = props.getProperty("FileEncryptor_last_base_dir", "");
112     JFileChooser chooser = new JFileChooser(base);
113     chooser.setDialogTitle(Language.translate("Chose a file to encrypt"));
114     chooser.setDialogType(chooser.FILES_ONLY);
115
116     EncryptOptionsPanel optionsPanel = new EncryptOptionsPanel(
117        props.getBoolean("WipeSourceAfterEncryption", false));
118     chooser.setAccessory(optionsPanel);
119
120     chooser.setFileSelectionMode(chooser.FILES_AND_DIRECTORIES);
121     int rep = chooser.showOpenDialog(parent);
122     if(rep==chooser.APPROVE_OPTION)
123     {
124       File file = chooser.getSelectedFile();
125
126       File destinationFile = null;
127       if(file.isFile())
128       {
129         destinationFile = new File(file.getAbsolutePath()+".snowEnc");
130       }
131       else
132       {
133         destinationFile = new File(file.getParentFile(), file.getName()+".snowEnc");
134       }
135
136
137
138       if(destinationFile.exists())
139       {
140
141           JOptionPane.showMessageDialog(parent,
142             Language.translate("Cannot encrypt file"),
143             Language.translate("Destination file \n %\nalready exist. Please delete it first.",
144                                destinationFile.getAbsolutePath()),
145             JOptionPane.ERROR_MESSAGE);
146
147       }
148
149       long fileSize = FileUtils.getSizeIncludingSubFiles(file);
150       int bufferSize = 256;
151
152       props.setBoolean("WipeSourceAfterEncryption", optionsPanel.doWipeSource());
153       props.setProperty("FileEncryptor_last_base_dir", file.getParent());
154
155       PassphraseSettingDialog psd = new PassphraseSettingDialog(
156           parent,
157           Language.translate("Passphrase"),
158           true,
159           Language.translate("Please enter and confirm the passphrase to use to encipher the file"));
160
161       if(!psd.wasCancelled())
162       {
163         ProgressModalDialog progressDialog = new ProgressModalDialog(
164            parent, Language.translate("Encrypting file"), false, true);
165
166         int progressToDo = (int) (fileSize/bufferSize);
167         if(optionsPanel.doWipeSource())
168         {
169           progressDialog.setProgressBounds(progressToDo*4);
170         }
171         else
172         {
173           progressDialog.setProgressBounds(progressToDo*3);
174         }
175
176
177         progressDialog.start();
178         progressDialog.setProgressValue(0, Language.translate("Encrypting file")+"...");
179         try
180         {
181           SecretKey key = psd.getKey();
182
183           if(file.isFile())
184           {
185              SimpleFileEncryptor.encryptFile(file, destinationFile, key, bufferSize, progressDialog);
186              progressDialog.setProgressValue( progressToDo, "Testing encrypted file...");
187
188              // test !!!
189
try
190              {
191                String JavaDoc problem = SimpleFileEncryptor.verifyEncryptedFile(file, destinationFile, key, bufferSize, progressDialog);
192                if(problem!=null)
193                {
194                   throw new Exception JavaDoc("Error during verify: "+problem);
195                }
196
197                if(optionsPanel.doWipeSource())
198                {
199                  progressDialog.setProgressValue( progressToDo*3, Language.translate("Wiping original")+"...");
200                  SimpleFileEncryptor.wipeFile(file, bufferSize, progressDialog, 3);
201                }
202              }
203              catch(Exception JavaDoc e2)
204              {
205                throw e2;
206              }
207           }
208           else
209           {
210              // directory
211
try
212              {
213                 SimpleFileEncryptor.encryptDirectory(file, destinationFile, optionsPanel.doWipeSource(), key, bufferSize, progressDialog);
214              }
215              catch(Exception JavaDoc e2)
216              {
217                throw e2;
218              }
219
220           }
221
222         }
223         catch(Exception JavaDoc e)
224         {
225           JOptionPane.showMessageDialog(parent,
226             e.getMessage(), Language.translate("Error during file ciphering"), JOptionPane.ERROR_MESSAGE);
227         }
228         finally
229         {
230           progressDialog.closeDialog();
231         }
232
233       }
234     }
235   }
236
237
238
239 } // SimpleFileEncryptorUI
Popular Tags