MD5加密算法

 

很多的網絡相關的軟件都需要用戶名密碼登錄,在開發的時候像這些密碼都是保存在SharedPreferences中,這些密碼保存在/data/data/包名/shared_prefs下,保存在一個XML文件中,如下:

可以用FileBrower查看


開始說道正題,MD5加密算法雖然現在有些人已經將其解開了,但是它的加密機制依然很強大,我想絕大對數還是不會解開的。MD5加密算法是單向加密,只能用你的密碼才能解開,要不就是會解密算法,否則想都別想解開。爲了防止這種情況的發生。還可以對加密過的密碼進行再次加密。

 

下面是個小例子:

main.xml

Java代碼 複製代碼 收藏代碼
  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>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="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

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

}

 程序很簡單,下面是運行的效果:




 

發佈了48 篇原創文章 · 獲贊 6 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章