xuxoxux
06-23-2009, 12:06 AM
Ok, So I am making a matrix encryption script. I know it is not the most secure, but for what I need it, it is sufficient.
The encryption part works just fine.....
The problem is that I am needing to also later decrypt the message...lol.
I dont know what is stopping this from working. Any help would be much appreciated.
I know for a fact the math is 100% correct. There must be some little PHP error in there that I am just constantly missing.
I am asking for syntax help.
Script ( matrix.php ):
<?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 .= ".";
}
$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;
$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 = $key_str[0];
$key_b = $key_str[1];
$key_c = $key_str[2];
$key_d = $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]) );
}
return $chrs;
}
$enc = matrix_encrypt("Message");
echo $enc;
echo "<br><br>";
echo matrix_decrypt($enc);
?>
The encryption part works just fine.....
The problem is that I am needing to also later decrypt the message...lol.
I dont know what is stopping this from working. Any help would be much appreciated.
I know for a fact the math is 100% correct. There must be some little PHP error in there that I am just constantly missing.
I am asking for syntax help.
Script ( matrix.php ):
<?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 .= ".";
}
$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;
$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 = $key_str[0];
$key_b = $key_str[1];
$key_c = $key_str[2];
$key_d = $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]) );
}
return $chrs;
}
$enc = matrix_encrypt("Message");
echo $enc;
echo "<br><br>";
echo matrix_decrypt($enc);
?>