Sudhip Nashi
Sudhip Nashi

Reputation: 43

Steganography function works fine with text but garbles binary output

I'm trying to embed a file in an image by using the last 2 LSB's of each color in each pixel (last 3 in the case of blue), and whenever I encode/decode, text works fine, but any binary data like ELF files, random data, etc get all mixed up. Here's my steganography and desteganography function:

bool steganographize(int height, int width, unsigned long long filesize, unsigned char *file, RGBTRIPLE image[height][width], bmpstatus about)
{
    int f = 0; //for char access
    int i = 0;
    int j = 0;

    for (i = 0 ; i < height; i++)
    {
        if (f >= filesize)
        {
            goto finis;
        }

        for (j = 0 ; j < width; j++)
        {
            if (f >= filesize)
            {
                goto finis;
            }

            image[i][j].rgbtRed = ((image[i][j].rgbtRed & ~0x3) | ((file[f] & 0x60) >> 5));
            image[i][j].rgbtGreen = ((image[i][j].rgbtGreen & ~0x3) | ((file[f] & 0x18) >> 3));
            image[i][j].rgbtBlue = ((image[i][j].rgbtBlue & ~0x7) | ((file[f] & 0x7)));
            f++;
        }
    }
finis:
    if (f == (filesize))
    {
        printf("%d\n", i);
        printf("%d\n", j);
        
        print_status("Embedded file into BMP");

        print_status("Inserting EOF");

        image[i][j].rgbtRed = 0;
        image[i][j].rgbtGreen = 92;
        image[i][j].rgbtBlue = 183;

        image[i][j + 1].rgbtRed = 12;
        image[i][j + 1].rgbtGreen = 57;
        image[i][j + 1].rgbtBlue = 237;

        image[i + 1][j].rgbtRed = 91;
        image[i + 1][j].rgbtGreen = 34;
        image[i + 1][j].rgbtBlue = 45;

        return true;
    }

    return false;
}
bool desteganographize(int height, int width, RGBTRIPLE image[height][width], bmpstatus about, FILE* output)
{
    unsigned char* buffer = calloc(height * width * 3, sizeof(unsigned char));
    int i = 0;
    int j = 0;
    int f = 0;

    for (i = 0; i < height; i++)
    {
        for (j = 0; j < width; j++)
        {
            if (image[i][j].rgbtRed == 0 && image[i][j].rgbtGreen == 92 && image[i][j].rgbtBlue == 183) {
                if (image[i][j + 1].rgbtRed == 12 && image[i][j + 1].rgbtGreen == 57 && image[i][j + 1].rgbtBlue == 237 
                    && image[i + 1][j].rgbtRed == 91 && image[i + 1][j].rgbtGreen == 34 && image[i + 1][j].rgbtBlue == 45) {
                        goto finis;
                    } 
            }

            unsigned char c = 
                    ((image[i][j].rgbtRed & 0x3) << 5) | 
                    ((image[i][j].rgbtGreen & 0x3) << 3 ) |
                    (image[i][j].rgbtBlue & 0x7);
            buffer[f] = c;
            f++;
        }
    }

    finis: 
        fwrite(buffer, sizeof(unsigned char), f, output);
        printf("%d\n", i);
        printf("%d\n", j);
        fclose(output);
        return true;
    
    return false;
}

Here's the relevant parts of my main function, as well:

int main(int argc, char** argv) {
        if (strstr(argv[1], encode) != NULL) {
          FILE *bmp = fopen(bitmap, "rb");
          bmpstatus *result = is_valid_bmp(bmp);

          if (result == NULL)
          {
            print_error("Bad BMP. Maybe you specified the wrong file?");
          }

          //Get BMP into memory
          char buffer[BUFFER_LEN];
          sprintf(buffer, "Found BMP of Height %d and width %d", result->height, result->width);
          print_status(buffer);
          cleararr(buffer);

          int width = result->width;
          int height = result->height;
          RGBTRIPLE(*image)
          [width] = calloc(height, width * sizeof(RGBTRIPLE));

          if (!scanimage(height, width, bmp, image, *result))
          {
              print_error("Error scanning BMP into memory. Probably not enough RAM.");
          }
          print_status("Scanned BMP into memory");

          //Scan file into memory
          unsigned long long filesize = file_size(fopen(filename, "rb"));

          sprintf(buffer, "Found file with %llu kilobytes", filesize / 1024);
          print_status(buffer);
          cleararr(buffer);

          if (filesize > ((unsigned long long)width * height * 3) / 8)
          {
              print_error("Bitmap not big enough to hold file");
          }

          unsigned char *file = calloc(filesize, sizeof(unsigned char));
          printf("%llu\n", filesize);

          if (!scan_file(file, filesize, fopen(filename, "rb")))
          {
              print_error("Error scanning file into memory");
          }
          print_status("Scanned file into memory");


          if (!steganographize(height, width, filesize, file, image, *result))
          {
              print_error("Error embedding file into BMP");
          }

          //Output manipulated BMP
          sprintf(buffer, "Outputting finished BMP to %s", output);
          print_status(buffer);
          outputimage(height, width, fopen(output, "wb"), image, *result);
          cleararr(buffer);


          free(result);
          free(image);
          free(file);
          print_success("Finished!");
          return 0;
  }

   if (strstr(argv[1], decode)) {
       bmpstatus *result = is_valid_bmp(input);

        if (result == NULL)
        {
            print_error("Bad BMP");
        }

        int height = result->height;
        int width = result->width;
        RGBTRIPLE(*image)
        [width] = calloc(height, width * sizeof(RGBTRIPLE));

        char buffer[BUFFER_LEN];

        sprintf(buffer, "Found a BMP with height %d and width %d", height, width);
        print_status(buffer);
        cleararr(buffer);

        if (!scanimage(height, width, input, image, *result))
        {
            print_error("Cannot scan BMP into memory. Not enough RAM, maybe?");
        }

        print_status("Scanned BMP into memory");

        char tmpname[16] = "/tmp/tmp.XXXXXX";

        mkstemp(tmpname);
        print_status("Made temporary file");

        FILE* finish = fopen(tmpname, "wb");

        if (!desteganographize(height, width, image, *result, finish)) {
            print_error("Could not extract file.");
        }

            free(result);
        free(image);
        return 0;
    }
}

It isn't the most elegant way, but it seems like it gets the job done (at least for text). bmpstatus is a struct that has the height and width of the bmp, filesize has the size of the input data, and RGBTRIPLE is a struct with 3 elements, red green and blue. I've verified that these two work. scanimage gets the BMP and puts it into an RGBTRIPLE matrix, and scan_file takes a file pointer and scans the data into an unsigned char array. All of these work, so I'm assuming it's something with the steganography functions themselves.

Upvotes: 1

Views: 108

Answers (1)

Sudhip Nashi
Sudhip Nashi

Reputation: 43

It turned out that I was only reading 7 bits from each char, instead of 8. Once I put replaced 0x60 with 0xe0 and 0x3 with 0x7 in the red component, it worked.

Upvotes: 2

Related Questions