Android 用MD5加密算法加密密码

来源:互联网 发布:无锡知原药业面试 编辑:程序博客网 时间:2024/05/22 01:47
      很多的网络相关的软件都需要用户名密码登录,在开发的时候像这些密码都是保存在SharedPreferences中,这些密码保存在/data/data/包名/shared_prefs下,保存在一个XML文件中,如下:


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


Log.xml:
[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <<span style="font-size:18px;">?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. <EditText   
  9. android:id="@+id/username"   
  10. android:layout_width="fill_parent"   
  11. android:layout_height="wrap_content"   
  12. android:layout_marginLeft="10dp"   
  13. android:layout_marginTop="20dp"   
  14. android:layout_marginRight="10dp"   
  15. android:hint="帐号"   
  16. />   
  17.   
  18. <EditText   
  19. android:id="@+id/password"   
  20. android:password="true"   
  21. android:layout_width="fill_parent"   
  22. android:layout_height="wrap_content"   
  23. android:layout_marginLeft="10dp"   
  24. android:layout_marginTop="10dp"   
  25. android:layout_marginRight="10dp"   
  26. android:hint="密码"   
  27. />   
  28.   
  29. <Button   
  30. android:id="@+id/save"   
  31. android:text="保存"   
  32. android:layout_width="fill_parent"   
  33. android:layout_height="wrap_content"   
  34. android:layout_marginLeft="10dp"   
  35. android:layout_marginTop="10dp"   
  36. android:layout_marginRight="10dp"   
  37. />   
  38.   
  39. <Button   
  40. android:id="@+id/login"   
  41. android:layout_width="fill_parent"   
  42. android:layout_height="wrap_content"   
  43. android:layout_marginLeft="10dp"   
  44. android:layout_marginTop="10dp"   
  45. android:layout_marginRight="10dp"   
  46. android:text="登录"   
  47. />   
  48. </LinearLayout> </span>  

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;"><?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> </span>  

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;">package eoe.md5demo;   
  2. import android.app.Activity;   
  3. import android.os.Bundle;   
  4. public class Login extends Activity {   
  5.   
  6. @Override   
  7. protected void onCreate(Bundle savedInstanceState) {   
  8. // TODO Auto-generated method stub   
  9. super.onCreate(savedInstanceState);   
  10. setContentView(R.layout.login);   
  11. }   
  12. </span>  
MD5Demo.java
[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. 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 encryptmd5 = 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[] md5Bytes = 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. }   
0 0