-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
210 lines (172 loc) · 5.04 KB
/
Main.java
File metadata and controls
210 lines (172 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* Name1: Hannah Anees
* EID1: ha9656
* email1: hannahanees@gmail.com
*
* Name2: Sunaina Krishnamoorthy
* EID2: sk42352
* email2: sunaina@utexas.edu
*
* Name3: Tesia Wu
* EID3: tjw2492
* email3: tesiawu@utexas.edu
*
* Name4: Robby Stigler
* EID4: rns777
* email4: robbystig@gmail.com
*
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
/*
* This is the main runner class for our unique encryption algorithm.
*
* It has the encrypt and decrypt methods, and helper methods to integrate
* the data into pictures
*/
public class Main
{
private static final int START_BYTE = 200;
// Main Runner method
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a message you want to hide in the image: ");
String msg = sc.nextLine();
int[] info = makeEncryptedImage(msg);
if(info == null) {
System.out.println("******Image too small.*******");
return;
}
System.out.println("Here is the key: " + info[0] + info[1]);
System.out.println("Would you like to decrypt a message? Enter Y or N:");
String answer = sc.nextLine();
if(answer.equals("Y") || answer.equals("y")) {
System.out.println("Enter the key for this image:");
String key = sc.nextLine();
if(key.equals("" + info[0] + info[1])) {
// Decrypt logic here
System.out.println(getMsgFromImage(info[0], info[1]));
} else {
System.out.println("Wrong key. Good bye.");
}
} else {
System.out.println("Ok. Good bye.");
}
}
// Incorporates the encrypted message into a user selected image
public static int[] makeEncryptedImage(String msg)
{
int msgLength = 0;
int[] ret = new int[2];
try
{
// Get the users chosen picture
JFileChooser fc = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif", "png", "bmp", "jpeg");
fc.setFileFilter(filter);
fc.setDialogTitle("Select an image to use for encryption!");
int bs = fc.showOpenDialog(null);
File img = fc.getSelectedFile();
byte[] imageInByte;
BufferedImage originalImage = ImageIO.read(img);
// convert BufferedImage to byte array
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(originalImage, "bmp", baos);
baos.flush();
imageInByte = baos.toByteArray();
baos.close();
byte[] msgBytes = msg.getBytes();
msgLength = msgBytes.length;
int numBytes = imageInByte.length - START_BYTE;
if(numBytes <= 0 || imageInByte.length - START_BYTE - msgLength < 0)
{
return null;
}
int upperbound = START_BYTE + (numBytes / 2);
int lowerbound = START_BYTE + 1;
int start = (int)(Math.random() * ((upperbound - lowerbound) + 1) + lowerbound);
int end = numBytes;
int bytesLeft = msgLength;
int msgStart = 0;
int imageNum = 1;
for (int i = start; i < end && (msgStart < msgLength); i++)
{
imageInByte[i] = msgBytes[msgStart];
msgStart++;
bytesLeft--;
}
// convert byte array back to BufferedImage
InputStream in = new ByteArrayInputStream(imageInByte);
if (in == null){
System.out.println("FAILED");
}
BufferedImage bImageFromConvert = ImageIO.read(in);
ImageIO.write(bImageFromConvert, "bmp", new File("Stegan.bmp"));
// reset byte array of this image
baos = new ByteArrayOutputStream();
ImageIO.write(originalImage, "bmp", baos);
baos.flush();
imageInByte = baos.toByteArray();
baos.close();
ret[0] = start;
ret[1] = msgLength;
System.out.println("Encryption done");
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
return ret;
}
// Given a messages length and a user selected image returns the embedded
// encrypted message
public static String getMsgFromImage(int start, int msgLength)
{
byte[] encrypted = null;
try
{
encrypted = new byte[msgLength];
int charsRead = 0;
JFileChooser fc = new JFileChooser();
int bs = fc.showOpenDialog(null);
File img = fc.getSelectedFile();
byte[] imageInByte;
//Turn picture into BufferedImage object
BufferedImage originalImage = ImageIO.read(img);
// convert BufferedImage to byte array
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(originalImage, "bmp", baos);
baos.flush();
imageInByte = baos.toByteArray();
baos.close();
//Read the specified bytes from the image
int index = start;
while(charsRead < msgLength && index < imageInByte.length)
{
encrypted[charsRead] = imageInByte[index];
charsRead++;
index++;
}
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
return new String(encrypted);
}
}