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