-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSteganographyUtil.java
More file actions
82 lines (64 loc) · 2.41 KB
/
Copy pathSteganographyUtil.java
File metadata and controls
82 lines (64 loc) · 2.41 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
package mo_phong_zalo2;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class SteganographyUtil {
public static File hideText(File imageFile, String message) throws Exception {
BufferedImage image = ImageIO.read(imageFile);
// Mã hóa bằng AES
String encryptedMessage = AESUtil.encrypt(message);
byte[] msgBytes = encryptedMessage.getBytes("UTF-8");
// Biến byte[] thành chuỗi bit
StringBuilder bitStr = new StringBuilder();
for (byte b : msgBytes) {
bitStr.append(String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0'));
}
// Thêm byte kết thúc 00000000
bitStr.append("00000000");
int width = image.getWidth();
int height = image.getHeight();
int bitIndex = 0;
outer:
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (bitIndex >= bitStr.length()) break outer;
int rgb = image.getRGB(x, y);
int r = (rgb >> 16) & 0xFF;
int g = (rgb >> 8) & 0xFF;
int b = rgb & 0xFF;
// Ghi 1 bit vào LSB của red
r = (r & 0xFE) | (bitStr.charAt(bitIndex++) - '0');
int newRGB = (r << 16) | (g << 8) | b;
image.setRGB(x, y, newRGB);
}
}
File out = new File("stego_" + imageFile.getName());
ImageIO.write(image, "png", out);
return out;
}
public static String revealText(File imageFile) throws Exception {
BufferedImage image = ImageIO.read(imageFile);
StringBuilder bits = new StringBuilder();
int width = image.getWidth();
int height = image.getHeight();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int rgb = image.getRGB(x, y);
int r = (rgb >> 16) & 0xFF;
bits.append(r & 1);
}
}
// Chuyển dãy bit thành byte[]
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (int i = 0; i + 8 <= bits.length(); i += 8) {
String byteStr = bits.substring(i, i + 8);
int charCode = Integer.parseInt(byteStr, 2);
if (charCode == 0) break; // null byte => kết thúc
baos.write(charCode);
}
String encrypted = baos.toString("UTF-8");
return AESUtil.decrypt(encrypted);
}
}