1. 程式人生 > >android中的MD5、Base64、DES/3DES/ADES加解密

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

tid log str dpa enc csdn 長度 ttext public

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加解密