没有MessageDigest类的MD5实现

时间:2014-11-10 13:07:21

标签: java hash cryptography md5

....嗨,伙计们。我尝试使用没有java api的java实现MD5算法(例如MessageDigest,java.security包中的所有API。) 因为我不是美国人,所以我不擅长英语,所以我的代码中没有任何评论。对不起:(

无论如何,我想知道是否有人修复我的代码? 我已经阅读了RFC1321,但它对我没有任何帮助。 我试图整天修复我的代码,没有成就..我无法获得正确的哈希值。 我不知道什么是错的,在哪里修理.. 你能救我吗?

我的代码中有3个类。

首先,MD5 Class。

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class MD5 {    
    defineT d = new defineT();

    byte [] padding = {
            (byte)0x80, 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
    };

    public int transform(FileInputStream fis) throws IOException{
        int bData = 0;
        if (fis == null)
            System.out.println("File is Not Exist");
        else {
            BufferedInputStream bis = new BufferedInputStream(fis);
            int length = bis.available();       
            byte[] byteArray = new byte[length];

            bData = bis.read(byteArray, 0, length); 

            int LastBlocklen = length%64;
            boolean lastBlockLengthOver = LastBlocklen>=56;

            int blockCount = lastBlockLengthOver ? length/64+2 : length/64+1;

            byte [][] blocks = new byte[blockCount][64];

            for(int i =0 ; i < length/64 ; i++){    
                for(int j=0; j< 64; j++){
                    blocks[i][j] = byteArray[i*64+j];
                }
            }

            for(int i = length/64; i < length/64 + 1; i++){ 
                for (int j = 0; j < LastBlocklen; j++) {
                    blocks[i][j] = byteArray[i*64+j];
                }
            }

            //padding
            if(lastBlockLengthOver){    
                blocks[blockCount-2][LastBlocklen] = (byte)0x80;
                int j = 0;
                for(int i = LastBlocklen; i < 64; i++){ 
                    blocks[blockCount-2][i] = (byte)padding[j];
                    j++;
                }

                for(int i = 0; i < 56; i++){                
                    blocks[blockCount-1][i] = (byte)padding[j];     
                    j++;
                }             
            } 

            else {                  
                int j = 0;
                for(int i = LastBlocklen; i < 56; i++){
                    blocks[blockCount-1][i] = (byte)padding[j];
                    j++;
                }
            }

            Long AppendLength = (long)length << 3;
            String tmp = Long.toHexString(AppendLength).format("%016X", AppendLength);
            byte [] AppendHex = new byte[8];

            int j = 0;

            for (int i = 15; i > 0; i--){   
                i--;
                AppendHex[j] = (byte)Integer.parseInt(tmp.substring(i, i+2),16);
                j++;
            }

            j = 0;
            for(int i = 56; i < 64; i++) {      
                blocks[blockCount-1][i] = AppendHex[j];
                j++;
            }

            long [] M = new long[blockCount*16];

            for(int i = 0; i < blockCount; i++){
                for(j = 0; j<16; j++){
                    long t1 = ((Byte.toUnsignedInt(blocks[i][4*j]) << 24)  + 
                            (Byte.toUnsignedInt(blocks[i][4*j+1]) << 16) + 
                            (Byte.toUnsignedInt(blocks[i][4*j+2]) << 8) + 
                            (Byte.toUnsignedInt(blocks[i][4*j+3])) << 0);

                    M[i*16+j] = d.uint32(t1);
                }
            }

            int N = M.length;
            d.core(M, N);
            System.out.println(Long.toHexString(d.a)+Long.toHexString(d.b)+Long.toHexString(d.c)+Long.toHexString(d.d));
            bis.close();
        }
        return bData;
    }
}

这是defineT类。 因为java没有unsigned int类型,所以我暂时使用uint32()函数来获得unsigned类型的效果。

public class defineT {
    public long a = 0x67452301L;
    public long b = 0xEFCDAB89L;
    public long c = 0x98BADCFEL;
    public long d = 0x10325476L;

    long S11 = 7;
    long S12 = 12;
    long S13 = 17;
    long S14 = 22;

    long S21 = 5;
    long S22 = 9;
    long S23 = 14;
    long S24 = 20;

    long S31 = 4;
    long S32 = 11;
    long S33 = 16;
    long S34 = 23;

    long S41 = 6;
    long S42 = 10;
    long S43 = 15;
    long S44 = 21;

    long [] T = returnT();
    public static long uint32(long n) {
        return n & 0xFFFFFFFFL;
    }

    public long rotateLeft(long x, long n) { 
        return uint32(((x) << (n)) | ((x) >>> (32-(n))));
    }

    public long F(long x, long y, long z){
        return uint32(((x) & (y)) | ((~x) & (z)));
    }

    public long G(long x, long y, long z){
        return uint32(((x) & (z)) | ((y) & (~z)));
    }

    public long H(long x, long y, long z){
        return uint32((x) ^ (y) ^ (z));
    }

    public long I(long x, long y, long z){
        return uint32((y) ^ ((x) | (~z)));
    }

    public long FF(long a,long b, long c, long d,long x, long s,int i){
        (a) = uint32(a+F ((b), (c), (d)) + uint32(x) + uint32(T[i])); 
        (a) = uint32(rotateLeft ((a), (s))); 
        (a) = uint32(a+b);
        return a;
    }

    public long GG(long a,long b, long c, long d,long x, long s,int i){
        (a) = uint32(a+G ((b), (c), (d)) + uint32(x) + uint32(T[i])); 
        (a) = uint32(rotateLeft ((a), (s))); 
        (a) = uint32(a+b);
        return a;
    }

    public long HH(long a,long b, long c, long d,long x, long s,int i){
        (a) = uint32(a+ H ((b), (c), (d)) + uint32(x) + uint32(T[i])); 
        (a) = uint32(rotateLeft ((a), (s))); 
        (a) = uint32(a+b);
        return a;
    }

    public long II(long a,long b, long c, long d,long x, long s,int i){
        (a) = uint32(a +I ((b), (c), (d)) + uint32(x) + uint32(T[i]));
        (a) = uint32(rotateLeft ((a), (s))); 
        (a) = uint32(a+b);
        return a;
    }

    public void core(long M[], int N) {
        for (int i = 0; i < N/16; i++){
            long x[] = new long[16];

            for (int j = 0; j < 16; j++){
                x[j] = M[16*i + j];
            }

            long AA = a;
            long BB = b;
            long CC = c;
            long DD = d;

            /* Round 1 */

            a = FF (a, b, c, d, x[ 0], S11,  1); /* 1 */
            d = FF (d, a, b, c, x[ 1], S12,  2); /* 2 */
            c = FF (c, d, a, b, x[ 2], S13,  3); /* 3 */
            b = FF (b, c, d, a, x[ 3], S14,  4); /* 4 */
            a = FF (a, b, c, d, x[ 4], S11,  5); /* 5 */
            d = FF (d, a, b, c, x[ 5], S12,  6); /* 6 */
            c = FF (c, d, a, b, x[ 6], S13,  7); /* 7 */
            b = FF (b, c, d, a, x[ 7], S14,  8); /* 8 */
            a = FF (a, b, c, d, x[ 8], S11,  9); /* 9 */
            d = FF (d, a, b, c, x[ 9], S12, 10); /* 10 */
            c = FF (c, d, a, b, x[10], S13, 11); /* 11 */
            b = FF (b, c, d, a, x[11], S14, 12); /* 12 */
            a = FF (a, b, c, d, x[12], S11, 13); /* 13 */
            d = FF (d, a, b, c, x[13], S12, 14); /* 14 */
            c = FF (c, d, a, b, x[14], S13, 15); /* 15 */
            b = FF (b, c, d, a, x[15], S14, 16); /* 16 */


            /* Round 2 */
            a=GG (a, b, c, d, x[ 1], S21, 17); /* 17 */
            d=GG (d, a, b, c, x[ 6], S22, 18); /* 18 */
            c=GG (c, d, a, b, x[11], S23, 19); /* 19 */
            b=GG (b, c, d, a, x[ 0], S24, 20); /* 20 */

            a=GG (a, b, c, d, x[ 5], S21, 21); /* 21 */
            d=GG (d, a, b, c, x[10], S22, 22); /* 22 */
            c=GG (c, d, a, b, x[15], S23, 23); /* 23 */
            b=GG (b, c, d, a, x[ 4], S24, 24); /* 24 */

            a=GG (a, b, c, d, x[ 9], S21, 25); /* 25 */
            d=GG (d, a, b, c, x[14], S22, 26); /* 26 */
            c=GG (c, d, a, b, x[ 3], S23, 27); /* 27 */
            b=GG (b, c, d, a, x[ 8], S24, 28); /* 28 */

            a=GG (a, b, c, d, x[13], S21, 29); /* 29 */
            d=GG (d, a, b, c, x[ 2], S22, 30); /* 30 */
            c=GG (c, d, a, b, x[ 7], S23, 31); /* 31 */
            b=GG (b, c, d, a, x[12], S24, 32); /* 32 */

            /* Round 3 */
            a=HH (a, b, c, d, x[ 5], S31, 33); /* 33 */
            d=HH (d, a, b, c, x[ 8], S32, 34); /* 34 */
            c=HH (c, d, a, b, x[11], S33, 35); /* 35 */
            b=HH (b, c, d, a, x[14], S34, 36); /* 36 */

            a=HH (a, b, c, d, x[ 1], S31, 37); /* 37 */
            d=HH (d, a, b, c, x[ 4], S32, 38); /* 38 */
            c=HH (c, d, a, b, x[ 7], S33, 39); /* 39 */
            b=HH (b, c, d, a, x[10], S34, 40); /* 40 */

            a=HH (a, b, c, d, x[13], S31, 41); /* 41 */
            d=HH (d, a, b, c, x[ 0], S32, 42); /* 42 */
            c=HH (c, d, a, b, x[ 3], S33, 43); /* 43 */
            b=HH (b, c, d, a, x[ 6], S34,  44); /* 44 */

            a=HH (a, b, c, d, x[ 9], S31, 45); /* 45 */
            d=HH (d, a, b, c, x[12], S32, 46); /* 46 */
            c=HH (c, d, a, b, x[15], S33, 47); /* 47 */
            b=HH (b, c, d, a, x[ 2], S34, 48); /* 48 */

            /* Round 4 */
            a=II (a, b, c, d, x[ 0], S41, 49); /* 49 */
            d=II (d, a, b, c, x[ 7], S42, 50); /* 50 */
            c=II (c, d, a, b, x[14], S43, 51); /* 51 */
            b=II (b, c, d, a, x[ 5], S44, 52); /* 52 */

            a=II (a, b, c, d, x[12], S41, 53); /* 53 */
            d=II (d, a, b, c, x[ 3], S42, 54); /* 54 */
            c=II (c, d, a, b, x[10], S43, 55); /* 55 */
            b=II (b, c, d, a, x[ 1], S44, 56); /* 56 */

            a=II (a, b, c, d, x[ 8], S41, 57); /* 57 */
            d=II (d, a, b, c, x[15], S42, 58); /* 58 */
            c=II (c, d, a, b, x[ 6], S43, 59); /* 59 */
            b=II (b, c, d, a, x[13], S44, 60); /* 60 */

            a=II (a, b, c, d, x[ 4], S41, 61); /* 61 */
            d=II (d, a, b, c, x[11], S42, 62); /* 62 */
            c=II (c, d, a, b, x[ 2], S43, 63); /* 63 */
            b=II (b, c, d, a, x[ 9], S44, 64); /* 64 */

            a = uint32(a + AA);
            b = uint32(b + BB);
            c = uint32(c + CC);
            d = uint32(d + DD);

        }
    }
    public long [] returnT(){
        long [] T = new long[65];
        double rad = 1;
        long k =  4294967296L;

        for(int i = 1; i < 65; i++){
            T[i] = uint32( (long)(Math.pow(2, 32) * Math.abs(Math.sin(rad))) );
            rad++;
        }
        return T;
    }
}

这是Run Class。

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Run {
    public static void main(String[] args){
        MD5 m = new MD5();

        try {
            FileInputStream input = new FileInputStream("\\Users\\Administrator\\Desktop\\DataStructure.txt");
            m.transform(input);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException ie){
            ie.printStackTrace();
        }
    }
}`

谢谢!

0 个答案:

没有答案
相关问题