📜 ⬆️ ⬇️

Encrypt files in black and white images.

Hi% user%! One boring evening I wanted to nakodit something. The text encoder is the first thing that came to my mind. But when he was ready, it turned out that he could encrypt any files! In this example, there is a limit on the file size. Files weighing more than 200 kb should not be fed to him. We will write the script itself 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.
Decrypt: read the file, determine the color of each pixel, get 0 and 1, get the text from them and write it to the file.

Let's get started!


To begin with, we will write some office 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 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, does the opposite.

Now the main thing - we encrypt and decrypt. I will not explain, since the code is already well commented.

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');

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

//// Simple code to calculate the height and width of the picture
$ l = strlen ( $ e );
$ h = floor ( sqrt ( $ l ));
while ( $ l % $ h ! == 0 ) $ h - ;
$ w = $ l / $ h ;
// Split 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 ); // Determine the black color
$ white = imagecolorallocate ( $ image , 255 , 255 , 255 ); //White
foreach ( $ wordDot as $ val )
{
// If 0, then we pick a 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 );


Decrypt

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 number of pixels in it (S = a * b)
$ wh = $ w * $ h ;
$ x = 0 ;
$ y = 0 ;

for ( $ t = 0 ; $ t < $ wh ; $ t ++ )
{
// Determine 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 ++ ;
}
// Reverse and convert to text
$ dt = ascii_decrypt ( strrev ( $ binary ));
// Write to file
file_put_contents ( str_replace ( '.png' , '' , $ fly ), $ dt );


Launch


You must have a PHP interpreter installed.
Linux (Unix):

Windows:


Conclusion


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

This code is not yet suitable for full encryption. There you can add a couple more tens of encryption levels: adding salt, compressing, converting everything into Morse code or Chinese characters. It depends on your degree of perversion of sophistication. I am working to enable it to encrypt large files. Soon I want to start the encryption service.
That's all, see you on Habré! Drop by my blog!

Ps. When encrypting executable files, a beautiful picture is obtained:
XLiveAchiever.asi


MD5Inside.exe


Pps. Decipher this message;)
,    ;)

Source: https://habr.com/ru/post/116761/


All Articles