overview
Recently, when doing a function in the background of resource managers, you need to pass information into the DLL.
The main thing is to pass some relatively simple parameters, including image fit, transparency and so on. There are many communication methods. After all, it is a function for practice, so I want to find some methods that I have not used before.
In my previous article, I introduced digital watermarking technology, two of which are to embed data in image files
https://www.cnblogs.com/zhaotianff/p/17222056.html
So here I also want to write this data directly in the image file and then read it in the DLL.
After checking the information online, there is indeed a relatively simple way to write your own data in a JPG file.
Moreover, this method can theoretically write data of unlimited size.
To make some digression, no matter what format the document has, it has a standard format. Generally, it includes file headers, file bodies, etc. After we are familiar with the file structure, we can theoretically write some of our own data in the file header without destroying the original file structure.
But this process takes some time.
我这里是直接使用了NVISO Labs 现有的方式,给有需要的小伙伴做一个分享。
realization principle
Let's first open an ordinary JPG file with a hex editor

You can see that the content of the first two bytes is FF D8
FF D8 => ** This is a flag indicating the beginning of the JPEG data stream **
When we find the mark at the beginning of the JPEG data stream, we can insert a "comment" mark after it.
FF FE =>** This is a "comment" flag that will also be ignored by the JPEG decoder. These tags are exactly how we are going to insert the data and still have a valid image **
**FF FE is followed by 2 bytes, which represents the size of the "comment" content. **
example
Suppose we write HelloWorld in a jpg file,"HelloWorld" is ten bytes, so we write after FF FE
00 0A(10 bytes) 48 65 6C 6C 6F 57 6F 72 6C 64
There are 14 bytes in total here, including the size of the head of **FF FE 2 bytes **** 000A 2 bytes **, and the content of **10 bytes **.
So we used a hex editor to insert 14 bytes after FF D8


Then we fill in the data with, like this

After saving the file, the JPG file can still be opened normally

** Code implementation **
Here, take C#as an example. The implementation methods in other languages are similar. I have written opacity and strch data here.
private void WriteImageInfoToFile(string filePath,double opacity,int stretch)
{
//读取文件数据
var buffer = System.IO.File.ReadAllBytes(filePath);
//判断是否是JPG文件
if (buffer[0] == 0xFF && buffer[1] == 0xD8)
{
//将原始数据扩容6个字节
var newBuffer = new byte[buffer.Length + 6];
//拷贝JPG文件开始标记 FF D8
Array.Copy(buffer, 0, newBuffer, 0, 2);
//设置数据
//注释标记
newBuffer[2] = 0xFF;
newBuffer[3] = 0xFE;
//大小 0x02
newBuffer[4] = 0;
newBuffer[5] = 0x02;
//数据
newBuffer[6] = (byte)nOpacity;
newBuffer[7] = (byte)stretch;
//将原图片剩下的数据拷贝到新buffer中
Array.Copy(buffer, 2, newBuffer, 7, buffer.Length - 2);
//写入文件
System.IO.File.WriteAllBytes(filePath, newBuffer);
}
}
When reading, just read according to the same rules.
resources
https://blog.nviso.eu/2020/07/13/how-to-embed-secret-data-in-jpeg-files/