Simple PHP Encrypt Decrypt Functions

Today I want to share small custom PHP encryption and custom PHP decryption Functions.PHP has a long list of encoding and decoding functions. Examples are md5, sha1 etc. But these functions only encode and do not decode any text. So here is a small function that can encode and also decode it. Its important sometimes for security and transferring sensitive data in a website.

There PHP Encrypt & Decrypt functions can be used in various different ways.

  • Encrypt & Decrypt passwords.
  • Encrypt/Decrypt data stored in cookies.
  • Secure data passed with post method from one PHP file to another.
  • Securing data passed through AJAX calls.
  • There are many implications of these functions depending upon the situation.

 

In the above code function simple_encrypt will be used to encrypt any data on server before displaying data to user. And function simple_decrypt will decrypt data which was encrypted using simple_encrypt function.

The $salt = “earlysandwich.com”  which is used as parameter in both functions means, if salt is provided when function called then use that salt else by default “earlysandwich.com” will be the salt. See the below example code for explanation.

In above code you can see that first I have included the functions.php file. After that I have encoded the text and provided a custom salt to replace the default salt. Salt is “secured” in Line 6.

After that using the decoding function I have decoded the encoded text using the same salt on Line 12.

Hope this tutorial will help you to make your PHP scripts secure in many different ways. Download the project files below.

3 Replies to “Simple PHP Encrypt Decrypt Functions”

  1. jas says:

    short n sweet. thank you very much, it really works.

  2. Rita Patel says:

    This encryption and decryption is URL safe ?
    I want to Like protecting the IDs from getting exposed via GET ….

  3. Cuperto says:

    Spent an hour testing various functions around and finally searched for “simple php function encode decode” and found your nice and simple function;

    Thanks for sharing

    You made my day 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

scroll to top