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; // MD5 Chaining Variable private static final long A = 0x67452301L; private static final long B = 0xEFCDAB89L; private static final long C = 0x98BADCFEL; private static final long D = 0x10325476L; // 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 long rotateLeft ( long x, long s){ return ( (int)x << s) | (( (int)x ) >>> ( 32 - s)); } // Pre-processing static long conutPaddingBits( long length){ long mod = length * BITS % BLOCK_SIZE; long cBits; if ( mod == 0){ cBits = MOD_SIZE; } else { cBits = ( MOD_SIZE + BLOCK_SIZE - mod) % BLOCK_SIZE; } return cBits / BITS; } static byte [] appendPaddingBits( String argv){ long msgLength = argv.length(); long bitLength = conutPaddingBits ( msgLength); long appLength = msgLength * BITS; char []temp = argv.toCharArray(); byte []message = new byte[(int) (msgLength + bitLength + APP_SIZE / 8)]; try{ // Save message //argv.getChars(0, (int) msgLength, temp, 0); // Pad out to mod 64. 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). byte []length = longToCharArray( appLength); for ( int i = 0; i < 8; i++) { message[ (int) (msgLength + bitLength + i)] = length[i]; } } catch ( IndexOutOfBoundsException evt){ System.out.println(evt.getMessage()); } return message; } // static byte [] longToCharArray ( long x){ byte [] charArray = new byte [8]; for (int i = 7; i >= 0; i--){ charArray[i] = (byte) ( x & 0xFF); x = x >>> 8; } return charArray; } 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(); long []chain = { A, B, C, D}; byte []result = appendPaddingBits(message); long []temp = new long [16]; System.out.println(result.length); for (int k = 0; k < result.length; k += BLOCK_SIZE / BITS){ long []state = { A, B, C, D}; for (int i = 0; i < 16; i++){ temp[ i] = ((long) result[ k + i * 4 + 3] & 0xFF) | ((long) (result[ k + i * 4 + 2] & 0xFF) << 8) | ((long) (result[ k + i * 4 + 1] & 0xFF) << 16) | ((long) (result[ k + i * 4 + 0] & 0xFF) << 24); } 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] + (long) 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(); } } //Create function array interface FunctionArrayInterface { long runIt(long X, long Y, long Z); } //fM, gM, hM and iM are basic MD5 methods. class F implements FunctionArrayInterface{ public long runIt(long X, long Y, long Z){ return (X & Y) | ( ~X & Z); } } class G implements FunctionArrayInterface{ public long runIt( long X, long Y, long Z){ return (X & Z) | ( Y & ~Z); } } class H implements FunctionArrayInterface{ public long runIt( long X, long Y, long Z){ return X ^ Y ^ Z; } } class I implements FunctionArrayInterface{ public long runIt( long X, long Y, long Z){ return Y ^ ( X | ~Z ); } }
计算出来结果是错的,求助。
[ 本帖最后由 洛云 于 2011-2-2 20:44 编辑 ]