欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

深入理解Android MD5數(shù)據(jù)加密

 更新時(shí)間:2016年09月21日 10:31:58   作者:總李寫(xiě)代碼  
在Android中需要對(duì)各種數(shù)據(jù)進(jìn)行加密的操作,比如用戶短信備份的數(shù)據(jù)加密、用戶賬戶登陸的密碼加密以及應(yīng)用于服務(wù)器連接傳遞重要數(shù)據(jù)的加密,用處非常的多,所以今天來(lái)總結(jié)一下MD5加密算法。

MD5加密

MD5是由MD2、MD3、MD4演變過(guò)來(lái)的,雖然MD5加密算法現(xiàn)在有些人已經(jīng)將其解開(kāi)了,但是它的加密機(jī)制依然很強(qiáng)大,我想絕大對(duì)數(shù)還是不會(huì)解開(kāi)的。MD5加密算法是單向加密,是不可逆的一種的加密方式,只能用你的密碼才能解開(kāi),要不就是會(huì)解密算法,否則想都別想解開(kāi)。

MD5加密的特點(diǎn)

     壓縮性:任意長(zhǎng)度的數(shù)據(jù),算出的MD5值長(zhǎng)度都是固定的。

     容易計(jì)算:從原數(shù)據(jù)計(jì)算出MD5值很容易。

     抗修改性:對(duì)原數(shù)據(jù)進(jìn)行任何改動(dòng),哪怕只修改1個(gè)字節(jié),所得到的MD5值都有很大區(qū)別。

     強(qiáng)抗碰撞:已知原數(shù)據(jù)和其MD5值,想找到一個(gè)具有相同MD5值的數(shù)據(jù)(即偽造數(shù)據(jù))是非常困難的。

MD5應(yīng)用場(chǎng)景

     一致性驗(yàn)證

     數(shù)字簽名

     安全訪問(wèn)認(rèn)證

MD5加密算法實(shí)現(xiàn)

1.)計(jì)算字符串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.)計(jì)算文件的MD5值

 // 計(jì)算文件的 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加密安全性探討:

雖然說(shuō)MD5加密本身是不可逆的,但并不是不可破譯的,網(wǎng)上有關(guān)MD5解密的網(wǎng)站數(shù)不勝數(shù),破解機(jī)制采用窮舉法,就是我們平時(shí)說(shuō)的跑字典。所以如何才能加大MD5破解的難度呢?

1.)對(duì)字符串多次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)然后進(jìn)行MD5加密

     用string明文的hashcode作為鹽,然后進(jìn)行MD5加密

     隨機(jī)生成一串字符串作為鹽,然后進(jìn)行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 "";
 }

總結(jié)

以上就是關(guān)于Android MD5數(shù)據(jù)加密的全部?jī)?nèi)容,希望能對(duì)Android開(kāi)發(fā)者們有所幫助,如有疑問(wèn)大家可以留言交流。

相關(guān)文章

最新評(píng)論