C#版本的JavaMd5加密

该代码实现了一个Java类,包含两个主要方法:JavaMd5用于进行Java兼容的MD5加密,内部调用了MD5_16方法进行16位MD5计算。Base64类提供了base64编码功能,包括对字节数组的编码。整个程序主要用于字符串的MD5加密及Base64转换。
摘要由CSDN通过智能技术生成
    public class JavaMd5Encrypt
    {
        /// <summary>
        /// Java适用的Md5加密
        /// </summary>
        /// <param name="encryptStr"></param>
        /// <returns></returns>
        public static string JavaMd5(string encryptStr)
        { 
            return Base64.encode(MD5_16(encryptStr));
        }

        /// <summary>
        /// Md5加密
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private static sbyte[] MD5_16(string input)
        {
            byte[] target = UTF8Encoding.UTF8.GetBytes(input);
            byte[] digest;
            MD5 md5 = MD5.Create();
            digest = md5.ComputeHash(target); // 想对多个byte[]对象进行MD5 Hash

            sbyte[] bSByte = new sbyte[digest.Length];
            for (int i = 0; i < digest.Length; i++)
            {
                if (digest[i] > 127)
                    bSByte[i] = (sbyte)(digest[i] - 256);
                else
                    bSByte[i] = (sbyte)digest[i];
            }
            return bSByte;
        }
    }

public class Base64
    {

        private static char[] encodeTable = new char[] {
			'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
			'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
			'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 
			'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
			'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
			'+', '/' };

        private static sbyte[] decodeTable = new sbyte[] {
			0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
			0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
			0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x3e, 0x7f, 0x7f, 0x7f, 0x3f,
			0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x7f, 0x7f, 0x7f, 0x40, 0x7f, 0x7f,
			0x7f, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
			0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f,
			0x7f, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
			0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f };

        /**
         * base64 
         * @param srcBuf
         * @param offset
         * @param length
         * @return
         */
        public static String encode(sbyte[] srcBuf, int offset, int length)
        {

            char[] DecBuf = new char[(length << 1) + 4];
            int i = offset, end = offset + length;
            int size = end - 2, counts = 0;
            short a, b, c;
            while (i < size)
            {
                a = srcBuf[i++];
                b = srcBuf[i++];
                c = srcBuf[i++];
                DecBuf[counts++] = encodeTable[(a >> 2) & 0x3f];
                DecBuf[counts++] = encodeTable[((b >> 4) & 0x0f) | ((a << 4) & 0x3f)];
                DecBuf[counts++] = encodeTable[((c >> 6) & 0x3) | ((b << 2) & 0x3f)];
                DecBuf[counts++] = encodeTable[c & 0x3f];
            }
            if (end - i == 2)
            {
                a = srcBuf[i++];
                b = srcBuf[i++];
                DecBuf[counts++] = encodeTable[(a >> 2) & 0x3f];
                DecBuf[counts++] = encodeTable[((b >> 4) & 0x0f)
                        | ((a << 4) & 0x3f)];
                DecBuf[counts++] = encodeTable[(b << 2) & 0x3f];
                DecBuf[counts++] = '=';
            }
            else if (end - i == 1)
            {
                a = srcBuf[i++];
                DecBuf[counts++] = encodeTable[(a >> 2) & 0x3f];
                DecBuf[counts++] = encodeTable[(a << 4) & 0x3f];
                DecBuf[counts++] = '=';
                DecBuf[counts++] = '=';
            }
            return new String(DecBuf, 0, counts);
        }
        /**
         * base64
         * @param srcBuf
         * @return
         */
        public static String encode(sbyte[] srcBuf)
        {
            return encode(srcBuf, 0, srcBuf.Length);
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值