-
Notifications
You must be signed in to change notification settings - Fork 1
/
IStegCLI.java
165 lines (154 loc) · 6.61 KB
/
IStegCLI.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
package isteg;
import java.nio.file.Paths;
import java.util.Scanner;
public class IStegCLI {
private static final String VCODE = "2.01";
private static Scanner scanner;
public static void main(String[] args) {
System.out.println("iSteg CLI v-"+VCODE+"\nEnter your choice:\n\t1. Hide a file with Steg\n\t2. Hide a message with Steg\n\t3. Extract stuff from Steg\n\tEnter any things to exit.");
scanner = new Scanner(System.in);
String ch = scanner.nextLine()+" "; // Avoiding IndexOutOfBoundsException
char choice = ch.charAt(0);
switch (choice) {
case '3':
readStuff();
break;
case '2':
hideMsg();
break;
case '1':
hideFile();
break;
default:
break;
}
}
private static void hideMsg() {
System.out.println("Enter file name in which your message will be hidden:");
String topFile = scanner.nextLine();
System.out.println("Enter Your Message:");
String msg = scanner.nextLine();
int bitCount = getBitCount();
System.out.println("Enter new file name:");
String newFileName = scanner.nextLine();
System.out.println("Enter Password (If you want not to encrypt, just press enter):");
String password = scanner.nextLine();
newFileName +=".png";
int err = Steg.write(Paths.get(topFile), msg, bitCount,password.toCharArray(), newFileName);
switch (err) {
case Steg.SUCCESS:
System.out.println("Successfully created steganographic file "+newFileName);
break;
case Steg.ERR_NOTANIMAGE:
System.out.println("The file \""+topFile+"\" doesn\'t contain proper image data");
break;
case Steg.ERR_FILEREAD:
System.out.println("Unable to read file \""+topFile+"\".");
break;
case Steg.ERR_FILEWRITE:
System.out.println("Unable to write file.");
break;
case Steg.ERR_LOWIMGSIZE:
System.out.println("The image resolution is too low to save all of the data.");
break;
case Steg.ERR_CIPHERFAILED:
System.out.println("Cypher error! Please try Again.");
break;
default:
break;
}
}
private static int getBitCount() {
int ret = 0;
do {
System.out.println("Choose One\n\t1. 1-LSB (Extremely low distruction to image, holds less data)\n\t2. 2-LSB (Low distruction to image, holds more data)(Recommended)");
String s = scanner.nextLine();
if(s.length()>0) {
if(Character.isDigit(s.charAt(0)))
ret = Integer.parseInt(s.substring(0,1));
}
} while(ret!=1 && ret!=2);
return ret;
}
private static void hideFile() {
System.out.println("Enter file name in which your file will be hidden:");
String topFile = scanner.nextLine();
System.out.println("Enter the name of the file u want to hide:");
String bottomFile = scanner.nextLine();
int bitCount = getBitCount();
System.out.println("Enter new file name:");
String newFileName = scanner.nextLine();
newFileName +=".png";
System.out.println("Enter Password (If you want not to encrypt, just press enter):");
String password = scanner.nextLine();
int err = Steg.write(Paths.get(topFile), Paths.get(bottomFile), bitCount,password.toCharArray(), newFileName);
switch (err) {
case Steg.SUCCESS:
System.out.println("Successfully created steganographic file "+newFileName);
break;
case Steg.ERR_NOTANIMAGE:
System.out.println("The file \""+topFile+"\" doesn\'t contain proper image data");
break;
case Steg.ERR_FILEREAD:
System.out.println("Unable to read file \""+topFile+"\" or "+"\""+bottomFile+"\"");
break;
case Steg.ERR_FILEWRITE:
System.out.println("Unable to write file.");
break;
case Steg.ERR_LOWIMGSIZE:
System.out.println("The image resolution is too low to save all of the data.");
break;
case Steg.ERR_CIPHERFAILED:
System.out.println("Cypher error! Please try Again.");
break;
default:
break;
}
}
private static void readStuff() {
System.out.println("Enter file name with extension: ");
String name = scanner.nextLine();
System.out.println("Password (Press enter if the steganographic data wasn\'t encrypted):");
String password = scanner.nextLine();
if(!name.endsWith(".png"))
name+=".png";
String[] data = Steg.read(Paths.get(name),password.toCharArray());
switch (Integer.parseInt(data[0])) {
case Steg.SUCCESS:
case Steg.SUCCESS_NOPASS:
if(data[2].equalsIgnoreCase("1")) {
System.out.println("Message extraction successful. The text is:\n");
System.out.println(data[1]);
}
else {
System.out.println("File extraction successful. The file name is: "+data[1]);
}
if(Integer.parseInt(data[0]) == Steg.SUCCESS_NOPASS)
System.out.println("The steganographic data wasn\'t encrypted");
break;
case Steg.ERR_NOTANIMAGE:
System.out.println("The file \""+name+"\" doesn\'t contain proper image data");
break;
case Steg.ERR_FILEREAD:
System.out.println("Unable to read file \""+name+"\"");
break;
case Steg.ERR_FILEWRITE:
System.out.println("Unable to write file.");
break;
case Steg.ERR_NOSTEG:
System.out.println("No steganographic data found on \""+name+"\"");
break;
case Steg.ERR_CIPHERFAILED:
System.out.println("Cipher failed. Please try again.");
break;
case Steg.ERR_PASSREQ:
System.out.println("\""+name+"\""+" contains encrypted steganographic data. Password required.");
break;
case Steg.ERR_WRONGPWD:
System.out.println("Password incorrect. Operation failed.");
break;
default:
break;
}
}
}