android 简单实现加密 MD5 流程 简单

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bbtianshi/article/details/80339909

我们要对应的敏感信息    加密 比如 手机号 密码什么的 

下面开始对应的代码 对应的一个MD5Util的一个工具类

public class MD5Util {
    public static String md5(String txt) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 问题主要出在这里,Java的字符串是unicode编码,不受源码文件的编码影响;而PHP的编码是和源码文件的编码一致,受源码编码影响。
            md.update(txt.getBytes("GBK"));
            StringBuffer buf = new StringBuffer();
            for (byte b : md.digest()) {
                buf.append(String.format("%02x", b & 0xff));
            }
            return buf.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}

下面对应的布局 Activity的布局xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.samsung.ceshi.panduanwangluo.PdwlActivity">

  <TextView
      android:text="手机号:"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
  <EditText
      android:id="@+id/phone"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
  <Button
      android:id="@+id/btn"
      android:text="加密后的"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
</LinearLayout>

 下面 就是 Activity 二行代码搞定

public class PdwlActivity extends AppCompatActivity {

    EditText phone;
    Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pdwl);
        phone = (EditText)findViewById(R.id.phone);
        btn = (Button) findViewById(R.id.btn);
 btn.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {
         String s = phone.getText().toString();
      //MD5Util.md5(s);
         Toast.makeText(PdwlActivity.this, MD5Util.md5(s),Toast.LENGTH_SHORT).show();
     }
 });
    }

}

猜你喜欢

转载自blog.csdn.net/bbtianshi/article/details/80339909
今日推荐