MD5加密算法:
package other;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/*
* MD5 算法
*/
public class MD5 {
// 全局数组
private final static String[] strDigits = { "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
public MD5() {
}
// 返回形式为数字跟字符串
private static String byteToArrayString(byte bByte) {
int iRet = bByte;
// System.out.println("iRet="+iRet);
if (iRet < 0) {
iRet += 256;
}
int iD1 = iRet / 16;
int iD2 = iRet % 16;
return strDigits[iD1] + strDigits[iD2];
}
// 返回形式只为数字
private static String byteToNum(byte bByte) {
int iRet = bByte;
System.out.println("iRet1=" + iRet);
if (iRet < 0) {
iRet += 256;
}
return String.valueOf(iRet);
}
// 转换字节数组为16进制字串
private static String byteToString(byte[] bByte) {
StringBuffer sBuffer = new StringBuffer();
for (int i = 0; i < bByte.length; i++) {
sBuffer.append(byteToArrayString(bByte[i]));
}
return sBuffer.toString();
}
public static String GetMD5Code(String strObj) {
String resultString = null;
try {
resultString = new String(strObj);
MessageDigest md = MessageDigest.getInstance("MD5");
// md.digest() 该函数返回值为存放哈希值结果的byte数组
resultString = byteToString(md.digest(strObj.getBytes()));
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
}
return resultString;
}
public static void main(String[] args) {
MD5 getMD5 = new MD5();
System.out.println(getMD5.GetMD5Code("000000"));
}
}
MD5解密算法:
package md5;
import java.util.Date;
/**
* @author greatwqs
* @see Md5密码破解
*/
public class MD5解密 {
private static final char code[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z', ',', '.', '/', ';', '\'', ':', '"',
'[', ']', '{', '}', '\\', '|', '!', '@', '#', '$', '%', '^', '&',
'*', '(', ')', '-', '_', '+', '=', '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9' };
/**
* 六位密码破解
*/
public static void Md5_6(String md5Password) {
String testPassword;
MD5 md5Obj = new MD5();
String result;
for (int a = 0; a < code.length; a++) {
testPassword = "";
testPassword += code[a];
for (int b = 0; b < code.length; b++) {
testPassword = testPassword.substring(0, 1);
testPassword += code[b];
for (int c = 0; c < codeength; c++) {
testPassword = testPassword.substring(0, 2);
testPassword += code[c];
for (int d = 0; d < code.length; d++) {
testPassword = testPassword.substring(0, 3);
testPassword += code[d];
for (int e = 0; e < code.length; e++) {
testPassword = testPassword.substring(0,
4);
testPassword += code[e];
for (int f = 0; f < code.length; f++) {
testPassword = testPassword.substring(0, 5);
testPassword += code[f];
// System.out.println(testPassword);
result = md5Obj.getMD5ofStr
(testPassword);
if (md5Password.equals(result)) {
System.out.println("密码
已经破解!");
System.out.println("明文
是:" + testPassword);
System.out.println("密文
是:" + md5Password);
return;
}
}
}
}
}
}
}
}
/**
* 七位密码破解,写法详见六位密码破解
*/
public static void Md5_7(String md5Password) {
}
/**
* 八位密码破解,写法详见六位密码破解
*/
public static void Md5_8(String md5Password) {
}
public static void main(String[] args) {
MD5 md5Obj = new MD5();
// MD5加密对象
String md5Password = md5Obj.getMD5ofStr(password);
// 把这个认为我要找到的经过加密的密码
System.out.println("密码破测试中!");
System.out.println("明文是:" + password);
System.out.println("密文是:" + md5Password);
System.out.println("程序时间计时器!");
System.out.println("开始时间:" + new Date());
Md5_6(md5Password);
// 依次调用6位破解到20位破解..
// Md5_7(testResult);
System.out.println("结束时间:" + new Date());
}
private static final String password = "aaa918";
/**
* 这里只是一个实现的方法, 在自己破解的时候把自己的密文直接贴出来,
* 进行破解, 这里只是进行一个aaaBc8加密的测试
*/
private static final String MD5PWD = "你的数据库中的密文";
}
}