Two way Encryption Example

By sean on Sep 08, 2008

simple encryption example. output is the sum of 2 and the ascii value of each character.
for example, below is listed a string of characters and their ascii value.
s = 115, e = 101, a = 97, n = 110

this snippet will take the ascii value and add 2 to it. the output would be ( within an array ):
s = 117, e = 103, a = 99, n = 112

finially, the snippet implode()'s the output into a string separated by character 46 ('.'). you can change the operation ('+') and the key ('2') to your desire for security and functionality purposes.


printf('encrypted: %s<br />decrypted: %s', _enc($s), _dec(_enc($s)));
function _enc($s)
    for( $i = 0; $i < strlen($s); $i++ )
        $r[] = ord($s[$i]) + 2;
    return implode('.', $r);

function _dec($s)
    $s = explode(".", $s);
    for( $i = 0; $i < count($s); $i++ )
        $s[$i] = chr($s[$i] - 2);
    return implode('', $s);


Sign in to comment.
Zmodem   -  Sep 09, 2008

XAMPP is my favorite.

Hawkee   -  Sep 09, 2008

Or you could use XAMPP or Apache2Triad to get Apache/PHP/mySQL running on Windows in just a few minutes.

sean   -  Sep 09, 2008

i believe you mean plus you would need to install php as well

jonesy44   -  Sep 09, 2008 :P

or host.. then run the function

sean   -  Sep 09, 2008

well it's not that simple. whatever your testing on, whether it be your host or locally, needs to have php installed. then before using the provided example, you would need to define $s with the string you wanted to encrypt; hence: _enc($s), _dec(_enc($s))

Lindrian   -  Sep 09, 2008

Create a file called "test.php" and put the code in it, aswell as the example, and open it.

EL   -  Sep 09, 2008

ugh sounds cool just wish i could test this as easy as mirc codes :(.`-.-´

sean   -  Sep 09, 2008

thank you Hawkee.
i think everything should be 'up to standard' now :P

Hawkee   -  Sep 08, 2008

Good example of a very basic two-way encryption function. Although the key, "2", could be elaborated on a bit more to make this more flexible and secure.

Also, I think a title more like "Two way Encryption Example" would be more appropriate for this snippet.

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.