MD5算法的JAVA实现,结果出错,望指点
调试了很久了,还是找不出根源。希望大家能够帮忙一下,二十分感谢。
我剩下的分数不多,希望大家见谅。
我剩下的分数不多,希望大家见谅。
//Create function array
interface FunctionArrayInterface {
int runIt(int X, int Y, int Z);
}
// fM, gM, hM and iM are basic MD5 methods.
class F implements FunctionArrayInterface {
public int runIt(int X, int Y, int Z) {
return (X & Y) | (~X & Z);
}
}
class G implements FunctionArrayInterface {
public int runIt(int X, int Y, int Z) {
return (X & Z) | (Y & ~Z);
}
}
class H implements FunctionArrayInterface {
public int runIt(int X, int Y, int Z) {
return X ^ Y ^ Z;
}
}
class I implements FunctionArrayInterface {
public int runIt(int X, int Y, int Z) {
return Y ^ (X | ~Z);
}
}/** 这是MD5的主要算法, 本算法只对字符串的MD5值进行计算 */
public class MD5 {
private static final byte SINGLE_ONE_BIT = (byte) 0X80;
private static final short BLOCK_SIZE = 512;
private static final short MOD_SIZE = 448;
private static final short APP_SIZE = 64;
private static final short BITS = 8;
// 定义四个主要参数
private static final int A = 0x67452301;
private static final int B = 0xEFCDAB89;
private static final int C = 0x98BADCFE;
private static final int D = 0x10325476;
// Constants for MD5 transform routine.
private static final byte X[][] = { { 0, 1 }, { 1, 5 }, { 5, 3 }, { 0, 7 } };
private static final byte S[][] = { { 7, 12, 17, 22 }, { 5, 9, 14, 20 },
{ 4, 11, 16, 23 }, { 6, 10, 15, 21 } };
// rotates x left s bits
static int rotateLeft(int x, int s) {
return (x << s) | (x >>> (32 - s));
}
// 计算要进行补位的位数
static int conutPaddingBits(int length) {
int mod = length * BITS % BLOCK_SIZE;
int cBits;
if (mod == 0) {
cBits = MOD_SIZE;
} else {
cBits = (MOD_SIZE + BLOCK_SIZE - mod) % BLOCK_SIZE;
}
return cBits / BITS;
}
// 对字符串进行补位
static byte[] appendPaddingBits(String argv) {
int msgLength = argv.length();
int bitLength = conutPaddingBits(msgLength);
long appLength = msgLength * BITS;
char[] temp = argv.toCharArray();
byte[] message = new byte[(int) (msgLength + bitLength + APP_SIZE / 8)];
try {
for (int i = 0; i < msgLength; i++) {
message[i] = (byte) temp[i];
}
for (int i = 1; i < bitLength; i++) {
message[(int) (msgLength + i)] = 0;
}
message[(int) msgLength] = SINGLE_ONE_BIT;
// Append length (before padding).
long tmpLength = appLength;
for (int i = 0; i < 8; i++) {
message[(int) (msgLength + bitLength + i)] = (byte) (tmpLength >>> ((7 - i) * 8));
}
} catch (IndexOutOfBoundsException evt) {
System.out.println(evt.getMessage());
}
return message;
}
// 计算字符串的MD5值的主要算法
static String calculateMD5(String message) {
FunctionArrayInterface[] auxi = new FunctionArrayInterface[4];
auxi[0] = new F();
auxi[1] = new G();
auxi[2] = new H();
auxi[3] = new I();
int[] chain = { A, B, C, D };
byte[] result = appendPaddingBits(message);
int[] temp = new int[16];
for (int k = 0; k < result.length; k += BLOCK_SIZE / BITS) {
int[] state = { A, B, C, D };
for (int i = 0; i < 16; i++) {
temp[i] = ((int) (result[k + i * 4 + 0] & 0xFF) << 24)
| ((int) (result[k + i * 4 + 1] & 0xFF) << 16)
| ((int) (result[k + i * 4 + 2] & 0xFF) << 8)
| ((int) result[k + i * 4 + 3] & 0xFF);
}
for (int roundIdx = 0, sIdx, wIdx; roundIdx < 4; roundIdx++) {
wIdx = X[roundIdx][0];
sIdx = 0;
for (int i = 0; i < 16; i++) {
// FF, GG, HH, and II transformations for rounds 1, 2, 3,
// and 4.
// Rotation is separate from addition to prevent
// recomputation.
state[sIdx] = state[(sIdx + 1) % 4]
+ rotateLeft(
(int) (state[sIdx]
+ auxi[roundIdx].runIt(
state[(sIdx + 1) % 4],
state[(sIdx + 2) % 4],
state[(sIdx + 3) % 4])
+ temp[wIdx] + (int) (Math.floor((0x40000000 * 4)
* Math.abs(Math.sin(roundIdx * 16
+ i + 1))))),
S[roundIdx][i % 4]);
sIdx = (sIdx + 3) % 4;
wIdx = (wIdx + X[roundIdx][1]) & 0x0F;
}
}
chain[0] += state[0];
chain[1] += state[1];
chain[2] += state[2];
chain[3] += state[3];
}
StringBuffer buf = new StringBuffer();
char[] bits = "0123456789ABCDEF".toCharArray();
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 8; j++) {
buf.append(bits[(int) (chain[i] >>> ((7 - j) * 4) & 0x0F)]);
}
}
return buf.toString();
}
public static void main(String[] args) {
String input = " ";
String result = MD5.calculateMD5(input);
System.out.println(result);
}
}
...全文
相关推荐
MD5和RSA加密算法Java完成实现
MD5和RSA加密算法Java完成实现,及实现RSA需要用的jar包。参见博文 http://blog.csdn.net/yanzi1225627/article/details/26508035
Java后端实现MD5加密的方法
有的时候因为业务的需要,我们要制作关于密码的修改功能。而关于密码的加密一般都是用MD5,那么这篇文章将介绍如何在Java的后端实现MD5加密,有需要的可以参考借鉴。
java 实现MD5加密算法的简单实例
主要介绍了java 实现MD5加密算法的简单实例的相关资料,这里提供实例帮助大家应用这样的加密算法,需要的朋友可以参考下
java实现MD5加密算法
实现了不可逆的MD5加密算法
java实现的MD5摘要算法完整实例
主要介绍了java实现的MD5摘要算法,结合完整实例形式分析了java实现md5单项加密的具体步骤与相关操作技巧,需要的朋友可以参考下


Java SE
