Bitshifting using BigIntegers in Java

I am using BigIntegers to implement DES encryption in Java

I execute BigInteger The leftshift (int n) method shifts the Java binary key with Java BigIntegers The key of n (KN) depends on the displacement result of kn-1 The problem I get is that I print the result after generating each key, and the transfer is not the expected output The key is divided into 2 CN and DN (left and right respectively)

I deliberately tried this: "to move left, move each bit to the left, except the first bit, and loop to the end of the block."

According to the transformation, it seems that O will eventually be attacked I don't know how to correct the problem

result:

c0:11110101010100110011000011110

d0:11110001111001100110101010100

c1:111101010101001100110000111100

d1:111100011110011001101010101000

c2:11110101010100110011000011110000

d2:11110001111001100110101010100000

c3:1111010101010011001100001111000000

d3:1111000111100110011010101010000000

c4:111101010101001100110000111100000000

d4:111100011110011001101010101000000000

c5:11110101010100110011000011110000000000

d5:11110001111001100110101010100000000000

c6:1111010101010011001100001111000000000000

d6:1111000111100110011010101010000000000000

c7:111101010101001100110000111100000000000000

d7:111100011110011001101010101000000000000000

c8:1111010101010011001100001111000000000000000

d8:1111000111100110011010101010000000000000000

c9:111101010101001100110000111100000000000000000

d9:111100011110011001101010101000000000000000000

c10:11110101010100110011000011110000000000000000000

d10:11110001111001100110101010100000000000000000000

c11:1111010101010011001100001111000000000000000000000

d11:1111000111100110011010101010000000000000000000000

c12:111101010101001100110000111100000000000000000000000

d12:111100011110011001101010101000000000000000000000000

c13:11110101010100110011000011110000000000000000000000000

d13:11110001111001100110101010100000000000000000000000000

c14:1111010101010011001100001111000000000000000000000000000

d14:11110001111001100110101010100000000000000000000000000000000

c15:11110101010100110011000011110000000000000000000000000000

d15:111100011110011001101010101000000000000000000000000000000000

Solution

BigInteger implements an integer of infinite precision, so moving left will continue to add zero to the left You need a rotation:

private static BigInteger rotateLeft(BigInteger bi) {
    BigInteger ret = bi.shiftLeft(1);
    if (ret.testBit(32)) {
        ret = ret.clearBit(32).setBit(0);
    }
    return ret;
}

This will be quite inefficient for 32-bit numbers, so you can also use primitives to rotate the 28 bits of Des I'm not familiar with DES algorithm, so I assume you need BigInteger to do other things

private static BigInteger rotateLeftPrimitive(BigInteger bi) {
    int value = bi.intValue();
    return BigInteger.valueOf(((value << 1) & 0xffffffe) | ((value >>> 27) & 1));
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>