MD5算法的JAVA实现,结果出错,望指点

貌似掉线 貌似掉线 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
调试了很久了,还是找不出根源。希望大家能够帮忙一下,二十分感谢。
我剩下的分数不多,希望大家见谅。
//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);
}
}
...全文

相关推荐

评论 6
收藏 已收藏
分享
返回顶部
貌似掉线
貌似掉线 领域专家: 操作系统技术领域 领域专家: 操作系统技术领域
Java SE 62,621 成员 307,251 内容