簡體   English   中英

Java中的C#BigInteger等效項

[英]C# BigInteger equivalent in Java

在將舊項目從C#遷移到Java時,我遇到了以下函數:

private static byte[] solveChallenge(byte[] data, int offset, int level){
        var x = new BigInteger(1, data, 00 + offset, 64);
        var n = new BigInteger(1, data, 64 + offset, 64);
        Console.Out.WriteLine("x " + x);
        Console.Out.WriteLine("n " + n);
        return x.ModPow(BigInteger.Two.Pow(level), n).ToByteArrayUnsigned();
}

第一個問題是Java需要使用一個int數組,因為它不支持4個字節。

我試圖遷移此功能,結果是:

private byte[] solveChallenge(int[] dummyData, int offset, int level) {
    int[] x = Arrays.copyOfRange(dummyData, 00 + offset, 64 + offset);
    int[] n = Arrays.copyOfRange(dummyData, 64 + offset, 64 + offset + 64);
    BigInteger bigInteger = this.toBigInteger(x, offset, level);
    BigInteger bigInteger2 = this.toBigInteger(n, offset, level);
    System.out.println("bigInteger : " + bigInteger);
    System.out.println("bigInteger2 : " + bigInteger2);
    return null; //only for debugging purposes since it doesn't calculate the right value
}

private BigInteger toBigInteger(int[] data, int offset, int length) {
    byte[] array = new byte[data.length * 4];
    ByteBuffer bbuf = ByteBuffer.wrap(array);
    bbuf.order(ByteOrder.BIG_ENDIAN);
    IntBuffer ibuf = bbuf.asIntBuffer();
    ibuf.put(data);
    int pos = bbuf.position();
    return new BigInteger(1, array);
}

帶有偽數據的字節數組如下:

byte[] dummyData = { 3, 184, 233, 18, 14, 29, 29, 96, 191, 154, 42, 188, 39, 129, 28, 43, 179, 155, 17, 253, 104, 150, 211, 242, 218, 37, 111, 45, 234, 231, 159, 226, 229, 241, 118, 6, 93, 154, 216, 39, 147, 240, 235, 37, 235, 4, 162, 199, 3, 146, 219, 250, 55, 145, 187, 109, 129, 154, 21, 99, 195, 213, 24, 171, 72, 186, 108, 207, 23, 245, 227, 206, 155, 28, 83, 117, 208, 52, 146, 247, 107, 86, 250, 91, 120, 132, 103, 96, 151, 135, 239, 236, 171, 105, 148, 132, 210, 164, 24, 121, 46, 63, 26, 6, 147, 66, 0, 113, 226, 228, 96, 54, 247, 58, 216, 123, 239, 127, 35, 235, 117, 225, 156, 139, 231, 191, 23, 16, 199, 0, 0, 39, 16, 24, 148, 19, 244, 58, 27, 168, 77, 144, 41, 52, 231, 216, 63, 52, 240, 101, 221, 196, 61, 210, 132, 247, 242, 148, 127, 73, 20, 136, 213, 82, 154, 2, 118, 247, 27, 193, 213, 31, 20, 107, 184, 115, 129, 102, 95, 130, 42, 105, 70, 228, 111, 211, 108, 149, 43, 200, 239, 220, 58, 61, 35, 169, 51, 37, 140, 12, 150, 126, 170, 183, 95, 104, 102, 218, 171, 207, 57, 172, 57, 179, 147, 134, 251, 49, 168, 124, 154, 198, 102, 204, 88, 99, 110, 128, 73, 0, 0, 0, 0, }
int[] dummyData = { 3, 184, 233, 18, 14, 29, 29, 96, 191, 154, 42, 188, 39, 129, 28, 43, 179, 155, 17, 253, 104, 150, 211, 242, 218, 37, 111, 45, 234, 231, 159, 226, 229, 241, 118, 6, 93, 154, 216, 39, 147, 240, 235, 37, 235, 4, 162, 199, 3, 146, 219, 250, 55, 145, 187, 109, 129, 154, 21, 99, 195, 213, 24, 171, 72, 186, 108, 207, 23, 245, 227, 206, 155, 28, 83, 117, 208, 52, 146, 247, 107, 86, 250, 91, 120, 132, 103, 96, 151, 135, 239, 236, 171, 105, 148, 132, 210, 164, 24, 121, 46, 63, 26, 6, 147, 66, 0, 113, 226, 228, 96, 54, 247, 58, 216, 123, 239, 127, 35, 235, 117, 225, 156, 139, 231, 191, 23, 16, 199, 0, 0, 39, 16, 24, 148, 19, 244, 58, 27, 168, 77, 144, 41, 52, 231, 216, 63, 52, 240, 101, 221, 196, 61, 210, 132, 247, 242, 148, 127, 73, 20, 136, 213, 82, 154, 2, 118, 247, 27, 193, 213, 31, 20, 107, 184, 115, 129, 102, 95, 130, 42, 105, 70, 228, 111, 211, 108, 149, 43, 200, 239, 220, 58, 61, 35, 169, 51, 37, 140, 12, 150, 126, 170, 183, 95, 104, 102, 218, 171, 207, 57, 172, 57, 179, 147, 134, 251, 49, 168, 124, 154, 198, 102, 204, 88, 99, 110, 128, 73, 0, 0, 0, 0, }

對於這兩個程序(Java的int [])。 現在棘手的部分是:C#版本中的兩個console.writeLines打印出這些值:

x 9684545129450563760811299662317393444224628107338840479787476089424784627212472709829491894762094799169291328402088637379520119916743215796610009247820616

n 9763871338200074467010224713304495633710029487465175174521229296454337320893449010179832428198865193070649591972415477477112413797655820164788963534901447

但是,java版本會打印出來:

bigInteger:

bigInteger2:

兩個程序的方法參數分別為1和10000(例如Java):

testclass.solveChallenge(dummyData, 1, 10000);

理想情況下,bigInteger應該具有與x和bigInteger2相同的值,例如n。 我的錯誤在哪里? 我已經搜尋了5個小時,但找不到任何錯誤:/

我終於找到了問題:來自Java的IntBuffer以4字節格式導入其數字(從邏輯上講,因為int包含4字節)。 這就是為什么它用int數組中的給定值的有符號值填充三個字節(零),最后一個填充。 該命令將光帶入了黑暗:

BigInteger x = new BigInteger("9684545129450563760811299662317393444224628107338840479787476089424784627212472709829491894762094799169291328402088637379520119916743215796610009247820616")

由於我能夠獲得應該獲得的實際BigInteger。 應用x.toByteArray()我已經看到那些字節以不同的方式填充(沒有三個零字節)。 通過仔細檢查C#生成的值,我發現它奇怪地總是使第二個變量的第一個字節為空(在我的情況下為n)。 應用這些規則后,我就能獲得所需的值。

這是我的最終解決方法:

private byte[] solveChallenge(int[] dummyData, int offset, int level) {
    int[] x = Arrays.copyOfRange(dummyData, 00 + offset, 65 + offset);
    int[] n = Arrays.copyOfRange(dummyData, offset, 65 + offset + 64);
    n[0] = 0; //null first byte in n, don't copy this line, this only works for my problem!
    BigInteger xInt = this.toBigInteger(x, offset);
    BigInteger nInt = this.toBigInteger(n, offset);
    BigInteger inner = pow(BigInteger.valueOf(2), BigInteger.valueOf(level));
    return xInt.modPow(inner, nInt).toByteArray();
}
private BigInteger pow(BigInteger base, BigInteger exponent) {
      BigInteger result = BigInteger.ONE;
      while (exponent.signum() > 0) {
        if (exponent.testBit(0)) result = result.multiply(base);
        base = base.multiply(base);
        exponent = exponent.shiftRight(1);
      }
      return result;
}
private BigInteger toBigInteger(int[] data, int offset, int length) {
    byte[] array = new byte[data.length * 4];
    ByteBuffer bbuf = ByteBuffer.wrap(array);
    IntBuffer ibuf = bbuf.asIntBuffer();
    ibuf.put(data);
    bbuf.order(ByteOrder.BIG_ENDIAN);
    int i = 0;
    byte[] newArr = new byte[(array.length / 4)-1];
    //convert to Little-Endian like systems and only keep every fourth byte
    for(int j = 0; j < array.length-1; ++j) {
        if(j % 4 == 0 && j > 0) {
            int pos = j - 1;
            newArr[i] = array[pos]; 
            ++i;
        }
    }
    return new BigInteger(1, newArr);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM