I found that there is no good encryption/decryption functions (with keys) built into php, so I decided to put together my own. I got some feedback on another forum, which helped me improve it and now, even my hacker friend on there can't decrypt it. My only worries are that it is bloated, and I could speed it up with losing security. Anyway, here is how to use it:
$crypt = new crypt; //sets up an instance of the class
$crypt->crypt_key('test_key'); //assigns an encryption key to the instance
$encrypted = $crypt->encode('test_data'); //encrypts the data using the key
$decrypted = $crypt->decode($encrypted); //decrypts the data using the key
The great thing about it is how easy it is to use it. It is quite secure though, and should hopefully baffle any hacker, whether they access to the source code or not. It uses base64 alot, generally to expand the key, as well as to widen the output from md5 and sha1 to a larger 64 set. At the end of the encrypt function and at the start of the decrypt function there is base64 , this is to make sure the encrypted text contains only general characters. I also use base64 before encrypting, this is to try to prevent any sort of dictionary attack on the data.
class crypt {
var $keys;
function crypt_key($ckey){
$this->keys = array();
$c_key = base64_encode(sha1(md5($ckey)));
$c_key = substr($c_key, 0, round(ord($ckey{0})/5));
$c2_key = base64_encode(md5(sha1($ckey)));
$last = strlen($ckey) - 1;
$c2_key = substr($c2_key, 1, round(ord($ckey{$last})/7));
$c3_key = base64_encode(sha1(md5($c_key).md5($c2_key)));
$mid = round($last/2);
$c3_key = substr($c3_key, 1, round(ord($ckey{$mid})/9));
$c_key = $c_key.$c2_key.$c3_key;
$c_key = base64_encode($c_key);
for($i = 0; $i < strlen($c_key); $i++){
$this->keys[] = $c_key[$i];
}
}
function encrypt($string){
$string = base64_encode($string);
$keys = $this->keys;
for($i = 0; $i < strlen($string); $i++){
$id = $i % count($keys);
$ord = ord($string{$i});
$ord = $ord OR ord($keys[$id]);
$id++;
$ord = $ord AND ord($keys[$id]);
$id++;
$ord = $ord XOR ord($keys[$id]);
$id++;
$ord = $ord + ord($keys[$id]);
$string{$i} = chr($ord);
}
return base64_encode($string);
}
function decrypt($string){
$string = base64_decode($string);
$keys = $this->keys;
for($i = 0; $i < strlen($string); $i++){
$id = $i % count($keys);
$ord = ord($string{$i});
$ord = $ord XOR ord($keys[$id]);
$id++;
$ord = $ord AND ord($keys[$id]);
$id++;
$ord = $ord OR ord($keys[$id]);
$id++;
$ord = $ord - ord($keys[$id]);
$string{$i} = chr($ord);
}
return base64_decode($string);
}
}
Anyway, what do you think??