1 | package org.expeditee.encryption.core;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.FileInputStream;
|
---|
5 | import java.io.FileOutputStream;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.nio.file.Path;
|
---|
8 | import java.nio.file.Paths;
|
---|
9 | import java.security.InvalidKeyException;
|
---|
10 | import java.security.NoSuchAlgorithmException;
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | import javax.crypto.Cipher;
|
---|
15 | import javax.crypto.CipherInputStream;
|
---|
16 | import javax.crypto.NoSuchPaddingException;
|
---|
17 | import javax.crypto.SecretKey;
|
---|
18 | import javax.crypto.spec.SecretKeySpec;
|
---|
19 |
|
---|
20 | import org.expeditee.core.Image;
|
---|
21 | import org.expeditee.encryption.CryptographyConstants;
|
---|
22 | import org.expeditee.items.Picture;
|
---|
23 |
|
---|
24 | @SuppressWarnings("serial")
|
---|
25 | public class EncryptedImage extends Image implements CryptographyConstants {
|
---|
26 |
|
---|
27 | /** String specifying the extension for encrypted images. */
|
---|
28 | public static String EXPEDITEE_ENCRYPTED_IMAGE_EXTENSION = ".eei";
|
---|
29 |
|
---|
30 | private EncryptedImage(boolean fromDisk) {
|
---|
31 | super(fromDisk);
|
---|
32 | }
|
---|
33 |
|
---|
34 | public static Image getNoise() {
|
---|
35 | return _manager.getNoise();
|
---|
36 | }
|
---|
37 |
|
---|
38 | public static Image getImage(String filename, byte[] keyBytes) {
|
---|
39 | if (filename.endsWith(EXPEDITEE_ENCRYPTED_IMAGE_EXTENSION)) {
|
---|
40 | File file = new File(filename);
|
---|
41 | if (!file.exists() || file.isDirectory()) { return null; }
|
---|
42 | try {
|
---|
43 | SecretKey key = new SecretKeySpec(keyBytes, SymmetricAlgorithm);
|
---|
44 | Cipher cipher = Cipher.getInstance(SymmetricAlgorithm);
|
---|
45 | cipher.init(Cipher.DECRYPT_MODE, key);
|
---|
46 | CipherInputStream fIn = new CipherInputStream(new FileInputStream(file), cipher);
|
---|
47 |
|
---|
48 | List<Byte> bytesArrayList = new ArrayList<Byte>();
|
---|
49 | int i;
|
---|
50 | while((i = fIn.read()) != -1) {
|
---|
51 | bytesArrayList.add((byte) i);
|
---|
52 | }
|
---|
53 | fIn.close();
|
---|
54 |
|
---|
55 | byte[] bytes = new byte[bytesArrayList.size()];
|
---|
56 | for (int o = 0; o < bytes.length; o++) {
|
---|
57 | bytes[o] = bytesArrayList.get(o);
|
---|
58 | }
|
---|
59 |
|
---|
60 | return _manager.createImage(bytes);
|
---|
61 | } catch (IOException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) {
|
---|
62 | e.printStackTrace();
|
---|
63 | return null;
|
---|
64 | }
|
---|
65 | } else {
|
---|
66 | return Image.getImage(filename);
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | public static boolean encryptImage(Picture picture, byte[] keyBytes) {
|
---|
71 | SecretKey key = new SecretKeySpec(keyBytes, SymmetricAlgorithm);
|
---|
72 | Path imagePath = Paths.get(picture.getPath());
|
---|
73 | String imageName = picture.getName();
|
---|
74 |
|
---|
75 | try {
|
---|
76 | Cipher cipher = Cipher.getInstance(SymmetricAlgorithm);
|
---|
77 | cipher.init(Cipher.ENCRYPT_MODE, key);
|
---|
78 | CipherInputStream fIn = new CipherInputStream(new FileInputStream(imagePath.toFile()), cipher);
|
---|
79 | imagePath = imagePath.getParent().resolve(imageName.substring(0, imageName.lastIndexOf('.')) + EXPEDITEE_ENCRYPTED_IMAGE_EXTENSION);
|
---|
80 | FileOutputStream fOut = new FileOutputStream(imagePath.toFile());
|
---|
81 |
|
---|
82 | int i;
|
---|
83 | while((i = fIn.read()) != -1) {
|
---|
84 | fOut.write(i);
|
---|
85 | }
|
---|
86 |
|
---|
87 | fIn.close();
|
---|
88 | fOut.flush();
|
---|
89 | fOut.close();
|
---|
90 | } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IOException e) {
|
---|
91 | e.printStackTrace();
|
---|
92 | return false;
|
---|
93 | }
|
---|
94 |
|
---|
95 | return true;
|
---|
96 | }
|
---|
97 |
|
---|
98 | public static void encryptImage(String filename, byte[] keyBytes) {
|
---|
99 |
|
---|
100 | }
|
---|
101 | }
|
---|