亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

用MD5處理明文密碼加密

系統 2009 0

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

可以用FileBrower查看


用MD5處理明文密碼加密

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

?

下面是個小例子:

main.xml

    <?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

    <?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

    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

    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;  
    }  
  
}  
  

?

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

?


用MD5處理明文密碼加密

?


用MD5處理明文密碼加密

?


用MD5處理明文密碼加密
?

轉自: http://hualang.iteye.com/blog/1150795

用MD5處理明文密碼加密


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲欧美专区精品久久 | 欧美特黄级乱色毛片 | 日本一级毛片2021免费 | 成人免费国产欧美日韩你懂的 | 成人a毛片高清视频 | 日本老太做爰xx | 日本在线视频精品 | 欧美a级成人淫片免费看 | 久热国产精品 | 香蕉大黄香蕉在线观看 | 国产a国产 | 亚洲精品二区中文字幕 | 天天草夜夜操 | 日本免费黄色小视频 | 日本免费一区二区三区a区 日本免费一区二区三区看片 | 奇米色吧 | 欧美特级毛片 | 精品一区二区三区在线成人 | 夜夜草天天干 | 欧美洲精品亚洲精品中文字幕 | 成人一级网站 | 亚洲精品98久久久久久中文字幕 | 拍拍拍精品视频在线观看 | 精品一区二区久久久久久久网精 | 欧美激情在线一区二区三区 | 欧美精欧美乱码一二三四区 | 亚洲网站在线看 | 99热免费在线观看 | 午夜欧美精品久久久久久久 | 影视先锋av资源噜噜 | 五月婷婷在线视频观看 | 国产91在线 | 欧美 | 日韩免费视频一区二区 | 91精品视频在线播放 | 国产人成精品 | 五月天丁香婷婷综合 | 99re这里有免费视频精品 | 狠狠狠综合色 | 国产亚洲精品资源一区 | 99re这里只有精品66 | 女bbbbxxxx毛片视频0 |