Depth analysis: MySQL encryption and decryption

There are several internal MySQL database encryption and decryption functions function


Type fields need to be encrypted is VARBINARY, BLOB storage type


AES_ENCRYPT (str, key): Returns the key with the key results of the use of the Advanced Encryption Standard encryption algorithm string str, calling the result is a binary string AES_ENCRYPT

AES_DECRYPT (str, key): Returns the key with the key results using Advanced Encryption Standard algorithm decryption string str

mysql> CREATE TABLE userdata(username VARCHAR(20),pwssword VARCHAR(50),encryptedpassword VARBINARY(100));

mysql> INSERT INTO userdata(username,pwssword,encryptedpassword) VALUES('smith','abdABC123',AES_ENCRYPT('abdABC123','key'));

mysql> SELECT * FROM userdata;

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

| username | pwssword  | encryptedpassword |

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

| smith    | abdABC123 | .|

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

1 row in set (0.01 sec)

mysql> SELECT username,pwssword,AES_DECRYPT(encryptedpassword,'key') FROM userdata;

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

| username | pwssword  | AES_DECRYPT(encryptedpassword,'key') |

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

| smith    | abdABC123 | abdABC123                            |

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

1 row in set (0.00 sec)


MD5 (str): MD5 encryption methods on str

mysql> SELECT MD5('abcABC123');

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

| MD5 ( 'abcABC123') |

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

| 480aeb42d7b1e3937fe8db12a1ffe6d8 |

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

1 row in set (0.00 sec)


SHA (str): SHA way to encrypt str

mysql> SELECT SHA('abcABC123');

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

| SHA ( 'abcABC123') |

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

| 0a6807c0856b137fb44ce239587e4f34e011b005 |

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

1 row in set (0.00 sec)


SHA1 (str): SHA1 encryption methods on str

mysql> SELECT SHA1('abcABC123');

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

| SHA1 ( 'abcABC123') |

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

| 0a6807c0856b137fb44ce239587e4f34e011b005 |

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

1 row in set (0.00 sec)


ENCODE (str, key): the string key as the secret key used to encrypt the string str, the encryption result is a binary number

DECODE (str, key): using a string key as the secret key to decrypt the string str

mysql> INSERT INTO userdata(username,pwssword,encryptedpassword) VALUES('KT','abdABC123',ENCODE('abdABC123','key'));

mysql> SELECT * FROM userdata WHERE username='KT';

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

| username | pwssword  | encryptedpassword |

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

| KT       | abdABC123 | 1       |

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

1 row in set (0.00 sec)

mysql> SELECT username,pwssword,DECODE(encryptedpassword,'key') FROM userdata WHERE username='KT';

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

| username | pwssword  | DECODE(encryptedpassword,'key') |

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

| KT       | abdABC123 | abdABC123                       |

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

1 row in set, 1 warning (0.00 sec)


ENCRYPT (str, salt): Use UNIXcrypt () function, using keywords salt (a password can only determine a string, like a key) encrypted string str

mysql> SELECT ENCRYPT('abdABC123','key');

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

| ENCRYPT('abdABC123','key') |

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

| kezazmcIo.aCw |

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

1 row in set, 1 warning (0.00 sec)


PASSWORD (str): Returns the string str encrypted version of the encryption process is irreversible, and UNIX password encryption process uses a different algorithm

mysql> INSERT INTO userdata(username,pwssword,encryptedpassword) VALUES('Tom','abdABC123',PASSWORD('abdABC123'));

mysql> SELECT * FROM userdata WHERE username='Tom';

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

| username | pwssword  | encryptedpassword                         |

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

| Tom      | abdABC123 | *6970C4ACB558CFFCAD8DE8DA17CDD40CFC023FDA |

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

1 row in set (0.00 sec)


mysql> SELECT PASSWORD('abcde');

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

| PASSWORD('abcde')                         |

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

| * 8DC54F2E15823C98AEA063E339A5D4C53D1A471A |

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

1 row in set, 1 warning (0.00 sec)


--------------------- 



Guess you like

Origin blog.51cto.com/14028890/2423460