简体   繁体   中英

how to perform bitwise permutation in java

I'm implementing DES also in java and i am confused first as to how to get plain text into its corresponding 64 bit binary blocks and them permuting it using permutation tables.

eg. I want to get 000001010011100101110111000001000100100011010001010110 from 0123456789ABCDEF, how would I do this?

Afterwards i want to permute 000001010011100101110111000001000100100011010001010110 using the table

private static final byte[] IP = { 
        58, 50, 42, 34, 26, 18, 10, 2,
        60, 52, 44, 36, 28, 20, 12, 4,
        62, 54, 46, 38, 30, 22, 14, 6,
        64, 56, 48, 40, 32, 24, 16, 8,
        57, 49, 41, 33, 25, 17, 9,  1,
        59, 51, 43, 35, 27, 19, 11, 3,
        61, 53, 45, 37, 29, 21, 13, 5,
        63, 55, 47, 39, 31, 23, 15, 7
    };

You need to understand the relationship between the Initial Permutation and the Left and Right Blocks you're loading:

Big   MSB7     Input Block (64 bits)                    
End   Bit                                      Left  Register (32 bits)
2------6-------58 50 42 34 26 18 10  2             1  2  3  4  5  6  7  8
4------4-------60 52 44 36 28 20 12  4             9 10 11 12 13 14 15 16
6------2-------62 54 46 38 30 22 14  6            17 18 19 20 21 22 23 24
8------0-------64 56 48 40 32 24 16  8            25 26 27 28 29 30 31 32

                                               Right Register (32 bits)
1------7-------57 49 41 33 25 17  9  1             1  2  3  4  5  6  7  8
3------5-------59 51 43 35 27 19 11  3             9 10 11 12 13 14 15 16
5------3-------61 53 45 37 29 21 13  5            17 18 19 20 21 22 23 24
7------1-------63 55 47 39 31 23 15  7            25 26 27 28 29 30 31 32

Input Byte      8  7  6  5  4  3  2  1

An Input Block is eight 8 bit bytes (64 bits) that's going into two 32 bit blocks (Left and Right). IBM used a big endian bit-in-byte representation.

The Input Block would be numbered 1 through 64 with 1 through 8 representing the first byte and 1 the MSB of that byte.

The odd bits go to the Right Block, the even bits go to Left Block.

The Input Byte numbers and bits-in-types representations shown above can be used to map input bytes to Left and Right Block representations.

For your example your input bytes are comprised of two hex digits and are 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD and 0xEF. The first byte provides input block bits 1 through 8, the second 9 through 16, the third 17 through 24, the fourth 25 through 32, the fifth 33 through 40, the sixth 41 through 48, the seventh 49 through 56 and the eighth input block bits 57 through 64.

The actual effect is to serially load eight 8 bit shift registers, four each comprising the Left and Right Registers from an 8 bit interface in a hardware implementation. For round operations the Left and Right Registers are also capable of being parallel loaded or parallel read as well as serially shifted out for the Inverse Initial Permutation (which reverses the odd/even relationship).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM