-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSteganography.java
More file actions
204 lines (173 loc) · 5.37 KB
/
Steganography.java
File metadata and controls
204 lines (173 loc) · 5.37 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
package steganography;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import java.lang.Math;
import javax.imageio.ImageIO;
/**
* Classe dedita alla scrittura e alla lettura di testo in codifica ASCII all'interno di un'immagine.
*
*/
public class Steganography {
private String imagePath, destinationPath;
/**
* Costruttore
*
* @param imagePath
* @throws FileNotFoundException
*/
public Steganography(String imagePath) throws FileNotFoundException {
setImagePath(imagePath);
}
/**
*
* @param imagePath
* @param destinationPath
* @throws FileNotFoundException
*/
public Steganography(String imagePath, String destinationPath)
throws FileNotFoundException {
this(imagePath);
setDestination(destinationPath);
}
/**
* Operazione di lettura dall'immagine identificata dall'immagine salvata
*/
public String readImage() {
return read(getImagePath());
}
/**
* Operazione di lettura dall'immagine
*
* @param filename
*/
public static String read(String filename) throws FileNotFoundException, IOException{
StringBuilder x = new StringBuilder();
BufferedImage mod = ImageIO.read(new File(filename));
for (int i = 0; i < mod.getWidth(); i++) {
for (int j = 0; j < mod.getHeight(); j++) {
x.append(Math.abs(mod.getRGB(i, j) % 2));
}
}
return AsciiToString(x.toString());
}
/**
* Operazione di scrittura sull'immagine e salvataggio
*
* @param message
*/
public void writeImage(String message) throws IOException,IllegalArgumentException{
writeImage(message, null);
}
/**
* Operazione di scrittura sull'immagine e salvataggio
*
* @param message
* @param destinationPath
*/
public void writeImage(String message, String destinationPath) throws IOException,IllegalArgumentException {
BufferedImage img = ImageIO.read(new File(getImagePath()));
String stega = AsciiToBinary(message);
int index = 0, value;
for (int i = 0; i < img.getWidth(); i++) {
for (int j = 0; j < img.getHeight(); j++) {
value = 0;
index = i * img.getHeight() + j;
if (index < stega.length()
&& stega.charAt(index) == '1') value = 1;
img.setRGB(i, j, img.getRGB(i, j) - (img.getRGB(i, j) % 2)
- value);
}
}
if (index < stega.length()){
throw new IllegalArgumentException("Message too long!");
}
if(this.getDestination() == null){
int start = getImagePath().lastIndexOf('/');
start = (start == -1) ? 0 : start + 1;
setDestination(getImagePath().substring(0, start)
+ "mod"
+ getImagePath().substring(start, getImagePath().indexOf('.'))
+ ".png");
}
ImageIO.write(img, "png", new File(this.getDestination()));
}
/**
*
* @return string
*/
public String getImagePath() {
return this.imagePath;
}
/**
* Assegna il valore indicato come origine dell'immagine su cui effettuare la codificare.
*
* @param path
* @throws FileNotFoundException
*/
public void setImagePath(String path) throws FileNotFoundException {
File f = new File(path);
if (!f.exists() || f.isDirectory()) {
throw new FileNotFoundException();
};
this.imagePath = path;
}
/**
* @return string
*/
public String getDestination() {
return this.destinationPath;
}
/**
*
* @param path
*/
public void setDestination(String path) {
File f = new File(path);
if (f.exists() || f.isDirectory()) {
throw new FileNotFoundException();
};
this.destinationPath = path;
}
/**
* Analizza e converte una stringa in una sequenza di numeri secondo la
* codifica ASCII
*
* @param asciiString
* @return string
*/
public static String AsciiToBinary(String asciiString) {
byte[] bytes = asciiString.getBytes();
StringBuilder binary = new StringBuilder();
for (byte b : bytes) {
int val = b;
for (int i = 0; i < 8; i++) {
binary.append((val & 128) == 0 ? 0 : 1);
val <<= 1;
}
}
return binary.toString();
}
/**
* Analizza e converte una sequenza si numeri in una stringa secondo la
* codifica ASCII
*
* @param intString
* @return string
*/
public static String AsciiToString(String intString) {
StringBuilder result = new StringBuilder();
int nextChar = 0;
for (int i = 0; i < intString.length() / 8; i++) {
nextChar = 0;
for (int j = 0; j < 8; j++) {
nextChar += ((int) intString.charAt((j + i * 8))) % 2
* Math.pow(2, 8 - (j % 8) - 1);
}
result.append((char) nextChar);
}
return result.toString();
}
}