BMP bitmap file encryption

Source: Internet
Author: User

24-bit BMP bitmap file
To use BMP bitmap for encryption, you must first understand the storage format of BMP files. The 24-bit true-color BMP bitmap file consists of three
Part:
The first part is the BMP file header. The first two bytes are "BM" and are used to identify BMP files. The first two bytes are 3rd, 4, 5, and 6.
Bytes are the size of the bitmap file, in bytes. 7th, 8, 9, and 10 bytes are retained and must be 0.
11th, 12, 13, and 14 bytes indicate the offset of the bitmap array to the file header.
The second part is the bitmap information: Starting from 29th bytes, 29th and 30 bytes describe the number of pixels; 35th
, 36, 37, and 38 bytes determine the number of image bytes, but this item is usually null.
The third part is the bitmap array: Starting from 39th bytes, each 3 bytes represents a pixel.
The red, green, and blue brightness values of the pixel. Store other important information in a bitmap file
Storage space, you need to start from here.
Implementation Principle
According to the brightness formula: I = 0.3r + 0.59G + 0.11b, we can know that the human eye is most sensitive to the green component and
The amount is least sensitive. If the green component value changes to one unit, the blue component changes to five, and the red Component Changes to two units.
. Converted to binary, the red component can change its lower two, the green component can change its second bit, the blue component
It can be changed to the lower three. In this way, we can get 2 + 1 + 3 = 6 bytes from the three bytes that describe each pixel.
The remainder information bit is used to store the bitstream of other files. According to this ratio, the maximum value obtained from a 24-bit BMP bitmap is
The storage space is (bitmap file size-40) × 6)/(3 × 8), which is about 1/4 of the length of the bitmap file.

Because each 3 bytes in a bitmap file can only provide storage space of 6 bits, 12 bytes are required for full protection.
3 bytes of the source file.
The encryption protocol established in this article is to first input the password through the program window, from the bitmap file's 39th bytes, that is, the bitmap Array
The source file name, the separator "*", the source file length, the separator "*", and the source
File Content. Each byte of the source file content must be different from the password to further encrypt the file.
Effect.
Note: If the file is intercepted during network transmission, the encrypted file will be restored from
The possibility is very small. First, there is no difference between the encrypted file and the common BMP file.
The size of the bitmap file does not change before and after encryption, so it will not attract the attention of the interceptors. Secondly, since there is only about 1
/4 space contains implicit information. For encrypted information, other 3/4 of the content is hidden. Cryptography
, Usually increase the encryption bit to improve the reliability of encryption, apparently this encryption with 75% encryption code
The method is quite reliable. Finally, the attacker does not know the protocol used for encryption, and the content to be encrypted is
The same encryption key has undergone an exclusive or operation, and it is difficult to decrypt the password without correct decryption.
Source file encryption
Because bitwise operations are frequently performed during encryption, we choose micorsoft, which is advantageous in this respect.
Visual c ++ 6.0 is used as a development tool.
The key to encryption is to make up the information to be encrypted into a bit stream, and enter the information in the specified format into the bitmap file of the carrier.
Positioning. First, make preparations. Because the bytes of each 12 bitmap files can be used to save three source files
And 12 is an integer multiple of 3, so every 12 bytes can be set as a loop. In the following five Arrays
The stored data is used to process the shift volume and mask value of each byte in each cycle:
// Shift sequence when the padding is entered
Int move1 [13] = {6, 5,-, 0 };
// Byte mask sequence of the source file
Unsigned char mask1 [13] = {128,112, 1,192, 7 };
// Bitmap file byte mask Sequence
Unsigned char
Mask2 [13] = {252,254,248,252,254,248,252,254,251,252,252,254,248 };
// When the value is 1, the cache pointer that stores the information to be encrypted is incremented by one
Int Add1 [13] = {, 1 };
// When the value is 1, the cache pointer for storing the BMP file is incremented by one
Int Add2 [13] = {, 1 };
Next, use the open () and readfile () Functions of the cfile class to encrypt the source files and use them as the encryption carrier.
The contents of Bitmap files are read to the cache buf1 and buf2 respectively. It must be emphasized that the cache type cannot be
The common char type must be unsigned char. Otherwise, an error occurs when processing files with Chinese characters. For example
If the size of the source file and the bitmap file of the carrier does not meet the conditions, a null pointer Exception error may occur.
The size of the source file and the bitmap file of the carrier must be compared before the encryption algorithm is implemented.
The following is the main code:
// Enter the file name, file length, delimiter, and content of the source file in the buf3 cache.
Total = 3 + filename1.getlength () + Str. getlength () + filelen1;
Buf3 = new unsigned char [total];
For (INT I0 = 0; I0 <filename1.getlength () + Str. getlength () + 1; I0 ++)
Buf3 [I0] = title [I0];
I0 ++;
// Store other information starting from 39th bytes
Pointer2 = 38;
Buf3 [I0] = '*';
// The file content is different from the encryption key or
For (INT I1 = 0; I1 <filelen1; I1 ++)
Buf3 [I0 + I1 + 1] = buf1 [I1] ^ psw;
While (pointer1 <= total)
{
// Fill the bit streams extracted from the buf3 cache with the redundant bit bits of each byte of the bitmap of the carrier
If (move1 [pointer3]> 0)
Buf2 [pointer2] = (buf2 [pointer2] & mask2 [pointer3]) | (buf3 [pointer1] & mask1 [Poin
Ter3])>
Move1 [pointer3]);
Else
Buf2 [pointer2] = (buf2 [pointer2] & mask2 [pointer3]) | (buf3 [pointer1] & mask1 [Poin
Ter3])
<(Move1 [pointer3] * (-1 )));
If (Add1 [pointer3] = 1)
Pointer1 ++; // corrected the pointer
If (Add2 [pointer3] = 1)
Pointer2 ++; // corrected the pointer
Pointer3 ++; // corrected the pointer
Pointer3 % = 13;
}
When the program jumps out of the while loop, the source file information and the bitmap information have been fused according to the protocol, and the rest of the work is to adjust
Use the write () and close () member functions of the cfile class of MFC to save the generated ciphertext from the cache to the files on the disk.
Is a bitmap file containing encrypted information.
You can use ACDSee and other image View software to compare the original bitmap files without any other information and the new fusion.
Bitmap files with ciphertext information cannot be distinguished by the naked eye, and the file length has not changed.
.
Recovery of encrypted information
Like most encryption and decryption programs, the decryption and encryption processes of this method are similar, as long as the process is reversed
Yes. The main idea of its implementation is to extract the indicated location information from the bitmap file as the carrier according to the protocol.
Bit stream, and split the bit stream into original source information. First, set the shift value and mask:
// Shift sequence when merging file information bytes
Int move2 [13] = {6, 5,-, 0 };
// Mask sequence of Bitmap File bytes
Unsigned char mask2 [13] = };
// When the value is 1, the cache pointer that stores the information to be restored is incremented by one
Int Add1 [13] = {, 1 };
// When the value is 1, add a cache pointer to the content of the BMP file containing the encrypted information
Int Add2 [13] = {, 1 };
Then, the bitmap file content containing the encrypted information is read through file processing functions such as open () and read () of the cfile class.
In the cache, the following operations are performed to extract and restore the ciphertext:
// The information of other files is stored starting from 39th bytes.
Pointer2 = 38;
// Clear buf3 Cache
Memset (buf3, 0, filelen2 );
// Calculate the exclusive or operation value
Psw = makepsw ();
While (true)
{
// Extract the stored information stream from the byte of the bitmap file and form the object byte sequence to be restored.
If (move2 [pointer3]> 0)
Buf3 [pointer1] | = (buf2 [pointer2] & mask2 [pointer3]) <move2 [pointer3];
Else
Buf3 [pointer1] | = (buf2 [pointer2] & mask2 [pointer3])> (move2 [pointer3] * (-1 ));

If (Add1 [pointer3] = 1)
{
If (buf3 [pointer1] = '*')
// Delimiter count
Times ++;
If (pointer1> = 0) & (Times = 0 ))
{
Cstring STR;
Str. Format ("% C", buf3 [pointer1]);
// Extract the file name to be restored
Filename1 + = STR;
}
If (Times = 1) & (buf3 [pointer1]! = '*'))
{
Temp [pointer4] = buf3 [pointer1]; pointer4 ++;
}
If (Times = 2) & (buf3 [pointer1] = '*'))
{
Temp [pointer4] = 0;
// Extract the length of the file to be restored
Filelen1 = static_cast <DWORD> (atoi
(Temp ));
Pointer4 = 0;
}
If (times> = 2)
{
// Restore the source file content through the exclusive or operation again
Buf1 [pointer4] = buf3 [pointer1] ^ psw;
Pointer4 ++;
}
If (pointer4> filelen1)
// Jumps out of the loop after the file is restored
Break;
Pointer1 ++; // corrected the pointer
}
If (Add2 [pointer3] = 1)
Pointer2 ++; // corrected the pointer
Pointer3 ++; // corrected the pointer
Pointer3 % = 13;
}
Finally, the decrypted information is retrieved Based on the file name and file extension of the source file that has been extracted from the bitmap.
Restore the file according to the original file name. Apparently, if the password entered during decryption is incorrect, the decrypted message
Information cannot be identified.
Knot
This method hides important information in bitmap files, which can play a very good protective role, especially when its carrier is
Almost no one can realize that the 24-bit BMP bitmap file contains other information. We are using electronics
When sending an email, you can first hide the file in a bitmap and then send the bitmap as an attachment,
Even if the email is intercepted and intercepted, it will not cause leaks.
Http://www.blog.ccoo.cn/nbk3/lshow.asp? Id = 65264

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.