How to Encrypt and Decrypt

Encryption and Decryption of the data is very important when it comes to security,also for programmers when they want to develop a Login system it is also very important for them to store data into the database in the encrypted format because if any one have access to the database cannot see the actual password of the users.

There are many ways to encrypt and decrypt data some does at the front end level by their own encryption algorithm, but here I will show you a very easy way to encrypt and decrypt data at the database level.

So for this purpose MySQL gives you some built functions which you can use so here I will show you Encryption using AES_ENCRYPT() and for Decryption AES_DECRYPT() functions.

Inserting Encrypted data into the table:

 

INSERT INTO userdata (username,pasword,encryptedpassword)VALUES (‘smith’,'htims’,AES_ENCRYPT(‘htims’,'key’));

In the above insert statement three fields i.e (username,pasword,encryptedpassword) are inserted in the table called “userdata” inthe values you can notice that at the third field AES_ENCRYPT() function is used and two parameters are passed to that function first one ‘htims’ is the text to be encrypted and the second one ‘key’ is the encryption key you can specify any key, but remember this key because this key will be used when you decrypt the data. Here is a screen shot of the table showing how data looks like in the table after insertion:

 

 

Inserted Record

 

Retrieving Decrypted data from the table:

SELECT username,pasword, AES_DECRYPT(encryptedpassword,’key’) FROM userdata ;

In the above select  statement data is retrieved from the table userdata, Here you can notice a function AES_DECRYPT() which is used for decrypting the encrypted data and it requires two parameters one is the text or the field to be decrypted and the key which you specify at the time of encryption where you used AES_ENCRYPT() function. Following is the screen shot of the select statement.

 

Selected Record

 

In the above screen shot you can see that the pasword and the decryptedpassword field is the same as you have decrypted your encrypted password.

WELL after so much demand for video demonstration following is the video demo,have look and don’t forget to give feed back

 

Tagged: , , , ,
  • Glen

    I always regard this type of information informative, as its good to know about it, but never to put it to use for a production site.

    Passwords should always been encrypted one-way, no one should ever been able to decrypt them using any DB or script function, with that being said the likes of allowing the encryption and decryption of e-mail address would be the only thing i would say use this for as that extra security if some takes control of your database is good practice to help keep the hijacking and spaming down.

    Any other details like credit card details, etc should be handled by companies that are designed to handle that kind of stuff, this removes liability from you in the event of a hacked database… and your users will thank you for it if it ever does happen.

    • Syed Asad Ahmed

      Thanks for the appreciation and I totally agree with you :)

      • ROCKESH RONITH

        hello !

        i have encryption password which is encrypted by md5() function in php….But still i m looking for how can i decrypt the password……

        Anyone is here who can help me.

  • http://siznews.com Azeem

    Great article good job :D

  • http://www.zeeshansaeed.com Zeeshan Saeed

    A very useful and handy post.

  • Hector Tello

    good job

    • http://codereflex.net Syed Asad Ahmed

      Thanks Hector Tello :)

  • Kunalblr

    It didnt worked for me :(

    mysql> select host,user,password,AES_DECRYPT(‘encryptedpassword’,'key’) from user;+———–+——+——————————————-+—————————————-+| host      | user | password                                  | AES_DECRYPT(‘encryptedpassword’,'key’) |+———–+——+——————————————-+—————————————-+| localhost | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | NULL                        |+———–+——+——————————————-+—————————————-+1 row in set (0.00 sec)

    • SyedAsadAhmed

      Please describe your task in detail.so that it can sort out

  • http://www.facebook.com/cathryz Cathy Ravello

    can you tell me what is the use of the $key?,. im a newbie,. So i really dont know it,. any anSwer would be appreciated,.. tnX!!

    • SyedAsadAhmed

      Well the $key is a set of characters which you will use while decrypting your text the key that was used in the encryption.For complete demonstration see the video in this article.

      Hope this will help you Cathy Ravello.

  • Jaime

    HELLO I JUST WANT TO ASK WHAT TYPE OF DATATYPE YOU USE IN YOUR ENCRYPTEDPASSWORD AND WHEN I TRY YOUR VIDEO TUTORIAL ITS THE DECRYPTED PASSWORD RESULT IS NULL?

    • SyedAsadAhmed

      @Jaime the type of “ENCRYPTEDPASSWORD” is varchar,Can you send me your table for further investigation why you are getting NULL.

  • Jaime

    I just want to share a decrypt syntax i use is i.e.
    SELECT username,pasword, CAST(AES_DECRYPT(encryptedpassword,’key’) as char) FROM userdata

GetSocial