MySQL encryption and decryption functions

Source: Internet
Author: User

There are several encryption functions in MySQL:

Password (): creates an encrypted password string, which is suitable for the security system inserted into MySQL.
System. This encryption process is irreversible and uses different algorithms than the UNIX password encryption process. It is mainly used for the authentication system of MySQL.
 
Encrypt (,): uses the Unix crypt () system to encrypt the string. The encrypt () function receives the string to be encrypted and (optional) the salt used for the encryption process (a string that can uniquely identify the password, just like a key). Note that it is not supported on Windows.

Encode (,) decode (,): encrypts and decrypts strings. This function has two parameters: the encrypted or decrypted string and the key used as the basis for encryption or decryption. The encode result is a binary string stored as blob. Relatively weak encryption

MD5 (): Calculate the MD5 checksum of a string (128 bits)

Sha5 (): calculates the string's sha5 checksum (160 bits)

The Checksum returned by the above two functions is in hexadecimal format and is applicable to passwords used in the authentication system.

Aes_encrypt aes_decrypt example

Insert into users (TEST) values (aes_encrypt ('teststr', 'salt '));

Select aes_decrypt (test, 'salt') from users;

PS: Linux is required and aes_encrypt encryption results are best stored as blob

Encrypt select aes_encrypt (name, 'Password ');
Decrypt select aes_decrypt (aes_encrypt (name, 'Password'), 'Password ');
 
########################
How to use MySQL's aes_encrypt and aes_decrypt to store passwords in a database

Here's the scenario. you are building a custom member login area to a website. you need to store the user's name, email address and a password. the name and email can be stored in 'plain text', but for added security, you want to store the password in
Encrypted format (in case someone steals the database somehow, or just for your users 'peace of mind ).
This mini-tutorial assumes you already know how to connect to your database and work with PHP/MySQL.
The benefit of using aes_encrypt and aes_decrypt is that you can both encrypt the password, then decrypt the password whenever necessary. this is helpful if you ever want to display the password to the user in an email, or if you're encrypting other account
Information that you need to display.
View the code here.
1: The Key
For this to work, you must define a "key" to use when encrypting and decrypting the information from the database. it wocould be best to store this key somewhere on your server outside of the main directory in which you're working. this key can be whatever
You want it to be, but you must also reference the same key during encrypting and decryption.
$ Key = 'asksdfnsdfkeisdjahdldsdf1235uuiidfsdf ';
2: encrypt the password
Mysql_query ("insert into users (user_first, user_last, user_password) values ('". $ _ post ['first']. "','". $ _ post ['last']. "', aes_encrypt ($ _ post ['Password'], $ key ))");
3: decrypt the password
Now, to display the decrypted password, you'll need a query similar to the one below:
$ Password = mysql_fetch_row (mysql_query ("select aes_decrypt (user_password, '$ key') from users where user_id = 4 "));
Echo $ password [0];
So, using aes_encrypt and aes_decrypt can be very useful when you need to store encrypted information in a database as well as display the original, unencrypted information. remember, you must use a 'key' in order to "unlock" and display the encrypted information.

Please explain:
Select aes_encrypt ('secret data', 'key ')
Union
Select aes_decrypt ('601_eb1_ad9beae9aa057af49bdff5', 'key ')
Union
Select aes_decrypt (aes_encrypt ('secret data', 'key'), 'key ')

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.