如何 使 Java、C# md5 加密的值保持一致

Java C# md5 加密值保持一致,一般是编码不一致造成的值不同

JAVA (加密:123456) C#(加密:123456)
UTF-8 e10adc3949ba59abbe56e057f20f883e UTF8 e10adc3949ba59abbe56e057f20f883e
UTF-16LE ce0bfd15059b68d67688884d7a3d3e8c Unicode ce0bfd15059b68d67688884d7a3d3e8c
US-ASCII e10adc3949ba59abbe56e057f20f883e ASCII e10adc3949ba59abbe56e057f20f883e
ISO-8859-1 e10adc3949ba59abbe56e057f20f883e --- ---
UTF-16BE ef4dafda494ad517e9823ae7d102a4c8 BigEndianUnicode ef4dafda494ad517e9823ae7d102a4c8
UTF-16LE ce0bfd15059b68d67688884d7a3d3e8c
UTF-16 5231722c0787fbf7b277a4a136f6e245
--- --- UTF32 4fc043750a2441defd8e35d2e23e84f0
--- --- UTF7 e10adc3949ba59abbe56e057f20f883e

Java 代码如下

package com.vipsoft.core.util;

import java.security.MessageDigest;
public class Md5Helper {

    public static final String EMPTY_STRING = "";
    private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

    private static String byteToHexString(byte b) {
        int n = b;
        if (n < 0) {
            n = 256 + n;
        }
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }

    private static String byteArrayToHexString(byte[] b) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            sb.append(byteToHexString(b[i]));
        }
        return sb.toString();
    }

    public static String MD5Encode(String origin) {
        String result = null;
        try {
            result = origin;
            MessageDigest md = MessageDigest.getInstance("MD5");
            result = byteArrayToHexString(md.digest(result.getBytes("UTF-8")));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
}

C# 代码如下

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace VipSoft.Core
{
    public class Md5Helper
    {
        public static string MD5Encode(string origin)
        {
            var md5 = new MD5CryptoServiceProvider();
            byte[] clearBytes = System.Text.Encoding.UTF8.GetBytes(origin);
            byte[] targetData = md5.ComputeHash(clearBytes);
            string byte2String = BitConverter.ToString(targetData); 
            return byte2String.Replace("-","").ToLower();
        }
    }
}

C# => System.Text.Encoding.UTF8
JAVA => StandardCharsets

package java.nio.charset;

/**
 * Constant definitions for the standard {@link Charset Charsets}. These
 * charsets are guaranteed to be available on every implementation of the Java
 * platform.
 *
 * @see <a href="Charset#standard">Standard Charsets</a>
 * @since 1.7
 */
public final class StandardCharsets {

    private StandardCharsets() {
        throw new AssertionError("No java.nio.charset.StandardCharsets instances for you!");
    }
    /**
     * Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the
     * Unicode character set
     */
    public static final Charset US_ASCII = Charset.forName("US-ASCII");
    /**
     * ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1
     */
    public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
    /**
     * Eight-bit UCS Transformation Format
     */
    public static final Charset UTF_8 = Charset.forName("UTF-8");
    /**
     * Sixteen-bit UCS Transformation Format, big-endian byte order
     */
    public static final Charset UTF_16BE = Charset.forName("UTF-16BE");
    /**
     * Sixteen-bit UCS Transformation Format, little-endian byte order
     */
    public static final Charset UTF_16LE = Charset.forName("UTF-16LE");
    /**
     * Sixteen-bit UCS Transformation Format, byte order identified by an
     * optional byte-order mark
     */
    public static final Charset UTF_16 = Charset.forName("UTF-16");
}

Charset 是 Java 中的一个类,它代表了字符集。字符集是一组字符的集合,每个字符都有一个唯一的数字表示。Charset 类提供了一组预定义的常量来表示常见的字符集。

以下是一些常见的 Charset 编码常量:

  1. US-ASCII

    • Charset.forName("US-ASCII")
    • 7位ASCII字符集,包括128个字符。
  2. ISO-8859-1 (Latin-1)

    • Charset.forName("ISO-8859-1")
    • 对西欧语言进行编码,是ASCII的超集。
  3. UTF-8

    • Charset.forName("UTF-8")
    • 一种变长编码,用于表示Unicode字符集。它是目前最常用的编码之一,能够表示世界上几乎所有的书写系统。
  4. UTF-16

    • Charset.forName("UTF-16")
    • 使用16位单元对Unicode字符进行编码。它能够表示所有的Unicode字符。
  5. UTF-16BE 和 UTF-16LE

    • Charset.forName("UTF-16BE")
    • Charset.forName("UTF-16LE")
    • 它们是UTF-16的两种变体,分别代表大端和小端字节序。
  6. 其他
    还有许多其他的字符集和编码,例如 Charset.forName("GB2312")(用于简体中文)和 Charset.forName("Shift_JIS")(用于日语)等。

当处理文本数据时,选择合适的字符集非常重要,因为错误的字符集可能会导致乱码或数据丢失。UTF-8 由于其广泛的兼容性和能够表示几乎所有的Unicode字符,通常是一个很好的选择。

https://www.cnblogs.com/zengguoyu/p/3973483.html

posted @ 2023-12-22 15:38  VipSoft  阅读(191)  评论(0编辑  收藏  举报