Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java BigInteger差分结果与C#_Java_C#_Biginteger - Fatal编程技术网

Java BigInteger差分结果与C#

Java BigInteger差分结果与C#,java,c#,biginteger,Java,C#,Biginteger,在C#中生成与Java中相同的BigInteger值时,我遇到了一些麻烦。在这两种语言中,我都是从一个字节数组中创建大整数,这些字节以相同的顺序保存相同的值。 Java代码: public class Test { private static byte[] RSAModulus = new byte[]{(byte) (0x9f), (byte) 0x39, (byte) 0x91, (byte) 0x77, (byte) 0xde, (byte) 0xf8,

在C#中生成与Java中相同的BigInteger值时,我遇到了一些麻烦。在这两种语言中,我都是从一个字节数组中创建大整数,这些字节以相同的顺序保存相同的值。 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#代码:

结果是:

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#对表示采用了不同的endian

:

大小是一个字节数组,以大的-endian字节顺序排列

:

BigInteger结构要求字节数组中的单个字节以小的endian顺序出现


我想除了微软,世界上每个人都使用big-endian。Python要求您指定所需的顺序。这可能仅仅是因为Java的“字节”不等同于C的“字节”吗?(它相当于C#'sbyte')。
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