-
Notifications
You must be signed in to change notification settings - Fork 1
/
Steganography.java
433 lines (301 loc) · 9.69 KB
/
Steganography.java
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import java.io.*;
import java.awt.*;
import java.util.*;
import java.awt.image.*;
import javax.imageio.*;
public class Steganography
{
static final int MAX_INT_LEN = 4;
static final int DATA_SIZE = 8;
public static String hide(String textFnm, String imFnm, String pass)
{
String inputText = readTextFile(textFnm);
if ((inputText == null) || (inputText.length() == 0))
return "NO MESSAGE FOUND";
byte[] stego = buildStego(inputText);
BufferedImage im1 = loadImage(imFnm);
File newfile = new File("newimage.png");
try{
ImageIO.write(im1, "png", newfile);
}catch(Exception e){System.out.println("Image not made : " + e);}
BufferedImage im = loadImage(newfile.getName());
if (im == null)
return "NO IMAGE FILE FOUND";
byte imBytes[] = accessBytes(im);
String fal="Embeding Unsuccessful";
if (!singleHide(imBytes, stego,pass)) // im is modified with the stego
return fal;
String fnm = getFileName(imFnm);
/*try{
FileOutputStream fout=new FileOutputStream(imFnm);
File file12=new File(imFnm);
fout.write(imBytes,0,imBytes.length);
}catch(Exception e){}
return true; */
writeImageToFile( fnm + "msg.png", im);
return (fnm+"msg.png");
}
private static String readTextFile(String fnm)
{
BufferedReader br = null;
StringBuffer sb = new StringBuffer();
try {
br = new BufferedReader(new FileReader( new File(fnm) )); String text = null;
File f = new File(fnm);
StringBuffer fname1= new StringBuffer();
fname1.append(f.getName());
fname1.setLength(30);
sb.append(fname1);
System.out.println(sb);
while ((text = br.readLine()) != null)
sb.append(text + "\n");
}
catch (Exception e) {
System.out.println("Could not completely read " + fnm);
return null;
}
finally {
try {
if (br != null)
br.close();
}
catch (IOException e) {
System.out.println("Problem closing " + fnm);
return null;
}
}
System.out.println("Read in " + fnm);
return sb.toString();
}
private static byte[] buildStego(String inputText)
{
// convert data to byte arrays
byte[] msgBytes = inputText.getBytes();
byte[] lenBs = intToBytes(msgBytes.length);
int totalLen = lenBs.length + msgBytes.length;
byte[] stego = new byte[totalLen];
System.arraycopy(lenBs, 0, stego, 0, lenBs.length); //System.arraycopy ka arg(source, source start, destinatn, destinatn start, destinatn's no. of bytes)
System.arraycopy(msgBytes, 0, stego, lenBs.length, msgBytes.length);
// System.out.println("Num. pixels to store message " + i + ": " + totalLen*DATA_SIZE);
return stego;
}
private static byte[] intToBytes(int i)
{
byte[] integerBs = new byte[MAX_INT_LEN];
integerBs[0] = (byte) ((i >>> 24) & 0xFF);
integerBs[1] = (byte) ((i >>> 16) & 0xFF);
integerBs[2] = (byte) ((i >>> 8) & 0xFF);
integerBs[3] = (byte) (i & 0xFF);
// for (int j=0; j < integerBs.length; j++)
// System.out.println(" integerBs[ " + j + "]: " + integerBs[j]);
return integerBs;
}
private static BufferedImage loadImage(String imFnm)
{
BufferedImage im = null;
try {
im = ImageIO.read( new File(imFnm) );
System.out.println("Read " + imFnm);
}
catch (IOException e)
{ System.out.println("Could not read image from " + imFnm); }
return im;
}
private static byte[] accessBytes(BufferedImage image)
{
WritableRaster raster = image.getRaster();
DataBufferByte buffer = (DataBufferByte) raster.getDataBuffer();
return buffer.getData();
}
private static boolean singleHide(byte[] imBytes, byte[] stego, String pass)
{
int imLen = imBytes.length;
System.out.println("Byte length of image: " + imLen);
int totalLen = stego.length;
System.out.println("Total byte length of message: " + totalLen);
if ((((totalLen*DATA_SIZE)+32)*4) > imLen)
{
System.out.println("Image not big enough for message");
return false;
}
hideStego(imBytes, pass.getBytes(), 0);
hideStego(imBytes, stego, 32);
return true;
}
private static void hideStego(byte[] imBytes, byte[] stego, int offset)
{
offset*=4;
for (int i = 0; i < stego.length; i++) {
int byteVal = stego[i];
for(int j=7; j >= 0; j--) {
int bitVal = (byteVal >>> j) & 1;
imBytes[offset] = (byte)((imBytes[offset] & 0xFE) | bitVal); //MAsk and insert
offset+=4;
}
}
}
private static String getFileName(String fnm)
{
int extPosn = fnm.lastIndexOf('.');
if (extPosn == -1) {
System.out.println("No extension found for " + fnm);
return fnm;
}
return fnm.substring(0, extPosn);
}
private static boolean writeImageToFile(String outFnm, BufferedImage im)
// save the im image in a PNG file called outFnm
{
if (!canOverWrite(outFnm))
return false;
try {
ImageIO.write(im, "png", new File(outFnm));
System.out.println("Image written to PNG file: " + outFnm);
return true;
}
catch(IOException e)
{ System.out.println("Could not write image to " + outFnm);
return false;
}
}
private static boolean canOverWrite(String fnm)
{
File f = new File(fnm);
if (!f.exists())
return true; // can overewrite since the file is new
Scanner in = new Scanner(System.in);
String response;
System.out.print("File " + fnm + " already exists. and will be overwrited");
/*while (true) {
System.out.print("Overwrite (y|n)? ");
response = in.nextLine().trim().toLowerCase();
if (response.startsWith("n")) // no
return true;
else if (response.startsWith("y")) // yes
return true;
}*/
return true;
}
// --------------------------- reveal a message -----------------------------------
public static String reveal(String imFnm, String pass)
{
BufferedImage im = loadImage(imFnm);
if (im == null)
return "NO IMAGE FOUND";
byte[] imBytes = accessBytes(im);
System.out.println("Byte length of image: " + imBytes.length);
byte[] passfile = extractfilename(imBytes,4,0);
StringBuffer passf= new StringBuffer();
for(int i=0;i<passfile.length;i++)
{passf.append((char)passfile[i]);}
String passf1=passf.toString();
passf1.trim();
System.out.println(passf1);
if(!pass.equals(passf1))
{
return "WRONG PASSWORD";
}
int msgLen = getMsgLength(imBytes, 32);
if (msgLen == -1)
return "NO MESSAGE FOUND";
System.out.println("Byte length of message: " + msgLen);
//System.out.println("aaya");
byte[] fname = extractfilename(imBytes, 30,((MAX_INT_LEN*DATA_SIZE)+32)); //getfilename
//System.out.println("aaya");
String filename = new String(fname);
filename.trim();
//System.out.println("aaya" + filename);
int L=(MAX_INT_LEN*DATA_SIZE)+(30*DATA_SIZE)+32;
byte[] msg = getMessage(imBytes, msgLen-30, L);
//System.out.println("aaya" +msg1[5]);
if (msg != null)
{
// String fnm = getFileName(imFnm);
try{
FileOutputStream fout=new FileOutputStream(filename);
File file12=new File(filename);
fout.write(msg,0,msg.length);
}catch(Exception e){}
return filename;
}
else {
System.out.println("No message found");
return "No message found";
}
}
private static int getMsgLength(byte[] imBytes, int offset)
{
byte[] lenBytes = extractHiddenBytes(imBytes, MAX_INT_LEN, offset);
if (lenBytes == null)
return -1;
// for (int j=0; j < lenBytes.length; j++)
// System.out.println(" lenBytes[ " + j + "]: " + lenBytes[j]);
// convert the byte array into an integer
int msgLen = ((lenBytes[0] & 0xff) << 24) |
((lenBytes[1] & 0xff) << 16) |
((lenBytes[2] & 0xff) << 8) |
(lenBytes[3] & 0xff);
// System.out.println("Message length: " + msgLen);
if ((msgLen <= 0) || (msgLen > imBytes.length)) {
System.out.println("Incorrect message length" + msgLen);
return -1;
}
// else
// System.out.println("Revealed message length: " + msgLen);
return msgLen;
}
private static byte[] getMessage(byte[] imBytes, int msgLen, int offset)
{
byte[] msgBytes = extractHiddenBytes(imBytes, msgLen, offset);
if (msgBytes == null)
return null;
//String msg = new String(msgBytes);
return msgBytes;
}
static byte[] extractfilename(byte[] imBytes,int size,int offset)
{
byte[] hiddBytes = extractHiddenBytes(imBytes,size,offset);
return hiddBytes;
}
private static byte[] extractHiddenBytes(byte[] imBytes, int size, int offset)
{
int finalPosn = offset + (size*DATA_SIZE);
if (finalPosn > imBytes.length) {
System.out.println("End of image reached");
return null;
}
byte[] hiddenBytes = new byte[size];
offset*=4;
for (int j = 0; j < size; j++) {
for (int i=0; i < DATA_SIZE; i++) {
hiddenBytes[j] = (byte) ((hiddenBytes[j] << 1) | (imBytes[offset] & 1));
offset+=4;
}
}
return hiddenBytes;
}
/*public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
Steganography s= new Steganography();
String s4="1";
String s5="2";
if(s4.equals(args[0]))
{
System.out.println("Enter txt file name");
String s1="";
String s2="";
s1=sc.next();
System.out.println("Enter image file name");
s2=sc.next();
s.hide(s1,s2);
}
if(s5.equals(args[0]))
{
System.out.println("Enter image file name");
String s1="";
s1=sc.next();
s.reveal(s1);
}
}*/
}