学习笔记之——android MD5加密(32位)和 Base64加密解密

来源:互联网 发布:mac浏览器打不开百度 编辑:程序博客网 时间:2024/06/05 06:57

     为了确保数据传输安全,一般都会采取加密方式

一.MD5加密数据

public class MD5Util {public static String getMD5Str(String str) {  MessageDigest messageDigest = null;  try {  messageDigest = MessageDigest.getInstance("MD5");  messageDigest.reset();  messageDigest.update(str.getBytes("UTF-8"));  } catch (NoSuchAlgorithmException e) {  System.out.println("NoSuchAlgorithmException caught!");  System.exit(-1);  } catch (UnsupportedEncodingException e) {  e.printStackTrace();  }  byte[] byteArray = messageDigest.digest();  StringBuffer md5StrBuff = new StringBuffer();  for (int i = 0; i < byteArray.length; i++) {              if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)  md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));  else  md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));  }  return md5StrBuff.toString();  } 
}
使用

MD5Util.getMD5Str(str);

转自:http://blog.csdn.net/lufanzheng/article/details/46729271


二.android Base64加密解密


// 加密传入的数据是byte类型的,并非使用decode方法将原始数据转二进制,String类型的数据 使用 str.getBytes()即可String str = "Hello!";// 在这里使用的是encode方式,返回的是byte类型加密数据,可使用new String转为String类型String strBase64 = new String(Base64.encode(str.getBytes(), Base64.DEFAULT));Log.i("Test", "encode >>>" + strBase64);// 这里 encodeToString 则直接将返回String类型的加密数据String enToStr = Base64.encodeToString(str.getBytes(), Base64.DEFAULT);Log.i("Test", "encodeToString >>> " + enToStr);// 对base64加密后的数据进行解密Log.i("Test", "decode >>>" + new String(Base64.decode(strBase64.getBytes(), Base64.DEFAULT)));

转自:http://www.tuicool.com/articles/EBBBni


0 0
原创粉丝点击