-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSteganography.java
More file actions
278 lines (259 loc) · 8.08 KB
/
Steganography.java
File metadata and controls
278 lines (259 loc) · 8.08 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import java.io.File;
import java.awt.Point;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.awt.image.DataBufferByte;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
public class Steganography{
public static void main(String[] args) {
}
/*
*encrypt an image with text, the output file will be of type .png
*@param path the path (folder) containing the image to modify
*@param original the name of the image to modify
*@param ext1 the extension type of the image to modify (jpg, png)
*@param stegan the output name of the file
*@param message the text to hide in the image
*@param type integer representing either basic or advanced encoding
*/
public boolean encode(String path, String original, String ext1, String stegan, String message)
{
String file_name = image_path(path,original,ext1);
BufferedImage image_orig = getImage(file_name);
// user space is not necessary for encryption
BufferedImage image = user_space(image_orig);
image = add_text(image,message);
return(setImage(image,new File(image_path(path,stegan,"png")),"png"));
}
/*
*decrypt assumes the image being used is of type .png, extracts the hidden text from an image
*@param path the path (folder) containing the image to extract the message from
*@param name the name of the image to extract the message from
*@param type integer representing either basic or advanced encoding
*/
public String decode(String path, String name)
{
byte[] decode;
try
{
// user space is necessary for decryption
BufferedImage image = user_space(getImage(image_path(path,name,"png")));
decode = decode_text(get_byte_data(image));
return(new String(decode));
}
catch(Exception e)
{
JOptionPane.showMessageDialog(
null,
"There is no hidden message in this image!",
"Error",
JOptionPane.ERROR_MESSAGE
);
return "";
}
}
/*
*returns the complete path of a file, in the form: path\name.ext
*@param path the path (folder) of the file
*@param name the name of the file
*@param ext the extension of the file
*@return a String representing the complete path of a file
*/
private String image_path(String path, String name, String ext)
{
return path + "/" + name + "." + ext;
}
/*
*get method to return an image file
*@param f the complete path name of the image.
*@return A bufferedImage of the supplied file path
*@see steganography.image_path
*/
private BufferedImage getImage(String f)
{
BufferedImage image = null;
File file = new File(f);
try
{
image = ImageIO.read(file);
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(
null,
"Image could not be read!",
"Error",
JOptionPane.ERROR_MESSAGE
);
}
return image;
}
/*
*set method to save an image file
*@param image the image file to save
*@param file file to save the image to
*@param ext the extension and thus format of the file to be saved
*@return returns true if the save is succesful
*/
private boolean setImage(BufferedImage image, File file, String ext)
{
try
{
// delete resources used by the file
file.delete();
ImageIO.write(image,ext,file);
return true;
}
catch(Exception e)
{
JOptionPane.showMessageDialog(
null,
"File could not be saved!",
"Error",
JOptionPane.ERROR_MESSAGE
);
return false;
}
}
/*
*handles the addition of text into an image
*@param image the image to add hidden text to
*@param text the text to hide in the image
*@return returns the image with the text embedded in it
*/
private BufferedImage add_text(BufferedImage image, String text)
{
// convert all items to byte arrays
byte img[] = get_byte_data(image);
byte msg[] = text.getBytes();
byte len[] = bit_conversion(msg.length);
try
{
// 0 bytes of space for first position
encode_text(img, len, 0);
// 4 bytes of space for length: 4 bytes * 8 bit = 32 bits
encode_text(img, msg, 32);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(
null,
"Target File cannot hold message!",
"Error",
JOptionPane.ERROR_MESSAGE
);
}
return image;
}
/*
*creates a user space version of a Buffered Image, for editing and saving bytes
*@param image the image to put into user space, removes compression interferences
*@return the user space version of the supplied image
*/
private BufferedImage user_space(BufferedImage image)
{
// create new_img with the attributes of image
BufferedImage new_img = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
Graphics2D graphics = new_img.createGraphics();
graphics.drawRenderedImage(image, null);
// release all allocated memory for this image
graphics.dispose();
return new_img;
}
/*
*gets the byte array of an image
*@param image the image to get byte data from
*@return returns the byte array of the image supplied
*@see Raster
*@see WritableRaster
*@see DataBufferByte
*/
private byte[] get_byte_data(BufferedImage image)
{
WritableRaster raster = image.getRaster();
DataBufferByte buffer = (DataBufferByte)raster.getDataBuffer();
return buffer.getData();
}
/*
*gernerates proper byte format of an integer
*@param i the integer to convert
*@return returns a byte[4] array converting the supplied integer into bytes
*/
private byte[] bit_conversion(int i)
{
// originally integers (ints) cast into bytes
// byte byte7 = (byte)((i & 0xFF00000000000000L) >>> 56);
// byte byte6 = (byte)((i & 0x00FF000000000000L) >>> 48);
// byte byte5 = (byte)((i & 0x0000FF0000000000L) >>> 40);
// byte byte4 = (byte)((i & 0x000000FF00000000L) >>> 32);
// only using 4 bytes
byte byte3 = (byte)((i & 0xFF000000) >>> 24); //0
byte byte2 = (byte)((i & 0x00FF0000) >>> 16); //0
byte byte1 = (byte)((i & 0x0000FF00) >>> 8 ); //0
byte byte0 = (byte)((i & 0x000000FF));
// {0, 0, 0, byte0} is equivalent, since all shifts >= 8 will be 0
return(new byte[]{byte3,byte2,byte1,byte0});
}
/*
*encode an array of bytes into another array of bytes at a supplied offset
*@param image array of data representing an image
*@param addition array of data to add to the supplied image data array
*@param offset the offset into the image array to add the addition data
*@return returns data Array of merged image and addition data
*/
private byte[] encode_text(byte[] image, byte[] addition, int offset)
{
// check that the data and offset will fit in the image
if(addition.length + offset > image.length)
{
throw new IllegalArgumentException("File not long enough!");
}
// loop through each additional byte
for(int i = 0; i < addition.length; ++i)
{
// loop through the 8 bits of each byte
int add = addition[i];
// ensure the new offset value carries on through both loops
for(int bit = 7; bit >= 0; --bit, ++offset)
{
// assign an integer to b, shifted by bit spaces AND 1
// a single bit of the current byte
int b = (add >>> bit) & 1;
// assign the bit by taking: [(previous byte value) AND 0xfe] OR bit to add
// changes the last bit of the byte in the image to be the bit of addition
image[offset] = (byte)((image[offset] & 0xFE) | b );
}
}
return image;
}
/*
*retrieves hidden text from an image
*@param image array of data, representing an image
*@return array of data which contains the hidden text
*/
private byte[] decode_text(byte[] image)
{
int length = 0;
int offset = 32;
// loop through 32 bytes of data to determine text length
// i = 24 will also work, as only the 4th byte contains real data
for(int i = 0; i < 32; ++i)
{
length = (length << 1) | (image[i] & 1);
}
byte[] result = new byte[length];
// loop through each byte of text
for(int b = 0; b < result.length; ++b)
{
// loop through each bit within a byte of text
for(int i = 0; i < 8; ++i, ++offset)
{
// assign bit: [(new byte value) << 1] OR [(text byte) AND 1]
result[b] = (byte)((result[b] << 1) | (image[offset] & 1));
}
}
return result;
}
}