码迷,mamicode.com
首页 > 移动开发 > 详细

android中的MD5、Base64、DES/3DES/ADES加解密

时间:2018-03-20 20:47:40      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:ade   分享   else   size   roi   led   null   ast   sage   

MD5摘要算法:

技术分享图片

<span style="font-size:18px;">主要代码:
 String s = edit.getText().toString();
    if (TextUtils.isEmpty(s)){
        Toast.makeText(getActivity(), "摘要数据不可为空", Toast.LENGTH_SHORT).show();
    } else {
        try {
            //获取一种摘要算法
            MessageDigest instance = MessageDigest.getInstance("MD5");
            byte[] digest = instance.digest(s.getBytes("UTF-8"));
            StringBuilder builder = new StringBuilder();
            for (byte b : digest) {
                builder.append(String.format("%2x", b & 0xff));
            }
            String replace = builder.toString().replace(‘ ‘, ‘0‘).toUpperCase();
            text.setText(replace);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}</span>




Base64加解密:

技术分享图片

<span style="font-size:18px;">主要代码:
public void onClick(View v) {
    switch (v.getId()){
        case R.id.base64_encode:
            String src_s = src.getText().toString();
            if (TextUtils.isEmpty(src_s)){
                Toast.makeText(getActivity(), "原码不可为空", Toast.LENGTH_SHORT).show();
            } else {
                try {
                    String s = Base64.encodeToString(src_s.getBytes("UTF-8"), Base64.DEFAULT);
                    rlt.setText(s);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            break;
        case R.id.base64_decode:
            String rlt_s = rlt.getText().toString();
            if (TextUtils.isEmpty(rlt_s)){
                Toast.makeText(getActivity(), "编码不可为空", Toast.LENGTH_SHORT).show();
            } else {
                byte[] decode = Base64.decode(rlt_s, Base64.DEFAULT);
                try {
                    src.setText(new String(decode, "UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            break;
    }
}</span>



DES加解密:


技术分享图片

<span style="font-size:18px;">主要代码:
public void onClick(View view) {
    String key_str = key.getText().toString();
    Log.i("key_str-----",key_str+"----");
    if (key_str==null){
        Toast.makeText(getActivity(), "密钥不可为空", Toast.LENGTH_SHORT).show();
    } else {
        try {
            byte[] bytes = key_str.getBytes("UTF-8");
            byte[] keys = new byte[32];
            System.arraycopy(bytes, 0, keys, 0, Math.min(bytes.length, keys.length));
            //加解密时, 用到的key对象, 第一个參数的byte数组的长度必须符合第二个參数算法的要求。

SecretKey secretKey = new SecretKeySpec(keys, "Aes"); //加解密时,用到的工具对象,工具的算法和key的算法必须一样 Cipher cipher = Cipher.getInstance("Aes"); switch (view.getId()){ case R.id.des_encode: String src_str = src.getText().toString(); if (src_str==null){ Toast.makeText(getActivity(), "加密时,原文不可为空", Toast.LENGTH_SHORT).show(); } else { //初始化为加密模式 cipher.init(Cipher.ENCRYPT_MODE, secretKey); byte[] aFinal = cipher.doFinal(src_str.getBytes("UTF-8")); rlt.setText(Base64.encodeToString(aFinal, Base64.DEFAULT)); } break; case R.id.des_decode: String rlt_str = rlt.getText().toString(); if (rlt_str==null){ Toast.makeText(getActivity(), "解密时。密文不可为空", Toast.LENGTH_SHORT).show(); } else { cipher.init(Cipher.DECRYPT_MODE, secretKey); byte[] aFinal = cipher.doFinal(Base64.decode(rlt_str, Base64.DEFAULT)); src.setText(new String(aFinal, "UTF-8")); } break; } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } } } </span>



源代码地址:

http://download.csdn.net/detail/lzq520210/9419823

android中的MD5、Base64、DES/3DES/ADES加解密

标签:ade   分享   else   size   roi   led   null   ast   sage   

原文地址:https://www.cnblogs.com/llguanli/p/8612005.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!