391159a7-5d18-3cc4-8338-843f32e67d6b.gif84135f90-bf8f-3cb7-8947-5febcb12af9a.gif

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

可以用FileBrower查看


b297d585-3b11-3ea4-8926-10c969669456.gif  

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

 

下面是个小例子:

main.xml

 
  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_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >   
  7.     <EditText   
  8.         android:id="@+id/username"  
  9.         android:layout_width="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_width="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_width="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_width="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>  
 

 login.xml

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

 MD5Demo.java

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

 


 
 

FROM:http://blog.csdn.net/freestyleboy21/article/details/7009933

arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()