Hello, I got implementation of Data Encryption Standard implementation in java (internet code) but the only part that I cannot understand is the S-Box which shown below. I know how S-Box works but I don't understand this code especially &0x20 and stuffs like that. The S-Box takes a 6 bit word such as 110110 as input.

It takes the first and last bits (110110) as row and the inner bits (110110) as column. Can anyone explain how this code below achieve what the S-Box does. If I want to change the input as a 4-bits word, how does the code change to accomodate it?

`````` private static byte S(int boxNumber, byte src) {
// The first and last bits determine which 16-value row to
// reference, so we transform the 6-bit input into an
// absolute index based on the following bit shuffle:
// abcdef => afbcde
src = (byte) (src&0x20 | ((src&0x01)<<4) | ((src&0x1E)>>1));
return S[boxNumber-1][src];
}
``````

I have tried searching for notes on java shifting (bitwise) but cannot understand it clearly.

Thanks.

This may help: https://en.wikipedia.org/wiki/S-box

That said, the logical bit-wise operations are intended to mask specific bits to generate the index to the sbox for lookup. The value 0x20 == (binary) 100000 & src masks out all but the top bit in src.

value 0x01 == (binary) 000001 & src masks out all but the bottom bit in src. << 4 shifts that (if first bit is set) left by 4 bits, so if the lowest bit is set in src, the output of that is 010000.

The last element is 0x1e which is 011110 which will mask out the upper and lower bits of the 6-bit value, and shifting that right by one will result in possible a value (maximum) of 001111 binary.

Finally, the | (or) expressions combines all of those masked values into the real index to the s-box.

FWIW, the DES algorithm was the result of years of research by the top boffins at IBM and serious vetting by the NSA. It is not strong by today's standards, but it was good enough at the time that exporting the algorithms was prohibited for many years. Now, it's posted on Wikipedia! :-)