国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

深入理解Android MD5數據加密

2019-12-12 05:12:45
字體:
來源:轉載
供稿:網友

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乡城县| 武汉市| 海伦市| 三台县| 西乌珠穆沁旗| 叶城县| 延川县| 阳春市| 南充市| 资阳市| 三穗县| 额济纳旗| 西华县| 嘉定区| 德清县| 肇源县| 泗阳县| 连云港市| 苏尼特左旗| 青岛市| 红桥区| 衡水市| 定安县| 同江市| 封开县| 察雅县| 无锡市| 仙游县| 新密市| 海口市| 德保县| 洛宁县| 双鸭山市| 永胜县| 永顺县| 富民县| 隆德县| 岱山县| 阿克苏市| 兴安盟| 海宁市|