如何将我的简短 C# 代码转换为 java

How can I convert my short C# code to java

我正在尝试将以下 C# 代码转换为 java,以便将其添加到我的新应用程序中。 C# 代码是下面的简短代码,JAVA 是我设法获取的代码,但我确信我没有进行相同的转换,因为 JAVA 的输出与C#。 要使其与 C# 相同,需要对 JAVA 进行哪些更改?

    private static string GetMd5Sum(string productIdentifier)
    {
        var enc = Encoding.Unicode.GetEncoder();
        var unicodeText = new byte[productIdentifier.Length * 2];
        enc.GetBytes(productIdentifier.ToCharArray(), 0, productIdentifier.Length, unicodeText, 0, true);
        MD5 md5 = new MD5CryptoServiceProvider();
        var result = md5.ComputeHash(unicodeText);   
        var sb = new StringBuilder();
        for (var i = 0; i < result.Length; i++)
        {
            sb.Append(result[i].ToString("X2"));
        }
        return sb.ToString();
    }

我试过的是这样的:

package myTest; 
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Licensing {
    private static String GetMd5Sum(String productIdentifier)
    {
        byte[] unicodeText = new byte[productIdentifier.length() * 2];
        byte[] bt = productIdentifier.getBytes();
        int bb=0;
        for(int b = 0; b < bt.length; b++ ) {
            unicodeText[bb] = bt[b];
            bb++;
            unicodeText[bb] = 0;
            bb++;
        }
   
        String s = unicodeText.toString();
        byte[] md5Hash = getMd5(s).getBytes();
        return bytesToHex(md5Hash);
    }
       
    final protected static char[] decimalArray = "0123456789".toCharArray();
    public static String bytesToDecimal(byte[] bytes) {
        char[] decimalChars = new char[bytes.length * 4];
        for ( int j = 0; j < bytes.length; j++ ) {
            int v = bytes[j] & 0xFF;
            decimalChars[j * 4] = decimalArray[v / 100];
            decimalChars[j * 4 + 1] = decimalArray[(v / 10) % 10];
            decimalChars[j * 4 + 2] = decimalArray[v % 10];
            decimalChars[j * 4 + 3] = ' ';
        }
        return new String(decimalChars);
    }
    
    private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = HEX_ARRAY[v >>> 4];
            hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
        }
        return new String(hexChars);
    }
       
    private static String getMd5(String input) {
        try {

            // Static getInstance method is called with hashing MD5
            MessageDigest md = MessageDigest.getInstance("MD5");

            // digest() method is called to calculate message digest
            // of an input digest() return array of byte
            byte[] messageDigest = md.digest(input.getBytes());

            // Convert byte array into signum representation
            BigInteger no = new BigInteger(1, messageDigest);

            // Convert message digest into hex value
            String hashtext = no.toString(16);
            while (hashtext.length() < 32) {
                hashtext = "0" + hashtext;
            }
            return hashtext;
        }

        // For specifying wrong message digest algorithms
        catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

}

这应该会产生相同的结果:

import java.security.*;
import java.io.*;
import java.math.*;

private static String GetMd5Sum(String productIdentifier) throws UnsupportedEncodingException, NoSuchAlgorithmException {
    byte[] bytesOfMessage = productIdentifier.getBytes("UTF-16LE");
    MessageDigest md = MessageDigest.getInstance("MD5");
    byte[] digest = md.digest(bytesOfMessage);
    BigInteger i = new BigInteger(1, digest);
    return String.format("%12X", i);
}

此答案基于 this answer 但更改为使用 UTF-16LE 以匹配 C# 的 Encoding.Unicode