4

我在 .Net 项目中使用 BigInteger 的单声道实现(链接)在 Java 中我使用 java.math.BigInteger。

相同的代码在 Java 中会产生不同的结果。

.Net 代码

    String inputBytes = "8E5BD77F0DCC30864634C134E28BFB42A149675A320786B616F4530708350D270353C30A40450325801B7AFED12BCCA274B8187072A89CC0CC3F95A24A8251243C1835898246F4D64CA3AC61DB841518F0E8FBC8996A40EB626153AE7F0BB87FD713FAC522719431428DE178E780A3FA45788A72C431926AED990E6DA268D2CC";
    String modulus = "00e6b4b4511e0bd1b3d9c82ee189ba6d0c70b1466d94126f99a741af99a92701a789451742a357ddb61a4dea409965ec58dcaa5e30826de871b04700ed0fd46b1693446049734e8f95faba2bf9301347e63ba1771650e71982adef0cca6890b6f7baa7f5421a6533652f4b70c3c4270c480cf54cc06635f22901a42716d1dadf4f";
    String exp = "010001";

    BigInteger mModulus = new BigInteger(hexStringToByteArray(modulus));
    BigInteger mExponent = new BigInteger(hexStringToByteArray(exp));
    BigInteger input = new BigInteger(hexStringToByteArray(inputBytes));
    BigInteger output = input.ModPow(mExponent, mModulus);
    Console.WriteLine("==RESULT==" + byteArray2Hex(output.GetBytes()));   


    public static byte[] hexStringToByteArray(string hexString)
    {
        if (hexString.Length % 2 != 0)
            throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "The binary key cannot have an odd number of digits: {0}", hexString));

        byte[] HexAsBytes = new byte[hexString.Length / 2];
        for (int index = 0; index < HexAsBytes.Length; index++)
        {
            string byteValue = hexString.Substring(index * 2, 2);
            HexAsBytes[index] = byte.Parse(byteValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
        }

        return HexAsBytes; 
    }

    ==RESULT==01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF003020300C06082A864886F70D02050500041009EB0D996BFC1EFA5675997712A1AB6E

Java 代码。相同的 inputBytes 数组,相同的指数和模数,但结果不同。

String inputBytes = "8E5BD77F0DCC30864634C134E28BFB42A149675A320786B616F4530708350D270353C30A40450325801B7AFED12BCCA274B8187072A89CC0CC3F95A24A8251243C1835898246F4D64CA3AC61DB841518F0E8FBC8996A40EB626153AE7F0BB87FD713FAC522719431428DE178E780A3FA45788A72C431926AED990E6DA268D2CC";
    String modulus = "00e6b4b4511e0bd1b3d9c82ee189ba6d0c70b1466d94126f99a741af99a92701a789451742a357ddb61a4dea409965ec58dcaa5e30826de871b04700ed0fd46b1693446049734e8f95faba2bf9301347e63ba1771650e71982adef0cca6890b6f7baa7f5421a6533652f4b70c3c4270c480cf54cc06635f22901a42716d1dadf4f";
    String exp = "010001";

    BigInteger mModulus = new BigInteger(hexStringToByteArray(modulus));
    BigInteger mExponent = new BigInteger(hexStringToByteArray(exp));
    BigInteger input = new BigInteger(hexStringToByteArray(inputBytes));

    BigInteger output = input.modPow(mExponent, mModulus);
    System.out.println("==RESULT==" + Utils.byteArray2Hex(output.getBytes()));

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

==RESULT==6ce02bd9536ad76bcfd7633b6a2305ed98b43b0bb5fc2acbf984566f1ab35db02e651e9ed8793bf64b018455872b8ae3a06af082e8d680df407ea1e5df1336a19c6f3e116c6ff1940066396afa1de5633fad814fb42790b3af0e62e6dd53977f78794b2d105cdca9272f9c0feea119fe2c9691b6f6e21db3065fb25d840acea2

我不明白为什么结果不同。

PS 例如,如果我使用 InputBytes

String inputBytes = "242e35241b85fcfd75a53441ef9fc0941064c16f8e4555dabef5ce8ebc91400c6961b6b607e5dd762dbcabce51b11c8594e7d7183786c8e3c5300c7583c1871fc6f350b817682150b5cd0430ca9a2c3f8315b425c8fea0e7cc18187237ed47d29b082e7e7154888d5fb09f092a6dd5e2d3dac9df8de45837b708b5ae17f03e7f";

Java 和 .Net 中的结果是一样的

==RESULT==01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff003020300c06082a864886f70d02050500041046fd8e86a4833e7141cbe4718e8e92f7

魔法在哪里?

4

2 回答 2

9

从文档中java.math.BigInteger(byte[])

将包含 BigInteger 的二进制补码表示的字节数组转换为 BigInteger。输入数组假定为大端字节序:最高有效字节位于第零个元素中。

从文档中System.Numerics.BigInteger(byte[])

值数组中的各个字节应按 little-endian 顺序,从最低位字节到最高位字节。

因此,您可能只想尝试反转您所拥有的值之一的输入字节 - 目前尚不清楚您应该反转哪个集合,因为我们不知道您要表示什么值。我建议在每种情况下在构造后立即添加仅打印正常十进制表示的诊断 - 如果这些不同,则其余代码无关紧要。

于 2012-04-12T07:14:21.013 回答
2

我通过在 inputBytes 的开头添加 0 位来解决我的问题。

于 2012-04-13T05:11:16.443 回答