Base64、16进制字符串和byte[]互转

1.C#用法

安装BouncyCastle.Crypto依赖包

        /// <summary>
        /// Base64字符串转16进制字符串
        /// </summary>
        /// <param name="base64String"></param>
        /// <returns></returns>
        public static string Base64StringToHexString(string base64String)
        {
            var byteArr= Base64.Decode(base64String);
            var hexStr = Hex.ToHexString(byteArr);
            return hexStr;
        }

        /// <summary>
        /// 16进制字符串转Base64字符串
        /// </summary>
        /// <param name="base64String"></param>
        /// <returns></returns>
        public static string HexStringToBase64String(string hexString)
        {
            var byteArr = Hex.Decode(hexString);
            var base64Str = Base64.ToBase64String(byteArr);
            return base64Str;
        }

        /// <summary>
        /// 16进制字符串进行MD5加密
        /// </summary>
        /// <param name="str_md5_in">16进制字符串</param>
        /// <returns></returns>
        public static string HexStrToMD5Hash(string str_md5_in)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] bytes_md5_in = Hex.Decode(str_md5_in);
            byte[] bytes_md5_out = md5.ComputeHash(bytes_md5_in);
            string str_md5_out = BitConverter.ToString(bytes_md5_out);
            str_md5_out = str_md5_out.Replace("-", "");
            return str_md5_out;
        }

        /// <summary>
        /// Base64字符串进行MD5加密
        /// </summary>
        /// <param name="str_md5_in">Base64字符串</param>
        /// <returns></returns>
        public static string Base64StrToMD5Hash(string str_md5_in)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] bytes_md5_in = Base64.Decode(str_md5_in);
            byte[] bytes_md5_out = md5.ComputeHash(bytes_md5_in);
            string str_md5_out = BitConverter.ToString(bytes_md5_out);
            str_md5_out = str_md5_out.Replace("-", "");
            return str_md5_out;
        }

     /// <summary>
        /// 创建指定长度的0~9和A~Z的随机字符串
        /// </summary>
        /// <param name="strLength">字符长度 默认4</param>
        /// <returns></returns>
        public static string CreateRandomString(int strLength = 4)
        {
            string chkCode = string.Empty; 
            char[] character = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N','O', 'P','Q', 'R', 'S', 'T','U','V', 'W', 'X', 'Y','Z' };
            Random rnd = new Random();
            //生成验证码字符串 
            for (int i = 1; i <= strLength; i++)
            {
                chkCode += character[rnd.Next(character.Length)];
            }
            return chkCode;
        }

        /// <summary>
        /// 普通字符串转16进制字符串
        /// </summary>
        /// <param name="str">普通字符串</param>
        /// <returns>16进制字符串</returns>
        public static string StringToHexString(string str)
        {
            byte[] byteArr = Encoding.UTF8.GetBytes(str);
            var hexStr = BitConverter.ToString(byteArr).Replace("-", "");
            return hexStr;
        }

 

 

2.Java用法:

 

    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                    + Character.digit(s.charAt(i + 1), 16));
        }
        return data;
    }

    /**
     * hex转byte数组
     * @param hex
     * @return
     */
    public static byte[] hexToByte(String hex){
        int m = 0, n = 0;
        int byteLen = hex.length() / 2; // 每两个字符描述一个字节
        byte[] ret = new byte[byteLen];
        for (int i = 0; i < byteLen; i++) {
            m = i * 2 + 1;
            n = m + 1;
            int intVal = Integer.decode("0x" + hex.substring(i * 2, m) + hex.substring(m, n));
            ret[i] = Byte.valueOf((byte)intVal);
        }
        return ret;
    }

    /**
     * byte数组转hex
     * @param bytes
     * @return
     */
    public static String byteToHex(byte[] bytes){
        String strHex = "";
        StringBuilder sb = new StringBuilder("");
        for (int n = 0; n < bytes.length; n++) {
            strHex = Integer.toHexString(bytes[n] & 0xFF);
            sb.append((strHex.length() == 1) ? "0" + strHex : strHex); // 每个字节由两个字符表示,位数不够,高位补0
        }
        return sb.toString().trim();
    }
    
    public static void main(String[] args) {
        String hexStr = "0470dc9e408352055e7c8c10a4143870120a44dd61100182a7ccc31b24d1cce360a0270c8cabe038b642ef77a3f80309871d83a9eec3fc9198f1e4a35213b65e9d";
        byte[] byteArr=Hex.decode(hexStr);
        String base64Str=Base64.getEncoder().encodeToString(byteArr);
        byte[] byteArr1=Base64.getDecoder().decode(base64);
        String hexStr1=Hex.toHexString(byteArr1);
    }

 

posted @ 2022-08-09 16:07  杜子烟  阅读(1925)  评论(0编辑  收藏  举报