2

I need to store XML data on a server that only accepts jpeg images. I thought of writing my XML data inside a valid jpeg file. After all, other than the jpeg header, the content of the image file is arbitrary data right?

Is it possible to produce a valid jpeg file, but have its "body" filled with custom bytes?

Of course, I also need to be able to decode the custom jpeg file and restore the data.

I'm not familiar with the jpeg file format, so I'd appreciate an explicit example.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Frederic
  • 2,034
  • 3
  • 18
  • 22

4 Answers4

3

Perhaps just appending the data to a small jpeg will work?

  • Create a small jpeg.
  • Append your (obfuscated/encrypted) XML to the file.
  • Upload to server.

FWIW, you can easily see this works using a Hex editor. Just create a small jpeg and append your xml to the end. Then open it using any image editor.

This is a perfectly valid thing to do to a jpeg file:

Will random data appended to a JPG make it unusable?

Community
  • 1
  • 1
  • That does work indeed. I was worried that the extra bytes would get shopped off by the server, but they weren't. It's not exactly how I wished to do it since I am not sure the resulting image has a 100% jpeg-conformity state. – Frederic Feb 02 '11 at 16:32
  • This is a good alternative. I prefer not set this as the answer since it's more like a workaround than an answer to the question. – Frederic Feb 03 '11 at 11:51
2

Uhmmm it is a strange architecture... but anyway I think this post would be useful: How to Add 'Comments' to a JPEG File Using C# so the proposal is add the data as a metadata of a jpeg blank image.

Community
  • 1
  • 1
Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
0

If you want to add your data as the actually jpeg data, you first create a BitmapSource with BitmapSource.Create and put your data in the buffer parameter. Than use the JpegBitmapEncoder to save it as a jpeg file (an example is here).

However, as far as I know, the .Net jpeg encoder is not lossless (even if you set it's quality to 100%) so you will need a third party library that can encode JPEG lossless.

Andrei Pana
  • 4,484
  • 1
  • 26
  • 27
0

I don't know of a JPEG specific way but there is a PNG/GIF method to encode arbitrary data and pixels. Check out this post. Some sites allow you to upload PNGs and GIFs renamed to JPEG so you could try that. http://blog.nihilogic.dk/2008/05/compression-using-canvas-and-png.html

He's saving javascript but you could use and text, really.

Chris Haas
  • 53,986
  • 12
  • 141
  • 274