Android 用MD5加密算法加密密码


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

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

java代码:

  1. <pre name="code" class="html"><?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_width="fill_parent"   
  5. android:layout_height="fill_parent"   
  6. >   
  7.   
  8.   
  9. <EditText   
  10. android:id="@+id/username"   
  11. android:layout_width="fill_parent"   
  12. android:layout_height="wrap_content"   
  13. android:layout_marginLeft="10dp"   
  14. android:layout_marginTop="20dp"   
  15. android:layout_marginRight="10dp"   
  16. android:hint="帐号"   
  17. />   
  18.   
  19.   
  20. <EditText   
  21. android:id="@+id/password"   
  22. android:password="true"   
  23. android:layout_width="fill_parent"   
  24. android:layout_height="wrap_content"   
  25. android:layout_marginLeft="10dp"   
  26. android:layout_marginTop="10dp"   
  27. android:layout_marginRight="10dp"   
  28. android:hint="密码"   
  29. />   
  30.   
  31.   
  32. <Button   
  33. android:id="@+id/save"   
  34. android:text="保存"   
  35. android:layout_width="fill_parent"   
  36. android:layout_height="wrap_content"   
  37. android:layout_marginLeft="10dp"   
  38. android:layout_marginTop="10dp"   
  39. android:layout_marginRight="10dp"   
  40. />   
  41.   
  42.   
  43. <Button   
  44. android:id="@+id/login"   
  45. android:layout_width="fill_parent"   
  46. android:layout_height="wrap_content"   
  47. android:layout_marginLeft="10dp"   
  48. android:layout_marginTop="10dp"   
  49. android:layout_marginRight="10dp"   
  50. android:text="登录"   
  51. />   
  52. </LinearLayout>   

login.xml

  1. <span style="color: rgb(68, 68, 68); font-family: 'DejaVu Sans', 'Lucida Grande', Tahoma, 'Hiragino Sans GB', STHeiti, SimSun, sans-serif; font-size: 14px; line-height: 21px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-weight: 700; "></span></span><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>   
  2. <LinearLayout   
  3. xmlns:android="http://schemas.android.com/apk/res/android"   
  4. android:layout_width="match_parent"   
  5. android:layout_height="match_parent"   
  6. android:orientation="vertical">   
  7. <TextView   
  8. android:layout_width="fill_parent"   
  9. android:layout_height="wrap_content"   
  10. android:text="login successful!"   
  11. />   
  12. </LinearLayout>   

login.java

  1. <span style="color: rgb(68, 68, 68); font-family: 'DejaVu Sans', 'Lucida Grande', Tahoma, 'Hiragino Sans GB', STHeiti, SimSun, sans-serif; font-size: 14px; line-height: 21px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-weight: 700; "><span style="color: rgb(68, 68, 68); font-family: 'DejaVu Sans', 'Lucida Grande', Tahoma, 'Hiragino Sans GB', STHeiti, SimSun, sans-serif; font-size: 14px; line-height: 21px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-weight: 700; "></span></span></span></span><pre name="code" class="html">package eoe.md5demo;   
  2.   
  3.   
  4. import android.app.Activity;   
  5. import android.os.Bundle;   
  6.   
  7.   
  8. public class Login extends Activity {   
  9.   
  10.   
  11. @Override   
  12. protected void onCreate(Bundle savedInstanceState) {   
  13. // TODO Auto-generated method stub   
  14. super.onCreate(savedInstanceState);   
  15. setContentView(R.layout.login);   
  16. }   
  17. }   

MD5Demo.java

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

相关内容