PHP Code:
PHP Version 5.2.9-1
I have already taken the period issue and solved it after thinking about a solution for an hour.....( I am a slow thinker. )
PHP Code:
<?php
/**
Author: Eric ( xuxoxux )
*/
function matrix_encrypt($string) {
$a = rand(1,9);
$b = rand(1,9);
$c = rand(1,9);
$d = rand(1,9);
if ( strlen($string)&1 ) {
$string .= ".";
$period = 1;
} else {
$period = 0;
}
$str_array = str_split($string);
for ( $i=0; $i < strlen($string); $i++ ) {
$chr_array[$i] = ord( $str_array[$i] );
}
for ( $j=0; $j < strlen($string); ) {
$str_A = $chr_array[$j];
$str_B = $chr_array[$j+1];
$end_array[$j] = ( $a * $str_A ) + ( $b * $str_B );
$end_array[$j+1] = ( $c * $str_A ) + ( $d * $str_B );
$j += 2;
}
for ( $k=0; $k < strlen($string); $k++ ) {
$final .= $end_array[$k] . "-";
}
$key = $a . $b . $c . $d . $period;
$final .= $key;
return $final;
}
function matrix_decrypt($string) {
$enc_msg = explode("-", $string);
$len = count($enc_msg);
$key_s = $enc_msg[$len-1];
$key_str = str_split($key_s);
$key_a = (int)$key_str[0];
$key_b = (int)$key_str[1];
$key_c = (int)$key_str[2];
$key_d = (int)$key_str[3];
$Ia = $key_d * (1/($key_a*$key_d - $key_b*$key_c));
$Ib = $key_b * (-1/($key_a*$key_d - $key_b*$key_c));
$Ic = $key_c * (-1/($key_a*$key_d - $key_b*$key_c));
$Id = $key_a * (1/($key_a*$key_d - $key_b*$key_c));
array_pop($enc_msg);
for ( $j=0; $j <= count($enc_msg); ) {
$str_A = $enc_msg[$j];
$str_B = $enc_msg[$j+1];
$end_array[$j] = ( $Ia * $str_A ) + ( $Ib * $str_B );
$end_array[$j+1] = ( $Ic * $str_A ) + ( $Id * $str_B );
$j += 2;
}
for ( $i=0; $i < count($enc_msg); $i++ ) {
$chrs .= chr( round($end_array[$i]) );
}
if ( (int)$key_str[4] == 1 ) {
$chrs = substr($chrs, 0, -1);
}
return $chrs;
}
$enc = matrix_encrypt("Message!");
echo $enc;
echo "<br>";
echo matrix_decrypt($enc);
echo "<br><br>";
$enc = matrix_encrypt("Messages!");
echo $enc;
echo "<br>";
echo matrix_decrypt($enc);
?>
I simply append a number that will flag whether there needs to be a period removed or not.
Bookmarks