Skip to main content

PHP Encryption Class

PS_Encrypt is a simple PHP encryption class to encrypt/decrypt data. You can use this class in my login script to encode/decode passwords. Click here to download.

Main features:

  1. Simple to implement, easy to understand and customize
  2. UsesĀ Rijndael 256 bit encryption
  3. Adds additional noise to the encoded data for more security

Usage Instructions

Include the encryption class in you application

The first step is to include the ps_encrypt.php file in your script.

//Include the PS_Encrypt class

Set the encryption key

Create a PS_Encrypt object and set the key with which your data will be encrypted.

$encrypt = new PS_Encrypt();

//Set the encryption key. The key must remain same for your application.

Important: The key must remain the same throughout your application. You can not change the key once the data has been encrypted. If you change the encryption key, you won’t be able decrypt any data you might have encrypted previously.

Encrypt data

$encrypted_data = $encrypt->encrypt('secret_password');
//You can now store the $encrypted_data to your database

Decrypt data

$decrypted_data = $encrypt->decrypt($encrypted_data);
echo $decrypted_data; //Will output "secret_password"

Note: This class uses mcrypt library. Make sure that your PHP setup has mcrypt enabled otherwise this class won’t work.

6 thoughts to “PHP Encryption Class”

  1. I’d like to use this code as the information I am going to be storing is going to be very sensitive and I want the passwords to be as protected as possible.
    I’m having issues trying to understand your code. How does it work in conjunction with your login script? Also is there away to prevent the user from logging in till an administrator has granted them access?

    1. This class is not restricted to my login script. It provides generic encryption/decryption functionality and can be used in any PHP project. My login script will need to be modified a bit to be able to use this class. I will try to come up with an updated login script whenever I get some time away from work.

  2. Hi, First of all let me thank for your great work with this and other scripts.

    I’m having a problem in the decrypt process.

    I keep getting this Warning:

    Warning: mcrypt_decrypt() [function.mcrypt-decrypt]: The IV parameter must be as long as the blocksize

    Can you give me some help with this?

    Best Regards

Comments are closed.