#!/usr/bin/php
<?php
# KSEncriptor, (c) Kolesnikov Sergey, 2010#
error_reporting(0);
set_time_limit(0);
ini_set("memory_limit", "1024M");
$VERSION = '1.0';
if($argv[1] == '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');
//Содержимое файла преобразуем в нолики и единицы...
//И зачем-то переворачиваем)
$e = strrev(ascii_encrypt($file));
////Нехитрый код для вычисления высоты и ширины рисунка
$l = strlen($e);
$h = floor(sqrt($l));
while($l%$h !== 0) $h--;
$w = $l/$h;
//Разбиваем всю кашу по одному символу
$wordDot = str_split($e, 1);
$line = 0;
$column = 0;
$image = imagecreate($w,$h);
$black = imagecolorallocate($image,0,0,0);//Определяем черный цвет
$white = imagecolorallocate($image,255,255,255);//Белый
foreach($wordDot as $val)
{
//Если 0, то русуем белую точку, а если 1 - черную
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++;
}
//Сохраняем результат
imagepng($image,'./'.$fl.'.png');
imagedestroy($image);
}
elseif($argv[1] == 'decrypt' )
{
echo "Input file: ";
$fly = readln();
if(file_exists($fly)) $image = imagecreatefrompng($fly); else die("File not found!\n");
$w = imagesx($image);
$h = imagesy($image);
$wh = $w*$h;
$x = 0;
$y = 0;
for($t=0; $t < $wh; $t++)
{
$rgb = imagecolorat($image, $x, $y);
$colors = imagecolorsforindex($image, $rgb);
$cl = $colors['red'].$colors['green'].$colors['blue'];
if($cl == '255255255') $binary .= '0';
if($cl == '000') $binary .= '1';
if($x == $w-1) {$y++; $x=-1;}
$x++;
}
$dt = ascii_decrypt(strrev($binary));
file_put_contents(str_replace('.png', '', $fly), $dt);
}
else exit("encrypt OR decrypt?\n");
echo "OK!\n";
#----
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;
}
?>