2

I am trying to implement a cipher that uses a 128 bit key. Part of the key schedule is to rotate the key 29 bits to the right, but I am unsure how to do that since there is no single data type in Java that can hold the whole key. I have it stored in two longs, one for the upper half and one for the lower half. Here is the bit math that I have that I thought should work but isn't doing the trick:

keyLower >>>= 29;
keyLower |= keyUpper << 35;
keyUpper >>>= 29;
keyUpper |= keyLowerCopy << 29;

Can anyone help me out?

dylanrb123
  • 61
  • 1
  • 6

2 Answers2

3

You have a typo on the last line:

//                          vv
keyUpper |= keyLowerCopy << 29;

Looks like it's supposed to be << 35.

Radiodef
  • 37,180
  • 14
  • 90
  • 125
-1

Take a look at BigInteger.

Immutable arbitrary-precision integers. [...] Semantics of shift operations extend those of Java's shift operators to allow for negative shift distances. A right-shift with a negative shift distance results in a left shift, and vice-versa. The unsigned right shift operator (>>>) is omitted, as this operation makes little sense in combination with the "infinite word size" abstraction provided by this class.

Luigi Cortese
  • 10,841
  • 6
  • 37
  • 48
  • The question was about rotation, not just shift alone. Besides that you wouldn't want to use `BigInteger` in a symmetric cipher. – Maarten Bodewes Mar 21 '15 at 12:29
  • you're right, it should have been a comment, not an answer. I thought this could help, being that a data type (wrap class) able to store the whole key – Luigi Cortese Mar 21 '15 at 19:18
  • 1
    Fair enough. A BigInteger is usually a good choice for storing a 128 bit number. However, in crypto you really don't want to use an immutable object instance for each value, both for performance and security reasons. Besides that operations required for crypto - *including* the rotate function - don't work that well on unbounded, signed values. – Maarten Bodewes Mar 21 '15 at 19:21