Refactoring in DESEngine
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.OutputLengthException;
import org.bouncycastle.crypto.params.KeyParameter;
+import org.bouncycastle.util.Pack;
/**
* a class that provides a basic DES engine.
@@ -407,15 +408,8 @@ protected void desFunc(
{
int work, right, left;
- left = (in[inOff + 0] & 0xff) << 24;
- left |= (in[inOff + 1] & 0xff) << 16;
- left |= (in[inOff + 2] & 0xff) << 8;
- left |= (in[inOff + 3] & 0xff);
-
- right = (in[inOff + 4] & 0xff) << 24;
- right |= (in[inOff + 5] & 0xff) << 16;
- right |= (in[inOff + 6] & 0xff) << 8;
- right |= (in[inOff + 7] & 0xff);
+ left = Pack.bigEndianToInt(in, inOff);
+ right = Pack.bigEndianToInt(in, inOff + 4);
work = ((left >>> 4) ^ right) & 0x0f0f0f0f;
right ^= work;
@@ -429,11 +423,11 @@ protected void desFunc(
work = ((right >>> 8) ^ left) & 0x00ff00ff;
left ^= work;
right ^= (work << 8);
- right = ((right << 1) | ((right >>> 31) & 1)) & 0xffffffff;
+ right = (right << 1) | (right >>> 31);
work = (left ^ right) & 0xaaaaaaaa;
left ^= work;
right ^= work;
- left = ((left << 1) | ((left >>> 31) & 1)) & 0xffffffff;
+ left = (left << 1) | (left >>> 31);
for (int round = 0; round < 8; round++)
{
@@ -483,13 +477,7 @@ protected void desFunc(
left ^= work;
right ^= (work << 4);
- out[outOff + 0] = (byte)((right >>> 24) & 0xff);
- out[outOff + 1] = (byte)((right >>> 16) & 0xff);
- out[outOff + 2] = (byte)((right >>> 8) & 0xff);
- out[outOff + 3] = (byte)(right & 0xff);
- out[outOff + 4] = (byte)((left >>> 24) & 0xff);
- out[outOff + 5] = (byte)((left >>> 16) & 0xff);
- out[outOff + 6] = (byte)((left >>> 8) & 0xff);
- out[outOff + 7] = (byte)(left & 0xff);
+ Pack.intToBigEndian(right, out, outOff);
+ Pack.intToBigEndian(left, out, outOff + 4);
}
}
