---
title: "6位MD5加密解密"
author: "AndLi"
date: "2011-12-03T22:11:51Z"
tags: []
categories: ["移动开发"]
slug: "6-md5"
draft: false
lastmod: "2011-12-03T22:11:51Z"
id: 1290754
url: "https://andli.iteye.com/blog/1290754"
---

```
package com.test;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

public class Test {
	private static final String password = "111111";
	/**
	 * @author lilin
	 * @date 2011-8-13 上午11:21:38
	 * @Title: main
	 * @param @param args
	 * @return void
	 */
	private static final char code[] = { '1', '2', '3', '4', '5', '6' };

	public static void main(String[] args) {
		Test md5Obj = new Test();
		// 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());
	}

	/**
	 * 六位密码破解
	 */
	public static void Md5_6(String md5Password) {
		String testPassword;
		Test md5Obj = new Test();
		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 < code.length; 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];
								result = md5Obj.getMD5ofStr

								(testPassword);
								if (md5Password.equals(result)) {
									System.out.println("密码已经破解!");
									System.out.println("明文是:" + testPassword);
									System.out.println("密文是:" + md5Password);
									return;
								}
							}
						}
					}
				}
			}
		}
	}

	/**
	 * MD5 加密
	 */
	private String getMD5ofStr(String str) {
		MessageDigest messageDigest = null;

		try {
			messageDigest = MessageDigest.getInstance("MD5");

			messageDigest.reset();

			messageDigest.update(str.getBytes("UTF-8"));
		} catch (NoSuchAlgorithmException e) {
			System.out.println("NoSuchAlgorithmException caught!");
			System.exit(-1);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		byte[] byteArray = messageDigest.digest();

		StringBuffer md5StrBuff = new StringBuffer();

		for (int i = 0; i < byteArray.length; i++) {
			if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
				md5StrBuff.append("0").append(
						Integer.toHexString(0xFF & byteArray[i]));
			else
				md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
		}

		return md5StrBuff.toString();
	}

}
```