继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

android数据加密(一)MD5加密

米脂
关注TA
已关注
手记 492
粉丝 88
获赞 590

首先得注意下MD5加密是不可逆的,其他的没什么难点

public class MD5_SHA{
 

/**

    s为需要加密的字段拼接后的字符串

*/
 public static String md5(String s) {
  char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    'A', 'B', 'C', 'D', 'E', 'F' };
  try {
   byte[] btInput = s.getBytes();
   // 获得MD5摘要算法的 MessageDigest 对象

   //// 如果输入“SHA”,就是实现SHA加密
   MessageDigest mdInst = MessageDigest.getInstance("MD5");
   // 使用指定的字节更新摘要
   mdInst.update(btInput);
   // 获得密文
   byte[] md = mdInst.digest();
   // 把密文转换成十六进制的字符串形式
   int j = md.length;
   char str[] = new char[j * 2];
   int k = 0;
   for (int i = 0; i < j; i++) {
    byte byte0 = md[i];
    str[k++] = hexDigits[byte0 >>> 4 & 0xf];
    str[k++] = hexDigits[byte0 & 0xf];
   }
   return new String(str);
  } catch (Exception e) {
   e.printStackTrace();
   return null;
  }
 }
}

原文链接:http://www.apkbus.com/blog-337311-60089.html

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP