Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2016 July MySQL: Password Encryption using the Advanced Encryption Standard Algorithm (AES_ENCRYPT())

MySQL: Password Encryption using the Advanced Encryption Standard Algorithm (AES_ENCRYPT())

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

This is going to be one of most important article for the MySQL community because I am going to share, what should be our best practice to store encrypted passwords into MySQL Database Server.

The MySQL provides different algorithm and function to encrypt and decrypt password data or any other sensitive information so that no one can access it in plain text format.

For different Encryption and Compression functions you can visit this MySQL Developer official site.

Generally, people are using MD5 and SHA algorithm for password encryption, but both are easy to break and vulnerable, so we should not use this in our general practice.

We should also not use PASSWORD() function because it is used by the authentication system in MySQL Server.

Advanced Encryption Standard Algorithm (AES):

This is one of the important encryption algorithm and it is highly secure because it encrypts the string using the encryption key string and returns an encrypted binary string output.

MySQL provides AES_ENCRYPT() to encrypt the string in binary format and AES_DECRYPT() to decrypt the string in plain text.

The only one problem is, we should hide the key value for security purpose by setting object level permission or we can create a view to hide the encryption key value.

You can create BINARY or BLOB data type to store AES encrypted password.

Below is a small demonstration on this:

The syntax:

1
2
AES_ENCRYPT(str,key_str)
AES_DECRYPT(crypt_str,key_str)

First, create a table with sample data:

1
2
3
4
5
6
7
8
9
10
CREATE TABLE tbl_AESPassword
(
ID INTEGER
,UserPassword BLOB
);
 
INSERT INTO tbl_AESPassword
VALUES
(1,AES_ENCRYPT('Anvesh','8'))
,(2,AES_ENCRYPT('Patel','88'));

SELECT first row by comparing password using defined encryption key:

1
2
3
4
5
6
7
8
SELECT *
FROM tbl_AESPassword
WHERE UserPassword = AES_ENCRYPT('Anvesh','8')
 
-- Result: Returned only first record.
ID | UserPassword
---------------------------
1 | BLOB

To DECRYPT the password into plain text:

1
2
3
4
5
6
7
8
mysql> SELECT AES_DECRYPT(AES_ENCRYPT('Anvesh','8'),'8');
 
+--------------------------------------------+
| AES_DECRYPT(AES_ENCRYPT('Anvesh','8'),'8') |
+--------------------------------------------+
| Anvesh |
+--------------------------------------------+
1 row in set (0.00 sec)

Jul 26, 2016Anvesh Patel
PostgreSQL: Should we create Multiple Databases OR create Multiple Schemas?MySQL: Choose Data type and length of the Secure Hash Algorithm (SHA)
Anvesh Patel
Anvesh Patel

Database Engineer

July 26, 2016 MySQLAES, AES_DECRYPT, AES_ENCRYPT, Anvesh Patel, database, database research and development, dbrnd, MySQL, MySQL Command, MySQL Database Administrator, MySQL Database Designing, MySQL Database Programming, MySQL Error, MySQL Performance Tunning, MySQL Query, MySQL Tips and Tricks, Password, password encryption
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....