MD5+Base64 Java與C#一致

Java與C#MD5結果不一致是因爲在兩種語言中byte的範圍不同,C#中byte的範圍是0~255,而Java中byte的範圍是-128~+127,所以要想Java與C#默認的MD5加密結果一致,則要將Java中byte爲負的值加256變爲正與C#中byte的範圍一致


public String MD5(String basicText){
        byte[] basicTextByte = null;
        try {

            basicTextByte = basicText.getBytes("UTF-8");
            basicTextByte = DigestUtils.md5(basicTextByte);
            int []out = new int[basicTextByte.length] ;
            int i;
            for (int offset = 0; offset < basicTextByte.length; offset++) {
                i = basicTextByte[offset];
                if (i < 0)
                    i += 256;
                out[offset] = i;
            }
            return String.valueOf(toBase64(out));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }
public String toBase64(int[] data) throws UnsupportedEncodingException {
        if (data.length < 0)
            return "";
        int[] text = data;
        char[] base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();// 加密
        int lengthDataBits = text.length * 8;
        int fewerThan24bits = lengthDataBits % 24;// 加密字符串長度是否超過24
        int numberTriplets = lengthDataBits / 24;
        int number = fewerThan24bits != 0 ? numberTriplets + 1 : numberTriplets;// 計算字符串加密後字符總個數
        char[] toBase64Text = new char[number * 4];// 用來保存結果
        int s1, s2, s3;
        int index = 0, order = 0;
        for (int i = 0; i < numberTriplets; i++) {
            s1 = text[index++];
            s2 = text[index++];
            s3 = text[index++];
            toBase64Text[order++] = base[(s1 & 0xFC) >> 2];// 第一個6位
            toBase64Text[order++] = base[((s1 & 0x03) << 4) + ((s2 & 0xF0) >> 4)];// 第二個6位
            toBase64Text[order++] = base[((s2 & 0x0F) << 2) + ((s3 & 0xC0) >> 6)];// 第三個6位
            toBase64Text[order++] = base[s3 & 0x3f];// 第四個6位
        }
        /**
         * 一個字節的情況:將這一個字節的8個二進制位最後一組除了前面加二個0以外,後面再加4個0。這樣得到一個二位的Base64編碼,
         * 再在末尾補上兩個"="號。
         */
        if (fewerThan24bits == EIGHTBIT) {
            int last = text[index++];
            toBase64Text[order++] = base[(last & 0xFC) >> 2];
            toBase64Text[order++] = base[((last & 0x03) << 4)];
            toBase64Text[order++] = PAD;
            toBase64Text[order++] = PAD;
        }
        /**
         * 二個字節的情況:將這二個字節的一共16個二進制位,轉成三組,最後一組除了前面加兩個0以外,後面也要加兩個0。
         * 這樣得到一個三位的Base64編碼,再在末尾補上一個"="號。
         */
        if (fewerThan24bits == SIXTEENBIT) {
            s1 = text[index++];
            s2 = text[index++];
            toBase64Text[order++] = base[(s1 & 0xFC) >> 2];
            toBase64Text[order++] = base[(s1 & 0x03) << 4 + ((s2 & 0xF0) >> 4)];
            toBase64Text[order++] = base[(s2 & 0x0f) << 2];
            toBase64Text[order++] = PAD;
        }
        return new String(toBase64Text);
    }

public static string MD5(string basicText)
 {
            byte[] basicTextByte = Encoding.UTF8.GetBytes(basicText);
            HashAlgorithm hasher;       
            hasher = new MD5CryptoServiceProvider();                    
            return Convert.ToBase64String(hasher.ComputeHash(basicTextByte));
  }





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章