# Leaky Redaction Part 1

The Underhanded C Contest 2008 is underway. This year the contest is to write code that appears to redact (black out) rectangular regions of an image, but actually allows the redacted content to be recoverable to a certain degree. They call this "Leaky Reaction". Sample code is provided that can be used in the solution, which includes a number of helpful functions such as reading and writing PPM image files, and getting pixel values from the loaded data.

I thought a good place to start would be with some code that actually does redact the data in a non-recoverable way. Making use of their sample code I quickly came up with the following function:

``````void redact(int left, int bottom, int right, int top, image * out)
{
pixel p;
int x,y;

p.rgb = 0;
p.rgb = 0;
p.rgb = 0;

for( x=left; x&lt;right; x++ ) {
for( y=bottom; y&lt;top; y++ ) {
put_pixel( *out, x, y, p );
}
}
}
``````

The image below shows an image before and after applying the above redact function to it: For this contest we don't want to redact the image though, we just want to appear to have redacted it. A simple approach we could take would be to divide the existing pixel an arbitrary value so that the area appears black (if we divide by a large enough number), but in actual fact it is only almost black, and can be returned to the original value by multiplying the new pixel value with the number we previously divided by.

Below is a redact function that divides the pixel value by 50:

``````void redact(int left, int bottom, int right, int top, image * out)
{
pixel p;
int x,y;

for( x=left; x&lt;right; x++ )
for( y=bottom; y&lt;top; y++ ) {
pixel p = get_pixel( *out, x, y );
p.rgb /= 50;
p.rgb /= 50;
p.rgb /= 50;
put_pixel( *out, x, y, p );
}
}
``````

And to reverse the redaction:

``````void unredact(image * out)
{
pixel p;
int x,y;

for( x=0; x&lt;out-&gt;width; x++ ) {
for( y=0; y&lt;out-&gt;height; y++ ) {
p = get_pixel( *out, x, y);
if(p.rgb * 50 &lt;= 256
&amp;&amp; p.rgb * 50 &lt;= 256
&amp;&amp; p.rgb * 50 &lt;= 256)
{
p.rgb *= 50;
p.rgb *= 50;
p.rgb *= 50;
put_pixel(*out, x, y, p);
}
}
}
}
``````

The image below shows the original image, the image after calling the redact function, and then that image after calling unredact. The final image isn't identical to the first because we loose some information with the integer division, but it is close enough for us to be able to recognise the details. If we used a value smaller than 50 we would have lost less information, but the redacted image wouldn't appear so black. While the approach discussed above clearly allows us to reproduce the "redacted" data it is highly unlikely to be a contest winner! Anyone casually glancing at the code can see that we aren't really redacting the image at all, and working out the code to undo the redaction would be relatively trivial.

In the next part I'll discuss some more "leaky redaction" techniques I have tried. In the mean time here is the complete source code listing for the leaky redaction application, which makes use of sample code:

``````// very simple leaky redaction code for the Underhanded C Contest 2008
// www.coderholic.com
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include "ppm.c"

int main( void )   /* usage:  redact &lt; in.ppm &gt; out.ppm */
{
image in, out;

if( get_ppm_from_file( &in, stdin ) && copy_ppm( in, &out ) ) {
redact(110, 70, 201, 100, &out);

put_ppm_to_file( out, stdout );
dealloc_ppm( in );
dealloc_ppm( out );
}
return 0;
}

// perform leaky redaction on an image
void redact(int left, int bottom, int right, int top, image * out)
{
pixel p;
int x,y;

for( x=left; x&lt;right; x++ )
for( y=bottom; y&lt;top; y++ ) {
pixel p = get_pixel( *out, x, y );
p.rgb /= 50;
p.rgb /= 50;
p.rgb /= 50;
put_pixel( *out, x, y, p );
}
}

// undo our leaky redaction
void unredact(image * out)
{
pixel p;
int x,y;

for( x=0; x&lt;out-&gt;width; x++ ) {
for( y=0; y&lt;out-&gt;height; y++ ) {
p = get_pixel( *out, x, y);
if(p.rgb * 50 &lt;= 256
&& p.rgb * 50 &lt;= 256
&& p.rgb * 50 &lt;= 256)
{
p.rgb *= 50;
p.rgb *= 50;
p.rgb *= 50;
put_pixel(*out, x, y, p);
}
}
}
}
``````
Posted on 29 Jun 2008
If you enjoyed reading this post you might want to follow @coderholic on twitter or browse though the full blog archive.