密码的“MD5”进行加密,确保密码的安全
2017-05-20 00:00
197 查看
MD5算法 哈希算法
MD5算法具有以下特点:
1、压缩性:任意长度的数据,算出的MD5值长度都是固定的。
2、容易计算:从原数据计算出MD5值很容易。
3、抗修改性:对原数据进行任何改动,哪怕只修改1个字节,所得到的MD5值都有很大区别。
4、强抗碰撞:已知原数据和其MD5值,想找到一个具有相同MD5值的数据(即伪造数据)是非常困难的。
import java.security.MessageDigest;
public class MD5Util {
public static String digest16(String inStr) {
return digest(inStr, 16);
}
public static String digest(String inStr) {
return digest(inStr, 32);
}
private static String digest(String inStr, int rang) {
MessageDigest md5 = null;
if ( StringUtil.isEmpty(inStr) ) {
return "";
}
try {
md5 = MessageDigest.getInstance("MD5");
} catch (Exception e) {
e.printStackTrace();
return "";
}
char[] charArray = inStr.toCharArray();
byte[] byteArray = new byte[charArray.length];
for (int i = 0; i < charArray.length; i++) {
byteArray[i] = (byte) charArray[i];
}
byte[] md5Bytes = md5.digest(byteArray);
StringBuilder hexValue = new StringBuilder();
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16)
hexValue.append("0");
hexValue.append(Integer.toHexString(val));
}
if ( rang == 32 ) {
return hexValue.toString();
} else {
return hexValue.toString().substring(8, 24);
}
}
public static void main(String args[]) {
String s = new String("admin");
System.out.println(digest(s));
}
}
MD5算法具有以下特点:
1、压缩性:任意长度的数据,算出的MD5值长度都是固定的。
2、容易计算:从原数据计算出MD5值很容易。
3、抗修改性:对原数据进行任何改动,哪怕只修改1个字节,所得到的MD5值都有很大区别。
4、强抗碰撞:已知原数据和其MD5值,想找到一个具有相同MD5值的数据(即伪造数据)是非常困难的。
import java.security.MessageDigest;
public class MD5Util {
public static String digest16(String inStr) {
return digest(inStr, 16);
}
public static String digest(String inStr) {
return digest(inStr, 32);
}
private static String digest(String inStr, int rang) {
MessageDigest md5 = null;
if ( StringUtil.isEmpty(inStr) ) {
return "";
}
try {
md5 = MessageDigest.getInstance("MD5");
} catch (Exception e) {
e.printStackTrace();
return "";
}
char[] charArray = inStr.toCharArray();
byte[] byteArray = new byte[charArray.length];
for (int i = 0; i < charArray.length; i++) {
byteArray[i] = (byte) charArray[i];
}
byte[] md5Bytes = md5.digest(byteArray);
StringBuilder hexValue = new StringBuilder();
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16)
hexValue.append("0");
hexValue.append(Integer.toHexString(val));
}
if ( rang == 32 ) {
return hexValue.toString();
} else {
return hexValue.toString().substring(8, 24);
}
}
public static void main(String args[]) {
String s = new String("admin");
System.out.println(digest(s));
}
}
相关文章推荐
- 使用phpass对密码进行加密安全度比md5高
- 使用MD5对密码进行加密
- java中使用MD5对密码进行加密
- 使用MD5对密码进行加密的功能的实现
- 对密码字段进行加密(MD5)
- MD5对密码进行加密,用Apache codec 替换Sun内部API sun.misc.BASE64Encoder
- Java的MD5对密码进行加密和验证的类
- java中使用md5对密码进行加密
- C#:使用MD5对密码进行加密!
- 使用MD5对密码进行加密
- Android 使用MD5对SharedPreferences密码进行加密
- 使用C# 用MD5进行密码加密源代码
- 数据类型转换、密码安全加密MD5方法
- 登录注册功能使用md5对密码进行加密
- 使用MD5对明文密码进行加密
- Android中使用MD5对密码进行加密
- java中使用MD5对密码进行加密
- [置顶] App安全登录之密码、通讯加密方法(MD5,Base64,RSA,AES)
- 用ASP的安全验证来对windows 2000的管理者密码进行在线修改。
- 为大家经常为md5加密过的常用admin,admin888,0000密码