MD5+Base64 Java与C#一致

来源:互联网 发布:发起人肉骨茶 知乎 编辑:程序博客网 时间:2024/06/02 07:16

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));  }