· 5 years ago · Apr 10, 2020, 09:34 AM
1import java.io.File;
2import java.io.FileInputStream;
3import java.io.FileOutputStream;
4import java.io.IOException;
5import java.util.logging.Level;
6import java.util.logging.Logger;
7import javax.swing.JFileChooser;
8
9
10@SuppressWarnings("serial")
11public class EncryptTXT extends javax.swing.JFrame {
12
13 String key="Java@123##"; //This is the Key for Encryption
14 /** Creates new form EncryptTXT */
15 public EncryptTXT() {
16 initComponents();
17 }
18
19
20 // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
21 private void initComponents() {
22
23 jPanel1 = new javax.swing.JPanel();
24 jLabel1 = new javax.swing.JLabel();
25 jScrollPane1 = new javax.swing.JScrollPane();
26 jLabel2 = new javax.swing.JLabel();
27 jLabel3 = new javax.swing.JLabel();
28 jScrollPane2 = new javax.swing.JScrollPane();
29 jLabel4 = new javax.swing.JLabel();
30 jButton2 = new javax.swing.JButton();
31 jButton1 = new javax.swing.JButton();
32 jButton3 = new javax.swing.JButton();
33
34 setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
35 setResizable(false);
36
37 jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
38
39 jLabel1.setText("Original File:");
40
41 jScrollPane1.setViewportView(jLabel2);
42
43 jLabel3.setText("Encrypted File:");
44
45 jScrollPane2.setViewportView(jLabel4);
46
47 jButton2.setText("Open Encrypted file folder");
48 jButton2.addActionListener(new java.awt.event.ActionListener() {
49 public void actionPerformed(java.awt.event.ActionEvent evt) {
50 jButton2ActionPerformed(evt);
51 }
52 });
53
54 jButton1.setText("Encrypt..");
55 jButton1.addActionListener(new java.awt.event.ActionListener() {
56 public void actionPerformed(java.awt.event.ActionEvent evt) {
57 jButton1ActionPerformed(evt);
58 }
59 });
60
61 jButton3.setText("Decrypt..");
62 jButton3.addActionListener(new java.awt.event.ActionListener() {
63 public void actionPerformed(java.awt.event.ActionEvent evt) {
64 jButton3ActionPerformed(evt);
65 }
66 });
67
68 javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
69 jPanel1.setLayout(jPanel1Layout);
70 jPanel1Layout.setHorizontalGroup(
71 jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
72 .addGroup(jPanel1Layout.createSequentialGroup()
73 .addContainerGap()
74 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
75 .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE)
76 .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE)
77 .addComponent(jLabel1)
78 .addComponent(jLabel3)
79 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
80 .addComponent(jButton2)
81 .addGap(127, 127, 127))
82 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
83 .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
84 .addComponent(jButton3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)))
85 .addContainerGap())
86 );
87 jPanel1Layout.setVerticalGroup(
88 jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
89 .addGroup(jPanel1Layout.createSequentialGroup()
90 .addGap(12, 12, 12)
91 .addComponent(jButton1)
92 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
93 .addComponent(jButton3)
94 .addGap(18, 18, 18)
95 .addComponent(jLabel1)
96 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
97 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
98 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
99 .addComponent(jLabel3)
100 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
101 .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
102 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
103 .addComponent(jButton2)
104 .addContainerGap(35, Short.MAX_VALUE))
105 );
106
107 javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
108 getContentPane().setLayout(layout);
109 layout.setHorizontalGroup(
110 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
111 .addGroup(layout.createSequentialGroup()
112 .addContainerGap()
113 .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
114 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
115 );
116 layout.setVerticalGroup(
117 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
118 .addGroup(layout.createSequentialGroup()
119 .addContainerGap()
120 .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
121 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
122 );
123
124 pack();
125 }// </editor-fold>//GEN-END:initComponents
126
127 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
128 try{
129 JFileChooser fc=new JFileChooser();
130 fc.showOpenDialog(null);
131 String path=fc.getSelectedFile().getAbsolutePath();
132 jLabel2.setText(path);
133 File f=fc.getSelectedFile();
134 FileInputStream fis=new FileInputStream(f);
135 FileOutputStream fos=new FileOutputStream("encrypted.txt");
136 Thread.sleep(2000);
137
138 DESEncrypt.encrypt(key, fis, fos);
139
140 jLabel4.setText("C:\\Users\\tosha\\OneDrive\\Desktop\\project\\encrypted.txt");
141 }catch(Exception e)
142 {
143 System.out.print("Exception in EncryptTXT");
144 }
145 }//GEN-LAST:event_jButton1ActionPerformed
146
147 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
148
149 Runtime runtime=Runtime.getRuntime();
150 try {
151 runtime.exec("explorer.exe C:\\Users\\tosha\\OneDrive\\Desktop\\project");
152 } catch (IOException ex) {
153 Logger.getLogger(EncryptTXT.class.getName()).log(Level.SEVERE, null, ex);
154 }
155
156 }//GEN-LAST:event_jButton2ActionPerformed
157
158 private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
159
160 try{
161 JFileChooser fc=new JFileChooser();
162 fc.setCurrentDirectory(new File("C:\\Users\\tosha\\OneDrive\\Desktop\\project"));
163 fc.showOpenDialog(null);
164 String path=fc.getSelectedFile().getAbsolutePath();
165 jLabel2.setText(path);
166 File f=fc.getSelectedFile();
167 FileInputStream fis=new FileInputStream(f);
168 FileOutputStream fos=new FileOutputStream("decrypted.txt");
169 DESEncrypt.decrypt(key, fis, fos);
170 Thread.sleep(2000);
171 jLabel4.setText("C:\\Users\\tosha\\OneDrive\\Desktop\\project\\decrypted.txt");
172 }catch(Exception e)
173 {
174 System.out.print("Exception in EncryptTXT");
175 }
176 }//GEN-LAST:event_jButton3ActionPerformed
177
178 /**
179 * @param args the command line arguments
180 */
181 public static void main(String args[]) {
182 java.awt.EventQueue.invokeLater(new Runnable() {
183
184 public void run() {
185 new EncryptTXT().setVisible(true);
186 }
187 });
188 }
189 // Variables declaration - do not modify//GEN-BEGIN:variables
190 private javax.swing.JButton jButton1;
191 private javax.swing.JButton jButton2;
192 private javax.swing.JButton jButton3;
193 private javax.swing.JLabel jLabel1;
194 private javax.swing.JLabel jLabel2;
195 private javax.swing.JLabel jLabel3;
196 private javax.swing.JLabel jLabel4;
197 private javax.swing.JPanel jPanel1;
198 private javax.swing.JScrollPane jScrollPane1;
199 private javax.swing.JScrollPane jScrollPane2;
200 // End of variables declaration//GEN-END:variables
201}
202
203
204
205
206
207DESEncrypt.java
208------------------------------
209import java.io.IOException;
210import java.io.InputStream;
211import java.io.OutputStream;
212import javax.crypto.Cipher;
213import javax.crypto.CipherInputStream;
214import javax.crypto.CipherOutputStream;
215import javax.crypto.SecretKey;
216import javax.crypto.SecretKeyFactory;
217import javax.crypto.spec.DESKeySpec;
218
219
220public class DESEncrypt {
221 public static void encrypt(String key, InputStream is, OutputStream os) throws Exception {
222 encryptOrDecrypt(key, Cipher.ENCRYPT_MODE, is, os);
223 }
224
225 public static void decrypt(String key, InputStream is, OutputStream os) throws Exception {
226 encryptOrDecrypt(key, Cipher.DECRYPT_MODE, is, os);
227 }
228
229 public static void encryptOrDecrypt(String key, int mode, InputStream is, OutputStream os) throws Exception {
230
231 DESKeySpec dks = new DESKeySpec(key.getBytes());
232 SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
233 SecretKey desKey = skf.generateSecret(dks);
234 Cipher cipher = Cipher.getInstance("DES");
235
236 if (mode == Cipher.ENCRYPT_MODE) {
237 cipher.init(Cipher.ENCRYPT_MODE, desKey);
238 CipherInputStream cis = new CipherInputStream(is, cipher);
239 makeFile(cis, os);
240 } else if (mode == Cipher.DECRYPT_MODE) {
241 cipher.init(Cipher.DECRYPT_MODE, desKey);
242 CipherOutputStream cos = new CipherOutputStream(os, cipher);
243 makeFile(is, cos);
244 }
245 }
246
247 public static void makeFile(InputStream is, OutputStream os) throws IOException {
248 byte[] bytes = new byte[64];
249 int numBytes;
250 while ((numBytes = is.read(bytes)) != -1) {
251 os.write(bytes, 0, numBytes);
252 }
253 os.flush();
254 os.close();
255 is.close();
256 }
257
258}