關於 PHP, Linux, Open Source 及個人生活記載的網誌。
RSS icon
  • MySQL 編碼函式

    Posted on August 31st, 2007 Sam Tang No comments

    MySQL 內建提供了一些編碼函式,以下會介紹 ENCODE() 及 DECODE() 的作用及用法:

    ENCODE(str,pass_str)
    將字串 str 加密,並使用 "pass_str" 作為加密鑰匙,例如:

    mysql> select encode("testing string", "mykey");
    + – - – - – - – - – - – - – - – - – +
    | encode(“testing string”, “mykey”) |
    + – - – - – - – - – - – - – - – - – +
    | �k��y����ۙ�( |
    + – - – - – - – - – - – - – - – - – +
    1 row in set (0.00 sec)

     
    DECODE(crypt_str,pass_str)
    decode() 的作用就是對 encode() 加密的內容解碼,其中 crypt_str 是經過 encode() 加密的二進字串,而 pass_str 就是解密鑰匙。如果使用的鑰匙與加密時的鑰匙不同,回傳的結果便不會正確,用法如下:

    mysql> select decode(encode("testing string", "mykey"), "mykey");
    + – - – - – - – - – - – - – - – - – - – - – - – - – - +
    | decode(encode(“testing string”, “mykey”), “mykey”) |
    + – - – - – - – - – - – - – - – - – - – - – - – - – - +
    | testing string |
    + – - – - – - – - – - – - – - – - – - – - – - – - – - +
    1 row in set (0.01 sec)

    Leave a reply

    *
    To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
    Click to hear an audio file of the anti-spam word