⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 md5encoder.java

📁 java写的MD5加密程序
💻 JAVA
字号:
/**
 * 
 */
package md5encoder;

/**
 * md5 类实现了RSA Data Security, Inc.在提交给IETF 的RFC1321中的MD5 message-digest 算法。
 */

public class MD5Encoder
{
	/**
	 * 补位:用于将数据扩展至K*512+448位。即K*64+56个字节,K为整数。补一个1,然后补0至满足上述要求。
	 */
	static final byte[]	PADDING	=
			{ -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0	};

	/*
	 * 下面这些S11-S44实际上是一个4*4的矩阵,在原始的C实现中是用#define 实现的, 这里把它们实现成为static
	 * final是表示了只读,切能在同一个进程空间内的多个Instance间共享
	 * 
	 *  7  5  4  6
	 * 12  9 11 10
	 * 17 14 16 15
	 * 22 20 23 21
	 * 
	 */
	static final int	S[]		=
								{ 7, 5, 4, 6, 12, 9, 11, 10, 17, 14, 16, 15,
			22, 20, 23, 21		};

	/**
	 * 用来把一个byte类型的数转换成十六进制的ASCII表示,
	 * @param	二进制数据
	 * @return	转换后的十六进制表示的字符串
	 */
	public static String byteToHex(byte b)
	{
		char[] hexDigit =
		{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
				'E', 'F' };
		char hb, lb;
		hb = hexDigit[(b >>> 4) & 0X0F];
		lb = hexDigit[b & 0X0F];
		String s = "" + hb + lb;
		return s;
	}

	/**
	 * 自定义的把byte型的变量按照不考虑正负号的原则的扩展成long型变量
	 * @param	扩展前的byte型数据
	 * @return	扩展后的long型数据
	 */
	public static long unsigned(byte b)
	{
		return b < 0 ? b & 0x7F + 128 : b;
	}

	private byte[]	buffer		= new byte[2048]; // 缓冲
	private int		curBufLen	= 0;			// 缓冲的当前长度

	private long[]	count		= new long[2];	// 位长以2^64为进位

	/* digest是最新一次计算结果的2进制内部表示,表示128bit的MD5值. */
	private byte[]	digest		= new byte[16];

	/*
	 * 下面的三个成员是MD5计算过程中用到的3个核心数据, 在原始的C实现中被定义到MD5_CTX结构中
	 */
	private long[]	state		= new long[4];	// state数组依次为ABCD

	/**
	 * MD5Encoder类的标准无参的构造函数,创建类的实例但不执行操作
	 */
	public MD5Encoder()
	{
		return;
	}

	/**
	 * 最主要的公共方法,用于加密字符串。
	 * @param	要进行MD5变换的字符串
	 * @return	变换完的结果
	 */
	public String getEncodedString(String inBuf)
	{
		String digestHexStr = "";

		init(); //初始化
		padding(inBuf); // 填充位
		encode(); // 加密
		longToByte(digest, state, 16); // 将结果转换成二进制
		for (int i = 0; i < 16; i++) // 将转换后的十六进制代码作为字符串输出
		{
			digestHexStr += byteToHex(digest[i]);
		}
		return digestHexStr;
	}

	/*
	 * 加密函数
	 */
	private void encode()
	{
		byte[] block = new byte[64];
		int i;
		for (i = 0; i < curBufLen; i += 64)
		{
			memoryCopy(block, 0, buffer, i, 64);
			transform(block);
		}
	}

	/*
	 * 位填充函数 
	 */
	private void padding(String inBuf)
	{

		byte[] bits = new byte[8];
		int index, padLen;

		curBufLen = memoryCopy(buffer, curBufLen, inBuf.getBytes(), 0, inBuf
				.length());
		/* 保存位长 */
		count[0] = inBuf.length();
		longToByte(bits, count, 8);

		/* 将输出长度填充至 56 mod 64 */
		index = curBufLen % 64;
		padLen = (index < 56) ? (56 - index) : (120 - index);
		try
		{
			curBufLen = memoryCopy(buffer, curBufLen, PADDING, 0, padLen);
			curBufLen = memoryCopy(buffer, curBufLen, bits, 0, 8);
		} catch(Exception e)
		{
			System.err.println("Error: The source string is too long. Only "+buffer.length+" byte(s) is allowed. (Code: 1)");
		}
	}

	/*
	 * byteToLong把byte数组按顺序合成成long数组,因为java的long类型是64bit的,
	 * 只合成低32bit,高32bit清零,以适应原始C实现的用途
	 */
	private void byteToLong(long[] output, byte[] input, int len)
	{
		int i, j;

		for (i = 0, j = 0; j < len; i++, j += 4)
			output[i] = unsigned(input[j]) | (unsigned(input[j + 1]) << 8)
					| (unsigned(input[j + 2]) << 16)
					| (unsigned(input[j + 3]) << 24);

		return;
	}

	/*
	 * F, G, H ,I 是4个基本的MD5函数,在原始的MD5的C实现中,由于它们是
	 * 简单的位运算,可能出于效率的考虑把它们实现成了宏,在java中,我们把它们 实现成了private方法,名字保持了原来C中的。
	 */
	private long f(long x, long y, long z)
	{
		return (x & y) | ((~x) & z);

	}

	private long g(long x, long y, long z)
	{
		return (x & z) | (y & (~z));
	}

	private long h(long x, long y, long z)
	{
		return x ^ y ^ z;
	}

	private long i(long x, long y, long z)
	{
		return y ^ (x | (~z));
	}

	/*
	 * FF,GG,HH和II将调用F,G,H,I进行进一步变换 进行四轮转换,为避免重复计算,循环被分开。
	 */
	private long FF(long a, long b, long c, long d, long x, long s, long ac)
	{
		a += f(b, c, d) + x + ac;
		a = ((int) a << s) | ((int) a >>> (32 - s));
		a += b;
		return a;
	}

	private long GG(long a, long b, long c, long d, long x, long s, long ac)
	{
		a += g(b, c, d) + x + ac;
		a = ((int) a << s) | ((int) a >>> (32 - s));
		a += b;
		return a;
	}

	private long HH(long a, long b, long c, long d, long x, long s, long ac)
	{
		a += h(b, c, d) + x + ac;
		a = ((int) a << s) | ((int) a >>> (32 - s));
		a += b;
		return a;
	}

	private long II(long a, long b, long c, long d, long x, long s, long ac)
	{
		a += i(b, c, d) + x + ac;
		a = ((int) a << s) | ((int) a >>> (32 - s));
		a += b;
		return a;
	}

	/* init是一个初始化函数,初始化核心变量,装入标准的幻数 */
	private void init()
	{
		count[0] = 0L;
		count[1] = 0L;

		// 四个32位被称作链接变量的整数参数
		state[0] = 0x67452301L;
		state[1] = 0xefcdab89L;
		state[2] = 0x98badcfeL;
		state[3] = 0x10325476L;

		curBufLen = 0;

		return;
	}

	/*
	 * longToByte把long数组按顺序拆成byte数组,因为java的long类型是64bit的, 只拆低32bit,以适应原始C实现的用途
	 */
	private void longToByte(byte[] output, long[] input, int len)
	{
		int i, j;

		for (i = 0, j = 0; j < len; i++, j += 4)
		{
			output[j] = (byte) (input[i] & 0xffL);
			output[j + 1] = (byte) ((input[i] >>> 8) & 0xffL);
			output[j + 2] = (byte) ((input[i] >>> 16) & 0xffL);
			output[j + 3] = (byte) ((input[i] >>> 24) & 0xffL);
		}
	}

	/*
	 * md5Memcpy是一个内部使用的byte数组的块拷贝函数,从input的inpos开始把len长度的
	 * 字节拷贝到output的outpos位置开始
	 */

	private int memoryCopy(byte[] output, int outpos, byte[] input, int inpos,
			int len)
	{
		int i;
		for (i = 0; i < len; i++)
		{
			output[outpos + i] = input[inpos + i];
		}
		return (outpos + len);
	}

	/*
	 * transform是MD5核心变换程序,由update调用,block是分块的原始字节
	 */
	private void transform(byte block[])
	{
		long a, b, c, d;
		long[] x = new long[16];

		a = state[0];
		b = state[1];
		c = state[2];
		d = state[3];
		byteToLong(x, block, 64);

		/* 第1轮 */
		a = FF(a, b, c, d, x[0], S[0], 0xd76aa478L); /* 1 */
		d = FF(d, a, b, c, x[1], S[1], 0xe8c7b756L); /* 2 */
		c = FF(c, d, a, b, x[2], S[2], 0x242070dbL); /* 3 */
		b = FF(b, c, d, a, x[3], S[3], 0xc1bdceeeL); /* 4 */
		a = FF(a, b, c, d, x[4], S[0], 0xf57c0fafL); /* 5 */
		d = FF(d, a, b, c, x[5], S[1], 0x4787c62aL); /* 6 */
		c = FF(c, d, a, b, x[6], S[2], 0xa8304613L); /* 7 */
		b = FF(b, c, d, a, x[7], S[3], 0xfd469501L); /* 8 */
		a = FF(a, b, c, d, x[8], S[0], 0x698098d8L); /* 9 */
		d = FF(d, a, b, c, x[9], S[1], 0x8b44f7afL); /* 10 */
		c = FF(c, d, a, b, x[10], S[2], 0xffff5bb1L); /* 11 */
		b = FF(b, c, d, a, x[11], S[3], 0x895cd7beL); /* 12 */
		a = FF(a, b, c, d, x[12], S[0], 0x6b901122L); /* 13 */
		d = FF(d, a, b, c, x[13], S[1], 0xfd987193L); /* 14 */
		c = FF(c, d, a, b, x[14], S[2], 0xa679438eL); /* 15 */
		b = FF(b, c, d, a, x[15], S[3], 0x49b40821L); /* 16 */

		/* 第2轮 */
		a = GG(a, b, c, d, x[1], S[4], 0xf61e2562L); /* 17 */
		d = GG(d, a, b, c, x[6], S[5], 0xc040b340L); /* 18 */
		c = GG(c, d, a, b, x[11], S[6], 0x265e5a51L); /* 19 */
		b = GG(b, c, d, a, x[0], S[7], 0xe9b6c7aaL); /* 20 */
		a = GG(a, b, c, d, x[5], S[4], 0xd62f105dL); /* 21 */
		d = GG(d, a, b, c, x[10], S[5], 0x2441453L); /* 22 */
		c = GG(c, d, a, b, x[15], S[6], 0xd8a1e681L); /* 23 */
		b = GG(b, c, d, a, x[4], S[7], 0xe7d3fbc8L); /* 24 */
		a = GG(a, b, c, d, x[9], S[4], 0x21e1cde6L); /* 25 */
		d = GG(d, a, b, c, x[14], S[5], 0xc33707d6L); /* 26 */
		c = GG(c, d, a, b, x[3], S[6], 0xf4d50d87L); /* 27 */
		b = GG(b, c, d, a, x[8], S[7], 0x455a14edL); /* 28 */
		a = GG(a, b, c, d, x[13], S[4], 0xa9e3e905L); /* 29 */
		d = GG(d, a, b, c, x[2], S[5], 0xfcefa3f8L); /* 30 */
		c = GG(c, d, a, b, x[7], S[6], 0x676f02d9L); /* 31 */
		b = GG(b, c, d, a, x[12], S[7], 0x8d2a4c8aL); /* 32 */

		/* 第3轮 */
		a = HH(a, b, c, d, x[5], S[8], 0xfffa3942L); /* 33 */
		d = HH(d, a, b, c, x[8], S[9], 0x8771f681L); /* 34 */
		c = HH(c, d, a, b, x[11], S[10], 0x6d9d6122L); /* 35 */
		b = HH(b, c, d, a, x[14], S[11], 0xfde5380cL); /* 36 */
		a = HH(a, b, c, d, x[1], S[8], 0xa4beea44L); /* 37 */
		d = HH(d, a, b, c, x[4], S[9], 0x4bdecfa9L); /* 38 */
		c = HH(c, d, a, b, x[7], S[10], 0xf6bb4b60L); /* 39 */
		b = HH(b, c, d, a, x[10], S[11], 0xbebfbc70L); /* 40 */
		a = HH(a, b, c, d, x[13], S[8], 0x289b7ec6L); /* 41 */
		d = HH(d, a, b, c, x[0], S[9], 0xeaa127faL); /* 42 */
		c = HH(c, d, a, b, x[3], S[10], 0xd4ef3085L); /* 43 */
		b = HH(b, c, d, a, x[6], S[11], 0x4881d05L); /* 44 */
		a = HH(a, b, c, d, x[9], S[8], 0xd9d4d039L); /* 45 */
		d = HH(d, a, b, c, x[12], S[9], 0xe6db99e5L); /* 46 */
		c = HH(c, d, a, b, x[15], S[10], 0x1fa27cf8L); /* 47 */
		b = HH(b, c, d, a, x[2], S[11], 0xc4ac5665L); /* 48 */

		/* 第4轮 */
		a = II(a, b, c, d, x[0], S[12], 0xf4292244L); /* 49 */
		d = II(d, a, b, c, x[7], S[13], 0x432aff97L); /* 50 */
		c = II(c, d, a, b, x[14], S[14], 0xab9423a7L); /* 51 */
		b = II(b, c, d, a, x[5], S[15], 0xfc93a039L); /* 52 */
		a = II(a, b, c, d, x[12], S[12], 0x655b59c3L); /* 53 */
		d = II(d, a, b, c, x[3], S[13], 0x8f0ccc92L); /* 54 */
		c = II(c, d, a, b, x[10], S[14], 0xffeff47dL); /* 55 */
		b = II(b, c, d, a, x[1], S[15], 0x85845dd1L); /* 56 */
		a = II(a, b, c, d, x[8], S[12], 0x6fa87e4fL); /* 57 */
		d = II(d, a, b, c, x[15], S[13], 0xfe2ce6e0L); /* 58 */
		c = II(c, d, a, b, x[6], S[14], 0xa3014314L); /* 59 */
		b = II(b, c, d, a, x[13], S[15], 0x4e0811a1L); /* 60 */
		a = II(a, b, c, d, x[4], S[12], 0xf7537e82L); /* 61 */
		d = II(d, a, b, c, x[11], S[13], 0xbd3af235L); /* 62 */
		c = II(c, d, a, b, x[2], S[14], 0x2ad7d2bbL); /* 63 */
		b = II(b, c, d, a, x[9], S[15], 0xeb86d391L); /* 64 */

		state[0] += a;
		state[1] += b;
		state[2] += c;
		state[3] += d;

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -