· 9 years ago · Jan 26, 2017, 03:48 PM
1package encryptUtils;
2
3 import javax.crypto.BadPaddingException;
4 import javax.crypto.Cipher;
5 import javax.crypto.IllegalBlockSizeException;
6 import javax.crypto.KeyGenerator;
7 import javax.crypto.NoSuchPaddingException;
8 import javax.crypto.SecretKey;
9
10 import java.io.File;
11 import java.io.FileInputStream;
12 import java.io.FileNotFoundException;
13 import java.io.FileOutputStream;
14 import java.io.IOException;
15 import java.io.UnsupportedEncodingException;
16
17 import java.security.cert.CertificateException;
18 import java.security.InvalidKeyException;
19 import java.security.KeyStore;
20 import java.security.KeyStoreException;
21 import java.security.NoSuchAlgorithmException;
22 import java.security.UnrecoverableKeyException;
23
24 public class AES {
25 public static void generateKey(
26 String password,
27 String userName)
28 throws CryptoException {
29 KeyGenerator KeyGen;
30
31 try {
32 KeyGen = KeyGenerator.getInstance("AES");
33 } catch (NoSuchAlgorithmException ex) {
34 throw new CryptoException("Error generateKey | KeyGenerator.getInstance", ex);
35 }
36
37 KeyGen.init(128);
38 SecretKey SecKey = KeyGen.generateKey();
39 saveKey(SecKey, password, userName);
40 }
41
42 private static void saveKey(
43 SecretKey key,
44 String password,
45 String userName)
46 throws CryptoException {
47 KeyStore keyStore;
48
49 try {
50 keyStore = KeyStore.getInstance("JCEKS");
51 } catch (KeyStoreException ex) {
52 throw new CryptoException("Error saveKey | KeyStore.getInstance", ex);
53 }
54
55 try {
56 keyStore.load(null, null);
57 } catch (IOException | NoSuchAlgorithmException | CertificateException ex) {
58 throw new CryptoException("Error saveKey | keyStore.load", ex);
59 }
60
61 try {
62 keyStore.setKeyEntry(userName, key, password.toCharArray(), null);
63 } catch (KeyStoreException ex) {
64 throw new CryptoException("Error saveKey | keyStore.setKeyEntry", ex);
65 }
66
67 try {
68 keyStore.store(new FileOutputStream(userName + ".jceks"), password.toCharArray());
69 } catch (KeyStoreException | IOException | NoSuchAlgorithmException | CertificateException ex) {
70 throw new CryptoException("Error saveKey | keyStore.store", ex);
71 }
72 }
73
74 public static SecretKey loadKey(
75 String password,
76 String userName)
77 throws CryptoException {
78 KeyStore keyStore;
79
80 try {
81 keyStore = KeyStore.getInstance("JCEKS");
82 } catch (KeyStoreException ex) {
83 throw new CryptoException("Error loadKey | KeyStore.getInstance", ex);
84 }
85
86 try {
87 keyStore.load(new FileInputStream(userName + ".jceks"), password.toCharArray());
88 } catch (IOException | NoSuchAlgorithmException | CertificateException ex) {
89 throw new CryptoException("Error loadKey | keyStore.load", ex);
90 }
91
92 SecretKey key;
93
94 try {
95 key = (SecretKey) keyStore.getKey(userName, password.toCharArray());
96 } catch (KeyStoreException | NoSuchAlgorithmException | UnrecoverableKeyException ex) {
97 throw new CryptoException("Error loadKey | key = (SecretKey) keyStore.getKey", ex);
98 }
99
100 return key;
101 }
102
103 public static byte[] encryptString(
104 SecretKey key,
105 String toEncrypt)
106 throws CryptoException {
107 byte[] toEncryptBytes;
108
109 try {
110 toEncryptBytes = toEncrypt.getBytes("UTF8");
111 } catch (UnsupportedEncodingException ex) {
112 throw new CryptoException("Error encryptString | String.getBytes", ex);
113 }
114
115 byte[] outputBytes = doCrypto(Cipher.ENCRYPT_MODE, key, toEncryptBytes);
116 return outputBytes;
117 }
118
119 public static String decryptString(
120 SecretKey key,
121 byte[] toDecryptBytes)
122 throws CryptoException {
123 byte[] outputBytes = doCrypto(Cipher.DECRYPT_MODE, key, toDecryptBytes);
124
125 String ciphtx = new String(outputBytes);
126 return ciphtx;
127 }
128
129 public static File encryptFile(
130 SecretKey key,
131 File inputFile)
132 throws CryptoException {
133 FileInputStream inputStream;
134
135 try {
136 inputStream = new FileInputStream(inputFile);
137 } catch (FileNotFoundException ex) {
138 throw new CryptoException("Error encryptFile | new FileInputStream", ex);
139 }
140
141 byte[] inputBytes = new byte[(int) inputFile.length()];
142
143 try {
144 inputStream.read(inputBytes);
145 } catch (IOException ex) {
146 throw new CryptoException("Error encryptFile | FileInputStream.read", ex);
147 }
148
149 byte[] outputBytes = doCrypto(Cipher.ENCRYPT_MODE, key, inputBytes);
150
151 FileOutputStream outputStream;
152 File outputFile = new File(inputFile.getName() + ".enc");
153
154 try {
155 outputStream = new FileOutputStream(outputFile);
156 } catch (FileNotFoundException ex) {
157 throw new CryptoException("Error encryptFile | new FileOutputStream", ex);
158 }
159
160 try {
161 outputStream.write(outputBytes);
162 } catch (IOException ex) {
163 throw new CryptoException("Error encryptFile | FileOutputStream.write", ex);
164 }
165
166 try {
167 inputStream.close();
168 } catch (IOException ex) {
169 throw new CryptoException("Error encryptFile | FileInputStream.close", ex);
170 }
171
172 try {
173 outputStream.close();
174 } catch (IOException ex) {
175 throw new CryptoException("Error encryptFile | FileOutputStream.close", ex);
176 }
177
178 return outputFile;
179 }
180
181 public static File decryptFile(
182 SecretKey key,
183 File inputFile)
184 throws CryptoException {
185 FileInputStream inputStream;
186
187 try {
188 inputStream = new FileInputStream(inputFile);
189 } catch (FileNotFoundException ex) {
190 throw new CryptoException("Error decryptFile | new FileInputStream", ex);
191 }
192
193 byte[] inputBytes = new byte[(int) inputFile.length()];
194
195 try {
196 inputStream.read(inputBytes);
197 } catch (IOException ex) {
198 throw new CryptoException("Error decryptFile | FileInputStream.read", ex);
199 }
200
201 byte[] outputBytes = doCrypto(Cipher.DECRYPT_MODE, key, inputBytes);
202 FileOutputStream outputStream;
203 File outputFile = new File("dec" + inputFile.getName().substring(0, inputFile.getName().length() - 4));
204
205 try {
206 outputStream = new FileOutputStream(outputFile);
207 } catch (FileNotFoundException ex) {
208 throw new CryptoException("Error decryptFile | new FileOutputStream", ex);
209 }
210
211 try {
212 outputStream.write(outputBytes);
213 } catch (IOException ex) {
214 throw new CryptoException("Error decryptFile | FileOutputStream.write", ex);
215 }
216
217 try {
218 inputStream.close();
219 } catch (IOException ex) {
220 throw new CryptoException("Error decryptFile | FileInputStream.close", ex);
221 }
222
223 try {
224 outputStream.close();
225 } catch (IOException ex) {
226 throw new CryptoException("Error decryptFile | FileOutputStream.close", ex);
227 }
228
229 return outputFile;
230 }
231
232 private static byte[] doCrypto(
233 int cipherMode,
234 SecretKey key,
235 byte[] inputBytes)
236 throws CryptoException {
237 Cipher cipher;
238
239 try {
240 cipher = Cipher.getInstance("AES");
241 } catch (NoSuchAlgorithmException | NoSuchPaddingException ex) {
242 throw new CryptoException("Error doCrypto | cipher.getInstance", ex);
243 }
244
245 try {
246 cipher.init(cipherMode, key);
247 } catch (InvalidKeyException ex) {
248 throw new CryptoException("Error doCrypto | cipher.init", ex);
249 }
250
251 byte[] outputBytes;
252
253 try {
254 outputBytes = cipher.doFinal(inputBytes);
255 } catch (IllegalBlockSizeException | BadPaddingException ex) {
256 throw new CryptoException("Error doCrypto | cipher.doFinal", ex);
257 }
258
259 return outputBytes;
260 }
261 }
262
263package encryptUtils;
264
265 import java.io.File;
266
267 import javax.crypto.SecretKey;
268
269 public class Main {
270
271 public static void main(
272 String[] args)
273 throws Exception {
274
275 /*
276 * AES
277 */
278 System.out.println( "----- AES -----" );
279
280 //change from File to String and give path to method
281 File inputFile = new File("pic.jpg");
282 //File inputFile = new File("cleartext.txt");
283
284 //Should be char array to empty when finished with
285 String password = "password";
286 String userName = "bob";
287
288 try {
289 System.out.println("n----- Generate Key -----");
290 AES.generateKey(password, userName);
291 System.out.println("----- ---------- -----n----- Load Key -----");
292 SecretKey AESkey = AES.loadKey(password, userName);
293
294 System.out.println("----- ---------- -----n----- File Encryption -----ninputFileName: " + inputFile.getName());
295 File encryptedFile = AES.encryptFile(AESkey, inputFile);
296 System.out.println( "encryptedFileName: " + encryptedFile.getName() + "n----- ---------- -----n----- File Decryption -----");
297 File decryptedFile = AES.decryptFile(AESkey, encryptedFile);
298
299 System.out.println( "decryptedFileName: " + decryptedFile.getName() + "n----- ---------- -----n----- String Encryption -----");
300 byte[]outputBytes = AES.encryptString(AESkey, "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.");
301 System.out.println(new String(outputBytes) + "n----- ---------- -----n----- String Decryption -----");
302 String decryptedString = AES.decryptString(AESkey, outputBytes);
303 System.out.println(decryptedString + "n----- ---------- -----n");
304 } catch (CryptoException ex) {
305 System.out.println(ex.getMessage());
306 ex.printStackTrace();
307 }
308
309 System.out.println("nEnd");
310 }
311 }