Use MySQL encryption functions to protect sensitive data on Web sites _ MySQL

Source: Internet
Author: User
Tags crypt
Using MySQL encryption functions to protect sensitive data on Web sites if you are running a Web application that uses MySQL, it is a great opportunity to store passwords or other sensitive information in the application. Protecting this data from access by hackers or prophers is an important concern, because you cannot allow unauthorized users to use or destroy applications, but also ensure your competitive advantage. Fortunately, MySQL comes with a lot of encryption functions designed to provide this type of security. This article outlines some of these functions and explains how to use them and how they provide different levels of security.

Bidirectional encryption

Let's start with the simplest encryption: two-way encryption. Here, a piece of data is encrypted by a key and can only be decrypted by the person who knows the key. MySQL has two functions to support this type of encryption: ENCODE () and DECODE (). The following is a simple example:

Mysql> insert into users (username, password) VALUES ('job', ENCODE ('essme', 'abracadaba '));
Query OK, 1 row affected (0.14 sec)

Among them, Joe's password is guessme, which is encrypted by the key abracadabra. Note that the encrypted result is a binary string, as shown below:

Mysql> SELECT * FROM users WHERE username = 'job ';
+ ---------- +
| Username | password |
+ ---------- +
| Joe | ?? I ??!? |
+ ---------- +
1 row in set (0.02 sec)

The abracadabra key is critical for restoring to the original string. This key must be passed to the DECODE () function to obtain the original unencrypted password. The following is how to use it:

Mysql> select decode (password, 'abracadaba') FROM users WHERE username = 'job ';
+ --------------------------------- +
| DECODE (password, 'abracadaba') |
+ --------------------------------- +
| Guessme |
+ --------------------------------- +
1 row in set (0.00 sec)

It should be easy to see how it runs in a Web application -- DECODE () uses a dedicated website key to unbind the password stored in the database when verifying the user's logon, and compare it with the content entered by the user. If you use PHP as your script language, you can query it as follows:

$ Query = "select count (*) FROM users WHERE username = '$ inputuser' and decode (password, 'abracadaba') =' $ inputpass'";?>

Note: Although the ENCODE () and DECODE () functions can meet most requirements, you may want to use more powerful encryption methods. In this case, you can use the AES_ENCRYPT () and AES_DECRYPT () functions. they work in the same way, but the encryption strength is higher.

Unidirectional encryption

One-way encryption is different from two-way encryption. Once data is encrypted, this process cannot be reversed. Therefore, password verification includes re-encryption of the user's input content, and comparing it with the saved ciphertext to see if it matches. A simple one-way encryption method is the MD5 verification code. MySQL's MD5 () function creates a "fingerprint" for your data and saves it for verification and testing. The following is a simple example of how to use it:

Mysql> insert into users (username, password) VALUES ('job', MD5 ('essme '));
Query OK, 1 row affected (0.00 sec)

Mysql> SELECT * FROM users WHERE username = 'job ';
+ ---------- + ---------------------------------- +
| Username | password |
+ ---------- + ---------------------------------- +
| Joe | 81a58e89df1f34c5487568e17327a219 |
+ ---------- + ---------------------------------- +
1 row in set (0.02 sec)

Now you can test whether the content entered by the user matches the saved password by obtaining the MD5 verification code and comparing it with the saved password, as shown below:

Mysql> select count (*) FROM users WHERE username = 'job' AND password = MD5 ('essme ');
+ ---------- +
| COUNT (*) |
+ ---------- +
| 1 |
+ ---------- +
1 row in set (0.00 sec)

Alternatively, consider using the ENCRYPT () function, which uses the system underlying crypt () system call to complete encryption. This function has two parameters: one is the string to be encrypted, and the other is the double (or multiple) character "salt ". It then encrypts the string with salt; this salt can then be used to encrypt the user's input again and compare it with the previously encrypted string. The following example shows how to use it:

Mysql> insert into users (username, password) VALUES ('job', ENCRYPT ('essme', 'AB '));
Query OK, 1 row affected (0.00 sec)

Mysql> SELECT * FROM users WHERE username = 'job ';
+ ---------- + --------------- +
| Username | password |
+ ---------- + --------------- +
| Joe | AB/G8gtZdMwak |
+ ---------- + --------------- +
1 row in set (0.00 sec)

The result is

Mysql> select count (*) FROM users WHERE username = 'job' AND password = ENCRYPT ('essme', 'AB ');
+ ---------- +
| COUNT (*) |
+ ---------- +
| 1 |
+ ---------- +
1 row in set (0.00 sec)

Note: ENCRYPT () can only be used on * NIX systems because it uses the underlying crypt () library.

Fortunately, the above example shows how to use MySQL to encrypt your data one-way and two-way, I also told you some ideas about how to protect the information security of databases and other sensitive databases. Happy programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.