I have come across a few petitions from some of you readers, and I will try to accomplish all of your requests as soon as possible. The first here is a class that I have built based on some ecnryption and decryption functions that I have used in the past. These functions can be found here: http://www.webmasterworld.com/php/3086138.htm?highlight=msg3086437
I took the liberty to create a class so that it can be downloaded by you guys. The use is simple and I can explain how to use it. Just include the class file and create a new object from it:
Usage
<?php
include_once('jtcrypt.php');
$enc = new jtcrypt();
?>
In order to encrypt or decrypt, we will need to send in the the string to process, and a passphrase called the key. This key is to make your encryption/decryption process unique to your website. The way to do it is as follows:
<?php
$pass = $_POST['password'];
$key = 'SomeKeyThatYouWant';
$enc_pass = $enc->encrypt($pass, $key);
$dec_pass = $enc->decrypt($enc_pass, $key);
echo 'The decrypted password is :' .$dec_pass.'<br />';
?>
Before Using!!
Make sure you initialize the the variables within the class before you encrypt or decrypt. This is to prevent errors.
<?php
$enc->init_values();
?>
Below I have the full code for the class. I will soon put it up on github so that it will be available for downloaded.
<?php
class jtcrypt{
private $result;
private $count;
public function __construct(){
$this->init_values();
}
public function encrypt($string, $key) {
for($i=0; $i<strlen($string); $i++) {
$char = substr($string, $i, 1);
$keychar = substr($key, ($i % strlen($key))-1, 1);
$char = chr(ord($char)+ord($keychar));
$this->result.=$char;
}
return base64_encode($this->result);
}
public function decrypt($string, $key) {
$string = base64_decode($string);
for($i=0; $i<strlen($string); $i++) {
$char = substr($string, $i, 1);
$keychar = substr($key, ($i % strlen($key))-1, 1);
$char = chr(ord($char)-ord($keychar));
$this->result.=$char;
}
return $this->result;
}
public function init_values(){
$this->result = '';
$this->count = 0;
}
}
?>