We encrypt files in black and white images

Published on April 03, 2011

We encrypt files in black and white images

Hi% user%! One boring evening I wanted to do something. The text encoder is the first thing that crossed my mind. But when he was ready, it turned out that he could encrypt any files! In this example, there is a file size limit. Files weighing more than 200 kb should not be fed. We will write the script in PHP.


Idea


Encrypt: read the contents of the file, convert it to 0 and 1, convert the numbers to pixels and write to the file.
We decrypt: we read the file, determine the color of each pixel, we get 0 and 1, from them we get the text and write it to the file.

Let's get started!


To begin with, we will write some utility functions:
function  readln () {
     $ stdin  =  fopen ( 'php: // stdin' ,  'r' ); 
    return  trim ( fread ( $ stdin ,  2048 ));
}

function  ascii_decrypt ( $ encryptedtext )
{
     $ encryptedtext  =  str_replace ( '' ,  '' ,  $ encryptedtext );
    $ encryptedtext  =  str_replace ( " \ n " ,  '' ,  $ encryptedtext);
    $ encryptedtext  =  str_replace ( " \ r " ,  '' ,  $ encryptedtext );

    $ blockstack  =  str_split ( $ encryptedtext ,  8 );
    $ plaintext  =  "" ;
    while ( list ( $ key ,  $ val )  =  each ( $ blockstack ))
    {
        $ plaintext  . =  bin2ascii ( $ val );
    }
    return  $ plaintext ;
}

function  ascii_encrypt ( $ plaintext )
{
     $ plaintextlength  =  strlen ( $ plaintext );
    $ i = 0 ;
    for ( $ x  =  0 ;  $ x  <  $ plaintextlength ;  $ x ++ )
    {
         $ pt  . =  ascii2bin ( $ plaintext [ $ x ]);
        // $ encryptedtext. = "";
    }
     return  $ pt ;
}

function  ascii2bin ( $ char )
{
     $ char =  decbin ( ord ( $ char ));
    $ char  =  str_pad ( $ char ,  8 ,  '0' , STR_PAD_LEFT);
    return  $ char ;
}

function  bin2ascii ( $ binary )
{
    $ char  =  substr ( $ binary ,  $ x ,  8 );
    $ char  =  chr ( bindec ( $ char ));
    return  $ char ;
}


I will explain.
The readln () function reads input text from the command line. ascii_encrypt () translates the text to 0 and 1, and ascii_decrypt () does the opposite. ascii2bin () converts a character into a sequence of 8 zeros and ones , and bin2ascii () , of course, performs the opposite action.

Now the main thing is to encrypt and decrypt. I will not explain, as the code is already well commented.

We encrypt

echo  "Input file:" ;
$ fl  =  readln ( $ fl );

if ( file_exists ( $ fl ))  $ file  =  stripslashes ( file_get_contents ( $ fl )); else  die ( "Input file not found! \ n " );
// header ('Content-Type: image / png');

// We convert the contents of the file to zeros and ones ... 
// And for some reason we turn it over)
$ e  =  strrev (ascii_encrypt ( $ file ));

//// Simple code for calculating the height and width of the picture
$ l  =  strlen ( $ e );
$ h  =  floor ( sqrt ( $ l ));
while ( $ l % $ h  ! ==  0 )  $ h - ;
$ w  =  $ l / $ h ;
// Break the whole mess one character at a time
$ wordDot  =  str_split ( $ e ,  1 );

$ line  =  0 ;
$ column  =  0 ;

$ image  =  imagecreate ( $ w, $ h );
$ black  =  imagecolorallocate ( $ image , 0 , 0 , 0 ); // Define the black color
$ white  =  imagecolorallocate ( $ image , 255 , 255 , 255 ); // White
foreach ( $ wordDot  as  $ val )
    {
        // If 0, then dye the white dot, and if 1 - black
        if ( $ val  ==  0 )    imageline ( $ image ,$ column , $ line , $ column , $ line , $ white );
                else     imageline ( $ image , $ column , $ line , $ column , $ line , $ black );
        if ( $ column  ==  $ w -1 ) { $ line ++ ; $ column = -1 ;}
            $ column ++ ;
        
    }

// Save the result
imagepng ( $ image , './' . $ Fl .'.png' );
imagedestroy ( $ image );


Decipher

echo  "Input file:" ;
$ fly  =  readln ();
if ( file_exists ( $ fly ))  $ image  =  imagecreatefrompng ( $ fly ); else  die ( "File not found! \ n " );

// Calculate the width and height of the image
         $ w  =  imagesx ( $ image );
        $ h  =  imagesy ( $ image );
// B the number of pixels in it (S = a * b)
         $ wh  =  $ w * $ h ;
        $ x  = 0 ;
        $ y  =  0 ;

        for ( $ t = 0 ;  $ t  <  $ wh ;  $ t ++ )
            {
// Define the color of the point. 
                $ rgb  =  imagecolorat ( $ image ,  $ x ,  $ y );
                $ colors  =  imagecolorsforindex ( $ image ,  $ rgb );
                $ cl  =  $ colors [ 'red' ] . $ colors [ 'green' ] . $ colors[ 'blue' ];
// If white, then set 0, and if black - 1
                if ( $ cl  ==  '255255255' )  $ binary  . =  '0' ;
                if ( $ cl  ==  '000' )  $ binary  . =  '1' ;
                if ( $ x  ==  $ w -1 ) { $ y ++ ; $ x = -1 ;}
                    $ x ++ ;
            }
// Flip and convert to text
$ dt  =  ascii_decrypt ( strrev ($ binary ));
// Write to file
file_put_contents ( str_replace ( '.png' ,  '' ,  $ fly ),  $ dt );


Launch


You must have the PHP interpreter installed.
Linux (Unix):
  • chmod + x classic.php
  • ./classic.php encrypt # Encrypt
  • ./classic.php decrypt # Decrypt

Windows:
  • path_to_php \ php.exe classic.php encript # Encrypt
  • path_to_php \ php.exe classic.php decrypt # Decrypt


Conclusion


If you are too lazy to collect all the code in pieces, then here it is in its entirety .

This code is not yet suitable for full encryption. There you can add a couple of dozen degrees of encryption: adding salt, compression, converting everything to Morse code or Chinese characters. It depends on the degree of your perversion of sophistication. I am working on it so that it can encrypt large files. Soon I want to start an encryption service.
That's all, see you on Habré! Check out my blog!

PS. When encrypting executable files, you get a beautiful picture:
XLiveAchiever.asi


MD5Inside.exe


PPS. Decrypt this message;)
Unscramble what is written here;)