PDA

View Full Version : PHP Help



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);

?>

tesco
06-23-2009, 12:45 AM
umm, it works fine here. :lol:
http://filesharingtalk.com/test2.php


What is it you think is wrong with it?


Only thing I noticed is that it adds a period (.) to the end of the message if it's an even number of characters, but doesn't remove it after decrypting...

Anyway, this is some nice work you did. Not secure but defiantly good enough for some little projects.
Mind if I hold onto it and maybe use it one day?

xuxoxux
06-23-2009, 04:15 AM
Humm....I found my fix by doing the following:



$key_a = (int)$key_str[0];
$key_b = (int)$key_str[1];
$key_c = (int)$key_str[2];
$key_d = (int)$key_str[3];


Oh well. To do the encryption, you needed to have an even number of letters of the message. So i decided it would be a period, though a space would be more appropriate.

Go ahead and store/use/redistribute, as long as you give me a little credit. :P

tesco
06-23-2009, 10:13 PM
I see.
What version of PHP were you testing this on? Because it worked fine on this server, no errors, with php5.2


To do the encryption, you needed to have an even number of letters of the message. So i decided it would be a period, though a space would be more appropriate. You should program it to remove that added period.;)

xuxoxux
06-24-2009, 05:57 AM
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

/**
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.