Click here to Skip to main content
15,881,588 members
Articles / Programming Languages / PHP
Technical Blog

Encrypt – Decrypt Class

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
4 Nov 2012CPOL 10.9K   4  
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;
    }
}
?>
This article was originally posted at http://www.jotorres.com/2012/11/encrypt-decrypt-class

License

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


Written By
Software Developer
Puerto Rico Puerto Rico
As a computer scientist, I strive to learn more everyday in my field. Everything I learn, I share with my community by writing articles for future references.

Comments and Discussions

 
-- There are no messages in this forum --