· 7 years ago · Mar 30, 2018, 11:32 AM
1/*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6package DESEcryptFrame;
7
8import java.io.BufferedReader;
9import java.io.File;
10import java.io.FileInputStream;
11import java.io.FileOutputStream;
12import java.io.FileReader;
13import java.io.IOException;
14import java.io.InputStream;
15import java.io.OutputStream;
16import java.security.InvalidAlgorithmParameterException;
17import java.security.InvalidKeyException;
18import java.security.NoSuchAlgorithmException;
19import java.security.spec.AlgorithmParameterSpec;
20import javax.crypto.Cipher;
21import javax.crypto.CipherInputStream;
22import javax.crypto.CipherOutputStream;
23import javax.crypto.KeyGenerator;
24import javax.crypto.NoSuchPaddingException;
25import javax.crypto.SecretKey;
26import javax.crypto.spec.IvParameterSpec;
27
28/**
29 *
30 * @author HP
31 */
32public class DESEcryptJFrame extends javax.swing.JFrame {
33
34 /**
35 * Creates new form DESEcryptJFrame
36 */
37 public DESEcryptJFrame() {
38 initComponents();
39 }
40
41 /**
42 * This method is called from within the constructor to initialize the form.
43 * WARNING: Do NOT modify this code. The content of this method is always
44 * regenerated by the Form Editor.
45 */
46 @SuppressWarnings("unchecked")
47 // <editor-fold defaultstate="collapsed" desc="Generated Code">
48 private void initComponents() {
49
50 jScrollPane1 = new javax.swing.JScrollPane();
51 jTextArea1 = new javax.swing.JTextArea();
52 jFileChooser1 = new javax.swing.JFileChooser();
53 jButton1 = new javax.swing.JButton();
54 jScrollPane2 = new javax.swing.JScrollPane();
55 jTextArea2 = new javax.swing.JTextArea();
56
57 setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
58
59 jTextArea1.setColumns(20);
60 jTextArea1.setRows(5);
61 jScrollPane1.setViewportView(jTextArea1);
62
63 jButton1.setText("jButton1");
64
65 jTextArea2.setColumns(20);
66 jTextArea2.setRows(5);
67 jScrollPane2.setViewportView(jTextArea2);
68
69 javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
70 getContentPane().setLayout(layout);
71 layout.setHorizontalGroup(
72 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
73 .addGroup(layout.createSequentialGroup()
74 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
75 .addGroup(layout.createSequentialGroup()
76 .addGap(130, 130, 130)
77 .addComponent(jButton1))
78 .addGroup(layout.createSequentialGroup()
79 .addGap(50, 50, 50)
80 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
81 .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE)
82 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE))))
83 .addGap(35, 35, 35)
84 .addComponent(jFileChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, 388, javax.swing.GroupLayout.PREFERRED_SIZE)
85 .addContainerGap(590, Short.MAX_VALUE))
86 );
87 layout.setVerticalGroup(
88 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
89 .addGroup(layout.createSequentialGroup()
90 .addGap(48, 48, 48)
91 .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
92 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
93 .addComponent(jButton1)
94 .addGap(52, 52, 52)
95 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
96 .addGap(91, 91, 91))
97 .addGroup(layout.createSequentialGroup()
98 .addComponent(jFileChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, 347, javax.swing.GroupLayout.PREFERRED_SIZE)
99 .addGap(0, 0, Short.MAX_VALUE))
100 );
101
102 pack();
103 }// </editor-fold>
104
105 /**
106 * @param args the command line arguments
107 */
108
109 private static Cipher encryptCipher;
110 private static Cipher decryptCipher;
111 private static final byte[] iv = { 11, 22, 33, 44, 99, 88, 77, 66 };
112
113
114 //get File from File Chooser
115 private String getFile(){
116 File f1 = jFileChooser1.getCurrentDirectory();
117 File f2;
118 String a= "";
119 System.out.println(f1.toString());
120 if (jFileChooser1.showOpenDialog(this) == jFileChooser1.APPROVE_OPTION) {
121 f2 = jFileChooser1.getSelectedFile();
122 a= f2.toString();
123 // read and/or display the file somehow. ....
124 }else{
125 System.out.println("No selected");
126 }
127 return a;
128 }
129 public static void main(String args[]) {
130 /* Set the Nimbus look and feel */
131 //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
132 /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
133 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
134 */
135
136 DESEcryptJFrame des = new DESEcryptJFrame();
137
138 String clearTextFile = des.getFile();
139 String cipherTextFile = "F:/desFile/pankaj/cipher.txt";
140 String clearTextNewFile = "F:/desFile/pankaj/Capture-new.jpg";
141
142
143 try {
144 // create SecretKey using KeyGenerator
145 SecretKey key = KeyGenerator.getInstance("DES").generateKey();
146 AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
147
148 // get Cipher instance and initiate in encrypt mode
149 encryptCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
150 encryptCipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
151
152 // get Cipher instance and initiate in decrypt mode
153 decryptCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
154 decryptCipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
155
156 // method to encrypt clear text file to encrypted file
157 encrypt(new FileInputStream(clearTextFile), new FileOutputStream(cipherTextFile));
158
159 // method to decrypt encrypted file to clear text file
160 decrypt(new FileInputStream(cipherTextFile), new FileOutputStream(clearTextNewFile));
161 System.out.println("DONE");
162 } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
163 | InvalidAlgorithmParameterException | IOException e) {
164 e.printStackTrace();
165 }
166
167
168
169 try {
170 for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
171 if ("Nimbus".equals(info.getName())) {
172 javax.swing.UIManager.setLookAndFeel(info.getClassName());
173 break;
174 }
175 }
176 } catch (ClassNotFoundException ex) {
177 java.util.logging.Logger.getLogger(DESEcryptJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
178 } catch (InstantiationException ex) {
179 java.util.logging.Logger.getLogger(DESEcryptJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
180 } catch (IllegalAccessException ex) {
181 java.util.logging.Logger.getLogger(DESEcryptJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
182 } catch (javax.swing.UnsupportedLookAndFeelException ex) {
183 java.util.logging.Logger.getLogger(DESEcryptJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
184 }
185 //</editor-fold>
186
187 /* Create and display the form */
188 java.awt.EventQueue.invokeLater(new Runnable() {
189 public void run() {
190 new DESEcryptJFrame().setVisible(true);
191 }
192 });
193 }
194 private static void encrypt(InputStream is, OutputStream os) throws IOException {
195
196 // create CipherOutputStream to encrypt the data using encryptCipher
197 os = new CipherOutputStream(os, encryptCipher);
198 writeData(is, os);
199 }
200
201 private static void decrypt(InputStream is, OutputStream os) throws IOException {
202
203 // create CipherOutputStream to decrypt the data using decryptCipher
204 is = new CipherInputStream(is, decryptCipher);
205 writeData(is, os);
206 }
207
208 // utility method to read data from input stream and write to output stream
209 private static void writeData(InputStream is, OutputStream os) throws IOException {
210 byte[] buf = new byte[1024];
211 int numRead = 0;
212 // read and write operation
213 while ((numRead = is.read(buf)) >= 0) {
214 os.write(buf, 0, numRead);
215 }
216 os.close();
217 is.close();
218 }
219
220 // Variables declaration - do not modify
221 private javax.swing.JButton jButton1;
222 private javax.swing.JFileChooser jFileChooser1;
223 private javax.swing.JScrollPane jScrollPane1;
224 private javax.swing.JScrollPane jScrollPane2;
225 private javax.swing.JTextArea jTextArea1;
226 private javax.swing.JTextArea jTextArea2;
227 // End of variables declaration
228
229}