Pages

Sunday, June 3, 2012

MySQL data Encryption and Decryption using AES_ENCRYPT and AES_DECRYPT

Encryption and Decryption using AES_ENCRYPT and AES_DECRYPT

First create a table called test_encdec :

mysql> create table test_encdec(

 -> name varchar(30),

 -> password varbinary(150)

 -> );

Query OK, 0 rows affected (0.14 sec)

mysql> desc test_encdec;

+----------+----------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+----------+----------------+------+-----+---------+-------+

| name | varchar(30) | YES | | NULL | |

| password | varbinary(150) | YES | | NULL | |

+----------+----------------+------+-----+---------+-------+

2 rows in set (0.00 sec)
 mysql> insert into  test_encdec values('S L N V Praveen',aes_encrypt('praveen','
myname'));
Query OK, 1 row affected (0.01 sec)

mysql> select * from  test_encdec;
+-----------------+------------------+
| name            | password         |
+-----------------+------------------+
| S L N V Praveen | 8#sTyn ]7?รป¬+W¬2 |
+-----------------+------------------+
1 row in set (0.02 sec)

mysql> select name,aes_decrypt(password,'myname') from  test_encdec;
+-----------------+--------------------------------+
| name            | aes_decrypt(password,'myname')     |
+-----------------+--------------------------------+
| S L N V Praveen | praveen                                   |
+-----------------+--------------------------------+
1 row in set (0.00 sec)

mysql> select aes_decrypt(password,'myname') from  test_encdec;
+--------------------------------+
| aes_decrypt(password,'myname') |
+--------------------------------+
| praveen                        |
+--------------------------------+
1 row in set (0.00 sec)

No comments:

Post a Comment