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

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

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

MD5加密

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

MD5加密的特點

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

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

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

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

MD5應(yīng)用場景

     一致性驗證

     數(shù)字簽名

     安全訪問認(rèn)證

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

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加密本身是不可逆的,但并不是不可破譯的,網(wǎng)上有關(guān)MD5解密的網(wǎng)站數(shù)不勝數(shù),破解機制采用窮舉法,就是我們平時說的跑字典。所以如何才能加大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)然后進(jìn)行MD5加密

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

     隨機生成一串字符串作為鹽,然后進(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ù)加密的全部內(nèi)容,希望能對Android開發(fā)者們有所幫助,如有疑問大家可以留言交流。

相關(guān)文章

最新評論