編輯:關於Android編程
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開發者們有所幫助,如有疑問大家可以留言交流。
通過案例分析Android WindowManager解析與騙取QQ密碼的過程
Windows Manager是一款窗口管理終端,可以遠程連接到Linux的X桌面進行管理,與服務器端產生一個session相互通信。最近在網上看見一個人在烏
Android官方導航欄ActionBar使用詳解
一、ActionBar概述ActionBar是androiD3.0以後新增的組件,主要用於標示應用程序以及用戶所處的位置並提供相關操作以及全局的導航功能。下
android開發之shape詳解
很多時候,使用shape能夠實現的效果,你用一張圖片也能夠實現,但問題是一張圖片無論你怎麼壓縮,它都不可能比一個xml文件小,因此,為了獲得一個高性能的手機App,我們在
Android中Service的詳細解釋與使用
Android中Service的詳細解釋與使用:概念:(1).Service可以說是一個在後台運行的Activity。它不是一個單獨的進程,它只需要應用告訴它要在後台做什