当前位置: 移动技术网 > 移动技术>移动开发>Android > 深入理解Android MD5数据加密

深入理解Android MD5数据加密

2019年07月24日  | 移动技术网移动技术  | 我要评论

md5加密

md5是由md2、md3、md4演变过来的,虽然md5加密算法现在有些人已经将其解开了,但是它的加密机制依然很强大,我想绝大对数还是不会解开的。md5加密算法是单向加密,是不可逆的一种的加密方式,只能用你的密码才能解开,要不就是会解密算法,否则想都别想解开。

md5加密的特点

     压缩性:任意长度的数据,算出的md5值长度都是固定的。

     容易计算:从原数据计算出md5值很容易。

     抗修改性:对原数据进行任何改动,哪怕只修改1个字节,所得到的md5值都有很大区别。

     强抗碰撞:已知原数据和其md5值,想找到一个具有相同md5值的数据(即伪造数据)是非常困难的。

md5应用场景

     一致性验证

     数字签名

     安全访问认证

md5加密算法实现

1.)计算字符串md5值

 public static string md5(string string) {
  if (textutils.isempty(string)) {
   return "";
  }
  messagedigest md5 = null;
  try {
   md5 = messagedigest.getinstance("md5");
   byte[] bytes = md5.digest(string.getbytes());
   string result = "";
   for (byte b : bytes) {
    string temp = integer.tohexstring(b & 0xff);
    if (temp.length() == 1) {
     temp = "0" + temp;
    }
    result += temp;
   }
   return result;
  } catch (nosuchalgorithmexception e) {
   e.printstacktrace();
  }
  return "";
 }

2.)计算文件的md5值

 // 计算文件的 md5 值
 public static string md5(file file) {
  if (file == null || !file.isfile() || !file.exists()) {
   return "";
  }
  fileinputstream in = null;
  string result = "";
  byte buffer[] = new byte[8192];
  int len;
  try {
   messagedigest md5 = messagedigest.getinstance("md5");
   in = new fileinputstream(file);
   while ((len = in.read(buffer)) != -1) {
    md5.update(buffer, 0, len);
   }
   byte[] bytes = md5.digest();

   for (byte b : bytes) {
    string temp = integer.tohexstring(b & 0xff);
    if (temp.length() == 1) {
     temp = "0" + temp;
    }
    result += temp;
   }
  } catch (exception e) {
   e.printstacktrace();
  }finally {
   if(null!=in){
    try {
     in.close();
    } catch (ioexception e) {
     e.printstacktrace();
    }
   }
  }
  return result;
 }

或者采用nio的方式

 public static string md5(file file) {
  string result = "";
  fileinputstream in = null;
  try {
   in = new fileinputstream(file);
   mappedbytebuffer bytebuffer = in.getchannel().map(filechannel.mapmode.read_only, 0, file.length());
   messagedigest md5 = messagedigest.getinstance("md5");
   md5.update(bytebuffer);
   byte[] bytes = md5.digest();
   for (byte b : bytes) {
    string temp = integer.tohexstring(b & 0xff);
    if (temp.length() == 1) {
     temp = "0" + temp;
    }
    result += temp;
   }
  } catch (exception e) {
   e.printstacktrace();
  } finally {
   if (null != in) {
    try {
     in.close();
    } catch (ioexception e) {
     e.printstacktrace();
    }
   }
  }
  return result;
 }

md5加密安全性探讨:

虽然说md5加密本身是不可逆的,但并不是不可破译的,网上有关md5解密的网站数不胜数,破解机制采用穷举法,就是我们平时说的跑字典。所以如何才能加大md5破解的难度呢?

1.)对字符串多次md5加密

 public static string md5(string string, int times) {
  if (textutils.isempty(string)) {
   return "";
  }
  string md5 = md5(string);
  for (int i = 0; i < times - 1; i++) {
   md5 = md5(md5);
  }
  return md5(md5);
 }

2.)md5加盐

加盐的方式也是多种多样

     string+key(盐值key)然后进行md5加密

     用string明文的hashcode作为盐,然后进行md5加密

     随机生成一串字符串作为盐,然后进行md5加密

 public static string md5(string string, string slat) {
  if (textutils.isempty(string)) {
   return "";
  }
  messagedigest md5 = null;
  try {
   md5 = messagedigest.getinstance("md5");
   byte[] bytes = md5.digest((string + slat).getbytes());
   string result = "";
   for (byte b : bytes) {
    string temp = integer.tohexstring(b & 0xff);
    if (temp.length() == 1) {
     temp = "0" + temp;
    }
    result += temp;
   }
   return result;
  } catch (nosuchalgorithmexception e) {
   e.printstacktrace();
  }
  return "";
 }

总结

以上就是关于android md5数据加密的全部内容,希望能对android开发者们有所帮助,如有疑问大家可以留言交流。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网