很多的网络相关的软件都需要用户名密码登录,在开发的时候像这些密码都是保存在SharedPreferences中,这些密码保存在/data/data/包名/shared_prefs下,保存在一个XML文件中,如下:

可以用FileBrower查看

开始说道正题,MD5加密算法虽然现在有些人已经将其解开了,但是它的加密机制依然很强大,我想绝大对数还是不会解开的。MD5加密算法是单向加密,只能用你的密码才能解开,要不就是会解密算法,否则想都别想解开。为了防止这种情况的发生。还可以对加密过的密码进行再次加密。

下面是个小例子:

main.xml

Xml代码 复制代码 收藏代码spinner.gif

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_;fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <EditText
  8. android:id="@+id/username"
  9. android:layout_;fill_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_marginLeft="10dp"
  12. android:layout_marginTop="20dp"
  13. android:layout_marginRight="10dp"
  14. android:hint="帐号"
  15. />
  16. <EditText
  17. android:id="@+id/password"
  18. android:password="true"
  19. android:layout_;fill_parent"
  20. android:layout_height="wrap_content"
  21. android:layout_marginLeft="10dp"
  22. android:layout_marginTop="10dp"
  23. android:layout_marginRight="10dp"
  24. android:hint="密码"
  25. />
  26. <Button
  27. android:id="@+id/save"
  28. android:text="保存"
  29. android:layout_;fill_parent"
  30. android:layout_height="wrap_content"
  31. android:layout_marginLeft="10dp"
  32. android:layout_marginTop="10dp"
  33. android:layout_marginRight="10dp"
  34. />
  35. <Button
  36. android:id="@+id/login"
  37. android:layout_;fill_parent"
  38. android:layout_height="wrap_content"
  39. android:layout_marginLeft="10dp"
  40. android:layout_marginTop="10dp"
  41. android:layout_marginRight="10dp"
  42. android:text="登录"
  43. />
  44. </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_;fill_parent" android:layout_height="fill_parent" > <EditText android:id="@+id/username" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="20dp" android:layout_marginRight="10dp" android:hint="帐号" /> <EditText android:id="@+id/password" android:password="true" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_marginRight="10dp" android:hint="密码" /> <Button android:id="@+id/save" android:text="保存" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_marginRight="10dp" /> <Button android:id="@+id/login" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_marginRight="10dp" android:text="登录" /> </LinearLayout>

login.xml

Xml代码 复制代码 收藏代码spinner.gif

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_;match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical">
  7. <TextView
  8. android:layout_;fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="login successful!"
  11. />
  12. </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_;match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="login successful!" /> </LinearLayout>

login.java

Java代码 复制代码 收藏代码spinner.gif

  1. package com.loulijun.md5demo;    
  2. import android.app.Activity;    
  3. import android.os.Bundle;    
  4. public class Login extends Activity {    
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {    
  7. // TODO Auto-generated method stub  
  8. super.onCreate(savedInstanceState);    
  9.         setContentView(R.layout.login);    
  10.     }    
  11. }  
package com.loulijun.md5demo; import android.app.Activity; import android.os.Bundle; public class Login extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.login); } }

MD5Demo.java

Java代码 复制代码 收藏代码spinner.gif

  1. package com.loulijun.md5demo;    
  2. import java.security.MessageDigest;    
  3. import android.app.Activity;    
  4. import android.content.Intent;    
  5. import android.content.SharedPreferences;    
  6. import android.os.Bundle;    
  7. import android.view.View;    
  8. import android.widget.Button;    
  9. import android.widget.EditText;    
  10. import android.widget.Toast;    
  11. public class MD5Demo extends Activity {    
  12. private EditText username,password;    
  13. private Button savebtn,loginbtn;    
  14.     String user,pass;    
  15. @Override
  16. public void onCreate(Bundle savedInstanceState) {    
  17. super.onCreate(savedInstanceState);    
  18.         setContentView(R.layout.main);    
  19.         username = (EditText)findViewById(R.id.username);    
  20.         password = (EditText)findViewById(R.id.password);    
  21.         savebtn = (Button)findViewById(R.id.save);    
  22.         loginbtn = (Button)findViewById(R.id.login);    
  23.         savebtn.setOnClickListener(new Button.OnClickListener()    
  24.         {    
  25. @Override
  26. public void onClick(View v) {    
  27.                 SharedPreferences pre = getSharedPreferences("loginvalue",MODE_WORLD_WRITEABLE);    
  28.                 pass = MD5(password.getText().toString());    
  29.                 user = username.getText().toString();    
  30. if(!pass.equals("")&&!user.equals(""))    
  31.                 {    
  32.                       pre.edit().putString("username", username.getText().toString()).    
  33.                       putString("password",encryptmd5(pass)).commit();    
  34.                       Toast.makeText(getApplicationContext(), "保存成功!", Toast.LENGTH_SHORT).show();    
  35.                 }else
  36.                 {    
  37.                     Toast.makeText(getApplicationContext(), "密码不能为空!", Toast.LENGTH_LONG).show();    
  38.                 }    
  39.             }    
  40.         });    
  41.         loginbtn.setOnClickListener(new Button.OnClickListener()    
  42.         {    
  43. @Override
  44. public void onClick(View v) {    
  45.                 SharedPreferences sp = getSharedPreferences("loginvalue", MODE_WORLD_READABLE);    
  46.                 String loginuser = sp.getString("username", null);    
  47.                 String loginpass = sp.getString("password", null);    
  48.                 user = username.getText().toString();    
  49.                 pass = password.getText().toString();    
  50.                 String passmd5 = MD5(pass);    
  51.                 String encryptmd5 = encryptmd5(passmd5);    
  52.                 System.out.println("username="+loginuser+"-------------password="+loginpass);    
  53.                   System.out.println("user=="+user+"-------------encryptmd5=="+encryptmd5);    
  54. if(!user.equals("")&&!pass.equals(""))    
  55.                   {    
  56. if( user.equals(loginuser)&& encryptmd5.equals(loginpass))    
  57.                       {    
  58.                           Intent intent = new Intent();    
  59.                           intent.setClass(MD5Demo.this, Login.class);    
  60.                           MD5Demo.this.startActivity(intent);      
  61.                           finish();    
  62.                       }else
  63.                       {                   
  64.                           Toast.makeText(getApplicationContext(), "密码是错误的!", Toast.LENGTH_LONG).show();    
  65.                       }    
  66.                   }else
  67.                   {    
  68.                       Toast.makeText(getApplicationContext(), "密码不能为空!", Toast.LENGTH_LONG).show();    
  69.                   }    
  70.             }    
  71.         });    
  72.     }    
  73. //MD5加密,32位  
  74. public static String MD5(String str)    
  75.     {    
  76.         MessageDigest md5 = null;    
  77. try
  78.         {    
  79.             md5 = MessageDigest.getInstance("MD5");    
  80.         }catch(Exception e)    
  81.         {    
  82.             e.printStackTrace();    
  83. return "";    
  84.         }    
  85. char[] charArray = str.toCharArray();    
  86. byte[] byteArray = new byte[charArray.length];    
  87. for(int i = 0; i &lt; charArray.length; i++)    
  88.         {    
  89.             byteArray[i] = (byte)charArray[i];    
  90.         }    
  91. byte[] md5Bytes = md5.digest(byteArray);    
  92.         StringBuffer hexValue = new StringBuffer();    
  93. for( int i = 0; i &lt; md5Bytes.length; i++)    
  94.         {    
  95. int val = ((int)md5Bytes[i])&0xff;    
  96. if(val &lt; 16)    
  97.             {    
  98.                 hexValue.append("0");    
  99.             }    
  100.             hexValue.append(Integer.toHexString(val));    
  101.         }    
  102. return hexValue.toString();    
  103.     }    
  104. // 可逆的加密算法  
  105. public static String encryptmd5(String str) {    
  106. char[] a = str.toCharArray();    
  107. for (int i = 0; i &lt; a.length; i++)     
  108.         {    
  109.                 a[i] = (char) (a[i] ^ 'l');    
  110.         }    
  111.         String s = new String(a);    
  112. return s;    
  113.     }    
  114. }   
package com.loulijun.md5demo; import java.security.MessageDigest; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MD5Demo extends Activity { private EditText username,password; private Button savebtn,loginbtn; String user,pass; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); username = (EditText)findViewById(R.id.username); password = (EditText)findViewById(R.id.password); savebtn = (Button)findViewById(R.id.save); loginbtn = (Button)findViewById(R.id.login); savebtn.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { SharedPreferences pre = getSharedPreferences("loginvalue",MODE_WORLD_WRITEABLE); pass = MD5(password.getText().toString()); user = username.getText().toString(); if(!pass.equals("")&&!user.equals("")) { pre.edit().putString("username", username.getText().toString()). putString("password",encryptmd5(pass)).commit(); Toast.makeText(getApplicationContext(), "保存成功!", Toast.LENGTH_SHORT).show(); }else { Toast.makeText(getApplicationContext(), "密码不能为空!", Toast.LENGTH_LONG).show(); } } }); loginbtn.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { SharedPreferences sp = getSharedPreferences("loginvalue", MODE_WORLD_READABLE); String loginuser = sp.getString("username", null); String loginpass = sp.getString("password", null); user = username.getText().toString(); pass = password.getText().toString(); String passmd5 = MD5(pass); String encryptmd5 = encryptmd5(passmd5); System.out.println("username="+loginuser+"-------------password="+loginpass); System.out.println("user=="+user+"-------------encryptmd5=="+encryptmd5); if(!user.equals("")&&!pass.equals("")) { if( user.equals(loginuser)&& encryptmd5.equals(loginpass)) { Intent intent = new Intent(); intent.setClass(MD5Demo.this, Login.class); MD5Demo.this.startActivity(intent); finish(); }else { Toast.makeText(getApplicationContext(), "密码是错误的!", Toast.LENGTH_LONG).show(); } }else { Toast.makeText(getApplicationContext(), "密码不能为空!", Toast.LENGTH_LONG).show(); } } }); } //MD5加密,32位 public static String MD5(String str) { MessageDigest md5 = null; try { md5 = MessageDigest.getInstance("MD5"); }catch(Exception e) { e.printStackTrace(); return ""; } char[] charArray = str.toCharArray(); byte[] byteArray = new byte[charArray.length]; for(int i = 0; i &lt; charArray.length; i++) { byteArray[i] = (byte)charArray[i]; } byte[] md5Bytes = md5.digest(byteArray); StringBuffer hexValue = new StringBuffer(); for( int i = 0; i &lt; md5Bytes.length; i++) { int val = ((int)md5Bytes[i])&0xff; if(val &lt; 16) { hexValue.append("0"); } hexValue.append(Integer.toHexString(val)); } return hexValue.toString(); } // 可逆的加密算法 public static String encryptmd5(String str) { char[] a = str.toCharArray(); for (int i = 0; i &lt; a.length; i++) { a[i] = (char) (a[i] ^ 'l'); } String s = new String(a); return s; } }

程序很简单,下面是运行的效果: