標籤:android 加密
上次寫到在進入手機但·防盜介面時需要有密碼限制,首先第一次進入時會彈出對話方塊提示使用者佈建密碼;再次進入時會要求使用者輸入密碼;這次來具體實現上述功能。
首次登入,設定密碼
首先,我們的密碼是儲存在SharePreference中的”password”欄位裡的,在登入時後台需要校正該欄位是否已經設定了密碼,若未設定則彈出對話方塊讓使用者佈建,否則要使用者輸入密碼進入手機防盜介面;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); sp = getSharedPreferences("config", Context.MODE_PRIVATE); // 判讀使用者是否已經設定了密碼 if (isPwdSetup()) { Log.i(TAG, "設定了密碼,彈出輸入密碼的對話方塊"); } else { Log.i(TAG, "未設定密碼,彈出設定密碼對話方塊"); showFirstEntryDialog(); } } /** * 檢查sharedpreference中是否有密碼的設定 * * @return */ private boolean isPwdSetup() { String password = sp.getString("password", null); if (password == null) { return false; } else { if ("".equals(password)) { return false; } else { return true; } } }
- showFirstEntryDialog(),彈出使用者佈建密碼對話方塊
/** * 第一次進入程式時彈出的設定密碼的對話方塊 * 使用自訂對話方塊樣式 */ private void showFirstEntryDialog() { dialog = new Dialog(this, R.style.MyDialog);// dialog.setContentView(R.layout.first_entry_dialog);// 設定要顯示的內容 View view = View.inflate(this, R.layout.first_entry_dialog, null); et_pwd = (EditText) view.findViewById(R.id.et_first_entry_pwd); et_pwd_confirm = (EditText) view.findViewById(R.id.et_first_entry_pwd_confirm); Button bt_confirm = (Button) view.findViewById(R.id.bt_first_dialog_confirm); Button bt_cancel = (Button) view.findViewById(R.id.bt_first_dialog_cancel); // 設定按鈕對應的點擊事件 bt_confirm.setOnClickListener(this); bt_cancel.setOnClickListener(this); dialog.setContentView(view); dialog.setCanceledOnTouchOutside(false);// 設定dialog不可以點擊其他地方時消失 dialog.setCancelable(false);// 設定dialog不可以點返回鍵時消失 dialog.show(); }
@Override public void onClick(View view) { switch(view.getId()){ // 點擊取消 case R.id.bt_first_dialog_cancel: dialog.dismiss(); break; case R.id.bt_first_dialog_confirm: String pwd = et_pwd.getText().toString().trim(); String pwd_confirm = et_pwd_confirm.getText().toString().trim(); // 輸入的密碼中包好空值 if("".equals(pwd) || "".equals(pwd_confirm)){ Toast.makeText(getApplicationContext(), "輸入不可為空!", Toast.LENGTH_LONG).show(); return; }else{ if(pwd.equals(pwd_confirm)){ Editor editor = sp.edit(); editor.putString("password", pwd); editor.commit(); } // 兩次輸入不一致 else{ Toast.makeText(getApplicationContext(), "兩次輸入密碼不相同!", Toast.LENGTH_LONG).show(); return; } } dialog.dismiss(); break; } }
效果如下:
初次進入手機防盜介面:
未輸入時點擊確定:
兩次輸入密碼不相同:
再次登入,輸入密碼
- 彈出對話方塊樣式:normal_entry_dialog.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="300dip" android:layout_height="280dip" android:gravity="center_horizontal" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登入" android:textColor="@color/textcolor" android:textSize="24sp" /> <LinearLayout android:layout_width="300dip" android:layout_height="wrap_content" android:background="#ffc8c8c8" android:orientation="vertical" > <EditText android:id="@+id/et_normal_entry_pwd" android:layout_width="300dip" android:layout_height="wrap_content" android:hint="請輸入密碼" android:password="true" /> </LinearLayout> <LinearLayout android:layout_width="300dip" android:layout_height="50dip" android:gravity="center" android:orientation="horizontal" > <Button android:id="@+id/bt_normal_dialog_confirm" android:layout_width="140dip" android:layout_height="40dip" android:background="@drawable/button_background" android:text="確定" android:textColor="#ffffffff" /> <Button android:id="@+id/bt_normal_dialog_cancel" android:layout_width="140dip" android:layout_height="40dip" android:layout_marginLeft="3dip" android:background="@drawable/button_background" android:text="取消" /> </LinearLayout></LinearLayout>
/** * 正常登入的對話方塊 * */ private void showNormalEntryDialog() { dialog = new Dialog(this, R.style.MyDialog); View view = View.inflate(this, R.layout.normal_entry_dialog, null); et_pwd = (EditText) view.findViewById(R.id.et_normal_entry_pwd); Button bt_confirm = (Button) view.findViewById(R.id.bt_normal_dialog_confirm); Button bt_cancel = (Button) view.findViewById(R.id.bt_normal_dialog_cancel); // 設定按鈕對應的點擊事件 bt_confirm.setOnClickListener(this); bt_cancel.setOnClickListener(this); dialog.setContentView(view); dialog.setCanceledOnTouchOutside(false);// 設定dialog不可以點擊其他地方時消失// dialog.setCancelable(false);// 設定dialog不可以點返回鍵時消失 dialog.show(); }
@Override public void onClick(View view) { switch(view.getId()){ case R.id.bt_normal_dialog_cancel: dialog.dismiss(); break; case R.id.bt_normal_dialog_confirm: String input_pwd = et_pwd.getText().toString(); if("".equals(input_pwd)){ Toast.makeText(getApplicationContext(), "輸入不可為空!", Toast.LENGTH_LONG).show(); return; }else{ String password = sp.getString("password", ""); if(!password.equals(input_pwd)){ Toast.makeText(getApplicationContext(), "輸入密碼不正確,請重新輸入!", Toast.LENGTH_LONG).show(); et_pwd.selectAll();// 使用者輸入錯誤後,對文本進行全選,方便使用者進行刪除重新輸入 return; } } Log.i(TAG, "載入手機防盜主介面"); dialog.dismiss(); break; } }
效果如下:
密碼加密儲存
目前我們的密碼儲存都是以明文儲存在SharePreference中的,因此有點Android開發基礎的人都可以擷取到我們設定的密碼。
考慮使用密碼編譯演算法對密碼加密後進行儲存。
使用JavaSe提供的MessageDigest類進行加密。MessageDigest支援的密碼編譯演算法包括:MD5、SHA-1、SHA-256。
package com.liuhao.mobilesafe.util;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;public class MD5Encoder { public static String encode(String pwd) { try { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] bytes = md.digest(pwd.getBytes()); StringBuffer sb = new StringBuffer(); for (byte b : bytes) { String str = Integer.toHexString(0xff & b);// byte是八位位元組儲存的,轉化為16進位數,直接與11111111相與 if (str.length() == 1) { sb.append("0" + str); } else { sb.append(str); } } return sb.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); throw new RuntimeException("不存在密碼編譯演算法"); } }}
這樣在儲存密碼時調用encode()方法即可對密碼進行儲存。在讀取時也要用加密後的密文與已儲存的進行對比。
editor.putString("password", MD5Encoder.encode(pwd));if(!password.equals(MD5Encoder.encode(input_pwd))){ Toast.makeText(getApplicationContext(), "輸入密碼不正確,請重新輸入!", Toast.LENGTH_LONG).show(); et_pwd.selectAll();// 使用者輸入錯誤後,對文本進行全選,方便使用者進行刪除重新輸入 return;}
其實我們僅僅簡單的一次加密也是很不保險的,雖說從演算法實現上來說md5加密是無法復原的,但是有些“別有用心”的人,竟然將所有可預見的字串對應的密文都算出來了,真是。。。
比如這個網站:http://www.cmd5.com/
驚呆了,有木有!
所以,以後在重要的網站設定密碼時一定要設的複雜一點!!!
【邊做項目邊學Android】手機安全衛士07-手機防盜之進入限制