[Java BigInteger与C#的差异结果

问题描述 投票:0回答:2

我在用Java获取与C#中相同的BigInteger值时遇到了一些麻烦。在这两种语言中,我都是通过按相同顺序保存相同值的字节数组创建BigIntegers的。Java代码:

public class Test {
    private static byte[] RSAModulus = new byte[]{(byte) (0x9f), (byte) 0x39, (byte) 0x91, (byte) 0x77, (byte) 0xde, (byte) 0xf8,
            (byte) 0x59, (byte) 0x69, (byte) 0xd7, (byte) 0x3d, (byte) 0x1c, (byte) 0xdf, (byte) 0x84, (byte) 0x4b, (byte) 0xd5,
            (byte) 0xb7, (byte) 0xca, (byte) 0x6b, (byte) 0xba, (byte) 0xab, (byte) 0x35, (byte) 0xb8, (byte) 0xea, (byte) 0x91,
            (byte) 0x94, (byte) 0x39, (byte) 0xe4, (byte) 0xbe, (byte) 0x12, (byte) 0xbc, (byte) 0x57, (byte) 0x71, (byte) 0xef,
            (byte) 0x62, (byte) 0x75, (byte) 0xef, (byte) 0x25, (byte) 0x68, (byte) 0x61, (byte) 0x45, (byte) 0xa5, (byte) 0xb9,
            (byte) 0x33, (byte) 0xda, (byte) 0x53, (byte) 0x9f, (byte) 0xc0, (byte) 0x71, (byte) 0xc1, (byte) 0xa7, (byte) 0xe5,
            (byte) 0xd3, (byte) 0x67, (byte) 0x98, (byte) 0x8e, (byte) 0xc1, (byte) 0x54, (byte) 0x1c, (byte) 0x3e, (byte) 0x70,
            (byte) 0xb2, (byte) 0x74, (byte) 0x49, (byte) 0xf3, (byte) 0xa2, (byte) 0x46, (byte) 0x7e, (byte) 0x8d, (byte) 0xf8,
            (byte) 0xe6, (byte) 0xd7, (byte) 0x13, (byte) 0x1a, (byte) 0xfb, (byte) 0x4a, (byte) 0x16, (byte) 0xc2, (byte) 0xde,
            (byte) 0xe8, (byte) 0x6b, (byte) 0x6e, (byte) 0x79, (byte) 0x0a, (byte) 0xc2, (byte) 0x8a, (byte) 0xaa, (byte) 0x5f,
            (byte) 0x0a, (byte) 0xf5, (byte) 0xd3, (byte) 0xaf, (byte) 0x66, (byte) 0x3f, (byte) 0x33, (byte) 0xd2, (byte) 0xc6,
            (byte) 0x88, (byte) 0xcc, (byte) 0xd4, (byte) 0x5c, (byte) 0x0a, (byte) 0x3b, (byte) 0x45, (byte) 0xb9, (byte) 0x49,
            (byte) 0xa7, (byte) 0x92, (byte) 0x90, (byte) 0x13, (byte) 0xcc, (byte) 0xb0, (byte) 0x04, (byte) 0x49, (byte) 0xbd,
            (byte) 0x7a, (byte) 0x8d, (byte) 0xdf, (byte) 0x21, (byte) 0xe2, (byte) 0x8b, (byte) 0xdd, (byte) 0x63, (byte) 0xa2,
            (byte) 0xbd, (byte) 0xae, (byte) 0x10, (byte) 0xbf, (byte) 0xc5};

    public static void main(String[] args) {
        BigInteger bigInt = new BigInteger( RSAModulus);
        System.out.println(bigInt);
        System.out.println(byteArrayToHex(bigInt.toByteArray()));
    }

    public static String byteArrayToHex(byte[] a) {
        StringBuilder sb = new StringBuilder(a.length * 2);
        for (byte b : a)
            sb.append(String.format("%02X", b).concat("-"));
        return sb.toString();
    }

C#代码:

 private static byte[] RSAModulus = new byte[] { (byte)(0x9f), (byte)0x39, (byte)0x91, (byte)0x77, (byte)0xde, (byte)0xf8, (byte)0x59, (byte)0x69, (byte)0xd7, (byte)0x3d, (byte)0x1c, (byte)0xdf, (byte)0x84, (byte)0x4b, (byte)0xd5, (byte)0xb7, (byte)0xca, (byte)0x6b, (byte)0xba, (byte)0xab, (byte)0x35, (byte)0xb8, (byte)0xea, (byte)0x91, (byte)0x94, (byte)0x39, (byte)0xe4, (byte)0xbe, (byte)0x12, (byte)0xbc, (byte)0x57, (byte)0x71, (byte)0xef, (byte)0x62, (byte)0x75, (byte)0xef, (byte)0x25, (byte)0x68, (byte)0x61, (byte)0x45, (byte)0xa5, (byte)0xb9, (byte)0x33, (byte)0xda, (byte)0x53, (byte)0x9f, (byte)0xc0, (byte)0x71, (byte)0xc1, (byte)0xa7, (byte)0xe5, (byte)0xd3, (byte)0x67, (byte)0x98, (byte)0x8e, (byte)0xc1, (byte)0x54, (byte)0x1c, (byte)0x3e, (byte)0x70, (byte)0xb2, (byte)0x74, (byte)0x49, (byte)0xf3, (byte)0xa2, (byte)0x46, (byte)0x7e, (byte)0x8d, (byte)0xf8, (byte)0xe6, (byte)0xd7, (byte)0x13, (byte)0x1a, (byte)0xfb, (byte)0x4a, (byte)0x16, (byte)0xc2, (byte)0xde, (byte)0xe8, (byte)0x6b, (byte)0x6e, (byte)0x79, (byte)0x0a, (byte)0xc2, (byte)0x8a, (byte)0xaa, (byte)0x5f, (byte)0x0a, (byte)0xf5, (byte)0xd3, (byte)0xaf, (byte)0x66, (byte)0x3f, (byte)0x33, (byte)0xd2, (byte)0xc6, (byte)0x88, (byte)0xcc, (byte)0xd4, (byte)0x5c, (byte)0x0a, (byte)0x3b, (byte)0x45, (byte)0xb9, (byte)0x49, (byte)0xa7, (byte)0x92, (byte)0x90, (byte)0x13, (byte)0xcc, (byte)0xb0, (byte)0x04, (byte)0x49, (byte)0xbd, (byte)0x7a, (byte)0x8d, (byte)0xdf, (byte)0x21, (byte)0xe2, (byte)0x8b, (byte)0xdd, (byte)0x63, (byte)0xa2, (byte)0xbd, (byte)0xae, (byte)0x10, (byte)0xbf, (byte)0xc5};


        BigInteger Modulus = new BigInteger(RSAModulus);
        Console.WriteLine(Modulus);
        Console.WriteLine(BitConverter.ToString(Modulus.ToByteArray()));

和结果:

C#:
-40907105363667855877152908619053117357772160341452142777989857601426818658568025381825514920073409533964568497621488817967751512954762186714705411964630948143292201585624154827519184247086915587921190770024413749024561021722615390665726203241414710662246795990783021124056737361327527049660747797088078579297
9F-39-91-77-DE-F8-59-69-D7-3D-1C-DF-84-4B-D5-B7-CA-6B-BA-AB-35-B8-EA-91-94-39-E4-BE-12-BC-57-71-EF-62-75-EF-25-68-61-45-A5-B9-33-DA-53-9F-C0-71-C1-A7-E5-D3-67-98-8E-C1-54-1C-3E-70-B2-74-49-F3-A2-46-7E-8D-F8-E6-D7-13-1A-FB-4A-16-C2-DE-E8-6B-6E-79-0A-C2-8A-AA-5F-0A-F5-D3-AF-66-3F-33-D2-C6-88-CC-D4-5C-0A-3B-45-B9-49-A7-92-90-13-CC-B0-04-49-BD-7A-8D-DF-21-E2-8B-DD-63-A2-BD-AE-10-BF-C5
Java:
-67957803197405163432279621608884085928691276607886366236885531026002470080889831781617385728107944022981162471612439220228243925827444081830715759495973881071943381352159034439014628324854518237785608368276142937558158061374892654396604680186975989182503650794599497824022258851557439385188454491467719589947
9F-39-91-77-DE-F8-59-69-D7-3D-1C-DF-84-4B-D5-B7-CA-6B-BA-AB-35-B8-EA-91-94-39-E4-BE-12-BC-57-71-EF-62-75-EF-25-68-61-45-A5-B9-33-DA-53-9F-C0-71-C1-A7-E5-D3-67-98-8E-C1-54-1C-3E-70-B2-74-49-F3-A2-46-7E-8D-F8-E6-D7-13-1A-FB-4A-16-C2-DE-E8-6B-6E-79-0A-C2-8A-AA-5F-0A-F5-D3-AF-66-3F-33-D2-C6-88-CC-D4-5C-0A-3B-45-B9-49-A7-92-90-13-CC-B0-04-49-BD-7A-8D-DF-21-E2-8B-DD-63-A2-BD-AE-10-BF-C5
java c# biginteger
2个回答
0
投票

看来Java和C#假定表示的字节顺序不同

https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#BigInteger(byte[])

大小为big-endian字节顺序的字节数组

https://docs.microsoft.com/en-us/dotnet/api/system.numerics.biginteger?view=netframework-4.8

BigInteger结构期望字节数组中的各个字节以little-endian顺序出现


0
投票

这里是区别:Java byte8-bit signed integer,而C#byteunsigned 8-bit integer

© www.soinside.com 2019 - 2024. All rights reserved.