MD5加密(密码加密)

来源:互联网 发布:原生js display 编辑:程序博客网 时间:2024/04/29 17:39

上篇文章http://blog.csdn.net/aierjun/article/details/52447529 图片三级缓存中的MD5加密就是这个



MD5Util


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


/**
 * Created by Administrator on 2016/9/6.
 */
public class MD5Utils {
    private static final String YAN="isgu&*%>qq:378592175(&378592175@qq.com)";


    public static String encode(String password){
        try {
            password=YAN+password;
            StringBuffer sb=new StringBuffer();
            MessageDigest digest=MessageDigest.getInstance("md5");
            byte[] bytes=digest.digest(password.getBytes());
            for (byte b:bytes){
                int n=b&0XFF;
                String s=Integer.toHexString(n);
                if (s.length()==1){
                    sb.append("0"+s);
                }else {
                    sb.append(s);
                }
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
}


TestMain


import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


import com.aierjun.essay.R;


/**
 * Created by Administrator on 2016/9/6.
 */
public class TestActivity extends Activity{
    private EditText userName;
    private EditText passWd;
    private TextView login;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        findView();
        initView();
        saveToback();
    }


    private void findView() {
        userName= (EditText) findViewById(R.id.username_login);
        passWd= (EditText) findViewById(R.id.passwd_login);
        login= (TextView) findViewById(R.id.login_login);
    }


    private void initView() {
        login.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction()==MotionEvent.ACTION_DOWN){
                    String pw=passWd.getText().toString();
                    String encodePwd=MD5Utils.encode(pw);
                    SharedPreferences userInfo=getSharedPreferences("user_info",0);
                    userInfo.edit().putString("name",userName.getText().toString()).commit();
                    userInfo.edit().putString("passwd",encodePwd).commit();
                }
                else if (event.getAction()==MotionEvent.ACTION_UP){


                }
                return false;
            }
        });
    }
    private void saveToback(){
        SharedPreferences userInfo=getSharedPreferences("user_info",0);
        String name=userInfo.getString("name","");
        String pass=userInfo.getString("passwd","");
        userName.setText(name);
        passWd.setText(pass);
    }
}


注意最后的回显,显示的是加密后的内容

0 0