MD5+Base64 Java与C#一致

 
<strong>//Java与C#MD5结果不一致是因为在两种语言中byte的范围不同,C#中byte的范围是0~255,而Java中byte的范围是-128~+127,所以要想Java与C#默认的MD5加密结果一致,则要将Java中byte为负的值加256变为正与C#中byte的范围一致</strong>

 

JAVA 

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

C# 

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

 

 

 

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值