Today, a friend from csdn sent a text message asking me how to write a file into an image. After learning C #, I wrote another tool like this.
For example, the company cannot carry the source code; it can carry images; the only thing to do is to pack the source code with rar, find a BMP file, open it, and add several character strings at the end, then add the RAR data. OK. Open the BMP file, find the feature string, copy the tail record, and save it to a new file;
This method can also be used for EXE files.
For ease of operation, I wrote a tool software in C #. The following is part of the code (I am a newbie, do not laugh ):
Private bool encodedatatobitmap (string srcbmpfile, string srcfile, string destbmp file ){
// Add it to the end of the file
System. Io. filestream SBF = NULL;
System. Io. filestream Sf = NULL;
System. Io. filestream DBF = NULL;
Byte [] srcbmpbyte;
Byte [] srcfilebyte;
Try {
SBF = new system. Io. filestream (srcbmpfile, system. Io. filemode. Open, system. Io. fileaccess. Read );
Sf = new system. Io. filestream (srcfile, system. Io. filemode. Open, system. Io. fileaccess. Read );
DBF = new system. Io. filestream (destbmp file, system. Io. filemode. createnew, system. Io. fileaccess. Write );
Srcbmpbyte = new byte [SBF. Length];
SBF. Read (srcbmpbyte, 0, (INT) SBF. Length );
Srcfilebyte = new byte [SF. Length]; // You can encrypt or compress the obtained data.
SF. Read (srcfilebyte, 0, (INT) Sf. Length );
DBF. Write (srcbmpbyte, 0, srcbmpbyte. Length );
DBF. Write (system. Text. encoding. Default. getbytes ("abcdefg"), 0, system. Text. encoding. Default. getbytes ("abcdefg"). Length );
DBF. Write (srcfilebyte, 0, srcfilebyte. Length );
Return true;
} Catch {
Return false;
} Finally {
If (SBF! = NULL)
SBF. Close ();
If (SF! = NULL)
SF. Close ();
If (DBF! = NULL)
DBF. Close ();
}
}
The Code is the same as above.
1. Read BMP data
2. Read File data
3. Create a New BMP file
4. Write BMP data
5. Write a feature string
6. Write File data
7. Complete.
The code for splitting the file is as follows:
Private bool decodedatafrombitmap (string srcbmpfile, string destfile ){
System. Io. filestream SBF = NULL;
System. Io. filestream df = NULL;
Byte [] srcbmpbyte;
Try {
SBF = new system. Io. filestream (srcbmpfile, system. Io. filemode. Open, system. Io. fileaccess. Read );
DF = new system. Io. filestream (destfile, system. Io. filemode. createnew, system. Io. fileaccess. Write );
Srcbmpbyte = new byte [SBF. Length];
SBF. Read (srcbmpbyte, 0, (INT) SBF. Length );
String F = "";
Int offset = 0;
For (INT I = 0; I <srcbmpbyte. Length-7; I ++ ){
F = "";
For (Int J = I; j <I + 7; j ++ ){
F + = (char) srcbmpbyte [J];
}
If (F = "abcdefg "){
Offset = I + 7;
Break;
}
}
If (offset = 0 ){
F = "";
For (INT I = srcbmpbyte. Length-7; I <srcbmpbyte. length; I ++ ){
F + = (char) srcbmpbyte [I];
}
If (F = "abcdefg "){
Offset = srcbmpbyte. Length-7;
} Else {
MessageBox. Show ("this file has not been added to data! ");
Return false;
}
}
DF. Write (srcbmpbyte, offset, srcbmpbyte. Length-offset );
Return true;
} Catch {
Return false;
} Finally {
If (SBF! = NULL) SBF. Close ();
If (DF! = NULL) dF. Close ();
}
}
The process is
1. Read BMP files
2. Create a new file
3. Search for a feature string
4. Write a new file (feature string offset Position + feature String Length)
5. Complete.
Is it easy? Download project source code