Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / PHP

Encrypt – Decrypt Class

0.00/5 (No votes)
4 Nov 2012CPOL 11.1K  
An encrypt-decrypyt class.

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
<?php
    /* Including encrypt class file */
    include_once('jtcrypt.php');
    /* Create a new object */
    $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
<?php 
    /* Encrypt a password */
    $pass = $_POST['password'];
    $key = 'SomeKeyThatYouWant';
    $enc_pass = $enc->encrypt($pass, $key);
    
    /* Do whatever you need to do with the encrypted phrase */
    // ..
    // ..
    
    /* Decrypt password */
    $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
<?php
    /* Initialize variable data */
    $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
<?php 
/**
* Author: Jorge Torres - jotorres1@gmail.com
* Website: http://www.jotorres.com
* Source Website: http://www.webmasterworld.com/php/3086138.htm?highlight=msg3086437
* Date: 10/17/2012
* Version: 1.0
* Description: An encryption and decryption class for small projects
* Documentation: http://www.jotorres.com/
*/

class jtcrypt{
    
    /**
     * @access private
     * @var string 
     */
    private $result;
    /**
     * @access private
     * @var int 
     */
    private $count;
        
    public function __construct(){
        // Initial values
        $this->init_values();
    }
    
    /**
    * Function to encrypt
    */
    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);
    }
    
    /**
    * Function to decrypt
    */
    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;
    }
    
    /**
    * Initialize values
    */
    public function init_values(){
        $this->result = '';
        $this->count = 0;
    }
}
?>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)