白黒画像のファイルを暗号化します

Hi%user%! ある退屈な夜、私は何かをしたかった。 テキストエンコーダーは、私の頭に浮かんだ最初のものです。 しかし、彼が準備ができたとき、彼は任意のファイルを暗号化できることが判明しました! この例では、ファイルサイズの制限があります。 200 kbを超えるファイルはフィードしないでください。 スクリプトをPHPで作成します。


アイデア


暗号化:ファイルの内容を読み取り、0と1に変換し、数値をピクセルに変換してファイルに書き込みます。
復号化:ファイルを読み取り、各ピクセルの色を決定し、0と1を取得して、テキストを取得してファイルに書き込みます。

さあ始めましょう!


まず、いくつかのユーティリティ関数を作成します。
関数 readln (){
$ stdin = fopen'php:// stdin''r' );
return trimfread$ stdin2048 ));
}

関数 ascii_decrypt$ encryptedtext
{
$ encryptedtext = str_replace''''$ encryptedtext );
$ encryptedtext = str_replace" \ n "''$ encryptedtext );
$ encryptedtext = str_replace" \ r "''$ encryptedtext );

$ blockstack = str_split$ encryptedtext8 );
$ plaintext = "" ;
whilelist$ key$ val= each$ blockstack ))
{
$ plaintext 。= bin2ascii( $ val );
}
$ plaintextを 返します。
}

関数 ascii_encrypt$ plaintext
{
$ plaintextlength = strlen$ plaintext );
$ i = 0 ;
for$ x = 0 ; $ x < $ plaintextlength ; $ x ++
{
$ pt 。= ascii2bin( $ plaintext [ $ x ]);
// $ encryptedtext。= "";
}
$ ptを 返します。
}

関数 ascii2bin$ char
{
$ char = decbinord$ char ));
$ char = str_pad$ char8'0' 、STR_PAD_LEFT);
$ charを 返します。
}

関数 bin2ascii$ binary
{
$ char = substr$ binary$ x8 );
$ char = chrbindec$ char ));
$ charを 返します。
}


説明します。
readln()関数は、コマンドラインから入力テキストを読み取ります。 ascii_encrypt()はテキストを0と1に変換し、 ascii_decrypt()はその逆を行います。 ascii2bin()は 、文字を8個のゼロと1のシーケンスに変換し、もちろんbin2ascii()は、逆のアクションを実行します。

今、主なことは暗号化と復号化です。 コードはすでに十分にコメントされているため、説明しません。

暗号化する

echo "入力ファイル:" ;
$ fl = readln( $ fl );

iffile_exists$ fl )) $ file = stripslashesfile_get_contents$ fl )); else die"入力ファイルが見つかりません! \ n " );
// header( 'Content-Type:image / png');

//ファイルの内容をゼロと1に変換します...
//そして、何らかの理由でそれを裏返します)
$ e = strrev (ascii_encrypt( $ file ));

////画像の高さと幅を計算する簡単なコード
$ l = strlen$ e );
$ h = floorsqrt$ l ));
while$ l $ h !== 0$ h- ;
$ w = $ l / $ h ;
//一度に1つのキャラクター全体を壊します
$ wordDot = str_split$ e1 );

$ line = 0 ;
$列 = 0 ;

$ image = imagecreate$ w$ h );
$ black = imagecolorallocate$ image0、0、0 ); //黒色を定義します
$ white = imagecolorallocate$ image255、255、255 ); //白
foreach$ wordDot as $ val
{
// 0の場合、白色点を染色し、1の場合-黒
if$ val == 0imageline$ image$ column$ line$ column$ line$ white );
else imageline$ image$ column$ line$ column$ line$ black );
if$ column == $ w -1 ){ $ line ++ ; $列 = -1 ;}
$カラム ++ ;

}

//結果を保存します
imagepng$ image' ./'。$ fl '.png' );
imagedestroy$ image );


解読

echo "入力ファイル:" ;
$ fly = readln();
iffile_exists$ fly )) $ image = imagecreatefrompng$ fly ); それ以外の場合"ファイルが見つかりません! \ n " );

//画像の幅と高さを計算します
$ w = imagesx$ image );
$ h = imagesy$ image );
// Bはその中のピクセル数です(S = a * b)
$ 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' ] $カラー [ 'blue' ];
//白の場合、0を設定し、黒の場合-1
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_contentsstr_replace'.png'''$ fly )、 $ dt );


打ち上げ


PHPインタープリターがインストールされている必要があります。
Linux(Unix):

Windows:


おわりに


すべてのコードを断片的に収集するのが面倒なら、 ここにその全体があります。

このコードは、完全な暗号化にはまだ適していません。 そこで、 数十度の暗号化を追加できます。ソルトの追加、圧縮、すべてをモールス符号または漢字に変換します。 それはあなた巧妙さの倒錯の程度に依存します。 大きなファイルを暗号化できるように取り組んでいます。 すぐに暗号化サービスを開始したいです。
以上で、Habréでお会いしましょう! 私のブログをチェックしてください!

PS。 実行可能ファイルを暗号化すると、美しい画像が得られます。
XLiveAchiever.asi


MD5Inside.exe


PPS このメッセージを解読します;)
,    ;)

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


All Articles