android md5 jar包,android 使用DigestUtilsmd5加密的方法

直接上代码吧。注意导入apache commons-codec的jar包。

1、布局文件

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context="com.example.hellom.MainActivity$PlaceholderFragment" >

android:id="@+id/notic"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello_world" />

android:id="@+id/notic01"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/notic"

android:layout_below="@+id/notic"

android:layout_marginTop="24dp"

android:text="@string/notic01" />

android:id="@+id/textIn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignBaseline="@+id/notic01"

android:layout_alignBottom="@+id/notic01"

android:layout_marginLeft="19dp"

android:layout_toRightOf="@+id/notic01"

android:ems="10" >

android:id="@+id/ciphertext"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/notic01"

android:layout_below="@+id/textIn"

android:layout_marginTop="33dp"

android:text="@string/notic02" />

android:id="@+id/ciphertext01"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignBaseline="@+id/ciphertext"

android:layout_alignBottom="@+id/ciphertext"

android:layout_alignLeft="@+id/textIn"

android:layout_alignRight="@+id/textIn"

android:text="" />

android:id="@+id/textOut"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_below="@+id/ciphertext"

android:layout_marginTop="23dp"

android:text="@string/notic04" />

android:id="@+id/text03"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignBottom="@+id/textOut"

android:layout_alignLeft="@+id/ciphertext01"

android:layout_alignRight="@+id/ciphertext01"

android:text="" />

android:id="@+id/translation01"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/textOut"

android:layout_alignParentBottom="true"

android:layout_marginBottom="97dp"

android:layout_marginLeft="16dp"

android:text="@string/notic03" />

android:id="@+id/translation02"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignBottom="@+id/translation01"

android:layout_alignRight="@+id/text03"

android:layout_marginRight="37dp"

android:text="@string/notic05" />

2、mainActivity

@Override

protected void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

if (savedInstanceState == null)

{

getFragmentManager().beginTransaction()

.add(R.id.container, new PlaceholderFragment()).commit();

}

}

3、fragment

public class PlaceholderFragment extends Fragment

{

//明文

public static EditText text01;

//密文

public static TextView text02;

//测试明文

public static TextView text03;

//加密事件按钮

public static Button translationBtn01;

//解密事件按钮

public static Button translationBtn02;

public PlaceholderFragment()

{

}

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState)

{

View rootView = inflater.inflate(R.layout.fragment_main, container,

false);

text01 = (EditText) rootView.findViewById(R.id.textIn);

text02 = (TextView) rootView.findViewById(R.id.ciphertext01);

text03 = (TextView) rootView.findViewById(R.id.text03);

translationBtn01 = (Button) rootView.findViewById(R.id.translation01);

translationBtn02 = (Button) rootView.findViewById(R.id.translation02);

translationBtn02.setEnabled(false);

return rootView;

}

@Override

public void onAttach(Activity activity)

{

// TODO Auto-generated method stub

super.onAttach(activity);

}

@Override

public void onActivityCreated(Bundle savedInstanceState)

{

// TODO Auto-generated method stub

super.onActivityCreated(savedInstanceState);

translationBtn01.setOnClickListener(new BtnOnClickListener(getActivity()));

translationBtn02.setOnClickListener(new BtnOnClickListener(getActivity()));

}

}

3、fragment中的按钮事件类(独立类的缺点是需要较多的static变量)

public class BtnOnClickListener implements OnClickListener

{

private Context context;

public BtnOnClickListener(Context context)

{

// TODO Auto-generated constructor stub

this.context = context;

}

@Override

public void onClick(View v)

{

// TODO Auto-generated method stub

int id = v.getId();

switch (id)

{

// 加密按钮事件按钮

case R.id.translation01:

{

if (TextUtils.isEmpty(PlaceholderFragment.text01.getText()))

{

Toast.makeText(context, "请输入需要加密的内容", Toast.LENGTH_SHORT)

.show();

} else

{

String text = PlaceholderFragment.text01.getText().toString()

.trim();

Toast.makeText(context, "开始加密", Toast.LENGTH_SHORT).show();

String text01 = new String(Hex.encodeHex(DigestUtils.md5(text)));

PlaceholderFragment.text02.setText(text01);

PlaceholderFragment.translationBtn02.setEnabled(true);

PlaceholderFragment.translationBtn01.setEnabled(false);

}

}

break;

case R.id.translation02:

{

Toast.makeText(context, "开始解密", Toast.LENGTH_SHORT).show();

PlaceholderFragment.text03.setText("MD5为不可逆加密,可尝试Base64.");

PlaceholderFragment.translationBtn02.setEnabled(false);

PlaceholderFragment.translationBtn01.setEnabled(true);

}

break;

default:

break;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值