【发布时间】:2021-07-02 17:05:42
【问题描述】:
我想从 BMP 图像文件中提取附加数据。
在我的函数中,选择 BMP 图像的数据流作为输入提供。在文件类型之后,我读取文件大小,然后我需要遍历流的其余部分。附加数据被附加到 BMP 文件中,因此在图像标题中编码的文件大小不变。
如何从文件大小字节数组中获取一个值,该值将确定在原始文件结束之前我需要读取多少字节? (我需要迭代正确数量的字节才能在附加数据之前获取)
private String getBMPAppendedData(DataInputStream in) {
StringBuilder message = new StringBuilder();
try {
// Read the fileType : always 0x4d42 = "BM"
in.readShort();
// Read the file size
byte[] fileSizeBytes = new byte[4];
in.readFully(fileSizeBytes);
// Read bytes in the loop
loop () {
in.readByte();
}
// Read appended byte by byte if present
boolean areSecretDataPresent = true;
while (areSecretDataPresent) {
try {
byte b = in.readByte();
message.append(String.format("%02X", b));
} catch (Exception e) {
areSecretDataPresent = false;
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
return message.toString();
}
【问题讨论】:
-
不要尝试这样做。该阵列并非 100% 安全使用。您可能会发现 BMP 文件的大小错误。而是使用给定的分辨率、颜色深度和填充字节 + 标题大小 + 其他表格大小自行计算估计的文件大小。
标签: java bmp steganography