-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSteganography.java
More file actions
72 lines (58 loc) · 2.31 KB
/
Steganography.java
File metadata and controls
72 lines (58 loc) · 2.31 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
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.charset.StandardCharsets;
public class Steganography {
public static void hideMessage(BufferedImage image, String message, String outputPath) throws IOException {
message += '\0'; // dodajemy znak końca wiadomości
byte[] msgBytes = message.getBytes(StandardCharsets.UTF_8);
int msgIndex = 0;
int bitIndex = 0;
outer:
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
int rgb = image.getRGB(x, y);
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;
if (msgIndex < msgBytes.length) {
// Ukrywamy bit w nieznaczącym bicie koloru niebieskiego
int bit = (msgBytes[msgIndex] >> (7 - bitIndex)) & 1;
blue = (blue & 0xFE) | bit;
bitIndex++;
if (bitIndex == 8) {
bitIndex = 0;
msgIndex++;
}
} else {
break outer;
}
int newRGB = (red << 16) | (green << 8) | blue;
image.setRGB(x, y, newRGB);
}
}
ImageIO.write(image, "png", new File(outputPath));
}
public static String extractMessage(BufferedImage image) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
int currentByte = 0;
int bitIndex = 0;
outer:
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
int rgb = image.getRGB(x, y);
int blue = rgb & 0xFF;
int bit = blue & 1;
currentByte = (currentByte << 1) | bit;
bitIndex++;
if (bitIndex == 8) {
if (currentByte == 0) break outer; // znak końca
out.write(currentByte);
currentByte = 0;
bitIndex = 0;
}
}
}
return out.toString(StandardCharsets.UTF_8);
}
}