source: trunk/src/org/expeditee/encryption/core/EncryptedImage.java@ 1427

Last change on this file since 1427 was 1427, checked in by bln4, 5 years ago

New piping and functionality implementation to support the encrypting of images.

File size: 3.1 KB
Line 
1package org.expeditee.encryption.core;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileOutputStream;
6import java.io.IOException;
7import java.nio.file.Path;
8import java.nio.file.Paths;
9import java.security.InvalidKeyException;
10import java.security.NoSuchAlgorithmException;
11import java.util.ArrayList;
12import java.util.List;
13
14import javax.crypto.Cipher;
15import javax.crypto.CipherInputStream;
16import javax.crypto.NoSuchPaddingException;
17import javax.crypto.SecretKey;
18import javax.crypto.spec.SecretKeySpec;
19
20import org.expeditee.core.Image;
21import org.expeditee.encryption.CryptographyConstants;
22import org.expeditee.items.Picture;
23
24@SuppressWarnings("serial")
25public 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}
Note: See TracBrowser for help on using the repository browser.