Hello everyone.
If we open the jpg file with notepad, we can see some text or gibberish that doesn't give us any meaning. How do I append some text (ie password hash) to it using Java? I'm attaching a password hash to the jpg file. Hope that anyone reading this will help me. Thank you in advance. :)

Dani AI

Generated

A brief, practical follow-up to the thread between and with safer alternatives and caveats for attaching data to a JPEG.

Appending bytes to the end of a JPEG can work in some cases (many viewers ignore trailing data), but it is fragile: editors, re-encoders, or image hosting services often strip or rewrite metadata and can drop any raw append. For a robust solution use standard JPEG metadata: EXIF (UserComment), JPEG COM (comment) or XMP. These are intended for embedded text and are preserved by more tools and libraries. See EXIF and XMP for format notes: EXIF, Extensible Metadata Platform.

For Java, prefer a library that reads/writes metadata rather than hand-editing bytes. Apache Commons Imaging can write EXIF/XMP safely; Drew Noakes' metadata-extractor is good for reading. Example (Commons Imaging style) that injects an EXIF UserComment without touching pixel data:

// requires Apache Commons Imaging on classpath
File src = new File("in.jpg");
TiffOutputSet outputSet = null;
IImageMetadata meta = Imaging.getMetadata(src);
if (meta instanceof JpegImageMetadata) {
    JpegImageMetadata jm = (JpegImageMetadata) meta;
    if (jm.getExif() != null) outputSet = jm.getExif().getOutputSet();
}
if (outputSet == null) outputSet = new TiffOutputSet();

TiffOutputDirectory exifDir = outputSet.getOrCreateExifDirectory();
exifDir.removeField(ExifTagConstants.EXIF_TAG_USER_COMMENT);
exifDir.add(ExifTagConstants.EXIF_TAG_USER_COMMENT, "sha256:...");

try (FileOutputStream fos = new FileOutputStream("out.jpg")) {
    new ExifRewriter().updateExifMetadataLossless(src, fos, outputSet);
}

Troubleshooting and security notes: many web services strip metadata; verify persistence after any processing step. If the goal is integrity verification, embed a cryptographic signature (HMAC or public-key signature) rather than a plain password hash. Never publish raw password hashes where they can be harvested; if the hash must travel with the file, encrypt or sign it and document the verification process. Libraries: Apache Commons Imaging and .

Recommended Answers

All 2 Replies

For reading binary data you should consider FileInputStream rather than FileReader, which is optimised for characters in some default character encoding. Thanks anyway. Problem solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.