PHP Encryption and Decryption cypher

I set myself a chalange last year (purley for fun) to write an encryption cypher without using someone elses code or without using the same methods. After researching the different types of encryption cyphers that are out there, I decided to go with a Symmetric Encryption cypher.

Symmetric Encryption cyphers basicaly use a key (word, phrase or string) to encrypt and decrypt the input and output data.

The code that I have wrote below is by no means bullet proof or useable in every environment (talking along the lines of encoding and characters).

Im not going to write much more on this as the code is not fully tested but does work I asure you. The code will encrypt and decrypt lower case, upper case, numerical and all special characters found on an english/american key pad.

<?php

header('Content-Type: text/html; charset=utf-8');
function ACT_encrypt_cipher($str, $key){
    if( is_string($str) && is_string($key) ) {
        $str = mb_convert_encoding($str, 'HTML-ENTITIES', "UTF-8");
        $sl = strlen($str); $kl = strlen($key);
        $fk = str_split(str_repeat($key, ceil($sl/$kl)));
        $es = ' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789&;:[]<>!"$%^*_-=+.,/\\()@?\'`';
        $ea = array( 'a' => array(), 'm' => array(), 'c' => array());
        foreach( str_split( $es ) as $key => $char ) { $ea['a'][] = $key; $ea['m'][] = md5($char); $ea['c'][] = $char; }
        $ns = '';
        $sp = str_split($str);
        foreach( $fk as $k => $_char ) {
            if( ! isset( $sp[$k] ) ) break;
            $cm = md5($sp[$k]);
            if( in_array( $cm, $ea['m'] ) ) {
                $kk = array_keys( $ea['m'], $cm );
                if( in_array( md5($_char), $ea['m'] ) ) {
                    $sk = array_keys( $ea['m'], md5($_char) );
                    $n = $ea['a'][$kk[0]] - $ea['a'][$sk[0]];
                    if( $n < 0 )
                        $n = $n + (strlen($es));
                    $ns .= $es[$n];
                }else
                    $ns .= $_char;
            }else
                $ns .= $_char;
        }
        return $ns;
    }
}
function ACT_decrypt_cipher($str, $key){
    if( is_string($str) && is_string($key) ) {
        $str = mb_convert_encoding($str, 'HTML-ENTITIES', "UTF-8");
        $sl = strlen($str); $kl = strlen($key);
        $fk = str_split(str_repeat($key, ceil($sl/$kl)));
        $es = ' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789&;:[]<>!"$%^*_-=+.,/\\()@?\'`';
        $ea = array( 'a' => array(), 'm' => array(), 'c' => array());
        foreach( str_split( $es ) as $key => $char ) { $ea['a'][] = $key; $ea['m'][] = md5($char); $ea['c'][] = $char; }
        $ns = '';
        $sp = str_split($str);
        foreach( $fk as $k => $_char ) {
            if( ! isset( $sp[$k] ) ) break;
            $cm = md5($sp[$k]);
            if( in_array( $cm, $ea['m'] ) ) {
                $kk = array_keys( $ea['m'], $cm );
                if( in_array( md5($_char), $ea['m'] ) ) {
                    $sk = array_keys( $ea['m'], md5($_char) );
                    $n = $ea['a'][$kk[0]] + $ea['a'][$sk[0]];
                    if( $n > (strlen($es)-1) )
                        $n = $n - strlen($es);
                    $ns .= $es[$n];
                }else
                    $ns .= $_char;
            }else
                $ns .= $_char;
        }
        return $ns;
    }
} 

$keyd = 's3cr3t';
$str = 'Me `and` my "email" [foo@example.co.uk], website (http://www.example.co.uk) went 4 a\ walk WHICH I might add cost \'£0.00\' (or $0.00?).';
$en = ACT_encrypt_cipher($str, $keyd);
$de = ACT_decrypt_cipher($en, $keyd);

echo "<strong>String:</strong>     $str<br />";
echo "<strong>Encrypted:</strong>  " . htmlentities($en) . "<br />";
echo "<strong>Decrypted:</strong>  $de";

?>

The output would be:

String:     Me `and` my "email" [foo@example.co.uk], website (http://www.example.co.uk) went 4 a\ walk WHICH I might add cost '£0.00' (or $0.00?).
Encrypted: tM?"I(*G?)6!ZMj%Q/ZH&=W)]Mu%U@\M-*W7bS;&Hc_Jp,1*"Beb1@Tz+e4c8Mu%U@\M-*W7bS/$4*)1?MH";Ht%T,"\Eq&n">?)Q-+1?%L^"Kla1!>gx*Q'^h7'Wa)L8IxGHF?[W'"pX9?G<C-
Decrypted: Me `and` my "email" [foo@example.co.uk], website (http://www.example.co.uk) went 4 a\ walk WHICH I might add cost '£0.00' (or $0.00?).

This article was written by Luke Snowden

Follow Luke Snowden on Twitter

Befriend Luke Snowden on Facebook

All context and code written within this article are the property of Luke Snowden, unless stated otherwise in which credit will be given.

This entry was posted in General. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy this password:

* Type or paste password here:

2,145 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">