Binary Help

Hello, I am currently in my last year of a four year computer science degree and I am taking C++ next quarter, so I decided to do some practice with this language. I am currently trying to write a Steganography program in C++ for practice.

In case your not familiar with what Steganography is, it is where you take an image, a bitmap in my case and hide messages in the picture by changing the LSB. Later, the image can be read and the LSB can be pulled and recombine to produce the message.

I have already done this with Java. In Java, you can open a bitmap, read it into a byte array and then you can perform operations on each element of the array, providing the header bytes are left untouched. In Java you can actually print out the array and see what each value is. I used a simple bitmap image (an all black one) and you can see that the output shows 255 for each of the bits for the 24 bit bmp that I am working with.

I am trying to do the same thing with in C++ but I can't quite get the same functionality. I have done some tests and found that I have successfully read the bitmap into an array, and wrote that array to another file and saved it. So I get two bitmaps that are identical. I tried to use a for loop to manipulate the data after index 54 (I discovered the header is 54 bytes in length) and found that the image produced is the same even if I try to set the values of those indices to 0 (white). I have also tried to print out the content of the array that I loaded the file into and it prints out a few random characters and a huge amount of blank space, but that's it. In Java, it would print out all the bytes. I tried opening the file with iOS::binary and then once without and found that the results are the same. So, I think this boils down to my lack of understanding of what is going on.

So, my goal is to either A.) Be able to see the actual contents of the array like I can in java (if it's possible), or B.) At the very least, be able to manipulate the LSB of each element of the array so that I can hide a message.

I am on a mac and using g++ for compiling. Here is my function of reading and writing the bitmap. Let me know if I am missing something, which I think I am.
Some parts of the code are commented out, my experiments and useful syntax noted here. If you guys want to see more of my code, let me know. Thank you very much for your time and help.

void File::FileReader(string *name)
{// this is reading the file normally, really.
int length;

ifstream fileStream;
// ifstream fileStream(*name, ios::binary); this can replace the open command.
fileStream.open(*name);// had ios::binary


fileStream.seekg(0, fileStream.end);// reads to the end of the file
length = fileStream.tellg();// gets the length of the file
fileStream.seekg(0, fileStream.beg);// resets reader

char *fileArray = new char[length];// stores the data from the bmp

fileStream.read(fileArray,length);// reads it in

fileStream.close();// closes after.

cout.write(fileArray, length);// prints the array to the screen

/* int x;
for(x = 0; x < length; x++)
{
cout << fileArray[x] << endl;
}
*/ This was just to see if the output would be different, it wasn't
/* int x;

for(x = 54; x < length; x++)
{
fileArray[x] = fileArray[x]-fileArray[x];
}// this for loop was an attempt to change the values of the array, didn't work.

ofstream newFile("picture3.bmp");
newFile.write(fileArray,length);// this is pretty cool. You can write to a file like this pretty quick.
*/
delete[] fileArray;
}

Here is the output I see in the terminal:

Welcome To Stenanography Lab

1.) Encode a Secret Message
2.) Decode a Secret Message

3.) Quit
Enter Choice Here: 1
Please Enter The File Name You Wish To Use Or Type Quit to Return To The Menu: picture.bmp
File Has Been Successfully Validated.
Press Enter To Continue
BM?q
6(???q

The BM?q and the next line are my attempts to print out the content of the array that should have the binary data from picture.bmp. I did redirection of the output and it actually printed out more stuff including the BM? stuff, but it's still not what I am after. It prints ^@ over and over again, which makes sense since the entire image is black. But I was hoping for values like 255 or binary value of 255.

Thanks again for all your help and time.

HiKenshi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
#include <limits>

// http://en.cppreference.com/w/cpp/language/type_alias
using byte = unsigned char ;

// https://cal-linux.com/tutorials/vectors.html
std::vector<byte> read_file( std::string path )
{
    std::ifstream file( path, std::ios::binary ) ;
    std::vector<byte> contents ;
    char c ;
    while( file.get(c) ) contents.push_back(c) ;
    return contents ;
}

std::ostream& dump_bits( const std::vector<byte>& seq, std::ostream& stm = std::cout )
{
    std::size_t cnt = 0 ;

    // http://en.cppreference.com/w/cpp/header/bitset
    using bits = std::bitset< std::numeric_limits<byte>::digits > ;
    for( byte b : seq ) stm << bits(b) << ( cnt++ % 8 == 7 ? '\n' : ' ' ) ;
    return stm << '\n' ;
}

byte set_lsb( byte b ) { return b | 1U ; }

byte unset_lsb( byte b ) { return b & ~1U ; }

int main()
{
    auto bytes = read_file(__FILE__) ;
    bytes.resize(30) ; // to limit the size of the output to the first 30 bytes
    dump_bits(bytes) << "----------------\n" ;

    for( byte& b : bytes ) b = set_lsb(b) ;
    dump_bits(bytes) << "----------------\n" ;

    for( byte& b : bytes ) b = unset_lsb(b) ;
    dump_bits(bytes) << "----------------\n" ;
}

http://coliru.stacked-crooked.com/a/f29a95914456ba30
Wow, see the stuff I was reading said there was not byte data type like in java. They all said that one had to use char or unsigned char. This looks like what I was looking for.

Thank you so much. I will run it when I get home to my mac. I will let you know if it works, it looks like it will.

Thank You Very Much.
Yep, this is the solution. Thanks very much. I can do pretty much all I need at this point.

Once Again, Thank You Very Much.
Topic archived. No new replies allowed.