深入理解Android MD5資料加密_Android

來源:互聯網
上載者:User

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開發人員們有所協助,如有疑問大家可以留言交流。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.