MySQL 加密解密 詳解
阿新 • • 發佈:2019-02-19
MySQL 資料庫內部有多種 加密函式 解密函式被加密的欄位的型別需要是 VARBINARY、BLOB 型別 儲存AES_ENCRYPT(str,key) : 返回用金鑰key對字串str利用高階加密標準演算法加密後的結果,呼叫AES_ENCRYPT的結果是一個二進位制字串AES_DECRYPT(str,key) : 返回用金鑰key對字串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方式對 str 加密mysql> SELECT MD5('abcABC123'); +----------------------------------+| MD5('abcABC123') |+----------------------------------+| 480aeb42d7b1e3937fe8db12a1ffe6d8 |+----------------------------------+1 row in set (0.00 sec)SHA(str) : SHA方式對 str 加密mysql> SELECT SHA('abcABC123');+------------------------------------------+| SHA('abcABC123') |+------------------------------------------+| 0a6807c0856b137fb44ce239587e4f34e011b005 |+------------------------------------------+1 row in set (0.00 sec)SHA1(str) : SHA1方式對 str 加密mysql> SELECT SHA1('abcABC123');+------------------------------------------+| SHA1('abcABC123') |+------------------------------------------+| 0a6807c0856b137fb44ce239587e4f34e011b005 |+------------------------------------------+1 row in set (0.00 sec)ENCODE(str,key) : 使用字串 key 作為祕鑰來加密字串str,加密結果是二進位制數DECODE(str,key) : 使用字串 key 作為祕鑰來解密字串strmysql> 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) : 使用UNIXcrypt()函式,用關鍵詞salt(一個可以惟一確定口令的字串,就像鑰匙一樣)加密字串strmysql> SELECT ENCRYPT('abdABC123','key');+----------------------------+| ENCRYPT('abdABC123','key') |+----------------------------+| kezazmcIo.aCw |+----------------------------+1 row in set, 1 warning (0.00 sec)PASSWORD(str) : 返回字串str的加密版本,這個加密過程是不可逆轉的,和UNIX密碼加密過程使用不同的演算法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)