編輯:關於 Android 開發
從 MySQL 5.6 開始,官方推出了加密工具 mysql_config_editor。在此之前,我們通常把帳號與密碼以明文寫入 my.cnf,如此一來使用 mysql 客戶端登入時便無需再指定帳密。有了 mysql_config_editor 之後,我們改將加密後的帳密存入一個二進位檔案,登入時由客戶端解密該檔以完成連線。由於加解密都在記憶體中進行,因此無法明文顯示檔案內容;只要妥善保存檔案權限,就能有效防止有心人士破解資料庫密碼。
使用方式如下:
mysql_config_editor set --login-path=client --host=localhost --user=localuser --password
這就設定了一組本地資料源:login-path 指定登入時的識別標識、host 為要連線的資料庫、user 為連線所用帳號、password 為連線所用密碼(此處假設輸入 password1)。若需遠端連線,還要加上特定埠號資訊。設定完成後,登入時只需執行 mysql --login-path=client 即可連上該資料庫。
該工具包含 set/remove/print/reset/help 等功能,以下僅分析 set 功能的實作。set 由 set_command 函數完成,主要負責配置帳密等資料源資訊並存入二進位檔:
- static int set_command(void)
- {
- DBUG_ENTER("set_command");
- DYNAMIC_STRING file_buf, path_buf;
- init_dynamic_string(&path_buf, "", MY_LINE_MAX, MY_LINE_MAX);
- init_dynamic_string(&file_buf, "", file_size, 3 * MY_LINE_MAX);
- if (tty_password)
- opt_password= get_tty_password(NullS);
- if (file_size)
- {
- if (read_and_decrypt_file(&file_buf) == -1) //如果文件存在,就讀取文件,並將文件的密文解密後存放到file_buf中.
- goto error;
- }
- dynstr_append(&path_buf, "["); /* --login=path */
- if (opt_login_path)
- dynstr_append(&path_buf, opt_login_path);
- else
- dynstr_append(&path_buf, "client");
- dynstr_append(&path_buf, "]");
- if (opt_user) /* --user */
- {
- dynstr_append(&path_buf, "\nuser = ");
- dynstr_append(&path_buf, opt_user);
- }
- if (opt_password) /* --password */
- {
- dynstr_append(&path_buf, "\npassword = ");
- dynstr_append(&path_buf, opt_password);
- }
- if (opt_host) /* --host */
- {
- dynstr_append(&path_buf, "\nhost = ");
- dynstr_append(&path_buf, opt_host);
- }
- if (opt_socket)
- {
- dynstr_append(&path_buf, "\nsocket = ");
- dynstr_append(&path_buf, opt_socket);
- }
- if (opt_port)
- {
- dynstr_append(&path_buf, "\nport = ");
- dynstr_append(&path_buf, opt_port);
- }
- dynstr_append(&path_buf, "\n");
- /* Warn if login path already exists */
- if (opt_warn && ((locate_login_path (&file_buf, opt_login_path)) != NULL))
- {
- int choice;
- printf ("WARNING : \'%s\' path already exists and will be " "overwritten. \n Continue? (Press y|Y for Yes, any " "other key for No) : ", opt_login_path);
- choice= getchar();
- if (choice != (int) 'y' && choice != (int) 'Y') //如果login-path存在是否選擇覆蓋
- goto done; /* skip */
- }
- /* Remove the login path. */
- remove_login_path(&file_buf, opt_login_path); //從原來文件中讀取的內容中,刪掉該login-path信息
- /* Append the new login path to the file buffer. */
- dynstr_append(&file_buf, path_buf.str); //將該login-path的信息加到file_buf的末尾
- if (encrypt_and_write_file(&file_buf) == -1) //將包含新的log-path的所有信息和原來的信息加密寫入文件
- goto error;
- done:
- dynstr_free(&file_buf);
- dynstr_free(&path_buf);
- DBUG_RETURN(0);
- error:
- dynstr_free(&file_buf);
- dynstr_free(&path_buf);
- DBUG_RETURN(-1);
- }
其邏輯重點在於幾個關鍵函數:read_and_decrypt_file(讀取並解密檔案內容)、locate_login_path(判斷 login-path 是否存在)、remove_login_path(刪除既有 login-path)、dynstr_append(將新的 login-path 附加到末尾),以及 encrypt_and_write_file(加密後寫入檔案)。
先來看加密後的檔案格式:

由於加密檔的前 4 個 byte 為 '\0'(未使用),可跳過;緊接的 20 個 bytes 存放對稱加密演算法的秘鑰,這部分在 read_login_key 呼叫前已讀取,因此也可跳過。所以 read_and_decrypt_file 的過程如下:
- /*
- Header length for the login file.
- 4-byte (unused) + LOGIN_KEY_LEN
- */
- #define MY_LOGIN_HEADER_LEN (4 + LOGIN_KEY_LEN)
- static int read_and_decrypt_file(DYNAMIC_STRING *file_buf)
- {
- DBUG_ENTER("read_and_decrypt_file");
- char cipher[MY_LINE_MAX], plain[MY_LINE_MAX];
- uchar len_buf[MAX_CIPHER_STORE_LEN];
- int cipher_len= 0, dec_len= 0;
- /* Move past key first. */
- if (my_seek(g_fd, MY_LOGIN_HEADER_LEN, SEEK_SET, MYF(MY_WME)) != (MY_LOGIN_HEADER_LEN))
- goto error; /* Error while seeking. */
- /* First read the length of the cipher. */
- while (my_read(g_fd, len_buf, MAX_CIPHER_STORE_LEN, MYF(MY_WME)) == MAX_CIPHER_STORE_LEN)
- {
- cipher_len= sint4korr(len_buf); //將密文的長度轉換成整形
- if (cipher_len > MY_LINE_MAX)
- goto error;
- /* Now read 'cipher_len' bytes from the file. */
- if ((int) my_read(g_fd, (uchar *) cipher, cipher_len, MYF(MY_WME)) == cipher_len) //讀取相應密文長度的密文
- {
- if ((dec_len= decrypt_buffer(cipher, cipher_len, plain)) < 0) //解密該密文
- goto error;
- plain[dec_len]= 0;
- dynstr_append(file_buf, plain); //將解密後的密文追加到file_buf中
- }
- }
- verbose_msg("Successfully decrypted the login file.\n");
- DBUG_RETURN(0);
- error:
- my_perror("couldn't decrypt the file");
- DBUG_RETURN(-1);
- }
因此該函數本質上是以下四步的循環,直到檔案中所有密文都被解密:1. 取得密文長度;2. 依長度讀取密文;3. 解密;4. 將解密結果附加到 file_buf。函數中透過 sint4korr 轉換密文長度,是因為每個 cipher 由「4 bytes 長度 + cipher 字串」組成,int4store 把長度存在前 4 個 bytes,sint4korr 則反向還原:
- #define int4store(T,A) do { *((char *)(T))=(char) ((A));\
- *(((char *)(T))+1)=(char) (((A) >> 8));\
- *(((char *)(T))+2)=(char) (((A) >> 16));\
- *(((char *)(T))+3)=(char) (((A) >> 24));\
- } while(0)
- #define sint4korr(A) (int32) (((int32) ((uchar) (A)[0])) +\
- (((int32) ((uchar) (A)[1]) << 8)) +\
- (((int32) ((uchar) (A)[2]) << 16)) +\
- (((int32) ((int16) (A)[3]) << 24)))
接著看 locate_login_path 的實作:
- static char* locate_login_path(DYNAMIC_STRING *file_buf, const char *path_name)
- {
- DBUG_ENTER("locate_login_path");
- char *addr= NULL;
- DYNAMIC_STRING dy_path_name;
- init_dynamic_string(&dy_path_name, "", 512, 512); // 初始化dy_path_name動態字符串
- //將dy_path_name 設置為[path_name]
- dynstr_append(&dy_path_name, "\n[");
- dynstr_append(&dy_path_name, path_name);
- dynstr_append(&dy_path_name, "]");
- //檢查第一個login-path是否就是要尋找的login-path
- if (file_buf->str == strstr(file_buf->str, dy_path_name.str + 1))
- addr= file_buf->str;
- else
- {
- addr= strstr(file_buf->str, dy_path_name.str);
- if (addr)
- addr ++; /* Move past '\n' */
- }
- dynstr_free(&dy_path_name);
- DBUG_RETURN(addr); //返回找到的login-path在file_buf的首地址
- }
該函數負責判斷 login-path 是否已存在,若存在則回傳它在 file_buf 中的首地址。
若 login-path 已存在,我們可能選擇先移除再新增,接著看 remove_login_path 的實作:
- static void remove_login_path(DYNAMIC_STRING *file_buf, const char *path_name)
- {
- DBUG_ENTER("remove_login_path");
- char *start=NULL, *end= NULL;
- int to_move, len, diff;
- if((start= locate_login_path(file_buf, path_name)) == NULL) //如果該login-path不存在,直接結束
- goto done;
- end= strstr(start, "\n["); //end為從start開始尋找,下一個login-path的起始位置
- if (end) //如果該login-path是file_buf中間的某一個login-path
- {
- end ++; /* Move past '\n' */
- len= ((diff= (start - end)) > 0) ? diff : - diff;
- to_move= file_buf->length - (end - file_buf->str);
- }
- else //如果該login-path是該file_buf中最後一個log-path
- {
- *start= '\0';
- file_buf->length= ((diff= (file_buf->str - start)) > 0) ? diff : - diff;
- goto done;
- }
- while(to_move --) //將該login-path之後的login-path整體前移,覆蓋move掉的login-path
- *(start ++)= *(end ++);
- *start= '\0';
- file_buf->length -= len;
- done:
- DBUG_VOID_RETURN;
- }
該函數負責以「整體前移覆蓋」的方式刪除既有 login-path 的字串,再以 dynstr_append 把新的 login-path 附加到末尾。
最後來看最核心的加密函數 encrypt_and_write_file:
- static int encrypt_and_write_file(DYNAMIC_STRING *file_buf)
- {
- DBUG_ENTER("encrypt_and_write_file");
- my_bool done= FALSE;
- char cipher[MY_LINE_MAX], *tmp= NULL;
- uint bytes_read=0, len= 0;
- int enc_len= 0; // Can be negative.
- if (reset_login_file(0) == -1) //清空文件,並重新生成隨機加密秘鑰,並將對稱加密秘鑰寫入文件頭部
- goto error;
- if (my_seek(g_fd, MY_LOGIN_HEADER_LEN, SEEK_SET, MYF(MY_WME)) != (MY_LOGIN_HEADER_LEN))
- goto error; /* Error while seeking. */
- tmp= &file_buf->str[bytes_read];
- while(! done)
- {
- len= 0;
- while(*tmp++ != '\n') //讀取file_buf中的每一行內容
- if (len < (file_buf->length - bytes_read))
- len ++;
- else
- {
- done= TRUE;
- break;
- }
- if (done)
- break;
- if ((enc_len= encrypt_buffer(&file_buf->str[bytes_read],++len,cipher+MAX_CIPHER_STORE_LEN)) < 0) //對讀到的這一行內容進行加密,並將密文存放到cipher + MAX_CIPHER_STORE_LEN的地址處
- goto error;
- bytes_read += len;
- if (enc_len > MY_LINE_MAX)
- goto error;
- /* Store cipher length first. */
- int4store(cipher, enc_len); //將密文的長度存放到cipher的頭部
- if ((my_write(g_fd, (const uchar *)cipher, enc_len + MAX_CIPHER_STORE_LEN, MYF(MY_WME))) != (enc_len + MAX_CIPHER_STORE_LEN)) //將該行加密過的密文寫到文件
- goto error;
- }
- verbose_msg("Successfully written encrypted data to the login file.\n");
- /* Update file_size */
- file_size= bytes_read; //更新文件大小
- DBUG_RETURN(0);
- error:
- my_perror("couldn't encrypt the file");
- DBUG_RETURN(-1);
- }
該函數的主要流程為:讀取 file_buf 中的一行 → 依產生的 KEY 加密並將結果放到 cipher+MAX_CIPHER_STORE_LEN → 將密文長度存放到 cipher 與 cipher+MAX_CIPHER_STORE_LEN 之間 → 將 cipher 寫入檔案 → 更新檔案大小。上述 1~5 步驟循環執行,直到 file_buf 內容全部加密並寫入檔案為止。
下一節將說明具體採用的加密演算法,並透過相關解密演算法撰寫程式對該檔進行解密。
Android App 架構演進:從 Activity 到 RxJava 與 MVP
早期架構的痛點在 Android 早期開發中,許多應用會直接把網路請求、資料轉換與 UI 顯示都放在 Activity 或 Fragment 中。這種做法在小型專案或許可
Android 自定義 ListView 實作教學
Android 自定義 ListView 實作教學ListView 是 Android 早期非常常見的列表元件,適合用來展示大量資料。本文將從系統內建的 ListView
Android Studio 項目提交與更新到 GitHub 的完整步驟
Android Studio 項目提交與更新到 GitHub 的完整步驟 使用 Android Studio 開發時,若想把專案同步到 GitHub,不必總是依賴命令列。
Android 動畫入門指南(一):View Animation 與 LayoutAnimation 完整解析
Android 動畫入門指南(一):View Animation 與 LayoutAnimation 完整解析 在 Android 開發中,動畫是提升使用者體驗的重要手段