Objective:
Whether the password is stored in the project or whether the file is the same file, will use the MD5 algorithm, today to summarize the MD5 encryption algorithm.
What is MD5 encryption?
MD5 English full name "Message-digest algorithm 5", translated by "Message digest algorithm 5", from the MD2, MD3, MD4 evolved, is a one-way encryption algorithm, is an irreversible encryption method.
What are the characteristics of MD5 encryption?
-
Compressibility: Any length of data, the calculated length of the MD5 value is fixed.
-
Easy to calculate: It is easy to calculate the MD5 value from the original data.
-
Anti-modification: Make any changes to the original data, even if only 1 bytes are modified, the resulting MD5 value is very different.
-
Strong anti-collision: known raw data and its MD5 value, it is very difficult to find a data with the same MD5 value (that is, falsification of data).
MD5 Application Scenario:
MD5 encryption Algorithm implementation: 1.) computes the string MD5 value
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.) Calculate the MD5 value of the file
/ / Calculate the MD5 value of the file
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;
}
or using 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 Encryption Security Discussion:
Although said MD5 encryption itself is irreversible, but is not unbreakable, online about MD5 decryption website Countless, crack mechanism use poor lifting method, is we usually say run dictionary. So how can we increase the difficulty of MD5 crack?
1.) Multiple MD5 encryption of strings
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 Add salt
The way to add salt is also varied
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 "";
}
Summarize:
The simple summary of MD5 encryption ends here.
Discussion and summary on MD5 encryption of Android data encryption