• Obfuscating Images

    Background

    I'm sure an implementation of this exists somewhere already, but I was bored today. The scripts I've provided are for educational purposes only. I do NOT suggest using them, and take no responsibility if you choose to for illegal purposes.

    So, say you have 2 dummy images, dummy1 and dummy2, and a real image you want to keep secret. For simplicity, assume all these images are the same size. Then, you can create a key image which generates the original secret image from the 2 dummy images.

    Method

    Each pixel has a color value of (red, green, blue), where all values are between 0 and 255. To generate a pixel for our key image, we could add the color components of the corresponding pixels of dummy1, dummy2, and the real image (mod 256). Mod 256 means that if you have a result over 255, you subtract 256 from the result until it's 255 or under again. This is so our color values remain legitimate.

    Say for the first pixel of dummy1 the color value is (173, 233, 15), dummy2 (65, 54, 22), and the real image (129, 243, 97). Then the color of the first pixel for the key image is (173 + 65 + 129, 233 + 54 + 243, 15 + 22 + 97) = (367, 530, 134) (mod 256) = (111, 18, 134). We would continue with this until we covered every pixel.

    To decode and get the secret image, simply do the process in reverse: real = key - dummy1 - dummy2 (mod 256). If you have a result under 0, add 256 until it's between 0 and 255 again (inclusive).

    The implication of this is that once you've generated the key image, you can delete the original secret one, and simply generate it from the dummy and key images if you have the proper tools.

    Example

    Dummy1:
    Attachment 432

    Dummy2:
    Attachment 433

    Real Image:
    Attachment 435

    The key becomes:
    Attachment 434

    Decoding back to our original image, with a few artifacts:
    Attachment 436

    Closing Thoughts

    For the above example, I used the php scripts which I wrote below, making use of the GD graphics library. Without heavy modification, I wouldn't suggest using these scripts for actual image secrecy. 2 reasons being that the number of dummy images used should be greatly increased, and the dummy images selected should have a lot more color and pattern variations. Then the images used to generate the key wouldn't be as recognizable in the final result.

    Why do this and not simply encrypt the file? I suppose one could host the dummy and key images in all separate locations behind over 9000 proxies..

    Code

    Encoding:
    Code:
    <?php
    
    // Images objects.
    $false_image = imagecreatefrompng("false.png");
    $false2_image = imagecreatefrompng("false2.png");
    $true_image = imagecreatefrompng("true.png");
    
    // Get size of images. Assume all are same size for simplicity.
    $size = getimagesize("false.png");
    $width = $size[0];
    $height = $size[1];
    
    // Key image.
    $key_image = imagecreatetruecolor($width, $height);
    
    for ($x = 0; $x < $width; $x++)
    {
        for ($y = 0; $y < $height; $y++)
        {
            
            // Get colors at current pixels for all 3 images.
            $rgb1 = imagecolorat($false_image, $x, $y);
            $colors1 = imagecolorsforindex($false_image, $rgb1);
            
            $rgb2 = imagecolorat($false2_image, $x, $y);
            $colors2 = imagecolorsforindex($false2_image, $rgb2);
    
            $rgb3 = imagecolorat($true_image, $x, $y);
            $colors3 = imagecolorsforindex($true_image, $rgb3);
            
            // Assign colors for key image to be the sum of each r,g,b component, mod 256.
            $red = $colors1["red"] + $colors2["red"] + $colors3["red"];
            while ($red > 255)
            {
                $red = $red - 256;
            }
            
            $green = $colors1["green"] + $colors2["green"] + $colors3["green"];
            while ($green > 255)
            {
                $green = $green - 256;
            }
            
            $blue = $colors1["blue"] + $colors2["blue"] + $colors3["blue"];
            while ($blue > 255)
            {
                $blue = $blue - 256;
            }
            
            // Assign colors to pixel of key image.
            $pixel_color = imagecolorallocate($key_image, $red, $green, $blue);
            imagesetpixel($key_image, $x, $y, $pixel_color);
    
        }
    }
    
    // Display key image.
    header('Content-Type: image/png');
    imagepng($key_image);
    ?>
    Decoding:
    Code:
    <?php
    
    $false_image = imagecreatefrompng("false.png");
    $false2_image = imagecreatefrompng("false2.png");
    $key_image = imagecreatefrompng("key.png");
    
    $size = getimagesize("false.png");
    $width = $size[0];
    $height = $size[1];
    
    $true_image = imagecreatetruecolor($width, $height);
    
    for ($x = 0; $x < $width; $x++)
    {
        for ($y = 0; $y < $height; $y++)
        {
            $rgb1 = imagecolorat($false_image, $x, $y);
            $colors1 = imagecolorsforindex($false_image, $rgb1);
            
            $rgb2 = imagecolorat($false2_image, $x, $y);
            $colors2 = imagecolorsforindex($false2_image, $rgb2);
            
            $rgb3 = imagecolorat($key_image, $x, $y);
            $colors3 = imagecolorsforindex($key_image, $rgb3);
            
            $red = $colors3["red"] - $colors2["red"] - $colors1["red"];
            while ($red < 255)
            {
                $red = $red + 256;
            }
            
            $green = $colors3["green"] - $colors2["green"] - $colors1["green"];
            while ($green < 255)
            {
                $green = $green + 256;
            }
            
            $blue = $colors3["blue"] - $colors2["blue"] - $colors1["blue"];
            while ($blue < 255)
            {
                $blue = $blue + 256;
            }
            
    
            $pixel_color = imagecolorallocate($true_image, $red, $green, $blue);
            imagesetpixel($true_image, $x, $y, $pixel_color);
    
        }
    }
    
    header('Content-Type: image/png');
    imagepng($true_image);
    ?>
    This article was originally published in forum thread: Obfuscating Images started by burroughs View original post